; RTC clock pulses.  These values specify the number of times per second
; INT 70h is called.  Default is 1024=1x

delay_2         equ     0Fh     ; 500mS pulse  (2 per second)
delay_4         equ     0Eh     ; 250mS pulse  (4 per second)
delay_8         equ     0Dh     ; 125mS pulse  (8 per second)
delay_16        equ     0Ch     ; 62.5mS pulse (16 per second)
delay_32        equ     0Bh     ; 31.25mS pulse (32 per second)
delay_64        equ     0Ah     ; 15.625mS pulse (64 per second)
delay_128       equ     09h     ; 7.8mS pulse   (128 per second)
delay_256       equ     08h     ; 3.9mS pulse   (256 per second)
delay_512       equ     07h     ; 1.95mS pulse  (512 per second)
delay_1024      equ     06h     ; .976mS pulse  (1024 per second)
delay_2048      equ     05h     ; .488mS pulse  (2048 per second)
delay_4096      equ     04h     ; .244mS pulse  (4096 per second)
delay_8192      equ     03h     ; .122mS pulse  (8192 per second)

; 02 and 01 duplicate 8 and 9.

delay_1x        equ     delay_64
delay_2x        equ     delay_128
delay_4x        equ     delay_256




rtc_off proc near
;disables the real time clock periodic timer

        push    ax
        cli
        mov     al, 0bh
        out     70h, al
        in      al, 71h
        mov     ah, al          
        and     ah, NOT BIT6    ; turn off BIT 6 in CMOS status register 0Bh
        mov     al, 0bh         ; which generates IRQ 8 to call INT 70
        out     70h, al
        mov     al, ah
        out     71h, al

        mov     al, 20h         ; signal end of interrupt
        out     20h, al
        out     0a0h, al
        sti
        pop     ax
        ret
rtc_off endp

;=======================================================================
;RTC timer stuff to throttle back CPU.
;
;
slowdown_cpu    proc    near

        push    ax        
        cli
        mov     al, 0ah
        out     70h, al
        in      al, 71h
        and     al, 0f0h
        or      al, cs:[interval] ;delay_1x    ;1010b      
        mov     ah, al
        mov     al, 0ah
        out     70h, al
        mov     al, ah
        out     71h, al


        mov     al, 0bh
        out     70h, al
        in      al, 71h
        mov     ah, al
        or      ah, BIT6         ; turn on BIT 6 in CMOS status register 0Bh
        mov     al, 0bh          ; which generates IRQ 8 to call INT 70
        out     70h, al          
        mov     al, ah
        out     71h, al


        ; make sure PIC A1 register isn't masking interrupt

        in      al, 0a1h          
        and     al, 0FEh
        out     0a1h, al
        sti
        pop     ax

	int 	70h			; force 1st RTC pulse

	ret
        
slowdown_cpu    endp


setDelayAmount	proc
	push	si
	push	ax
	lea	si, FLOPPER_TOP
	mov	al, cs:[delay]
	cmp	al, 0
	jnz	@f
        mov     al, 4
	mov	cs:[delay], al
@@:
	mov	ds:[si].delayAmount, al 	; save delay amount
        test    cs:[cmdlineflags], nofloppy
        jz      @f
        or      ds:[si].flagbyte, nofloppy
@@:
        pop     ax
	pop	si
	ret
setDelayAmount	endp


getPICMask      proc
; read the current mask from the interrupt controller to be restored on
; exit.

        push    ax
        in      al, 21h
        mov     cs:[PIC_MASK1], al

        in      al, 0a1h
        mov     cs:[PIC_MASK2], al
        pop     ax
        ret

PIC_MASK1       db      ?
PIC_MASK2       db      ?

getPICMask      endp



reset_pic       proc
; reset the 8259 interrupt controller to restore it to standard IRQs.
; Ie, IRQ 0-7  -> ISR 8-15h
;     IRQ 8-15 -> ISR 70-77h


        test    cs:[cmdlineflags], NOPIC        ; cmd line override?
        jnz     @f

	push	ax
        cli

	mov	al, 11h
	out	20h, al

        out     80h, al                         ; tiny delay

	mov	al, 08
	out	21h, al

        out     80h, al                         ; tiny delay

	mov	al, 04
	out	21h, al

        out     80h, al                         ; tiny delay

	mov	al, 01
	out	21h, al

        out     80h, al                         ; tiny delay

        mov     al, cs:[PIC_MASK1]              ; get current mask
        out     21h, al                         ; restore PIC


	; do upper controller too

	mov	al, 11h
	out	0a0h, al

        out     80h, al                         ; tiny delay

	mov	al, 70h
	out	0a1h, al

        out     80h, al                         ; tiny delay

	mov	al, 02
	out	0a1h, al

        out     80h, al                         ; tiny delay

	mov	al, 01
	out	0a1h, al

        out     80h, al                         ; tiny delay

        mov     al, cs:[PIC_MASK2]              ; get current mask
        out     0A1h, al                        ; restore PIC 2

        sti
        pop     ax
@@:
        ret
reset_pic       endp

