[comp.sys.apple2] Disk // register listing

cs4w+@andrew.cmu.edu (Charles William Swiger) (11/03/90)

Anybody have a detailed description of the $C0Ex range of addresses? 
(Which are the registers that control a 5.25" drive at the lowest level
possible.)

I've disassembled DOS 3.3 RWTS, the $C600 boot code, parts of ProDOS,
etc and the function of some locations are obvious, but others aren't. 
Thanks.



-- Charles William Swiger
    cs4w+@andrew.cmu.edu

MQUINN%UTCVM@PUCC.PRINCETON.EDU (11/03/90)

On Fri, 2 Nov 90 17:12:29 GMT Charles William Swiger said:
>Anybody have a detailed description of the $C0Ex range of addresses?
>(Which are the registers that control a 5.25" drive at the lowest level
>possible.)
>
>I've disassembled DOS 3.3 RWTS, the $C600 boot code, parts of ProDOS,
>etc and the function of some locations are obvious, but others aren't.
>Thanks.
>
>
>
>-- Charles William Swiger
>    cs4w+@andrew.cmu.edu

ADDRESS    LABEL     DESCRIPTION
-----------------------------------------------------------------
$C080     PHASEOFF    Stepper motor phase 0 off.
$C081     PHASEON     STEPPER MOTOR PHASE 0 ON.
$C082     PHASE1OFF   STEPPER MOTOR PHASE 1 OFF.
$C083     PHASE1ON    STEPPER MOTOR PHASE 1 ON.
$C084     PHASE2OFF     {                 }
$C085     PHASE2ON      { YOU CAN FIGURE  }
$C086     PHASE3OFF     {   OUT THESE     }
$C087     PHASE3ON      {                 }
$C088     MOTOROFF    TURN OFF MOTOR.
$C089     MOTORON     TURN MOTOR ON.
$C08A     DRVOEN      ENGAGE DRIVE 1
$C08B     DRV1EN      ENGANE DRIVE 2
$C08C     Q6L         STROBE DATA LATCH FOR I/O
$C08D     Q6H         LOAD DATA LATCH.
$C08E     Q7L         PREPARE LATCH FOR INPUT.
$C08F     Q7H         PREPARE LATCH FOR OUTPUT.

       Q7L WITH Q6L = READ
       Q7L W/ Q6H = SENSE WRITE PROTECT
       Q7H W/ Q6L = WRITE
       Q7H W/ Q6H = LOAD WRITE LATCH

---------------------------

You need to at $x0 to $C08n where x is the slot number.

---------------------------

Addresses $C080 through $C087 are for moving the drive arm.  The arm moves in
QUARTER tracks so each access to one 'SET' of those addresses moves the arm
1/4 a track:

LDA $C0E0
LDA $C0E1

will move the arm a quarter track ($C0E0 for slot 6).
To move a whole track, cycle through the whole set of stepper motor phases:

LDA $C0E1
LDA $C0E0
LDA $C0E3
LDA $C0E2
.
.
LDA $C0E7
LDA $C0E6

The first access to one of these addressess starts up the arm and the other
stops it.  To move the arm in the opposite direction, cycle through these
the other way (the PHASEON of each set always comes first).

You must start the drive up before (or turn it on) by accessing $C0E9 first.
Immediately after starting it up, you must tell it WHICH drive:

LDA $C0EA ---->  access drive 1
LDA $C0EB ---->  access drive 2

You might want to set it to READ first, immediately after starting up the
drive.  I can't remember how to do this, you could probably figure it out
with the Q6L - Q7H things and a scratch disk.
You can also check to see if the disk is write protected with those four
addresses, although I can't recall how right now.
The data latch out $C0EF is the address you write to to actually write a
byte to disk, but you must have enabled write mode first somehow using
Q6 and Q7 H&L.  To READ from the disk, read from address $C0EE, but you
must first anable reading AND allow the drive time to get up to speed.

For those of you wondering why ProDOS anly allows two drives per slot, it's
probably because there are only two addresses per slot ($C0xA & $C0xB) to
determine which drive to access.

I got this info from "Beneath Apple DOS"... The most useful book I've ever
read to this date.  I imagine that "Beneath Apple ProDOS" would be even
better.  Anyone got a copy they want to sell?
By the way, those books are from Quality Software

If there is anyone that hasn't read either of these books, I HIGHLY recommend
them!  "Beneath Apple DOS" covers virtually every conceivable thing you would
want to know about DOS 3.3, including copy protection schemes and how to
break them and even how to make your own (It's easier to make your own than
to crack one :)
Even though these books are outdated (well, the ProDOS one may not be), they're
still EXTREMELY informative on Disk Operating Systems in General.  I find mysel
f explaining how DOS's work to sceniors in computer science and even to
computer science proffessors just from the stuff I learned from the DOS 3.3
book!

 ____________________________________________________________________
|                                    |                               |
| This is your brain...              |  BITNET-- mquinn@utcvm        |
| This is your brain on drugs...     |  pro-line:                    |
| This is your brain on frog licking.|    mquinn@pro-gsplus.cts.com  |
|____________________________________|_______________________________|

