
 1000  *      .LIF
 1010  *---------------------------------
 1020  *      DOS 3.2.1 DISASSEMBLY $BD00-BE9F
 1030  *      BOB SANDER-CEDERLOF       3-3-81
 1040  *---------------------------------
 1050  CURRENT.TRACK       .EQ $478
 1060  DRIVE.1.TRACK       .EQ $478 THRU 47F (INDEX BY SLOT)
 1070  DRIVE.2.TRACK       .EQ $4F8 THRU 4FF (INDEX BY SLOT)
 1080  SEARCH.COUNT        .EQ $4F8
 1090  RETRY.COUNT         .EQ $578
 1100  SLOT                .EQ $5F8
 1110  SEEK.COUNT          .EQ $6F8
 1120  *---------------------------------
 1130  PHASE.OFF           .EQ $C080
 1140  PHASE.ON            .EQ $C081
 1150  MOTOR.OFF           .EQ $C088
 1160  MOTOR.ON            .EQ $C089
 1170  ENABLE.DRIVE.1      .EQ $C08A
 1180  ENABLE.DRIVE.2      .EQ $C08B
 1190  Q6L                 .EQ $C08C
 1200  Q6H                 .EQ $C08D
 1210  Q7L                 .EQ $C08E
 1220  Q7H                 .EQ $C08F
 1230  *---------------------------------
 1240  SECTOR     .EQ $2D
 1250  TRACK      .EQ $2E
 1260  VOLUME     .EQ $2F
 1270  DRIVE.NO   .EQ $35
 1280  DCT.PNTR   .EQ $3C,3D
 1290  BUF.PNTR   .EQ $3E,3F
 1300  MOTOR.TIME .EQ $46,47
 1310  IOB.PNTR   .EQ $48,49
 1320  *---------------------------------
 1330  PRE.NYBBLE          .EQ $B800
 1340  WRITE.SECTOR        .EQ $B86A
 1350  READ.SECTOR         .EQ $B8FD
 1360  READ.ADDRESS        .EQ $B965
 1370  POST.NYBBLE         .EQ $B9C1
 1380  SEEK.TRACK.ABSOLUTE .EQ $BA1E
 1390  *---------------------------------
 1400  ERR.WRITE.PROTECT   .EQ $10
 1410  ERR.WRONG.VOLUME    .EQ $20
 1420  ERR.BAD.DRIVE       .EQ $40
 1430  *---------------------------------
 1440         .OR $BD00
 1450         .TA $800
 1460  *---------------------------------
 1470  RWTS   STY IOB.PNTR SAVE ADDRESS OF IOB
 1480         STA IOB.PNTR+1
 1490         LDY #2
 1500         STY SEEK.COUNT  UP TO 2 RE-CALIBRATIONS
 1510         LDY #4
 1520         STY SEARCH.COUNT
 1530         LDY #1       POINT AT SLOT# IN IOB
 1540         LDA (IOB.PNTR),Y  SLOT# FOR THIS OPERATION
 1550         TAX
 1560         LDY #15      POINT AT PREVIOUS SLOT#
 1570         CMP (IOB.PNTR),Y  SAME SLOT?
 1580         BEQ .3       YES
 1590         TXA          SAVE NEW SLOT ON STACK
 1600         PHA
 1610         LDA (IOB.PNTR),Y  GET OLD SLOT#
 1620         TAX
 1630         PLA          STORE NEW SLOT #
 1640         PHA          INTO OLD SLOT# SPOT
 1650         STA (IOB.PNTR),Y
 1660  *---------------------------------
 1670  *      SEE IF OLD MOTOR STILL SPINNING
 1680  *---------------------------------
 1690         LDA Q7L,X    GO INTO READ MODE
 1700  .1     LDY #8       IF DATA DOES NOT CHANGE
 1710         LDA Q6L,X       FOR 96 MICROSECONDS,
 1720  .2     CMP Q6L,X       THEN THE DRIVE IS STOPPED
 1730         BNE .1       WOOPS! IT CHANGED!
 1740         DEY          TIME UP YET?
 1750         BNE .2       NO, KEEP CHECKING
 1760         PLA          GET NEW SLOT # AGAIN
 1770         TAX
 1780  *---------------------------------
 1790  .3     LDA Q7L,X    SET UP TO READ
 1800         LDA Q6L,X
 1810         LDA Q6L,X    GET CURRENT DATA
 1820         PHA          7 CYCLE DELAY
 1830         PLA
 1840         STX SLOT
 1850         CMP Q6L,X    SEE IF DATA CHANGED
 1860         PHP          SAVE ANSWER ON STACK
 1870         LDA MOTOR.ON,X   TURN ON MOTOR
 1880         LDY #6       COPY POINTERS INTO PAGE ZERO
 1890  .4     LDA (IOB.PNTR),Y
 1900         STA DCT.PNTR-6,Y
 1910         INY          DCT.PNTR .EQ $3C,3D
 1920         CPY #10      BUF.PNTR .EQ $3E,3F
 1930         BNE .4
 1940         LDY #3       GET MOTOR ON TIME FROM DCT
 1950         LDA (DCT.PNTR),Y
 1960         STA MOTOR.TIME+1  HIGH BYTE ONLY
 1970         LDY #2       GET DRIVE #
 1980         LDA (IOB.PNTR),Y
 1990         LDY #16      SEE IF SAME AS OLD DRIVE#
 2000         CMP (IOB.PNTR),Y
 2010         BEQ .5       YES
 2020         STA (IOB.PNTR),Y  UPDATE OLD DRIVE #
 2030         PLP          SET Z STATUS
 2040         LDY #0       TO FLAG MOTOR OFF
 2050         PHP
 2060  .5     ROR          CHECK LSB OF DRIVE #
 2070         BCC .6       DRIVE 2
 2080         LDA ENABLE.DRIVE.1,X
 2090         BCS .7       ...ALWAYS
 2100  .6     LDA ENABLE.DRIVE.2,X
 2110  .7     ROR DRIVE.NO SET SIGN BIT IF DRIVE 1
 2120         PLP          WAS MOTOR PROBABLY OFF?
 2130         PHP
 2140         BNE .9       NO, DEFINITELY ON
 2150  *---------------------------------
 2160  *      DELAY FROM 150 TO 180 MILLISECONDS,
 2170  *      DEPENDING ON WHAT GARBAGE IS IN A-REG
 2180  *---------------------------------
 2190         LDY #7       YES, WAIT A WHILE
 2200  .8     JSR $BA7F    ***BUG!!!*** SHOULD BE $BA7B
 2210         DEY          BUT IT WORKS ANYWAY....
 2220         BNE .8
 2230         LDX SLOT     RESTORE SLOT#
 2240  *---------------------------------
 2250  .9     LDY #4       GET TRACK #
 2260         LDA (IOB.PNTR),Y
 2270         JSR SEEK.TRACK
 2280         PLP          WAS MOTOR DEFINITELY ON?
 2290         BNE PROCESS.COMMAND  YES, MOTOR ON
 2300  *---------------------------------
 2310  *      MOTOR WAS OFF, SO WAIT REST OF MOTOR ON TIME
 2320  *      FOR APPLE DISK II, MOTOR ON TIME IS 1 SECOND.
 2330  *      PART OF THIS TIME IS COUNTED DOWN WHILE SEEKING
 2340  *      FOR THE TRACK.
 2350  *---------------------------------
 2360  .10    LDY #18      ABOUT 100 MICROSECONDS PER TRIP
 2370  .11    DEY
 2380         BNE .11
 2390         INC MOTOR.TIME
 2400         BNE .10
 2410         INC MOTOR.TIME+1
 2420         BNE .10
 2430  *---------------------------------
 2440  *      MOTOR ON AND UP TO SPEED, SO LET'S
 2450  *      FIND OUT WHAT THE COMMAND IS AND DO IT!
 2460  *---------------------------------
 2470  PROCESS.COMMAND
 2480         LDY #12      GET COMMAND
 2490         LDA (IOB.PNTR),Y
 2500         BEQ .8       NULL COMMAND, LET'S LEAVE
 2510         CMP #4       FORMAT?
 2520         BEQ .9       YES
 2530         ROR          SET CARRY=1 IF READ, =0 IF WRITE
 2540         PHP          SAVE ON STACK
 2550         BCS .1       READ
 2560         JSR PRE.NYBBLE  WRITE
 2570  .1     LDY #48      UP TO 48 RETRIES
 2580         STY RETRY.COUNT
 2590  .2     LDX SLOT     GET SLOT NUMBER AGAIN
 2600         JSR READ.ADDRESS
 2610         BCC .5       GOOD ADDRESS READ
 2620  .21    DEC RETRY.COUNT
 2630         BPL .2       KEEP TRYING
 2640  .3     LDA CURRENT.TRACK  GET TRACK WE WANTED
 2650         PHA          SAVE IT
 2660         LDA #96      PRETEND TO BE ON TRACK 96
 2670         JSR SETUP.TRACK
 2680         DEC SEEK.COUNT
 2690         BEQ .6       NO MORE RE-CALIBRATES
 2700         LDA #4
 2710         STA SEARCH.COUNT
 2720         LDA #0       LOOK FOR TRACK 0
 2730         JSR SEEK.TRACK
 2740         PLA          GET TRACK WE REALLY WANT
 2750  .4     JSR SEEK.TRACK
 2760         JMP .1
 2770  *---------------------------------
 2780  .5     LDY $2E      TRACK# IN ADDRESS HEADER
 2790         CPY CURRENT.TRACK
 2800         BEQ .10      FOUND RIGHT TRACK
 2810         LDA CURRENT.TRACK
 2820         PHA          SAVE TRACK WE REALLY WANT
 2830         TYA          SET UP TRACK WE ACTUALLY FOUNG
 2840         JSR SETUP.TRACK
 2850         PLA          TRACK WE WANT
 2860         DEC SEARCH.COUNT
 2870         BNE .4       TRY AGAIN
 2880         BEQ .3       TRY TO RE-CALIBRATE AGAIN
 2890  *---------------------------------
 2900  *      DRIVE ERROR, CANNOT FIND TRACK
 2910  *---------------------------------
 2920  .6     PLA          REMOVE CURRENT.TRACK
 2930         LDA #ERR.BAD.DRIVE
 2940  .7     PLP
 2950         JMP ERROR.HANDLER
 2960  *---------------------------------
 2970  *      NULL COMMAND, ON THE WAY OUT....
 2980  *---------------------------------
 2990  .8     BEQ RWTS.EXIT
 3000  *---------------------------------
 3010  *      FORMAT COMMAND
 3020  *---------------------------------
 3030  .9     LDY #3       GET VOLUME# WANTED
 3040         LDA (IOB.PNTR),Y
 3050         STA VOLUME   SET IN PLACE AND GO FORMAT
 3060         JMP FORMAT
 3070  *---------------------------------
 3080  *      READ OR WRITE COMMAND
 3090  *---------------------------------
 3100  .10    LDY #3       GET VOLUME# WANTED
 3110         LDA (IOB.PNTR),Y
 3120         PHA          SAVE DESIRED VOLUME# ON STACK
 3130         LDA VOLUME
 3140         LDY #14      STORE ACTUAL VOLUME NUMBER FOUND
 3150         STA (IOB.PNTR),Y
 3160         PLA          GET DESIRED VOLUME# AGAIN
 3170         BEQ .11      IF =0, DON'T CARE
 3180         CMP VOLUME   SEE IF RIGHT VOLUME
 3190         BEQ .11      YES
 3200         LDA #ERR.WRONG.VOLUME
 3210         BNE .7       UH OH!
 3220  *---------------------------------
 3230  .11    LDY #5       GET SECTOR# WANTED
 3240         LDA SECTOR   AND THE ONE WE FOUND
 3250         CMP (IOB.PNTR),Y   AND COMPARE THEM.
 3260         BNE .21      NOT THE RIGHT SECTOR
 3270         PLP          GET COMMAND FLAG AGAIN
 3280         BCC WRITE
 3290         JSR READ.SECTOR
 3300         PHP          SAVE RESULT; IF BAD, WILL BE COMMAND
 3310         BCS .21      BAD READ
 3320         PLP          THROW AWAY
 3330         JSR POST.NYBBLE
 3340         LDX SLOT
 3350  RWTS.EXIT
 3360         CLC
 3370         .HS 24       "BIT" TO SKIP NEXT INSTRUCTION
 3380  *---------------------------------
 3390  ERROR.HANDLER
 3400         SEC          INDICATE AN ERROR
 3410         LDY #13      STORE ERROR CODE
 3420         STA (IOB.PNTR),Y
 3430         LDA MOTOR.OFF,X
 3440         RTS
 3450  *---------------------------------
 3460  WRITE  JSR WRITE.SECTOR
 3470         BCC RWTS.EXIT
 3480         LDA #ERR.WRITE.PROTECT
 3490         BCS ERROR.HANDLER   ...ALWAYS
 3500  *---------------------------------
 3510  *      SEEK TRACK SUBROUTINE
 3520  *      (A) = TRACK# TO SEEK
 3530  *      (DRIVE.NO) IS NEGATIVE IF DRIVE 1
 3540  *                AND POSITIVE IF DRIVE 2
 3550  *---------------------------------
 3560  SEEK.TRACK
 3570         PHA          SAVE TRACK#
 3580         LDY #1       CHECK DEVICE CHARACTERISTICS TABLE
 3590         LDA (DCT.PNTR),Y   FOR TYPE OF DISK
 3600         ROR          SET CARRY IF TWO PHASES PER TRACK
 3610         PLA          GET TRACK# AGAIN
 3620         BCC .1       ONE PHASE PER TRACK
 3630         ASL          TWO PHASES PER TRACK, SO DOUBLE IT
 3640         JSR .1       FIND THE TRACK
 3650         LSR CURRENT.TRACK  DIVIDE IT BACK DOWN
 3660         RTS
 3670  *---------------------------------
 3680  .1     STA TRACK
 3690         JSR GET.SLOT.IN.Y
 3700         LDA DRIVE.1.TRACK,Y
 3710         BIT DRIVE.NO   WHICH DRIVE?
 3720         BMI .2       DRIVE 1
 3730         LDA DRIVE.2.TRACK,Y
 3740  .2     STA CURRENT.TRACK   WHERE WE ARE RIGHT NOW
 3750         LDA TRACK    WHERE WE WANT TO BE
 3760         BIT DRIVE.NO WHICH DRIVE?
 3770         BMI .3       DRIVE 1
 3780         STA DRIVE.2.TRACK,Y   DRIVE 2
 3790         BPL .4       ...ALWAYS
 3800  .3     STA DRIVE.1.TRACK,Y
 3810  .4     JMP SEEK.TRACK.ABSOLUTE
 3820  *---------------------------------
 3830  *      CONVERT SLOT*16 TO SLOT IN Y-REG
 3840  *---------------------------------
 3850  GET.SLOT.IN.Y
 3860         TXA          SLOT*16 FROM X-REG
 3870         LSR
 3880         LSR
 3890         LSR
 3900         LSR
 3910         TAY          SLOT INTO Y
 3920         RTS
 3930  *---------------------------------
 3940  *      SET UP CURRENT TRACK LOCATION
 3950  *      IN DRIVE.1.TRACK OR DRIVE.2.TRACK VECTORS,
 3960  *      INDEXED BY SLOT NUMBER.
 3970  *
 3980  *      (A) = TRACK# TO BE SET UP
 3990  *---------------------------------
 4000  SETUP.TRACK
 4010         PHA          SAVE TRACK # WE WANT TO SET UP
 4020         LDY #2       GET DRIVE NUMBER FROM IOB
 4030         LDA (IOB.PNTR),Y
 4040         ROR          SET CARRY IF DRIVE 1, CLEAR IF 2
 4050         ROR DRIVE.NO MAKE NEGATIVE IF 1, POSITIVE IF 2
 4060         JSR GET.SLOT.IN.Y
 4070         PLA          GET TRACK #
 4080         ASL          DOUBLE IT
 4090         BIT DRIVE.NO  WHICH DRIVE?
 4100         BMI .1       DRIVE 1
 4110         STA DRIVE.2.TRACK,Y
 4120         BPL .2       ...ALWAYS
 4130  .1     STA DRIVE.1.TRACK,Y
 4140  .2     RTS
 4150  *---------------------------------
 4160  FORMAT

