0100 * ATARI 1050 DISK DRIVE OPERATING SYSTEM -- part 4
0110 *
0120 * execute floppy test
0130 * fist format diskette enhanced density
0140 *
0150 FPTEST LDA SEKBUF+1 mark first 12 bytes from sector
0160     STA HZ9D
0170     LDA SEKBUF+2
0180     STA HZ9E
0190     LDX #9
0200 FPTST1 LDA SEKBUF+3,X
0210     STA BUFFER,X
0220     DEX 
0230     BPL FPTST1
0240     JSR DDF     format now
0250     BMI TFAIL   error, motor on/off every 2 seconds
0260     JSR VERIFO  ok, verify
0270     BIT ERROR
0280     BMI TFAIL   error?
0290     JSR WS13AT  write pattern to sector 1,3 of each track
0300 FPTST2 JSR M6DB6 fill sector buffer with $6DB6
0310     JSR TWR1    write it to other sectors
0320     JSR MOTONOF short delay
0330     JSR TVER1   verify test 1
0340     JSR MOTONOF delay
0350     JSR MDB66   fill sector with $DB66
0360     JSR TWR2    write it to other sectors
0370     JSR MOTONOF delay
0380     JSR TVER2   verify
0390     JSR MOTONOF delay
0400     JSR M6DB6   pattern 6DB6 again
0410     JSR WALTER1 write alternately
0420     JSR MOTONOF delay
0430     JSR VALTER1 verify alternately
0440     JSR MOTONOF delay
0450     JSR MDB66   pattern DB66
0460     JSR WALTER2 write alternately
0470     JSR MOTONOF delay
0480     JSR VALTER2 verify alternately
0490     JSR MOTONOF delay
0500     LDA #0      SEEK on track 0
0510     STA TRACK
0520     JSR SEEK
0530     LDA #1      sector 1
0540     STA SEKTOR
0550     STA SEKREG  to controller
0560     JSR TREAD   read sector 1
0570     INC SEKBUF+10 number of o.k. tests + 1
0580     BNE TST1
0590     INC SEKBUF+11
0600 TST1 JSR WOSV   write one sector, verify
0610     DEC HZ9D    number of test loops - 1
0620     BNE FPTST2  more to do
0630     LDA HZ9E
0640     BEQ TST2
0650     DEC HZ9E
0660     JMP FPTST2
0670 *
0680 TST2 LDA FCNTRL test loop done
0690     STA CSTAT   mark status
0700     LDX #$FF    reset stack
0710     TXS 
0720     JSR RESTORE to track 0
0730     JMP DRDY    back to main loop
0740 *
0750 * fatal error during test
0760 * turn motor on and off every 2 seconds
0770 *
0780 TFAIL LDA #8    invert motor bit
0790     EOR DRA
0800     STA DRA
0810     LDX #$14    2 seconds delay
0820     JSR DELAY2
0830     JMP TFAIL   loop forever
0840 *
0850 * read alternately from track 39 down and from track 0 up
0860 *
0870 VALTER2 LDA #$27 track 39
0880     STA SBUF
0890     LDA #0      and track 0
0900     STA SBUF+1
0910     LDA #$14
0920     STA SBUF+2  read 20 tracks
0930 VALT1 JSR SR2TRK SEEK, read 2 tracks
0940     INC SBUF+1  next track from begin
0950     DEC SBUF    next track from end
0960     DEC SBUF+2  read 20 tracks?
0970     BNE VALT1   no, continue
0980     RTS 
0990 *
1000 * read tracks alternately, 0 - 19 and 39 - 20
1010 *
1020 VALTER1 LDA #0  start with track 0
1030     STA SBUF
1040     LDA #$27    and track 39
1050     STA SBUF+1
1060     LDA #$14    20 tracks each
1070     STA SBUF+2
1080 VALT2 JSR SR2TRK SEEK, read 2 tracks
1090     INC SBUF+1
1100     DEC SBUF
1110     DEC SBUF+2  all done?
1120     BNE VALT2
1130     RTS 
1140 *
1150 * write tracks alternately, 39 - 20 and 0 - 19
1160 *
1170 WALTER1 LDA #$27 start with track 39
1180     STA SBUF
1190     LDA #0      and track 0
1200     STA SBUF+1
1210     LDA #$14    20 tracks each
1220     STA SBUF+2
1230 WALT1 JSR SW2TRK SEEK and write 2 tracks
1240     INC SBUF+1
1250     DEC SBUF
1260     DEC SBUF+2  all done?
1270     BNE WALT1
1280     RTS 
1290 *
1300 * write tracks alternately, 0 - 19 and 39 - 20
1310 *
1320 WALTER2 LDA #0  start with 0
1330     STA SBUF
1340     LDA #$27    and 39
1350     STA SBUF+1
1360     LDA #$14    20 tracks each
1370     STA SBUF+2
1380 WALT2 JSR SW2TRK write 2 tracks
1390     INC SBUF+1
1400     DEC SBUF
1410     DEC SBUF+2  all done?
1420     BNE WALT2
1430     RTS 
1440 *
1450 * test SEEK on track
1460 *
1470 TSEEK LDA SEKBUF+1 intended track in second byte after command
1480     CMP #$28
1490     BPL TSEEK1  only 39 tracks
1500     STA TRACK
1510     JSR SEEK    execute SEEK
1520     CLC 
1530     RTS 
1540 TSEEK1 SEC      wrong track
1550     RTS 
1560 *
1570 * read two tracks
1580 *
1590 SR2TRK LDA SBUF tracknumber 1
1600     STA TRACK
1610     JSR TRKVER  verify
1620     LDA SBUF+1  tracknumber 2
1630     STA TRACK
1640     JSR TRKVER  read also
1650     RTS 
1660 *
1670 * write test pattern to two tracks
1680 *
1690 SW2TRK LDA SBUF tracknumber 1
1700     STA TRACK
1710     JSR TRKWRT  write to track
1720     LDA SBUF+1  tracknumber 2
1730     STA TRACK
1740     JSR TRKWRT  also
1750     RTS 
1760 *
1770 * do step for testing
1780 *
1790 TSTEP LDA SEKBUF+1 do test step, direction in $01
1800     STA DIR
1810     JSR STEP
1820     RTS 
1830 *
1840 * fill sector with test pattern
1850 *
1860 WS13AT LDA #0
1870     STA TRACK
1880     LDA #0      pattern byte #1
1890     TAY         pattern byte #2
1900     JSR SEKPAT
1910 WS13AT1 LDA #1  fill sector 1
1920     STA SEKTOR
1930     JSR WOSV    write + verify sector
1940     BMI TWERR   write test error
1950     LDA #3      sector 3 also
1960     STA SEKTOR
1970     JSR WOSV    write
1980     BMI TWERR   on error to TWERR
1990     INC TRACK   next track
2000     LDA TRACK
2010     CMP #$28    until track 39
2020     BNE WS13AT1
2030     LDA #0
2040     STA TRACK   track 0
2050     LDX #9
2060 WS13AT2 LDA BUFFER,X move test bytes
2070     STA SEKBUF,X
2080     DEX 
2090     BPL WS13AT2
2100     LDA HZ9D    move counter for test
2110     STA SEKBUF+12
2120     LDA HZ9E
2130     STA SEKBUF+13
2140     LDA #1
2150     STA SEKTOR
2160     JSR WOSV    write to sector 1
2170     BMI TWERR
2180     RTS 
2190 *
2200 * pattern $6DB6 or $DB66, depending on choice ($009C)
2210 *
2220 MWAHL LDA MUSTERNR
2230     BNE MWAHL1  pattern $DB66?
2240     JMP M6DB6   no, $6DB6
2250 MWAHL1 JMP MDB66
2260 *
2270 * test read
2280 * read sector 1 if sector number <16, otherwise sector 3
2290 *
2300 TREAD LDA SEKREG
2310     CMP #$10    sector number <16?
2320     BPL TREAD1
2330     LDY #1      yes, read sector 1
2340     JMP TREAD2
2350 TREAD1 SEC      otherwise subtract 16 & read sector 3
2360     SBC #$10
2370     LDY #3
2380 TREAD2 STY SEKREG
2390     STY SEKTOR
2400     ASL A
2410     ASL A
2420     ASL A       calculate address of status byte
2430     ADC #0      for error (0 - $80)
2440     STA KOMND   move address to KOMND & JUMPL
2450     LDA #0
2460     STA JUMPL
2470     LDA #2      number of retries
2480     STA COUNT
2490 TREAD3 JSR RDSEKTOR read sector
2500     BEQ TREAD5
2510     DEC COUNT   error, retry
2520     BEQ TWERR
2530     LDA FCNTRL  get error type
2540     AND #$0C
2550     BEQ TREAD4  lost data?
2560     JMP TREAD3  if so, another try
2570 TWERR JMP TST2  end of test
2580 TREAD4 LDA CSTAT
2590     AND #$10    RECORD NOT FOUND?
2600     BEQ TREAD3  no, other error
2610     JSR RESTORE otherwise to track 0
2620     JSR SEEK    goto track, again
2630     JSR RDSEKTOR read, again
2640     BNE TWERR   no good to nobody
2650     LDA #$40    mark SEEK ERROR
2660     STA SEEKERR
2670     LDA #$FF
2680     STA FMERK+1 error occured
2690 TREAD5 RTS      end of test
2700 *
2710 * fill sector with pattern
2720 *
2730 M6DB6 LDA #0
2740     STA MUSTERNR indicate pattern 1
2750     LDA #$6D    first pattern byte
2760     LDY #$B6    second pattern byte
2770     JMP SEKPAT  fill sector
2780 *
2790 MDB66 LDA #$FF
2800     STA MUSTERNR indicate pattern 2
2810     LDA #$DB    first pattern byte
2820     LDY #$66    second pattern byte
2830 *
2840 * fill sector with pattern
2850 *
2860 SEKPAT LDX #0
2870 SEKPA1 STA SEKBUF,X put first byte
2880     INX 
2890     STY SEKBUF,X put second byte
2900     INX 
2910     BPL SEKPA1  until sector filled
2920     RTS 
2930 *
2940 * check error
2950 *
2960 FAUSW LDA FCNTRL move controller status
2970     STA CSTAT
2980     STY JUMPH   to aux. cell
2990     LDA CSTAT
3000     AND #$10    RNF error?
3010     BEQ FAUSW1
3020     LDA FMERK   yes, indicated by now?
3030     BEQ FAUSW2  no, do it
3040     LDA FMERK+2 write track error?
3050     BEQ FAUSW3  no, only read
3060     LDA #$80    indicate write error
3070     BNE FAUSW4
3080 FAUSW3 LDA #4   indicate read error
3090 FAUSW4 JMP FAUSW5 mark
3100 FAUSW2 LDA #$FF indicate error
3110     STA FMERK
3120     STA FMERK+1
3130     LDA FMERK+2 write error?
3140     BNE FAUSW6  br if so
3150     JSR WRSEKTOR otherwise, write again
3160     BNE FAUSW3  wrong, again
3170 FAUSW6 JMP FAUSW7
3180 FAUSW1 LDA CSTAT controller status
3190     AND #8      CRC error bit
3200     BEQ FAUSW8  no CRC error
3210     LDA #2      yes, indicate it
3220     BNE FAUSW5
3230 FAUSW8 LDA CSTAT
3240     AND #4      lost data?
3250     BEQ FAUSW9  br if not
3260     LDA #8      yes, indicate it
3270     BNE FAUSW5
3280 FAUSW9 LDA TRKERR impossible to verify track?
3290     BEQ FAUSW10 yes, only errors
3300     LDA #$10    error, but not fatal
3310     BNE FAUSW5
3320 FAUSW10 LDA #1  track verify FATAL ERROR
3330 FAUSW5 LDY #0
3340     STY FMERK+1 reset error mark
3350     STA SEEKERR indicate error code
3360     JSR TREAD   try another read, look after address for error
3370     LDA SEEKERR mark for test error
3380     LDY #0
3390     ORA (KOMND),Y in command byte or
3400     STA (KOMND),Y
3410     LDA SEEKERR
3420     CMP #8      lost data error?
3430     BEQ FAUSW11 br if so
3440     LDY #7      change error code to binary
3450     CMP #$10    on $10 error 7, non fatal read error
3460     BEQ FAUSW12
3470     DEY 
3480     CMP #$80    on $80 error 6, write error
3490     BEQ FAUSW13
3500     DEY 
3510     CMP #$40    on $40 error 5, SEEK error
3520     BEQ FAUSW13
3530     DEY 
3540     CMP #$04    on $04 error 4, read error
3550     BEQ FAUSW13
3560     DEY 
3570     CMP #$02    on $02 error 3, CRC error
3580     BEQ FAUSW13
3590     DEY 
3600 FAUSW12 STY HZA4 otherwise, error 2
3610     LDY #1
3620     LDA ERRADR  move error address
3630     STA (KOMND),Y to sector buffer
3640     LDY HZA4
3650 FAUSW13 LDA (KOMND),Y get byte, error number as offset
3660     CLC 
3670     ADC #1
3680     STA (KOMND),Y
3690     BPL FAUSW11
3700     BRK 
3710 FAUSW11 JSR WOSV write sector with verify
3720     BPL FAUSW7
3730     JMP TST2    error
3740 FAUSW7 JSR MWAHL other pattern
3750     LDY JUMPH
3760     RTS 
3770 *
3780 * write one sector with verify
3790 *
3800 WOSV LDA #$80
3810     STA ERROR   assume error
3820     LDA #2
3830     STA COUNT   try two times
3840     JSR WRITE2  write
3850     BIT ERROR   error?
3860     BMI WOSV1   br if so
3870     LDA #$80    assume error, again
3880     STA ERROR
3890     LDA #2      try two times
3900     STA COUNT
3910     JSR VERIFY  for verify
3920     BIT ERROR   error?
3930 WOSV1 RTS 
3940 *
3950 * test if step to next track is done in a certain time
3960 * a step to the start of sector 1 is indicated
3970 * at the end, the number of sectors is read
3980 * and compared with a maximum in the test command
3990 *
4000 TSTPRT JSR WSONE go read sector 1
4010     BCS TSTPRT1 br if no sector 1
4020     INC TRACK   step to next track
4030     JSR SEEK
4040     LDA #$C0    read address
4050     STA FCNTRL
4060     LDA #$D2    with timeout
4070     JSR TIMEOUT
4080     LDX #$7A    from sector buffer address $7A
4090 TSTPRT2 BIT DRA
4100     BVC TSTPRT1 IRQ from controller?
4110     BPL TSTPRT2 DRQ?
4120     LDA DATREG  yes, read data byte
4130     STA SEKBUF,X and store it
4140     INX         six bytes
4150     BPL TSTPRT2
4160     LDA TIM64   reset timer
4170     LDA #1
4180 TSTPRT3 BIT FCNTRL
4190     BNE TSTPRT3 wait for controller ready
4200     LDA FCNTRL
4210     BNE TSTPRT1 error?
4220     LDA ASEK    test if sector <= maximum
4230     CMP SEKTOR
4240     BEQ TSTPRT4 br if so
4250     BMI TSTPRT4
4260 TSTPRT1 SEC     step takes too long
4270     RTS 
4280 *
4290 TSTPRT4 CLC     no error
4300     RTS 
4310 *
4320 * test step rate for all tracks
4330 *
4340 TSTEPR JSR TSTPRT
4350     BCS TSTEPR1
4360     LDA TRACK   already on track 39
4370     CMP #$27
4380     BNE TSTEPR  no, continue
4390     CLC         yes, no error
4400 TSTEPR1 RTS 
4410 *
4420 * verify all tracks (0 - 39)
4430 *
4440 TVER2 LDA #0
4450     STA TRACK   from track 0
4460     LDA #$28    to track 39
4470     STA SBUF+2
4480 TVER21 JSR TRKVER
4490     INC TRACK
4500     DEC SBUF+2
4510     BNE TVER21
4520     RTS 
4530 *
4540 * verify all tracks (39 - 0)
4550 *
4560 TVER1 LDA #$27  read from 39
4570     STA TRACK   down to 0
4580 TVER11 JSR TRKVER
4590     DEC TRACK
4600     BPL TVER11
4610     RTS 
4620 *
4630 * write to all tracks (0 - 39)
4640 *
4650 TWR1 LDA #0     from track 0
4660     STA TRACK
4670     LDA #$28    to 39
4680     STA SBUF+2
4690 TWR11 JSR TRKWRT write
4700     INC TRACK
4710     DEC SBUF+2
4720     BNE TWR11
4730     RTS 
4740 *
4750 * write to all tracks (39 - 0)
4760 *
4770 TWR2 LDA #$27   write from 39
4780     STA TRACK   down to 0
4790 TWR21 JSR TRKWRT
4800     DEC TRACK
4810     BPL TWR21
4820     RTS 
4830 *
4840 * verify track
4850 *
4860 TRKVER JSR SEEK goto track
4870     LDA #0
4880     STA FMERK+2 mark verify track
4890     LDY #2      read from sector 3
4900 TRKVER1 LDA #0
4910     STA FMERK   no error
4920 TRKVER2 LDA SKEWTB,Y
4930     BMI TRKVER3 end of table?
4940     INY 
4950     STA SEKREG  read & verify sector
4960     JSR VERSEK
4970     BEQ TRKVER1
4980     LDA FCNTRL
4990     AND #$1C    error?
5000     BNE TRKVER4 br if so
5010     LDA #6
5020     STA COUNT   read 6 times
5030 TRKVER5 DEC COUNT
5040     BEQ TRKVER6
5050     JSR VERSEK  read another time
5060     BEQ TRKVER5
5070     LDA FCNTRL
5080     AND #$1C    mask error bits
5090     BNE TRKVER5
5100     LDA #$FF    mark error, other than CRC, LOST DATA, RNF
5110     STA TRKERR
5120     JMP TRKVER4
5130 TRKVER6 LDA #0  no error, drive not ready etc.
5140     STA TRKERR
5150 TRKVER4 JSR FAUSW check error
5160     LDA FMERK+1 again?
5170     BEQ TRKVER1 no, continue
5180     DEY         same sector again
5190     JMP TRKVER2
5200 TRKVER3 RTS 
5210 *
5220 * write track
5230 *
5240 TRKWRT JSR SEEK move head to track
5250     LDA #$FF
5260     STA FMERK+2 mark write track
5270     LDY #2      from sector 3
5280 TRKWRT1 LDA #0
5290     STA FMERK   no error, now
5300 TRKWRT2 LDA SKEWTB,Y
5310     BMI TRKVER  end of table?
5320     INY 
5330     STA SEKREG  select sector and write
5340     JSR WRSEKTOR
5350     BEQ TRKWRT1 o.k.?
5360     JSR FAUSW   check error
5370     LDA FMERK+1 again?
5380     BEQ TRKWRT1 repeat
5390     DEY         continue
5400     JMP TRKWRT2