parkern@jacobs.CS.ORST.EDU (Neil Parker) (11/08/90)

In article <9011030453.AA02428@apple.com> MQUINN%UTCVM@PUCC.PRINCETON.EDU writes:
>On Fri, 2 Nov 90 17:12:29 GMT Charles William Swiger said:
>>Anybody have a detailed description of the $C0Ex range of addresses?
>>(Which are the registers that control a 5.25" drive at the lowest level
>>possible.)
>>
>>I've disassembled DOS 3.3 RWTS, the $C600 boot code, parts of ProDOS,
>>etc and the function of some locations are obvious, but others aren't.
>>Thanks.
>>
>>
>>
>>-- Charles William Swiger
>>    cs4w+@andrew.cmu.edu
>
>ADDRESS    LABEL     DESCRIPTION
>-----------------------------------------------------------------
>$C080     PHASEOFF    Stepper motor phase 0 off.
>$C081     PHASEON     STEPPER MOTOR PHASE 0 ON.
>$C082     PHASE1OFF   STEPPER MOTOR PHASE 1 OFF.
>$C083     PHASE1ON    STEPPER MOTOR PHASE 1 ON.
>$C084     PHASE2OFF     {                 }
>$C085     PHASE2ON      { YOU CAN FIGURE  }
>$C086     PHASE3OFF     {   OUT THESE     }
>$C087     PHASE3ON      {                 }
>$C088     MOTOROFF    TURN OFF MOTOR.
>$C089     MOTORON     TURN MOTOR ON.
>$C08A     DRVOEN      ENGAGE DRIVE 1
>$C08B     DRV1EN      ENGANE DRIVE 2
>$C08C     Q6L         STROBE DATA LATCH FOR I/O
>$C08D     Q6H         LOAD DATA LATCH.
>$C08E     Q7L         PREPARE LATCH FOR INPUT.
>$C08F     Q7H         PREPARE LATCH FOR OUTPUT.
>
>       Q7L WITH Q6L = READ
>       Q7L W/ Q6H = SENSE WRITE PROTECT
>       Q7H W/ Q6L = WRITE
>       Q7H W/ Q6H = LOAD WRITE LATCH

So far, so good.  The explanation which followed, however, might be a bit
misleading.  The following is taken from "Beneath Apple DOS" and "Beneath
Apple ProDOS", which I have sitting in front of me as I type.  I am also
using "Use of the Disk II Interface Card Through Your Own Software" by John
Uhley, from the April 1983 issue of "Apple Orchard" (which goes into
considerably more detail about stepping tracks than "Beneath Apple *DOS").

Basically, each track (and half-track) may be considered to be "under" one
of the four phases of the stepper motor.

     Track  Phase
     ----   -----
     0      0
     0.5    1
     1      2
     1.5    3
     2      0
     2.5    1
     3      2
     3.5    3
     etc..

To figure the phase for a given (half-)track, multiply the track number by
2, and keep only the two low-order bits.

Stepping from one track to another is simply a matter of stepping one track
at a time from the original track to the destination track.  Thus, to step
inward from track A to track B, first step to (half-)track A+0.5, then to
(half-)track A+1, and so on, until you arrive at track B.  Likewise, to
step outward from track B to track A, first step to (half-)track B-0.5, then
to B-1, and so on until you arrive at track A.

An individual step (which must from the original half-track to one if its
immediatly neighboring half-tracks) is accomplished by turning on the
appropriate phase, waiting, and turning off the phase.  An appropriate wait
may be obtained by loading the accumulator with #$56 and doing a JSR to the
Monitor's WAIT routine ($FCA8).  (DOS and ProDOS are able to obtain
improved speed by taking into account the fact that once the head is
moving, it takes less time to make subsequent steps.)

Note that this scheme requires DOS to keep track of which track it's
on--there's no way to ask the drive where the head is.  If the current
track number is unknown, the head must be "recalibrated" by assuming that
we're currently at track 35 (or beyond), and then seeking to track 0 (this
is what causes that awful GRRRRRINDing sound when you boot a 5.25" disk).

Assuming that the disk motor is on, the following code (lifted from the
Apple Orchard article) will step from CURTRK to DESTRK on the disk drive
whose slot number (*16) is stored in SLOT:

MAINLOOP  LDA CURTRK
	  CMP DESTRK
	  BEQ ALLDONE
	  BCC MOVEUP
	  BCS MOVEDOWN
;
; MOVE DOWN TO LAST PHASE
;
MOVEDOWN  DEC CURTRK
	  JMP DOWORK
;
; MOVE UP TO NEXT PHASE
;
MOVEUP    INC CURTRK
;
; COMPUTE PHASE NUMBER
; FROM THE "NEW" CURTRK
;
DOWORK    LDA CURTRK
	  AND #$03
	  ASL
;
; GET INDEXING FOR CUR SLOT#
;
	  ORA SLOT
	  TAY
;
; TURN ON PHASE TO MOVE &
; WAIT FOR PHYSICAL ACTION
;
          LDA $C081,Y
	  LDA #$56
	  JSR $FCA8
