;
; ATASTAND.ASM - We try to put the drive into standby mode.
;

    cli

; Save old int 76h vector
    
    xor ax,ax
    mov es,ax
    es: 
    mov ax,[01d8]
    mov old76ofs,ax
    es:
    mov ax,[01da]
    mov old76seg,ax

; Take over INT 76h (hard disk interface)

    mov ax,offset new76
    es:
    mov [01d8],ax
    mov ax,cs
    es:
    mov [01da],ax

    sti                 ; enable interrupts
    
    mov ax,ds           ; set es...
    mov es,ax           ; ...back to ds

    mov al,0A0h          ; setup al to write to drive/head register... 
                        ; ...selecting drive 0 (0A0h)  (drive 1 = 0B0h)
    mov dx,01F6h        ; dx = drive/head register port
    out dx,al           ; drive number set (AND HEAD 0 IS SET HERE)
        
    mov al,096h         ; al = 96h
    mov dx,01F7h        ; dx = command register port
    out dx,al           ; Command away!!
    mov al,0E2h         ; al = E2h
    out dx,al           ; Command away!!

    
    xor ax,ax
theloop:       
    inc ax
    jmp theloop            ; Wait for our interrupt

loopdone:

    ; Ok,now we restore int vector
    cli
    xor ax,ax
    mov es,ax
    mov ax,old76ofs
    es:
    mov [01d8],ax
    mov ax,old76seg
    es:
    mov [01da],ax
    sti
    
    int 20h              ; outta here!

; Our int 76h ISR
new76:
    mov [0500h],ax      ; save our counter

    ; First ack the int
    mov al,20h
    out 0A0h,al         ; nonspecific EOI to PIC 2
    jcxz next           ; breathing room 
next: 
    jcxz next2
next2:
    out 20h,al          ; nonspecific EOI to PIC 1    

 ;   mov dx,01F7h        ; dx = status register port
 ;   in al,dx            ; read in status register (clears interrupt)
    
    popf
    pop ax
    pop ax
    mov ax,offset loopdone
    push ax
    sti
    ret

; end new76

old76ofs dw ?
old76seg dw ?

