,. ~ -$"'. ----·~'.! ,,,-.~- ., -·' ~_,___-~_ _.,,,.:,·,_._·~,~., __,__ ...,,_. ~ --- - _,~~ • ~---''- .~..A - - . • ~ • • • ·-. __ ,___ •. ~-"- -'- - ,.. _..,._ ·-·'·- SOFnlARE CONTROL OF THE DISK II OR IWM CONTROLLER PREPARED BY N t APRIL 26, l '984 · REVISION l, MAY 10, 1984 SOFTWARE CONTB.OL OF THE DISK II OR IWM CONTROLLER. Each of the eight expansion elota of the Apple II computer baa the ezcluaive uae of sixteen memory locations for 1/0 control. These memory loc.atioaa can be used as software switches. A eoftware routine can instruct the interface card in a particular slot to perfor11 a predefined hardware task bJ toggling a software switch. Whenever the Apple II addresses one of the sixteen 1/0 loca- tions allocated to a particular alot, the signal on pin 41 of that slot, called DEVICE SELECT/, switches to the active low state. This signal, 1n conjunction with the four low-order address lines (AO - A3), can be used to enable logic in the peripheral card to perform a particular task. The folloving table illustrates the memory space for the sixteen 1/0 locations for each expansion slot. TABLE 1: PERIPHERAL CARD I/O SPACE SLOT LOCATIONS 0 $C080 - $C08F l $C090 - $C09F 2 $COAO - $COAF 3 $COBO - $COBF 4 $COCO - $COCF 5 $CODO - $CODF 6 $COEO - $COEF 1 $COFO - $COFF One common method of accessing peripheral card I/O control softswitches through software is to use the Apple's (6502) indexed addressing mode. For example, a LDA $C080, X instruction can be used to access softswitch 0 in any slot, if the index register X is loaded with a value equal to the slot number times sixteen. Sixteen peripheral I/O addresses are used ~o co~trol the functions of the disk II controller. the following table illustrates the functions of the sixteen software switches. TABLE 2: DISK II CONTROLLER SOFTSWITCHES ADDRESS FUNCTION $C080, x PHASE 0 OFF $C081, x PHASE 0 ON $C082, x PHASE 1 OFF $C083, x PHASE l ON $C084, x PHASE 2 OFF $C085, x PHASE 2 ON $C086, x PHASE 3 OFF $C087, x PHASE 3 ON $C088, x TURN MOTOR OFF $C089, x TURN MOTOR ON $C08A, x SELECT DR.IVE 1 $C08B,, x SELECT DRIVE 2 $C08C, x Q6L $C08D, x Q6H $C08E. x Q7L $C08F, x Q7H The· index register X has the. value of •lot number times 16. The last four addresses have the following functions. FUNCTION L L READ H L SENSE WRITE PROTECT OR PREWRITE STATE L H WRITE B H WRITE LOAD In general, any valid 6502 instruction can be used to access the above eoft- awitch address, except a load instruction is used to read a byte of encoded data from the controller and a store instruction is used to write a byte of encoded data to the controller. Below are typical examples demonstrating the use of the disk II controller aoftswitches. It is assumed that both Q6 and Q7 are low at the beginning of the read/write examples. SELECT DRIVE LDA $C08A, X SELECT DRIVE 1 LDA $C08B, X SELECT DRIVE 2 The hardware design of the controller allows only one drive to be selected at one time. A LOA $C08A, X instruction will select drive 1 and deselect drive z. A LDA $C08B, X instruction will select drive 2 and deselect drive 1. MOTOR ON LDA $C089, X TURN MOTOR ON LDA $C088, X TURN MOTOR OFF It should be noted that there is only one interface signal (ENABLE/), going from the controller to each floppy disk drive, which is used to enable the drive' s read/write function and to turn on the motor. Both the select drive and the motor on instructions must. be executed in order to activate the ENABLE/ signal of a parti.cular drive. A typical program-will select the drive first and then turn on the motor at a later time. After the completion of the motor on instruction, the program should wait at least 1 second for the motor to come up to speed, before read/write functions can be performed reliably. The disk II controller hardware will keep the ENABLE/ signal to its active low state for approximately one second after the execution of the motor off in- struction, therefore read/write can be performed reliably within this period. To be on the safe side, the program should verify that the motor is spinning by monitoring the change in data pattern read from the drive. This delay in turning off the motor facilitate rapid and repeat access to the same drive. SENSE WRITE PROTECT LDA Q6H, X WRITE PROTECT SENSE MODE LDA Q7L, X READ CONTROLLER STATUS REG. BMI WRPROT BRANCH IF BIT 7 OP STATUS REG. IS HIGH The above instruction will load the content of the controller's status reg- ister into the accumulator. Bit 7 of the status register is the write protect .lag. A "one'" in bit 7 of the status register indicates that a write protected diskette is inserted into the drive. A BMI instruction will check the write protect flag. The program will branch to the WRPROT address label if the write protect flag is set. llEAD A DATA BYTE LDA Q7L, X MAKE SURE IN READ l«>DE LOOP LDA Q6L, X READ nlE BYTE BPL LOOP STAY IN nlE LOOP U THE M.S. BIT IS LOW • • • • LP LDA Q6L, X READ ANOTHER BYTE BPL LP SAY IN THE LOOP IF THE M.S. BIT IS LOW • NOTE: THERE SHOULD BE NO PAGE CROSSING FOR THE BPL INSTRUCTIONS. The LDA Q7L, X instruction makes sure that the controller is in read mode. The LDA Q6L, X instruction loads the contents of the controller's data shift register into the accumulator. Since the Apple GCR code requires that the most significant bit of every encoded data byte is high, the BPL instruction will force the program to stay in a two instruction loop 1mtil the M.S. bit of the controller data shift register is high. At the beginning of every byte time, the controller internal logic will clear the data shift register. As data bits are shifted into the data shift register, the M.S. bit of the register is high, if and only if a full byte of data is assembled in the register. The data byte will stay in the register for a little more than 7 us. Therefore, it is important to make sure that the BPL instruction does not cross the page boundary. This is necessary to ensure that the exe- cution time of the two ~nstruction loop (LDA, BPL} is no more than.7 us. WRITE A DATA BYTE LDA Q6H, X GO TO LDA Q7L, X PREWRITE STATE LDA DATA STA Q7H, X PARALLEL LOAD DATA INTO CONTROLLER - - - - - - - - - - LDA Q6L, X CONTROLLER SHIFT DATA OUT SERIALLY EXECUTION TIME OF THESE INSTRUCTIONS MUST BE EXACTLY • 32 CLOCK CYCLES • STA Q6H, X PARALLEL LOAD ANOTHER BYTE - - - - - - - - - - LDA Q6L, X SHIFT OUT DATA • • • LDA Q7L, X OUT OF WRITE MODE LDA .Q6L, X TO READ MODE NOTE: It is important to write a garbage byte (HEX FF) before turning off the write mode, so that the drive electronics has enough time to write the last valid data byte. .. The first two instruction• force the controller into the prewrite atate. These are the 1ame instructions to aenae the write protect flag. It ia important to execute these two instructions even the programmer does not want to 1en1e the write protect flag. The STA instruction loads the contents of the accum- ulator into the controller'• data shift register. The next instruction (LDA Q6L,' X] causes the data in the register to shift oat aerially. Q6H and Q78 are the conditions required for parallel loading the data into the controller. Shifting out the data aerially to the disk drive requires Q6L and Q7H. The first STA instruction sets Q7 high, because the conditions are Q6B and Q7L before executing this instruction. The condition. are Q6L and Q7H before the second STA instruction, therefore the second STA instruction sets Q6H. The execution time of the instructions between the end of two consecutive parallel load instructions {STA] has to be exactly 32 clock cycles, otherwise invalid data will be written on the diskette. In order to calculate the exe- cution time, it is important to note that the 6502 processor requires one additional execution cycle for branching or indexing operations crossing the page boundary. The program should switch the controller back to the read mode after all the data has been written. WRITE SELF SYNC BYTE LDA Q6H, X LDA Q7L, X LDA D$FF STA Q7H, X ' l'ARALLEL LOAD AUTO SYNC BYTE ORA Q6L, X START TO SHIFT OUT AUTO SYNC BYTE EXECUTION TIME ORA IS USED SO THAT LDA #$FF IS NOT I OF MUST THESE INSTRUCTIONS BE EXACTLY . NEEDED TO WRITE THE NEXT SYNC CYCLE 40 CLOCK CYCLES STA Q6H, x PARALLEL LOAD ANOTHER AUTO SYNC BYTE ORA Q6L, x SHIFT OUT SYllC BYTE SELF SYNC BYTE 40 CLOCK CYCLES • I LDA STA LDA DATA Q6H, X Q6L, X LOAD FIRST DATA BYTE SHIFT OOT DAtA BYTE DATA BYTE 32 CLOCK CYCLES • • ~~~~~~~~~~ STA Q6B, X LDA Q6L, X • • • LDA Q7L, X OUT OF WRITE MODE LDA Q6L, X TO READ MODE NOTE: Write a garbage byte {HEX FF) before turning off write mode. t The Apple CCR code uses a unique synchronization technique to deteraia.e the byte boundary. A •elf sync byte consists of eight bita of ·1· and two bit• of ·o.· The procedure to write a sync byte is the same as to write a data byte, except that the execution time of the instructions between the end of two consecutive parallel load sync byte instructions has to be exactlJ 40 us. During the write sync byte time, the processor loads eight bits of ·1· into the controller. After shifting out 8 bits of ·1·, the controller hardware will shift out ·o· until the next parallel load instruction. Since there are 40 us between two consecutive parallel load instructions and a 4 us bit time, 8 bits of •1• from the processor and 2 bits of ·o· appended by the coatroller hardware are shifted out to the drive. It is necessary to write at least five self sync bytes at the beginning of both the address and data field. READ SELF SYNC BYTE Due to the Apple GCR code's unique synchronization technique, the coutroller hardware will determine the byte boundary automatically. The following is a brief description of the Apple synchronization technique. FIGURE l: SYNCHRONIZATION PROCESS lST STH SYNC BYTE SYNC BYTE ,l l l l l l ll,OO,l l ll ll l l,PO). l lll l l lp0,11111111,00).1111111,00 1111111100,l l l l l l l l,001111111100,ll ll l 11lpop111111,oo lltlllllOOJllllll~OOtlllllllpO~lllllllpOJllllllbOO 111J1111001Jllllll0,0Jllllll~OOJ111llllp0Jlllllll,OO 111111110011111111001111111100,11111111,001111111100 ltlllf 110011~1111100\JllllllQOJlillll~OO!lllllllpO lllll~llOOllll}lllOOllJ.1111100,!llllll~OOJllllllbOO lllllll.,10011111Jll00111J.llll001,J.llllllq~ In the above diagram, each row of brackets represent what the controller will send out to the Apple II should the controller start reading at any given bit in the first self sync byte. The controller groups the self sync read data stream into 8-bit byte with a ·1· in the most significant bit of each byte. Any "O" bit between bytes are dropped out. From the above diagram, it is shown that the controller is able to group the data at the correct byte boundary within five byte time after the beginning of the read. This is alwa7s true for any bit position to start the reading. Therefore, a minimum of five self sync bytes are required for the controller to sync on the read data. After the fifth self sync byte, the controller has established the byte boundary and is able to read the data following the sync bytes correctly. The -05 AA 96" and "DS AA AD" address mark sequences follows the self sync bytes in the address and data field respectively. It is not necessary to read and verify the sync byte. In order to read/write a sector, the program should look for the "DS AA 96" sequence which are the address mark bytes for the address field. The. D5 and AA patterns are reserved for addreaa aark. These pattern• are not used to encode data. Therefore, byte ayncronizatioa for the addre11 field ia achieved by aearching for the •D5 >.A 96" aequence. Byte aynchronization for the data field is done by looking for the •Ds AA AD• aequence. SEEK'TO ANOTHER TRACK The stepper motor in the Diak II ia a four phase atepper motor. Eight 1/0 control aoftswitches are used to toggle the four phase on and off as shown in table 2. Two adjacent phases have to be activated in aequence in order to move the R/W head to the adjacent track. Activating the phases in ascending order (0, l, 2, 3, O, 1, ••• ) moves the head towards (inward) the center of the diskette. The head moves away (outward) from the center of the diskette when the phases are activated in descending order (3, 2, 1, O, 3, 2, ••• ). All even numbered tracks are positioned under phase 0 and all odd numbered ti:.acks are under phase 2. In order to step in a track, the phase 1 and then phase 2 have to be activated in sequence from an even numbered track, while the phase 3 and then phase 0 is activated in sequence from an odd numbered track. The phase 3 and then phase 2 sequence is used to step out a track from an even numbered track. For stepping out a track from an odd numbered track, the phase 1 and then phase 0 sequence is used. The spindle motor should be on for 150 ms before starting the seek operation. The following is an example to\ step in a track from an even numbered track. LDA $C083, X TURN ON PHASE 1 ll.5 msec delay loop • LDA $C085, X TURN ON PHASE 2 0.1 msec delay loop • LDA $C084, X TURN OFF PHASE 1 • 36.6 msec delay loop • LDA $C086, X TURN OFF PHASE 2 The above programming example is used to illustrate the timing required to step in a track from an even numbered track. The user may use a indexed look up table for the parameter required for different delay loops. No matter how many tracks to step, the user has to allow the last phase to be on for 36.6 msec, because this timing includes the head settling time requirement (25 ms) of the drive. For long seek (step a number of tracks), tvo adjacent phases can over- lap the phase on time in order to increase the torque of the stepper motor and to reduce the seek time. Since the timing between the phase ON/OFF time is critical, it is recommended that the user calls upon the SEEK ~outine in I the Apple DOS for seeking. Figure 2 shows the waveforms of the phases to seek from track 0 to track 9. ,"\ FIGURE 2: PHASE WAVEFORMS TO SEEK FROM TRACK 0 TO TRACK 9 .. 3.6 2.8 3 4. l 4 ~I~; { 2.9 ~~·~IL PHASE 3 11. l 18.6 3.6~ ~~ )"~ PHASE 2 1 13.2 1 19 1 re:-.s1 r1a- -, 1 36.6 L 4. 8 -.1 ~ I 3 .1 " ~ l a-tt "' l 2. 3. 2 ~ i..- I o.1 ~~ PHASE i I 16.2 I 19.4 I 1s.s l I 9.3 I 19.4 I________ I ~ iE-3.~ I -ti~ 2.8 I-~ ~3 I ~ ~4.8 PHASE 0 I lo.3 l . I s.s l I s.91 I 12.1 I_________ NOTE: ALL THE NUMBERS SHOWN IN THE DIAGRAM ARE IN MILLISECONDS. * ** ** ***** * **** DATE: Hay 29, 1984 ***************** •••4************ TO: Distribution *************** ************** FROM: N L ************** ************** SUBJECT: Software Routine to Determine A State Machine or IWM Controller The attached software routine will determine whether a state machin.e disk II controller or an IWM controller is installed in the system. This routine may be useful for programmer writing copy protection codes for Apple II. Upon exiting from the routine, the IWM controller will restore back to synchronous mode (line no. 46 and 47), since all our current applications of IWM for Apple II is in that mode. Future application of the IWM may require the asynchronous mode, the user should set up the mode register (line 46} accordingly. NL:npk Attachment DISTRIBUTION M. A:__ p B, c c B G J, s R w :E FJLE 181 •>JW1 flOwdl J • 18881 2. 18881 3 •THIS ROUTINE WILL DETERHJNE WHETHER A STATE HACHJNE DJSK JJ CCNTROLLI 88881 4 • OR AN IW'1 CCNTROLLER IS INSTALLED JN THE SYST8'1. 1888: S • UPON EXIT FRCli THE ROUTINE, Y•l MEANS JU1 CCNTROLLER AND Y•8 MEANS 8888: 6 •STATE MACHINE DISK JJ C~ROLLER. 8888: 7 • 88881 8 t ASSlJ1E A MOTOR OFF INSTRUCTJC't4 CLOA $C888,Xl HAS BEEN EXECUTED FOR 8888: 9 •TWO SECIJ'.lOS BEFORE THE USER CALLS €1'4 THIS ROUTINE. OTHERWISE, A 88881 18 t TWO SECIJ'.lO DELAY LOOP HUST BE ADDED AFTER THE FIRST HOTOR OFF 0888: 11 t INSTRUCTJa-. CLDA SC888,Xl AT THE BEGI~ING OF THIS ROUTINE. 8888: 12 I 8888: 13 t THE ENABLED DISK DRIVE WILL CCNTINUE TO BE Cl" FOR 1 SEC 8888: 14 t AFTER EXIT FR(J1 THIS ROUTINE. 6888: 15• 8888: 16 I ----- NEXT OBJECT FILE NttiE IS ILJH.8 1888: 1888 l7 ORG $1088 J800:AE 41 18 18 LOX SLOTX16 ;X REG=SLOT NO. X 16 t883:eo ss C8 19 LOA $C888,X ;MOTOR OFF 1eeo:A0 88 28 LOY 188 ;CLEAR REG. Y 1e0e:eo so C8 21 LOA $C88D,X ;Q6H 1000:00 sF C8 22 LOA $C88F ,X ;Q7H, ADDRESS MODE REG. l'" - ~A9 84 23 LOA #$84 . 90 BF ce 24 STA $C08F,X ;DISABLE TIMER BIT IN MODE REG • 1a13:BD SE C8 25 LOA tC08E,X ;Q7L, OUT OF WRITE NODE 1816:80 89 CB 26 LOA $C889,X ;HOTOR ct4 181 9 : 48 27 LOOP PHA Jl8 HSEC DELAY LOOP 181A:6S 28 PLA .;WAIT FOR THE MOTOR a-. 181s:48 29 ' PHA ;SIGNAL TO BE ACTIVE IN J 81C:68 38 PLA JTHE CCNTROLLER CARD 1810:48 31 PHA 181E:68 32 PLA 181F:48 33 PHA 1828:68 34 PLA 1S21:48 35 PHA 1822:68 36 PLA 1823:C8 37 INY 1824:08 F3 1819 38 BNE LOOP ;END OF DELAY LOOP 1s26:eo BE ce - 39 LOA $C88E,X J07L, READ STATUS REG. 1829:90 88 cs 41 STA tceee,x ;MOTOR OFF 182C:29 lF 41 ANO HlF JHASK 5 L.S. 8JTS 182E:C9 84 42 01P 1$84 ;CHECK TIMER BIT 1838:08 89 1838 43 BNE DISKII ;DISK II CC't4TROLLER IF NOT EQ l832:C8 44 I~ INY ;INCREMENT Y REG. 1833:80 SF CS 45 LOA tC88F,X ;07H, ADDRESS MODE REG. 1836:A9 88 46 LOA ff 88 1838:90 SF ce 47 STA tC88F,X 107H, RESTORE HOOE REG. lp 90 SE ca 48 DJSKII LOA $C88E,X ;Q7L, L .SD BC ce 49 LOA tC8BC,X ;Q6L 1 RESTORE TO READ HOOE 1841: 1041 58 FIN EQU 1841: 68 51 SLOTXl 6 OFB * $68 u.,/".. OJSKJI ?1141 FJN ?1832 Jt.11 1119 LOOP 1841 SUJTXl & •• SUCCESSFUL ASSEMBLY 1• NO ERRORS •· '~SEMBLER CREATED 0'4 15-JAN-84 21128 .TAL Llt-4ES ASSEMBLED Sl ~EE SPACE PAGE COltO' 89