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