home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 2 / CDPD_II_2352.bin / ab20 / ab20_archive / hardware / amscsi.lzh / scsi.asm < prev    next >
Assembly Source File  |  1987-05-26  |  6KB  |  206 lines

  1. ;       SCSI DRIVER - for AMSCSI hardware
  2. ;       NCR 53C80 - SCSI ctrl chip
  3. ;    Rich Frantz        may 1987
  4. ;    
  5. ;    This routine should be called from a DOS handler
  6. ;    it should be noted that all command data should be
  7. ;    checked for validity before calling. Also I have 
  8. ;    not finished the error handling.
  9. ;------------------------------------------------------------
  10. NCR    equ    $800001        ;SCSI ctrl chip
  11. Iid    equ    $80        ;d7 = init
  12. Tid    equ    $01        ;d1 = target
  13. M250    equ    $1E000        ;250 mSEC dly
  14. U200    equ    $64        ;200 uSEC dly
  15. ;-------------------------------------------------------------
  16. ;    a0 = IO_DATA - MUST BE SETUP BY CALLER
  17. ;    a6 = DEVICE POINTER - MUST BE SETUP BY CALLER
  18. ;       a4 = SCSI buffer pointer
  19. ;       a5 = unassigned 
  20. ;     d0 = IO_LENGTH ( in bytes) - MUST BE SETUP BY CALLER
  21. ;       d4 = IO_OFFSET / 256
  22. ;       d5 = unassigned
  23. ;-------------------------------------------------------------
  24.  
  25. ReadSCSI:    ;read data from the SCSI 
  26.     movem.l d4/d5/a4/a5,-(sp)    ;save regs
  27.     bsr.s Build_CMD            ;build the scsi cmd packet
  28.     move.b #$08,CMDBUF        ;set cmd to read
  29.     bsr SELph            ;select the target
  30.  
  31. ReadSCSI_End:    
  32.     movem.l (sp)+,d4/d5/a4/a5    ;restore regs
  33.     rts                ;return to caller
  34.  
  35. WriteSCSI:    ;send data to the SCSI
  36.     movem.l d4/d5/a4/a5,-(sp)    ;save regs
  37.     bsr.s Build_CMD            ;build the scsi cmd packet
  38.     move.b #$0A,CMDBUF        ;set cmd to write
  39.     bsr SELph            ;do it
  40.  
  41. WriteSCSI_End:
  42.     movem.l (sp)+,d4/d5/a4/a5    ;restoer regs
  43.     rts                ;return to caller
  44.  
  45. FormatSCSI:
  46.     movem.l d4/d5/a4/a5,-(sp)    ;save the regs
  47.     move.w #$0400,CMDBUF        ;note set the unit #
  48.     move.l #0,CMDBUF+2        ;clr the cmd packet
  49.     bsr SELph            ;do it
  50.  
  51. FormatSCSI_End:
  52.     movem.l (sp)+,d4/d5/a4/a5    ;restore th regs
  53.     rts                ; return to caller
  54.  
  55. Build_CMD:    ;build the scsi cmd packet
  56.     move.l d0,d5            ;get the #bytes to transfere
  57.     asr.l #8,d5            ;shift 8 palces to get sectors
  58.     and.l #$FF,d5            ;must be less than 1 byte
  59.     asl.l #8,d5            ;move to upper byte in word
  60.     move.w d5,CMDBUF+4        ;place in packet
  61.     asr.l #8,d4            ;convert to sectors
  62.     and.l #$000FFFFF,d4        ;mask out L U #
  63.     move.l d4,CMDBUF        ;place in packet
  64. Unit_No:    ; add the unit #
  65.  
  66.     rts                ;packet complete        
  67.     
  68. SELph:        ;select the target
  69.     move.b #0,NCR+2        ;clr NCR
  70.     move.b #0,NCR+4
  71.     move.b #0,NCR+8
  72. ARB:    move.b #Iid,NCR        ;ld init I D
  73.     or.b #1,NCR+4        ;set ARB bit
  74. NFREE:    btst.b #6,NCR+2        ;check arib in proc.
  75.     beq NFREE        ;wait
  76.     nop            ;delay for bus settle
  77.     nop
  78.     nop
  79.     nop
  80.     btst.b #5,NCR+2        ;check lost arb
  81.     bne.s SELph        ;try again
  82. ;
  83. ;check for higher priority
  84. ;
  85.     move.l #0,d5        ;lcr d5
  86.     move.b NCR,d5        ;load scsi data
  87.     sub.b #Iid,d5        ;sub our id
  88.     beq.s WIN        ;if = we are the only bidder
  89.     sub.b #Iid,d5        ;sub again
  90.     bmi.s WIN        ;if - we are the highest
  91.     bra.s SELph        ;try again
  92. WIN:    btst.b #5,NCR+2        ;check lost arb bit
  93.     bne.s SELph        ;if lost , retry
  94.     move.b #$0c,NCR+2    ;set SEL
  95.     nop            ;delay
  96.     nop
  97.     nop
  98.     nop
  99.     move.b #Iid,NCR        ;load init I D
  100.     or.b #Tid,NCR        ;load target I D
  101.     move.b #5,NCR+2        ;set SEL & Data Bus
  102.     and.b #$FE,NCR+4    ;clr ARB
  103.     and.b #$F7,NCR+2    ;clr BSY
  104.     move.l #M250,d5        ;load timer
  105. loop1:    btst.b #6,NCR+8        ;test BSY
  106.     bne.s SLECT        ;if busy , selected
  107.     subq #1,d5        ;dec timer
  108.     bne.s loop1        ;loop
  109.     move.b #0,NCR        ;clr I D's
  110.     move.l #U200,d5        ;load timer
  111. loop2:    btst.b #6,NCR        ;test BSY
  112.     bne.s SLECT        ;if busy , selected
  113.     subq #1,d5        ;dec timer
  114.     bne.s loop2        ;loop
  115.     bra error        ;selection failed
  116. SLECT:    and.b #$FB,NCR+2    ;clr SEL
  117. ; NextPhase
  118. NextPhase:    ;Check the SCSI bus phase
  119.     btst.b #7,NCR+8        ;check RESET
  120.     bne RSTph        ;reset phase
  121.     btst.b #6,NCR+8        ;check BUSY
  122.     beq Done        ;bus free
  123.     btst.b #3,NCR+8        ;check C/D
  124.     beq.s Dat        ;data in/out phase
  125.     btst.b #4,NCR+8        ;check MSG
  126.     bne.s Message        ;Message in/out phase
  127.     btst.b #2,NCR+8        ;check I/O
  128.     bne.s STATph        ;stat phase
  129.     bra.s CMDph        ;command phase
  130. Message:    ;check to see if it is message in or out
  131.     btst.b #2,NCR+8        ;check I/O
  132.     bne.s MSGINph        
  133.     bra.s MSGOUTph
  134. Dat:        ;check to see if it is data in or out
  135.     btst.b #2,NCR+8        ;check I/O
  136.     bne.s RDATph
  137.     bra.s SDATph    
  138. Done:        ;did we end normally ?
  139.     cmp.b #$FF,STAT        ;did we get a status byte
  140.     bne.s Return
  141. error:    move #1,d5        ;set error code *********************
  142. Return: rts
  143.  
  144. RSTph:        ;reset phase
  145.     bra.s Done        ;done
  146. CMDph:        ;send command
  147.     move.l #CMDBUF,a4    ;set the cmd buffer add
  148.     move.b #$02,NCR+6    ;set TCR to command phase
  149.     bra.s Send        ;send the command
  150.  
  151. STATph:        ;get status byte
  152.     move.l #STAT,a4        ;set the stat buffer add
  153.     move.b #$03,NCR+6    ;set TCR to status phase
  154.     bra.s Rec        ;get the stat
  155.  
  156. MSGINph:     ;get message from target
  157.     move.l #MSGINBUF,a4    ;set the message in buffer add
  158.     move.b #$07,NCR+6    ;set TCR to message in phase
  159.     bra.s Rec        ;get the message
  160.  
  161. MSGOUTph:    ;send message to target
  162.     bra NextPhase        ;error try again
  163.  
  164. RDATph:        ;receive data fron target
  165.     move.l #DATBUF,a4    ;set the data buffer add
  166.     move.b #$01,NCR+6     ;set TCR to receive data
  167.     bra.s Rec        ;get the data
  168.  
  169. SDATph:        ;send data to target
  170.     move.l #DATBUF,a4    ;set the data buffer add
  171.     move.b #$00,NCR+6    ;set TCR to send data
  172.     
  173. Send:        ;send x characters to the target
  174.     move.b (a4)+,NCR    ;set the byte
  175. Send1:    btst.b #5,NCR+8        ;test for REQ
  176.     beq.s Send1        ;wait for REQ
  177.     or.b #$10,NCR+2        ;set ACK
  178. Send2:    btst.b #5,NCR+8        ;test REQ
  179.     bne.s Send2        ;wait for /REQ
  180.     andi.b #$EF,NCR+2    ;clr ACK
  181.     btst.b #3,NCR+10    ;test pahse match
  182.     bne.s Send    
  183.     bra NextPhase
  184.  
  185. Rec:        ;receive x characters form the target
  186. Rec1:    btst.b #5,NCR+8        ;test REQ
  187.     beq.s Rec1        ;wait for REQ
  188.     move.b NCR,(a4)+    ;get byte from target
  189.     or.b #$10,NCR+2        ;set ACK
  190. Rec2:    btst.b #5,NCR+10    ;test REQ
  191.     bne.s Rec2        ;wait for /REQ
  192.     andi.b #$EF,NCR+2
  193.     btst.b #3,NCR+10    ;test for phase match
  194.     bne.s Rec    
  195.     bra NextPhase
  196.  
  197. CMDBUF    dc.b $0A,$00,$00,$00,$01,$00    ;command string (write 1 block)
  198.  
  199. STAT        ds.b $01    ;status word
  200.  
  201. MSGINBUF    ds.b $04    ;message buffer
  202.  
  203. DATBUF        ds.b $200    ;data buffer
  204.  
  205.     end
  206.