home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDX500 / IDE.S < prev    next >
Encoding:
Text File  |  2001-02-09  |  24.7 KB  |  904 lines

  1. ;+
  2. ; Edit History
  3. ;
  4. ; Sep-05-90    ml.    Created this for drives on the IDE bus.
  5. ;
  6. ; Mar-28-91    ml.    Do not use the IDE Sector Count Register 
  7. ;            to count down mulitiple sectors read or
  8. ;            write.  Some vendors (e.g. Conner and 
  9. ;            Seagate) seem to update this register 
  10. ;            too early, and every now and then, the
  11. ;            last sector of data would remain in the
  12. ;            internal sector buffer.  
  13. ;            
  14. ;            Code in ideread() and idewrite() are modified
  15. ;            not to use the IDE Sector Count Register.
  16. ;
  17. ; Aug-16-91    ml.    W4int() has been modified, in the case of
  18. ;            an error occurred, to return value in the 
  19. ;            Status register for driver code, and return 
  20. ;            value in the Error register for non-driver
  21. ;            code.
  22. ;
  23. ; Oct-10-91    ml.    Special-cased Conner drives for fmtunt() to
  24. ;            set drive to appropiate mode.  Added setmode().
  25. ;
  26. ; Oct-11-91    ml.    Special-cased Conner drives for read and write
  27. ;            to get current drive parameters.  Added gcparm().
  28. ;-
  29.  
  30. .include    "defs.h"
  31. .include    "sysvar.h"
  32. .include    "mfp.h"
  33. .include    "iderr.h"
  34. .include    "ide.h"
  35. .include    "blitter.h"
  36.  
  37. .extern    _useblit
  38.  
  39. ;+
  40. ; Wait for status to come back
  41. ;-
  42. w4int:    move.l    #D_WORST,d0    ; d0 = timeout limit
  43.     add.l    _hz_200,d0    ; d0 = expiration time
  44. .0:    btst.b    #5,GPIP        ; interrupt?
  45.     beq.s    .1        ; if so, out of the loop
  46.     cmp.l    _hz_200,d0    ; timeout?
  47.     bhi.s    .0        ; if not, wait some more
  48.     moveq    #$ff,d0        ; else, return timeout
  49.     bra.s    .3
  50. .1:    moveq    #0,d0        ; clear d0
  51.     move.b    IDESR,d0    ; d0.b = status returned
  52.     btst    #ERR,d0        ; any error?
  53.  
  54. .if    !DRIVER            ; for non-driver code
  55.     beq.s    .2        ; if no error, go on
  56.     move.b    IDEER,d0    ; else d0.b = error bits
  57.     bra.s    .3        ; return with error
  58.  
  59. .else                ; for driver code
  60.     bne.s    .3        ; if error, return
  61. .endif    ;!DRIVER
  62.  
  63. .2:    btst    #DRQ,d0        ; else DRQ?
  64.     bne.s    .3        ; if so, just return
  65.     moveq    #0,d0        ; else return OK
  66. .3:    rts            ; return status or error code
  67.  
  68.  
  69.  
  70. ;+
  71. ; ideread() - reads from 1 to 256 sectors as specified in the Task File,
  72. ;        beginning at the specified sector.
  73. ;        - sector count equal to 0 requests 256 sectors.
  74. ;
  75. ; ideread(nhd, nspt, sectnum, count, buf, pdev)
  76. ; WORD    nhd;        4(sp).w        ; # of data heads on pdev
  77. ; WORD    nspt;        6(sp).w        ; # of sectors per track
  78. ; LONG    sectnum;    8(sp).l        ; logical block address
  79. ; WORD    count;        $c(sp).w    ; # of sectors to read
  80. ; BYTE    *buf;        $e(sp).l    ; $f(sp)=high $10(sp)=mid $11(sp)=low
  81. ; WORD    pdev;        $12(sp).w    ; physical device number
  82. ;-
  83.     .globl    _ideread
  84. _ideread:
  85.     bsr    set_dhcs    ; set physical address
  86.     move.l    $e(sp),a0    ; a0 -> buffer to read into
  87.     move.b    $d(sp),IDESC    ; set sector count
  88.  
  89.     move.w    $c(sp),d1    ; d1.w = # of sectors to read
  90.     subq    #1,d1        ; dbra likes one less
  91.  
  92.     tst.b    _useblit    ; BLiTTER exists?
  93.     beq.s    .0        ; if not, don't use it
  94.     moveq    #0,d0        ; else it's a read
  95.     bsr    initblit    ; initialize the BLiTTER
  96.  
  97. .0:    move.b    #0,IDEDOR    ; enable interrupt
  98.     move.b    #READ,IDECR    ; set command code
  99. .1:    bsr    w4int        ; wait for interrupt
  100.     tst.w    d0        ; successful?
  101.     bmi.s    .2        ; if timed-out, return
  102.     btst    #DRQ,d0        ; DRQ?
  103.     beq.s    .2        ; if not, return
  104.                 ; else
  105.     bsr    readbuf        ; transfer data
  106.     dbra    d1,.1        ; go wait for next interrupt
  107.     moveq    #0,d0        ; everything is fine
  108. .2:    rts
  109.  
  110.  
  111. ;+
  112. ; idewrite() - writes from 1 to 256 sectors as specified in the Task File,
  113. ;        beginning at the specified sector.
  114. ;         - sector count equal to 0 requests 256 sectors.
  115. ;
  116. ; idewrite(nhd, nspt, sectnum, count, buf, pdev)
  117. ; WORD    nhd;        4(sp).w        ; # of data heads on pdev
  118. ; WORD    nspt;        6(sp).w        ; # of sectors per track
  119. ; LONG    sectnum;    8(sp).l        ; logical block address
  120. ; WORD    count;        $c(sp).w    ; # sectors to read
  121. ; BYTE    *buf;        $e(sp).l    ; $f(sp)=high $10(sp)=mid $11(sp)=low
  122. ; WORD    pdev;        $12(sp).w    ; physical device number
  123. ;-
  124.     .globl    _idewrite
  125. _idewrite:    
  126.     bsr    set_dhcs    ; set physical address
  127.     move.l    $e(sp),a0    ; a0 -> buffer to write from
  128.     move.b    $d(sp),IDESC    ; set sector count
  129.  
  130.     move.w    $c(sp),d1    ; d1.w = # of sectors to read
  131.     subq    #1,d1        ; dbra likes one less
  132.  
  133.     tst.b    _useblit    ; BLiTTER exists?
  134.     beq.s    .0        ; if not, don't use it
  135.     moveq    #1,d0        ; it's a write
  136.     bsr    initblit    ; initialize the BLiTTER
  137.  
  138. .0:    move.b    #0,IDEDOR    ; enable interrupt
  139.     move.b    #WRITE,IDECR    ; set command code
  140. .1:    btst.b    #DRQ,IDEASR    ; DRQ?
  141.     beq.s    .1        ; if not, wait longer
  142.  
  143. .2:    bsr    wrtbuf        ; transfer data
  144.     bsr    w4int        ; wait for interrupt
  145.     tst.w    d0        ; successful?
  146.     bmi.s    .3        ; if timed-out, return
  147.     btst    #DRQ,d0        ; DRQ?
  148.     beq.s    .3        ; if not, return
  149.     dbra    d1,.2        ; else go transfer data
  150.     moveq    #0,d0        ; everything is fine
  151. .3:    rts
  152.  
  153.  
  154. ;+
  155. ; set_dhcs() - convert a logical block address into a physical address.
  156. ;         - set drive #, head #, cylinder # and sector # in task file.
  157. ;
  158. ; Passed:
  159. ;    8(sp).w = nhd = # of data heads
  160. ;    $a(sp).w = nspt = # of sectors per track
  161. ;    $c(sp).l = logical block address
  162. ;    $16(sp).w = physical unit #
  163. ;-
  164. set_dhcs:
  165.     move.l    $c(sp),d1    ; d1.l = logical block address
  166.     move.w    8(sp),d2    ; d2.w = # of data heads
  167.     move.w    $a(sp),d0    ; d0.w = # of sectors per track
  168.     mulu    d0,d2        ; d2.l = # of sectors per cylinder
  169.                 ;      = # heads * # of sectors per track
  170.     divu.w    d2,d1        ; d1.w = cylinder #
  171.                 ;      = log block addr / #spc
  172.     move.b    d1,IDECL    ; set cylinder low
  173.     lsr.l    #8,d1        ; d1.b = cylinder high
  174.     move.b    d1,IDECH    ; set cylinder high
  175.     lsr.l    #8,d1        ; d1.l = sector # within the cyl
  176.     divu.w    d0,d1        ; d1.w = head #
  177.                 ;      = sector # within cyl / #spt
  178.     move.w    $16(sp),d0    ; d0.w = physical unit #
  179.     andi.b    #7,d0        ; mask off flags from physical unit #
  180.     lsl.b    #4,d0        ; shift unit # to place
  181.     or.b    d0,d1        ; or in drive #
  182.     move.b    d1,IDESDH    ; set drive and head #
  183.     swap    d1        ; d1.w = sector # (base 0)
  184.     addq.w    #1,d1        ;      = sector # + 1 (base 1)
  185.     move.b    d1,IDESN    ; set sector #
  186.     rts
  187.  
  188. ;+
  189. ; identify() - allows the Host to receive parameter information from
  190. ;           the drive.
  191. ;
  192. ; identify(pdev, buf)
  193. ; WORD    pdev;    4(sp).w        ; physical unit #
  194. ; BYTE    *buf;    6(sp).l        ; buffer to put data
  195. ;-
  196.     .globl    _identify
  197. _identify:
  198.     move.w    4(sp),d0    ; d0 = physical unit #
  199.     andi.b    #7,d0        ; mask off flags (if any)
  200.     lsl.b    #4,d0        ; shift unit # to place
  201.     move.b    d0,IDESDH    ; set drive #
  202.     move.l    6(sp),a0    ; a0 -> buffer
  203.  
  204.     tst.b    _useblit    ; BLiTTER exists?
  205.     beq.s    .0        ; if not, no need to init it
  206.     moveq    #0,d0        ; it's a read
  207.     bsr    initblit    ; initialize the BLiTTER
  208.  
  209. .0:    move.b    #0,IDEDOR    ; enable interrupt
  210.     move.b    #IDENTIFY,IDECR    ; set command code
  211.     bsr    w4int        ; wait for interrupt
  212.     tst.w    d0        ; successful?
  213.     bmi.s    .1        ; if timed-out, return
  214.     btst    #DRQ,d0        ; DRQ?
  215.     beq.s    .1        ; if not, return with error
  216.  
  217.     bsr    readbuf        ; read data
  218.     moveq    #0,d0        ; everything is fine
  219. .1:    rts 
  220.  
  221.  
  222. ;+
  223. ; awto() - set drive to Active mode with timeout counter (in 5s increments)
  224. ;
  225. ; awto(pdev, timeout)
  226. ; WORD    pdev;        4(sp).w        ; physical unit #
  227. ; WORD    timeout;    6(sp).w
  228. ;-
  229.     .globl    _awto
  230. _awto:    
  231.     move.w    4(sp),d0    ; d0 = physical unit #
  232.     andi.b    #7,d0        ; mask off flags (if any)
  233.     lsl.b    #4,d0        ; shift unit # to place
  234.     move.b    d0,IDESDH    ; set drive #
  235.     move.b    7(sp),IDESC    ; set timeout counter
  236.     move.b    #AWTO,IDECR    ; set command code
  237.     bra    w4int        ; go wait for interrupt
  238.  
  239.  
  240. ;+
  241. ; readbuf() - reads 512 bytes (128 longs) of data from the sector
  242. ;        buffer.
  243. ;
  244. ; Comments:
  245. ;    A tower of 8 move.l is used to try to speed up the transfer.
  246. ;
  247. ; Passed:
  248. ;    a0.l = buffer to store data read from sector buffer
  249. ;
  250. ;    if BLiTTER code
  251. ;    a1.l = base address of BLiTTER
  252. ;-
  253. readbuf:
  254.     tst.b    _useblit    ; BLiTTER exists?
  255.     beq.s    .0        ; if not, do programmed IO
  256.     move.w    #1,YCNT(a1)    ; one destination line
  257.     move.b    #$80,BUSY(a1)    ; start the BLiTTER
  258.     bsr    restart
  259.     addq.l    #2,DESTADDR(a1)    ; advance to next word of destination
  260.     rts
  261.                 ; Programmed IO
  262. .0:    moveq    #15,d0        ; d0 = (# of longs to read / 8) - 1
  263.     lea    IDEDR,a1    ; a1 -> data bus
  264. .1:    move.l    (a1),(a0)+    ; read data from bus
  265.     move.l    (a1),(a0)+    ; read data from bus
  266.     move.l    (a1),(a0)+    ; read data from bus
  267.     move.l    (a1),(a0)+    ; read data from bus
  268.     move.l    (a1),(a0)+    ; read data from bus
  269.     move.l    (a1),(a0)+    ; read data from bus
  270.     move.l    (a1),(a0)+    ; read data from bus
  271.     move.l    (a1),(a0)+    ; read data from bus
  272.     dbra    d0,.1        ; repeat until all done
  273.     rts
  274.  
  275.  
  276. ;+
  277. ; wrtbuf() - writes 512 bytes (128 longs) of data to sector buffer.
  278. ;
  279. ; Passed:
  280. ;    a0.l = buffer with data to write to sector buffer
  281. ;-
  282. wrtbuf:
  283.     tst.b    _useblit    ; BLiTTER exists?
  284.     beq.s    .0        ; if not, do programmed IO
  285.     move.w    #1,YCNT(a1)    ; one destination line
  286.     move.b    #$80,BUSY(a1)    ; start the BLiTTER
  287.     bsr    restart
  288.     addq.l    #2,SRCADDR(a1)    ; advance to next word of source
  289.     rts
  290.                 ; Programmed IO
  291. .0:    moveq    #15,d0        ; d0 = (# longs to write / 8) - 1
  292.     lea    IDEDR,a1    ; a1 -> data bus
  293. .1:    move.l    (a0)+,(a1)    ; write data to bus
  294.     move.l    (a0)+,(a1)    ; write data to bus
  295.     move.l    (a0)+,(a1)    ; write data to bus
  296.     move.l    (a0)+,(a1)    ; write data to bus
  297.     move.l    (a0)+,(a1)    ; write data to bus
  298.     move.l    (a0)+,(a1)    ; write data to bus
  299.     move.l    (a0)+,(a1)    ; write data to bus
  300.     move.l    (a0)+,(a1)    ; write data to bus
  301.     dbra    d0,.1        ;  repeat until all done
  302.     rts
  303.  
  304.  
  305. ;+
  306. ; initblit() - initialize the BLiTTER chip for 512 bytes I/O transfer
  307. ;
  308. ; Passed:
  309. ;    a0.l = destination address if read; source address if write
  310. ;    d0.w = flag to tell whether it's a read or a write
  311. ;-
  312. initblit:
  313.     lea    bBLiTTER,a1        ; a1 -> BLiTTER register map
  314.     tst.b    d0            ; read or write?
  315.     bne.s    ib0            ; (write)
  316.     move.l    #IDEDR,SRCADDR(a1)    ; source addr = IDE data register
  317.     move.l    a0,DESTADDR(a1)        ; destination addr = given buffer
  318.     move.w    #2,DESTXINC(a1)        ; words read
  319.     moveq    #0,d0
  320.     move.w    d0,SRCXINC(a1)        ; never increment source X
  321.     bra.s    ib1
  322.                     ; initialize BLiTTER to write to disk
  323. ib0:    move.l    a0,SRCADDR(a1)        ; source addr = write buffer
  324.     move.l    #IDEDR,DESTADDR(a1)    ; destination addr = IDE data reg
  325.     move.w    #2,SRCXINC(a1)        ; words write
  326.     moveq    #0,d0
  327.     move.w    d0,DESTXINC(a1)        ; never increment destination X
  328.  
  329. ib1:    move.w    d0,SRCYINC(a1)        ; never increment source Y
  330.     move.w    d0,DESTYINC(a1)        ; never increment destination Y
  331.     move.b    d0,SKEW(a1)        ; no skew
  332.     moveq    #$ff,d0
  333.     move.l    d0,ENDMASK1(a1)        ; change all bits at destination
  334.     move.w    d0,ENDMASK3(a1)        ; change all bits at destination
  335.     move.w    #$203,HOP(a1)        ; set HOP and OP to source
  336.     move.w    #256,XCNT(a1)        ; num of words to transfer
  337.     rts
  338.  
  339.  
  340. ;+
  341. ; restart() - restart the BLiTTER
  342. ;
  343. ; Passed:
  344. ;    a1.l = base address of BLiTTER
  345. ;-
  346. restart:
  347.     nop
  348.     tas    BUSY(a1)    ; restart BLiTTER and test if busy
  349.     bmi.s    restart        ; quit if not busy
  350.     rts
  351.  
  352.  
  353. ;+
  354. ; gcparm() - get current drive parameters
  355. ;
  356. ; gcparm(buf)
  357. ; char    *buf;    $4(sp).l    /* -> data returned by identify() */
  358. ;
  359. ; Returns:
  360. ;    d0.w = # of default cylinders
  361. ;    d1.w = # of default heads
  362. ;    d2.w = # of default sectors per track
  363. ;-
  364.     .globl    _gcparm
  365. _gcparm:
  366.     move.l    4(sp),a0    ; a0 -> data buffer
  367.     add.l    #CONMDL,a0    ; a0 -> where Conner model number is
  368.     move.l    a0,-(sp)
  369.     pea    cp2024
  370.     move.w    #6,-(sp)
  371.     bsr    strcmp        ; compare model# with "CP2024"
  372.     adda    #10,sp        ; clean up stack
  373.     tst.w    d0        ; is unit the CP2024 (Kato 20Mb)?
  374.     bne.s    gcp0        ; if not, handle the normal way
  375.                 ; else return default values of CP2024
  376.     move.w    #CP20NCYL,d0    ; d0.w = # of cylinders
  377.     move.w    #CP20NHEAD,d1    ; d1.w = # of heads
  378.     move.w    #CP20NSPT,d2    ; d2.w = # of spt
  379.     bra.s    gcpend
  380.  
  381. gcp0:    move.l    4(sp),a0
  382.     move.w    NCYL(a0),d0    ; d0.w = # of cylinders
  383.     move.w    NHEAD(a0),d1    ; d1.w = # of heads
  384.     move.w    NSPT(a0),d2    ; d2.w = # of sectors per track
  385.  
  386. gcpend:    rts
  387.  
  388.  
  389.  
  390. conner:    dc.b    "Conner",0
  391. .even
  392. cp2024:    dc.b    "CP2024",0
  393. .even
  394.  
  395.  
  396. ;+
  397. ; gparmfc() - get the current drive parameters for the C code
  398. ;
  399. ; gparmfc(addcyl, addhd, addspt, buf)
  400. ; char *addcyl;        $4(sp).l    /* the address of #cyl */
  401. ; char *addhd;        $8(sp).l    /* the address of #hd */
  402. ; char *addspt;        $c(sp).l     /* the address of #spt */
  403. ; char *buf;        $10(sp).l    /* the address of the date buffer */
  404. ; returns: put the values of #cyl, #hd, #spt into the given addresses 
  405. ;-
  406.     .globl _gparmfc
  407. _gparmfc:
  408.     move.l $10(sp),-(sp); push the buffer address into the stack
  409.     bsr       _gcparm        ;
  410.     adda   #4,sp        ; clean up stack
  411.     move.l $4(sp),a0    ; a0 -> data buffer
  412.     move.w d0,(a0)        ; move the #cyl into its addr
  413.     move.l $8(sp),a0    ; a0 -> data buffer
  414.     move.w d1,(a0)        ; move the #hd into its addr
  415.     move.l $c(sp),a0    ; a0 -> data buffer
  416.     move.w d2,(a0)        ; move the #spt into its addr
  417.     rts
  418.  
  419.  
  420.  
  421. ;+
  422. ; strcmp() - compare two strings
  423. ;
  424. ; Passed:
  425. ;    4(sp).w  = n (# of bytes to compare)
  426. ;    6(sp).l  = address of first string
  427. ;    10(sp).l = address of second string
  428. ;
  429. ; Returns:
  430. ;    d0.w = 0    if first n bytes of the 2 strings are the same
  431. ;         = non-0    otherwise
  432. ;-
  433. strcmp:    movem.l    d1/a0-a1,-(sp)    ; save registers d1, a0 and a1
  434.     move.w    16(sp),d1    ; d1 = byte count
  435.     subq.w    #1,d1        ; dbra likes one less
  436.     move.l    18(sp),a0    ; a0 -> string 1
  437.     move.l    22(sp),a1    ; a1 -> string 2
  438.     moveq    #1,d0        ; assume strings are not the same
  439. .0:    cmpm.b    (a0)+,(a1)+    ; characters the same?
  440.     bne.s    .1        ; if not, return
  441.     dbra    d1,.0        ; else compare next character
  442.     moveq    #0,d0        ; the strings are the same
  443. .1:    movem.l    (sp)+,d1/a0-a1    ; restore registers d1, a0 and a1
  444.     rts
  445.  
  446.  
  447. .if    !DRIVER
  448.  
  449. ;+
  450. ; recal() - moves the R/W heads from anywhere on the disk to cylinder 0.
  451. ;
  452. ; recal(pdev)
  453. ; WORD    pdev;    $4(sp).w
  454. ;-
  455.     .globl    _recal
  456. _recal:    move.w    4(sp),d0    ; d0 = physical unit #
  457.     andi.b    #7,d0        ; mask off flags (if any)
  458.     lsl.b    #4,d0        ; shift unit # to place
  459.     move.b    d0,IDESDH    ; write drive #
  460.     move.b    #0,IDEDOR    ; enable interrupt
  461.     move.b    #RECAL,IDECR    ; write command code
  462.     bra    w4int        ; go wait for interrupt
  463.  
  464.  
  465. ;+
  466. ; verify() - functions similarly to read() except that no data is
  467. ;         transferred to the host.
  468. ;-
  469.     .globl    _verify
  470. _verify:
  471.     move.w    $e(sp),d0    ; d0.w = physical unit #
  472.     move.l    4(sp),d1    ; d1.l = starting logical sector #
  473.     bsr    set_dhcs    ; set drive#, head#, cylinder# and sector#
  474.     move.l    $a(sp),a0    ; a0 -> buffer to write from
  475.     move.b    9(sp),IDESC    ; set sector count
  476.     move.b    #0,IDEDOR    ; enable interrupt
  477.     move.b    #VERIFY,IDECR    ; set command code
  478. .0:    bsr    w4int        ; wait for interrupt
  479.     bne.s    .1        ; if has error, return
  480.     tst.b    IDESC        ; more to verify?
  481.     bne.s    .0        ; if so, continue
  482.     moveq    #0,d0        ; everything's fine
  483. .1:    rts
  484.  
  485.  
  486. ;+
  487. ; fmtunt() - formats a unit
  488. ;       - always formats sectors as good ones.
  489. ;       - interleave 1:1.
  490. ;
  491. ; fmtunt(pdev)
  492. ; WORD    pdev;    4(sp).w
  493. ;-
  494.     .globl    _fmtunt
  495. _fmtunt:
  496.     move.l    d3,-(sp)    ; save d3
  497.     move.w    8(sp),-(sp)    ; set selected unit to the appropiate
  498.     bsr    setmode        ;  mode and get drive parameters in d1-d3
  499.     addq    #2,sp        ; clean up stack
  500.     tst.w    d0        ; setmode() ok?
  501.     bne    .5        ; if not, return
  502.                 ; else fill format data
  503.     bsr    clrsbuf        ; clear scratch buffer
  504.     moveq    #1,d0        ; d0 = sector # (starts with sector 1)
  505.     lea    sbuf,a1        ; a1 -> _identify() data buffer
  506. .0:    move.b    d0,(a1)+    ; set sector #
  507.     clr.b    (a1)+        ; format sector good
  508.     addq.b    #1,d0        ; next sector #
  509.     cmp.b    d0,d3        ; all sector # set?
  510.     bcc.s    .0        ; if not, continue
  511.                 ; format unit
  512.     move.w    8(sp),d3    ; d3.w = physical unit #
  513.     andi.b    #7,d3        ; mask off flags (if any)
  514.     lsl.b    #4,d3        ; shift physical unit # into place
  515.     move.b    d3,IDESDH    ; set physical unit #
  516.     subq.w    #1,d1        ; dbra likes one less
  517.     subq.w    #1,d2        ; dbra likes one less
  518.     move.w    d2,d3        ; d3 = # heads - 1
  519.     lea    sbuf,a0        ; a0 -> format data
  520. .1:    move.w    d3,d2        ; reinitialize head number for next cylinder
  521. .2:    movem.l    d1-d2/a0,-(sp)    ; save cylinder and head count
  522.     bsr    _fmttrk        ; format track of current cylinder and head
  523.     bne.s    .4        ; if fail, return
  524. .3:    movem.l    (sp)+,d1-d2/a0    ; restore cylinder and head count
  525.     dbra    d2,.2        ; for all heads
  526.     dbra    d1,.1        ; for all cylinders
  527.     moveq    #0,d0        ; everything's fine
  528.     bra.s    .5        ; and return
  529. .4:    adda.l    #12,sp        ; else clean up stack
  530. .5:    move.l    (sp)+,d3    ; restore d3
  531.     rts
  532.  
  533.  
  534.  
  535. ;+
  536. ; setmode() - set the unit to its default mode and return the default
  537. ;        drive parameters.
  538. ;
  539. ; setmode(pdev)
  540. ; WORD    pdev;    $4(sp).w
  541. ;
  542. ; Returns:
  543. ;    d0.w = 0    if successful
  544. ;         = non-0    if failed
  545. ;    d1.w = # of default cylinders
  546. ;    d2.w = # of default heads
  547. ;    d3.w = # of default sectors per track
  548. ;
  549. ; Comments:
  550. ;    This routine distinguishes the Conner drives from other vendors'
  551. ; because Conner saves their default values in a reserved area. (Sigh)
  552. ; Furthermore, since the default parameters are not stored on the Conner 
  553. ; CP2024 (20Mb) drives at all, they are being hardwired into the code. 
  554. ; (I know, it's terrible!)
  555. ;-
  556. setmode:
  557.     pea    sbuf        ; scratch buffer
  558.     move.w    8(sp),-(sp)    ; physical unit #
  559.     bsr    _identify    ; identify(pdev, buf)
  560.     addq.w    #6,sp        ; clean up stack
  561.     tst.w    d0        ; successful?
  562.     bne    smend        ; if not, return
  563.                 ; else 
  564.     lea    sbuf,a1        ; a1 -> data buffer
  565.     move.l    #sbuf+MDLNUM,-(sp)
  566.     pea    conner
  567.     move.w    #6,-(sp)
  568.     bsr    strcmp        ; compare model# with "Conner"
  569.     adda    #10,sp        ; clean up stack
  570.     tst.w    d0        ; is unit a Conner drive?
  571.     bne.s    other        ; if not, handle the normal way
  572.                 ; else
  573.     move.l    #sbuf+CONMDL,-(sp)
  574.     pea    cp2024
  575.     move.w    #6,-(sp)
  576.     bsr    strcmp        ; compare model# with "CP2024"
  577.     adda    #10,sp        ; clean up stack
  578.     tst.w    d0        ; is unit the CP2024 (Kato 20Mb)?
  579.     bne.s    nkato        ; if not, parms at reserved area
  580.                 ; else return default values of CP2024
  581.     move.w    #CP20NCYL,d1    ; d1.w = # of cylinders
  582.     move.w    #CP20NHEAD,d2    ; d2.w = # of heads
  583.     move.w    #CP20NSPT,d3    ; d3.w = # of spt
  584.     bra.s    smend        ; no need to init parm, just return
  585.  
  586.                 ; for other Conner drives
  587. nkato:    move.w    CPNCYL(a1),d1    ; get parameters at Conner
  588.     moveq    #0,d2        ;   reserved location
  589.     move.b    CPNHEAD(a1),d2
  590.     moveq    #0,d3
  591.     move.b    CPNSPT(a1),d3
  592.     bra.s    sminit        ; go do initparm
  593.  
  594. other:    move.w    NCYL(a1),d1    ; d1.w = # of cylinders
  595.     move.w    NHEAD(a1),d2    ; d2.w = # of heads
  596.     move.w    NSPT(a1),d3    ; d3.w = # of sectors per track
  597.  
  598. sminit:    movem.l    d1-d3,-(sp)    ; save drive parameters
  599.     move.w    d3,-(sp)    ; sectors per track
  600.     move.w    d2,-(sp)    ; # of heads
  601.     move.w    20(sp),-(sp)    ; physical unit #
  602.     bsr    _initparm    ; set drive to default mode
  603.     addq.w    #6,sp        ; clean up stack
  604.     movem.l    (sp)+,d1-d3    ; restore drive parameters
  605.  
  606. smend:    rts
  607.  
  608.  
  609. ;+
  610. ; clrsbuf() - clear the scratch buffer
  611. ;-
  612. clrsbuf:
  613.     movem.l    d0/a0,-(sp)    ; save d0 and a0
  614.     lea    sbuf,a0        ; a0 -> scratch buffer
  615.     move.w    #127,d0        ; d0 = counter
  616. .0:    clr.l    (a0)+        ; clear 4 bytes
  617.     dbra    d0,.0        ; repeat until done
  618.     movem.l    (sp)+,d0/a0    ; restore d0 and a0
  619.     rts
  620.  
  621.  
  622. ;+
  623. ; fmttrk() - formats a track with format data provided.
  624. ;
  625. ; Passed:
  626. ;    d1.w = cylinder #
  627. ;    d2.w = head #
  628. ;    a0.l -> format data
  629. ;-
  630.     .globl    _fmttrk
  631. _fmttrk:
  632.     andi.b    #$f0,IDESDH    ; erase previous head #
  633.     or.b    d2,IDESDH    ; set new head #
  634.     move.b    d1,IDECL    ; set cylinder low
  635.     lsr.w    #8,d1        ; d0.b = cylinder high
  636.     move.b    d1,IDECH    ; set cylinder high
  637.     move.b    #0,IDEDOR    ; enable interrupt
  638.  
  639.     tst.b    _useblit    ; use BLiTTER?
  640.     beq.s    .0        ; if not, no need to init it
  641.     moveq    #1,d0        ; write through BLiTTER
  642.     bsr    initblit    ; initialize the BLiTTER
  643.  
  644. .0:    move.b    #FMTTRK,IDECR    ; set command code
  645. .1:    btst.b    #DRQ,IDEASR    ; DRQ?
  646.     beq.s    .1        ; if not, wait longer
  647.  
  648.     bsr    wrtbuf        ; write format data to sector buffer
  649.     bra    w4int
  650.  
  651.  
  652. ;+
  653. ; seek() - initiates a seek to the track and selects the head 
  654. ;       specified in the Task File.
  655. ;
  656. ; seek(pdev, head, cyl)
  657. ; WORD    pdev;    $4(sp).w
  658. ; WORD    head;    $6(sp).w
  659. ; WORD    cyl;    $8(sp).w
  660. ;-
  661.     .globl    _seek
  662. _seek:    move.w    4(sp),d0    ; d0 = physical unit #
  663.     andi.b    #7,d0        ; mask off flags
  664.     lsl.b    #4,d0        ; shift unit # to place
  665.     or.b    7(sp),d0    ; b4 of d0 = drive #; b3-b0 of d0 = head #;
  666.     move.b    d0,IDESDH    ; set drive and head #
  667.     move.b    #0,IDEDOR    ; enable interrupt
  668.     move.b    9(sp),IDECL    ; set cylinder low
  669.     move.b    8(sp),IDECH    ; set cylinder high
  670.     move.b    #SEEK,IDECR    ; set command code
  671.     bra    w4int        ; go wait for interrupt
  672.  
  673.  
  674. ;+
  675. ; diag() - performs the internal diagnostic tests implemented by 
  676. ;       the drive.
  677. ;-
  678.     .globl    _diag
  679. _diag:    move.b    #0,IDEDOR    ; enable interrupt
  680.     move.b    #DIAG,IDECR    ; set command code
  681.     bra    w4int
  682.  
  683.  
  684. ;+
  685. ; initparm() - enables the host to set the head switch and cylinder
  686. ;           increment points for multiple sector operations.
  687. ;
  688. ; initparm(pdev, head, spt)
  689. ; WORD     pdev;    4(sp).w
  690. ; WORD    head;    6(sp).w
  691. ; WORD    spt;    8(sp).w
  692. ;-
  693.     .globl    _initparm
  694. _initparm:
  695.     move.w    4(sp),d0    ; d0 = physical unit #
  696.     andi.b    #7,d0        ; mask off flags
  697.     lsl.w    #4,d0        ; shift it into place
  698.     move.b    d0,IDESDH    ; set physical unit #
  699.     move.w    6(sp),d0    ; d0 = # of heads
  700.     subq.b    #1,d0        ; maximum head #
  701.     or.b    d0,IDESDH    ; set head #
  702.     move.b    9(sp),IDESC    ; set sectors per track
  703.     move.b    #0,IDEDOR    ; enable interrupt
  704.     move.b    #INITPARM,IDECR    ; set command code
  705.     bra    w4int        ; go wait for interrupt
  706.  
  707.  
  708. ;+
  709. ; rsbuf() - allows the Host to read the current contents of the
  710. ;        drive's sector buffer.
  711. ;
  712. ; rsbuf(pdev, buf)
  713. ; WORD    pdev;    4(sp).w
  714. ; BYTE    *buf;    6(sp).l
  715. ;-
  716.     .globl    _rsbuf
  717. _rsbuf:    move.w    4(sp),d0    ; d0 = physical unit #
  718.     andi.b    #7,d0        ; mask off flags (if any)
  719.     lsl.w    #4,d0        ; shift it into place
  720.     move.b    d0,IDESDH    ; set physical unit #
  721.     move.l    6(sp),a0    ; a0 -> buffer
  722.     move.w    #256,d1
  723.  
  724.     tst.b    _useblit    ; BLiTTER exists?
  725.     beq.s    .0        ; if not, don't use it
  726.     moveq    #0,d0        ; it's a read
  727.     bsr    initblit    ; initialize the BLiTTER
  728.  
  729. .0:    move.b    #0,IDEDOR    ; enable interrupt
  730.     move.b    #RSBUF,IDECR    ; set command code
  731.     bsr    w4int        ; go wait for interrupt
  732.     tst.w    d0        ; successful?
  733.     bmi.s    .1        ; if timed-out, return
  734.     btst    #DRQ,d0        ; DRQ?
  735.     beq.s    .1        ; if not, return with error
  736.  
  737.     bsr    readbuf        ; read data
  738.     moveq    #0,d0        ; everything is fine
  739. .1:    rts 
  740.  
  741.  
  742. ;+
  743. ; wsbuf() - allows the Host to overwrite the contents of the drive's
  744. ;        sector buffer.
  745. ;
  746. ; wsbuf(pdev, buf)
  747. ; WORD    pdev;    4(sp).w
  748. ; BYTE    *buf;    6(sp).l
  749. ;-
  750.     .globl    _wsbuf
  751. _wsbuf:    move.w    4(sp),d0    ; d0 = physical unit #
  752.     andi.b    #7,d0        ; mask off flags (if any)
  753.     lsl.w    #4,d0        ; shift it into place
  754.      move.b    d0,IDESDH    ; set physical unit #
  755.     move.l    6(sp),a0    ; a0 -> buffer
  756.     move.w    #256,d0        ; d0 = word count
  757.  
  758.     tst.b    _useblit    ; BLiTTER exists?
  759.     beq.s    .0        ; if not, don't use it
  760.     moveq    #0,d0        ; it's a read
  761.     bsr    initblit    ; initialize the BLiTTER
  762.  
  763. .0:    move.b    #WSBUF,IDECR    ; set command code
  764. .1:    btst    #DRQ,IDESR    ; DRQ?
  765.     beq.s    .1        ; if not, wait some more
  766.  
  767.     bsr    wrtbuf        ; write data
  768.     moveq    #0,d0        ; everything is fine
  769.     rts
  770.  
  771.  
  772. ;+
  773. ; standby() - set drive to Standby mode
  774. ;
  775. ; standby(pdev)
  776. ; WORD    pdev;    4(sp).w        ; physical unit #
  777. ;-
  778.     .globl    _standby
  779. _standby:
  780.     move.w    4(sp),d0    ; d0 = physical unit #
  781.     andi.b    #7,d0        ; mask off flags (if any)
  782.     lsl.b    #4,d0        ; shift unit # to place
  783.     move.b    d0,IDESDH    ; set drive #
  784.     move.b    #STANDBY,IDECR    ; set command code
  785.     bra    w4int        ; go wait for interrupt
  786.  
  787.  
  788. ;+
  789. ; active() - set drive to Active mode
  790. ;
  791. ; active(pdev)
  792. ; WORD    pdev;    4(sp).w        ; physical unit #
  793. ;-
  794.     .globl    _active
  795. _active:
  796.     move.w    4(sp),d0    ; d0 = physical unit #
  797.     andi.b    #7,d0        ; mask off flags (if any)
  798.     lsl.b    #4,d0        ; shift unit # to place
  799.     move.b    d0,IDESDH    ; set drive #
  800.     move.b    #ACTIVE,IDECR    ; set command code
  801.     bra    w4int        ; go wait for interrupt
  802.  
  803.  
  804. ;+
  805. ; sbwto() - set drive to Standby mode with timeout counter (in 5s increments)
  806. ;
  807. ; sbwto(pdev, timeout)
  808. ; WORD    pdev;        4(sp).w        ; physical unit #
  809. ; WORD    timeout;    6(sp).w
  810. ;-
  811.     .globl    _sbwto
  812. _sbwto:    
  813.     move.w    4(sp),d0    ; d0 = physical unit #
  814.     andi.b    #7,d0        ; mask off flags (if any)
  815.     lsl.b    #4,d0        ; shift unit # to place
  816.     move.b    d0,IDESDH    ; set drive #
  817.     move.b    7(sp),IDESC    ; set timeout counter
  818.     move.b    #SBWTO,IDECR    ; set command code
  819.     bra    w4int        ; go wait for interrupt
  820.  
  821.  
  822. ;+
  823. ; ssc() - set sector count wrt current mode of drive
  824. ;
  825. ; ssc(pdev)
  826. ; WORD    pdev;    4(sp).w        ; physical unit #
  827. ;-
  828.     .globl    _ssc
  829. _ssc:    
  830.     move.w    4(sp),d0    ; d0 = physical unit #
  831.     andi.b    #7,d0        ; mask off flags (if any)
  832.     lsl.b    #4,d0        ; shift unit # to place
  833.     move.b    d0,IDESDH    ; set drive #
  834.     move.b    #SSC,IDECR    ; set command code
  835.     bra    w4int        ; go wait for interrupt
  836.  
  837.  
  838. ;+
  839. ; sbres() - set drive to Standby mode.
  840. ;      - drive will not wake up until reset is sent to drive
  841. ;
  842. ; sbres(pdev)
  843. ; WORD    pdev;    4(sp).w        ; physical unit #
  844. ;-
  845.     .globl    _sbres
  846. _sbres:    
  847.     move.w    4(sp),d0    ; d0 = physical unit #
  848.     andi.b    #7,d0        ; mask off flags (if any)
  849.     lsl.b    #4,d0        ; shift unit # to place
  850.     move.b    d0,IDESDH    ; set drive #
  851.     move.b    #SBRES,IDECR    ; set command code
  852.     bra    w4int        ; go wait for interrupt
  853.  
  854.  
  855. ;+
  856. ; slave() - test if the slave drive exists
  857. ;
  858. ; Returns: 0 - if slave does not exist
  859. ;       1 - if slave exists
  860. ;-
  861.     .globl    _slave
  862. _slave:    moveq    #0,d0        ; assume slave does NOT exist
  863.     bset.b    #4,IDESDH    ; set drive bit to 1 (slave)
  864.     btst.b    #DRDY,IDESR    ; is slave ready?
  865.     beq.s    .0        ; if not, no slave
  866.     moveq    #1,d0        ; else, slave exists
  867. .0:    rts
  868.  
  869.  
  870. ;+
  871. ; _iderdy() - test if the IDE drive is ready
  872. ;
  873. ; Passed:
  874. ;    d0.b = IDE drive unit #
  875. ;
  876. ; Returns: 0 - if drive is NOT ready
  877. ;       1 - if drive is ready
  878. ;-
  879.     .globl    _iderdy
  880. _iderdy:
  881.     andi.b    #7,d0        ; mask off flags (if any)
  882.     lsl.b    #4,d0        ; shift unit # to place
  883.     move.b    d0,IDESDH    ; set drive #
  884.     move.b    #$50,d1        ; ready status
  885.     move.l    #IDERDY,d0    ; set up timer
  886.     add.l    _hz_200,d0
  887. ir0:    cmp.b    IDEASR,d1    ; is drive ready and not busy?
  888.     beq.b    ir1        ; if so, return with drive ready
  889.     cmp.l    _hz_200,d0    ; time-out yet?
  890.     bcc.b    ir0        ; if not, wait longer
  891.     moveq    #0,d0        ; else return drive NOT ready
  892.     rts
  893. ir1:    moveq    #1,d0        ; else, drive is ready
  894.     rts
  895.  
  896.  
  897. .data
  898. sbuf:    dcb.b    512,0        ; scratch buffer
  899.  
  900. .endif    ;!DRIVER
  901.  
  902.  
  903.