Commodore 1571 firmware
Format Routine
.page
.subttl 'lccfmt2a.src'
gaptbl .byte $21,$22,$23,$24,$25,$26,$27,$28,$29
gp2cmd=*-gaptbl
gp2tbl .byte 2,2,4,6,8,8,11,19,22
; clear at least 6522 bytes written (trk1*1.08%)
spdchk ldy #0 ; 516153 write 82% sync
ldx #28
jsr jclear ; clear whole disk & more
jsr fil_syn ; fill with sync
jsr kill ; go read mode
ldy #mscnt
2$ ldx #mscnt
3$ bit dskcnt ; try to find sync
bpl 5$ ; got sync
dex
bne 3$
dey
bne 2$
4$ lda #2
jmp jfmte10 ; error
5$ ldy #0
ldx #0
6$ bit dskcnt ; wait for sync to go away
bpl 6$ ; got sync
7$ lda dskcnt ; count time till next sync
iny ; lsb not used
bne 8$
inx ; msb
8$ and #$80
bne 7$ ; br, still no sync
lda #0
sta tsttrk
txa ; msb
;*********************************************************
;* --- lookup gap2 in table --- *
;* x : 2 2 2 2 2 2 2 2 29 *
;* gap2 : 0 0 0 0 0 0 0 1 16 *
;* speed : fast -------------------------------- slow *
;*********************************************************
ldx #gp2cmd-1
9$ cmp gaptbl,x ; lookup timing gap
beq 10$
dex
bpl 9$
bmi 4$ ; none
10$ lda gp2tbl,x ; look it up
sta dtrck
rts
jformt lda ftnum ; test if formating done
bpl 1$ ; yes
lda #$60 ; status = stepping
sta drvst
lda fmtsid
bne 3$
lda #01
.byte skip2
3$ lda #36
sta drvtrk ; track36
sta ftnum ; track count 36
cmp #36 ; sclr carry flag
jsr set_side
lda #256-92 ; step back 45 tracks
sta steps
lda dskcnt ; phase a
and #$ff-03
sta dskcnt
lda #10
sta cnt ; init error count
jmp jend ; go back to controler
1$ ldy #00
lda (hdrpnt),y
cmp ftnum
beq 2$
lda ftnum
sta (hdrpnt),y
jmp jend
2$ lda dskcnt ; check wps
and #$10
bne toptst ; ok
lda #8
jmp jfmterr ; wp error
fil_syn ldx #20 ; 20*256 bytes of sync
lda #$ff ; write syncs
1$ bit pota1
bmi 1$
sta data2
bit byt_clr
dey
bne 1$
dex
bne 1$
rts
toptst lda tsttrk
bpl 1$
jsr spdchk
1$ lda dtrck
; create header images
clc
lda #>bufs
sta hdrpnt+1
lda #00
sta hdrpnt ; point hdr point to buf0
sta sect
ldy #0
2$ lda hbid ; hbid cs s t id id 0f 0f
sta (hdrpnt),y
iny
lda #00 ; check sum is zero for now
sta (hdrpnt),y
iny
lda sect ; store sector #
sta (hdrpnt),y
iny
lda ftnum ; store track #
sta (hdrpnt),y
iny
lda dskid+1 ; store id low
sta (hdrpnt),y
iny
lda dskid ; store id hi
sta (hdrpnt),y
iny
lda #$0f ; store gap1 bytes
sta (hdrpnt),y
iny
sta (hdrpnt),y
iny
tya ; save this point
pha
ldx #07
lda #00
sta chksum ; zero checksum
3$ dey
lda (hdrpnt),y
eor chksum
sta chksum
dex
bne 3$
sta (hdrpnt),y ; store checksum
pla
tay ; restore pointer
inc sect ; goto next sector
lda sect ; test if done yet
cmp sectr
bcc 2$ ; more to do
lda #3
sta bufpnt+1 ; $03XX
jsr fbtog ; convert to gcr with no bid char
; move buffer up 79 bytes
ldy #$ff-69 ; for i=n-1 to 0:mem+i+69|:=mem+i|:next
4$ lda (hdrpnt),y ; move buf0 up 69 bytes
ldx #69
stx hdrpnt
sta (hdrpnt),y
ldx #00
stx hdrpnt
dey
cpy #$ff
bne 4$
ldy #68 ; #bytes to move
5$ lda ovrbuf+$bb,y
sta (hdrpnt),y
dey
bpl 5$
; create data block of zero
clc
lda #>bufs
adc #02
sta bufpnt+1 ; point to buf2
lda #00
tay ; init offset
6$ sta (bufpnt),y
iny
bne 6$
; convert data block to gcr
; write image
; leave it in ovrbuf and buffer
jsr chkblk ; get block checksum
sta chksum
jsr bingcr
; start the format now
; write out sync header gap1
; data block
lda #0 ; init counter
sta fmhdpt
ldx #6 ; clear 8% of disk
jsr jclear ; clear disk
7$ ldy #numsyn ; write 4 gcr bytes
8$ bit pota1
bmi 8$
lda #$ff ; write sync
sta data2
bit byt_clr ; clear pa latch
dey
bne 8$
ldx #10 ; write out header
ldy fmhdpt
9$ bit pota1
bmi 9$
lda (hdrpnt),y ; get header data
sta data2
bit byt_clr
iny
dex
bne 9$
; * write out gap1
ldy #gap1-2 ; write gcr bytes
10$ bit pota1
bmi 10$
lda #$55
sta data2
bit byt_clr
dey
bne 10$
; * write out data block
lda #$ff ; write data block sync
ldy #numsyn
11$ bit pota1
bmi 11$
sta data2
bit byt_clr
dey
bne 11$
ldy #$bb ; write out ovrbuf
12$ bit pota1
bmi 12$
lda ovrbuf,y
sta data2
bit byt_clr
iny
bne 12$
13$ bit pota1
bmi 13$
lda (bufpnt),y
sta data2
bit byt_clr
iny
bne 13$
lda #$55 ; write gap2(dtrck)
ldy dtrck
14$ bit pota1
bmi 14$
sta data2
bit byt_clr
dey
bne 14$
lda fmhdpt ; advance header pointer
clc
adc #10
sta fmhdpt
; done writing sector
dec sect ; go to next on
beq 15$ ; br, no more to do
jmp 7$
15$ bit pota1 ; wait for last one to write
bmi 15$
bit byt_clr
16$ bit pota1 ;wait for last one to write
bmi 16$
bit byt_clr
jsr kill ; goto read mode