;
; ATATRY.ASM - We try to to ATA stuff
;

    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
    mov al,0ECh          ; al = IDENTIFY DRIVE command
    mov dx,01F7h        ; dx = command register port
    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
    
; Flip bytes in Model Number field
    mov di,0636h
again:
    mov ax,[di]
    xchg al,ah
    mov [di],ax
    inc di
    inc di
    cmp di,065Eh
    jb again
    
    mov si,0600h
    xor cx,cx
    call printmsg

    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)
    mov di,0600h        ; setup our buffer
    cld                 ; make sure we go forward
    mov cx,100h         ; 200h bytes to transfer (512)
    mov dx,01F0h        ; dx = data register port 
    rep insw            ; read data!                 
    
    popf
    pop ax
    pop ax
    mov ax,offset loopdone
    push ax
    ret

; end new76

printmsg:
        lodsb                    ; String [si] to al
        inc cx
        cmp cx,513              
        je loc_ret
        mov  ah,0Eh
        mov  bx,7
        int  10h            ; Video display   ah=functn 0Eh
                        ;  write char al, teletype mode
        jmp  short printmsg      ; (0152)
loc_ret:    ret
; end printmsg


old76ofs dw ?
old76seg dw ?
