; ==== ISUAL Flight Software -- Univ of California, Berkeley, SSL ====
; clock.a -- DPU Internal Time clock
;
; Revision History:
;  18-Dec-2002 - SPG - ISUAL Flight Software rev 7.3
;    First Official Release
;  21-Dec-2002 - SPG - ISUAL Flight Software rev 7.F
;    CLOCK_INIT now preserves SCIF_CTL_O b3
;
;   6-Aug-2003
;    EEPROM_ to EE_

        include clock.i
        include debug.i
        include util.i
        include ioports.i
        include eeprom.i
;
clock_init:
        push psw

; If power-on reset, set default time
        in STATUS6_I
        ani 10h
        jz npwro
        mvi a,0
        out SC_TIME_0_O ; (low) (write first)
        out SC_TIME_1_O 
        out SC_TIME_2_O 
        out SC_TIME_3_O 
        out SC_TIME_4_O 
        out SC_TIME_5_O  ; (high) (write last)
; force a PPS
        in SCIF_STS_I
        ani 0Fh         ; preserve bits 0..3
        ori 60h         ; PPS1+PPS2
        out SCIF_CTL_O  ; make PPS interrupt
npwro:
        pop psw
        ret
;
;
CLOCK_SET:
; HL points to 6-byte time
        push psw
        push b
        push h
;
; registers are in reverse order
        lxi b,5
        dad b
;
        mov a,M
        dcx h
        out SC_TIME_0_O  ; (low) (write first)
        mov a,M
        dcx h
        out SC_TIME_1_O 
        mov a,M
        dcx h
        out SC_TIME_2_O 
        mov a,M
        dcx h
        out SC_TIME_3_O 
        mov a,M
        dcx h
        out SC_TIME_4_O 
        mov a,M
        dcx h
        out SC_TIME_5_O ; (high) (write last)
;
        pop h
        pop b
        pop psw
        ret
;
; read clock
;
CLOCK_READ:
; HL pointer to 6 bytes of returned time
        push psw
        push b
        push h
; point to end of time string
        lxi b,5
        dad b
        push h
;
        rim
        mov b,a         ; remember original interrupt-enabled status
;
        di              ; disable interrupts
;
read_again:
        pop h
        push h
        in SC_TIME_0_I  ; read time, high bytes first        
        mov M,a
        dcx h
        in SC_TIME_1_I 
        mov M,a
        dcx h
        in SC_TIME_2_I 
        mov M,a
        dcx h
        in SC_TIME_3_I 
        mov M,a
        dcx h         
        in SC_TIME_4_I  
        mov M,a
        dcx h
        in SC_TIME_5_I            
        mov M,a
; now check if the clock ticked during the read
        in SCIF_STS_I
        ani 10h         ; bit 4: 1=time-read fault
        jnz read_again
;
        pop h
        mov a,b         ; recall interrupt-enabled status at entry
        ani 8           ; test RIM EI bit (3)
        jz noei         ; Interrupt had not been enabled.
        ei              ; It was originally enabled; turn it on again
noei:
        pop h
        pop b
        pop psw
        ret
;

; convert 10-byte time to seconds

; The "ISUAL Epoch" for converting between
; 10-byte UT and 6-byte "MET"
; is:  year 2000, Day 1, UT 00:00:00
        
; (code for this is in "EEPROM.A")
CLOCK_10_to_6:
; HL - points to 10-byte time
; DE - points to 6-byte result
        push psw
        push b
        push d
        push h

        out WATCHDOG_PET_O

        in BANK_SELECT_I
        push psw                ; preserve Bank
        mvi a,EE_BANK
        out BANK_SELECT_O       ; select EEPROM bank
        call EE_10_6          ; call EEPROM code

cret:   pop psw
        out BANK_SELECT_O       ; restore Bank
        pop h
        pop d
        pop b
        pop psw
        ret


CLOCK_6_TO_10:
; HL - points to 6-byte time
; DE - points to 10-byte result
        push psw
        push b
        push d
        push h

        out WATCHDOG_PET_O

        in BANK_SELECT_I
        push psw                ; preserve Bank
        mvi a,EE_BANK
        out BANK_SELECT_O       ; select EEPROM bank
        call EE_6_10          ; call EEPROM code
        jmp cret


