
;=========================================================;
; Send Atapi Packet                              11/12/03 ;
;---------------------------------------------------------;
; DOS EXTREME OS V0.01                                    ;
; by Craig Bamford(ASHLEY4).                              ;
;                                                         ;
;   Input:                                                ;
;          esi, points to 12 byte packet.                 ;
;                                                         ;
;=========================================================;


Send_Atapi_Packet:
        xor   edx,edx
        mov   ebx,0x0000ffff
        mov   ecx,ebx
        mov   dx,0x0177                                 ; Read Status = 7 + the base = 177
StatusReg1_1:                                  
        in    al,dx                                     ; Mov data from port to al
        and   al,0x80                                   ; Check bit 7(busy bit) ,loop if not 0
        jz    WriteCommand1_1                           ; Jump to WriteCommand1_1 if al bit7 =0
        loop  StatusReg1_1                              ; If al bit 7 =1,loop StatusReg1_1
        ret
WriteCommand1_1:                               
        dec   edx 
        xor   eax,eax                                   ; Bit 4 ,0 to select primary drive, 1 to select secondary drive
        out   dx,al                                     ; Write al to port (register)
        mov   ecx,ebx                                   ; Mov cx, number of loops(bx=0xffff)
        inc   edx  
StatusReg2_1:                                  
        in    al,dx                                     ; Mov data from port to al
        and   al,0x80                                   ; Check bit 7(busy bit) ,loop if not 0
        jz    WriteCommand2_1                           ; Jump to WriteCommand2_1 if al bit7 =0
        loop  StatusReg2_1                              ; If al bit 7 =1,loop StatusReg2_1
DeviceBusy_1:                                           ; time out:-(
        
        ret                                   

 ;----------------------------------------------------;
 ; Write Command                                      ;
 ;----------------------------------------------------; 

WriteCommand2_1:                               
              
        mov   al,0xA0                                   ; Add the 8 bit code to al
        out   dx,al                                     ; Write al to port (register)
        mov   ecx,ebx                                   ; Mov cx, number of loops(bx=0xffff)
StatusReg3_1:                                 
        in    al,dx                                     ; Mov data from port to al
        test  al,0x80                                   ; Does bit 7 = 0 (busy bit)
        jnz   DrqErrorCheck1_1                          ; Jump to "DrqErrorCheck1_1" if bit7 is 1
        test  al,0x08                                   ; Does bit3 = 0 (DRQ bit)
        jnz   WriteCommandPacket1_1                     ; Jump to label if bit3 is 1,we can transfer data to port:-) 
DrqErrorCheck1_1:                              
        loop  StatusReg3_1                              ; Loop label
        ret
      
 ;----------------------------------------------------;
 ; Write Command Packet                               ;
 ;----------------------------------------------------; 

WriteCommandPacket1_1:                         
        mov   dx,0x0170                                 ; Read date/write = 0              
        mov   cx,6                                      ; Mov number of words to mov into cx
WritePacket1_1:                                
        lodsw                                           ; Mov a word from packet to ax, inc esi 2
        out   dx,ax                                     ; Write ax to port (register)
        loop  WritePacket1_1                            ; Loop label

        mov   ecx,ebx                                   ; Mov cx, number of loops
        mov   dx,0x0177                                 ; Read Status = 7 + the base = 177 
StatusReg4_1:                                 
        in    al,dx                                     ; Mov data from port to al
        test  al,0x80                                   ; Does bit 7 = 0 (busy bit)
        jnz   DrqErrorCheck2_1                          ; Jump to "DrqErrorCheck2_1" if bit7 is 1
        ret

 ;----------------------------------------------------;
 ; Drq Error Check                                    ;
 ;----------------------------------------------------; 

DrqErrorCheck2_1:                             
        push  ecx                                       ; Save old cx
        mov   ecx,ebx                                   ; Mov cx, number of loops(bx=0xffff)
BusyDelay_1:                                            ; Label
        nop                                             ; Do a null operation(xchg ax,ax)
        nop                                             ; Do a null operation(xchg ax,ax)
        nop                                             ; Do a null operation(xchg ax,ax)
        nop                                             ; Do a null operation(xchg ax,ax)
        loop  BusyDelay_1                               ; Loop label
        pop   ecx                                       ; Get old cx
        loop  StatusReg4_1                              ; Loop label
     
error_1:
        
        ret


 ;--------------------------------------------------------------------------------------------;
 ;                                           Data                                             ;
 ;--------------------------------------------------------------------------------------------;