;
; TURN OFF PHASE (ALWAYS) %
; LOOP BACK TO CHECK ON NEW
; CURTRK-DESTRK RELATIONSHIP
;
	  LDA $C080,Y
	  JMP MAINLOOP
;
; ALL DONE... QUIT
;
ALLDONE    (Your code continues here)

Most other drive operations are simpler than stepping.

Turning on the drive:
	LDX SLOT
	LDA $C089,X
Once the drive is on, you have to wait for the motor to come up to speed.
The Apple Orchard article uses the following code for the delay:
	LDA #$EF
	STA WAIT
	LDA #$D8
	STA WAIT+1
MWAITA  LDY #$12
MWAITB  DEY
	BNE MWAITB
	INC WAIT
	BNE MWAITA
	INC WAIT+1
	BNE MWAITA
where WAIT is a location on zero-page.  Again, DOS is able to decrease the
delay by some clever timing.

Selecting drive 1:
	LDX SLOT
	LDA $C08A,X

Selecting drive 2:
	LDX SLOT
	LDA $C08B,X

Sensing write-protect:
	LDX SLOT
	LDA $C08D,X
	LDA $C08E,X
	BMI PROTECTED

Setting READ mode (necessary for reading):
	LDX SLOT
	LDA $C08E,X

Reading a byte (after selecting READ mode):
	LDX SLOT
READLP	LDA $C08C,X
	BPL READLP

Writing data:
First, sense the write-protect status.  Then,
	LDX SLOT
	LDA DATA1
	STA $C08F,X
	ORA $C08C,X
	(wait)
	LDA DATA2
	STA $C08D,X
	ORA $C08C,X
	(wait)
	LDA DATA3
	STA $C08D,X
	ORA $C08C,X
	(etc...)
The wait time must be such that the accesses to $C08C come EXACTLY 32
machine cycles apart.  Note also that the first data byte is written
differently than subsequent bytes--the access to $C08F in necessary to set
WRITE mode.  After all data is written, you should access $C08E to select
READ mode, in order to prevent data from being erased.

At one point (in the disk formatting code), DOS 3.3 uses code like this to
write data at 32-cycle intervals:
	LDA #$D5	(2 cycles)
	JSR WRITE9	(6)
	LDA #$AA	(2)
	JSR WRITE9	(6)
	.
	.
	.
WRITE9	CLC		(2)
WRITE7	PHA		(3)
	PLA		(4)
WRITE	STA $C08D,X	(5)
	ORA $C08C,X	(4)
	RTS		(6)

There is a LOT more to know about disk formatting than what is covered in
this post.  If you can find a copy of "Beneath Apple DOS", buy it--it will
tell you just about everything there is to know about the format of a 5.25"
disk.  "Beneath Apple ProDOS" has a similar, but somewhat abbreviated
discussion.

>|                                    |                               |
>| This is your brain...              |  BITNET-- mquinn@utcvm        |
>| This is your brain on drugs...     |  pro-line:                    |
>| This is your brain on frog licking.|    mquinn@pro-gsplus.cts.com  |
>|____________________________________|_______________________________|

          - Neil Parker
--
Neil Parker                 No cute ASCII art...no cute quote...no cute
parkern@jacobs.cs.orst.edu  disclaimer...no deposit, no return...
parker@corona.uoregon.edu   (This space intentionally left blank:           )

nparker@cie.uoregon.edu (Neil Parker) (11/09/90)

In article <1990Nov08.105414.22053@scion.CS.ORST.EDU> parkern@jacobs.cs.orst.edu (Neil Parker) writes:
>[lotsa stuff deleted]
>Assuming that the disk motor is on, the following code (lifted from the
>Apple Orchard article) will step from CURTRK to DESTRK on the disk drive
>whose slot number (*16) is stored in SLOT:

I realized after I posted this routine that I failed to state that CURTRK
and DESTRK should contain HALF-TRACK numbers, not TRACK numbers.  To
obtain the half-track number, multiply the track number by two.  For
example,
	LDA CURRENT_TRACK
	ASL
	STA CURTRK
	LDA DEST_TRACK
	ASL
	STA DESTRK

I apologize for any confusion caused by this omission.

          - Neil Parker
--
Neil Parker                 No cute ASCII art...no cute quote...no cute
nparker@cie.uoregon.edu     disclaimer...no deposit, no return...
parker@corona.uoregon.edu   (This space intentionally left blank:           )
parkern@jacobs.cs.orst.edu  (Sorry about the proliferation of net addresses...
the news reader on my usual machine is broken tonight, so I had to use
another machine.)

msuacm@plains.NoDak.edu (MSU ACM Student Chapter) (11/10/90)

I need a bit of help with the 3.5" disk drive register listings.  I know
basically well how the 5.25" disk works from Beneath Apple Dos and a bunch
of other miscellaneous publications... but what are the register listings
for the 3.5" drive???

Also... could somebody PLEASE binscii me a WORKING copy of the Chameleon?
One version I downloaded crashes into the monitor while trying to analyze
what disks are online.  I don't want the version that you ]EXEC into 
memory... I'd just like a working copy.

Thanks to all who reply...
Eric Ondler
<msuacm@plains.nodak.edu>