; ==== ISUAL Flight Software -- Univ of California, Berkeley, SSL ====
; DAY.a -- Day Sensor Functions
;
; Includes interrupt service routine (RST4)
; shared between Day-sensors and Filter wheel

; Revision History:
;  18-Dec-2002 - SPG - ISUAL Flight Software rev 7.3
;    First Official Release
;  31-dec-2002
;    Removed debug.i
;  26-Mar-2003
;    mask out "raw" bits in DAY_SENS_STS_I
;    initialize thresholds=50h

        include day.i
        include ioports.i
        include util.i
        include clock.i
        include gbl.i
        include filter.i
;
status  ds 1
;
DAY_INIT:
        push psw
        push h
;
; start off with "any" sensor selected
; state-change interrupts enabled
; 7: Enable day sensor 4
; 6: Enable day sensor 3
; 5: Enable day sensor 2
; 4: Enable day sensor 1
; 3: Enable day sensor 4 state-change interrupt
; 2: Enable day sensor 3 state-change interrupt
; 1: Enable day sensor 2 state-change interrupt
; 0: Enable day sensor 1 state-change interrupt
        mvi a,0         ; disables all day-sensors
        out DAY_SENS_CTL_O
        sta status      
; set default threshholds=50h
        mvi a,050h
        out DAY1_THR_O
        out DAY2_THR_O

;
; Setup interrupt
        mvi A,JMP_OPCODE
        sta RST_4
        lxi h,DAY_ISR     ; address of Interrupt handler
        shld RST_4+1      
;
; enable interrupt
        in IRQ_ENABLE_I
        ori 10h   ; bit 4 enables Day/FW RST4 interrupt
        out IRQ_ENABLE_O
;
        pop h
        pop psw
        ret

DAY_SENSOR_DESIGNATE:
; A - bits 0..1 select sensors
        push psw
        push b
        in DAY_SENS_STS_I
        ani 80h         ; preserve b7
        mov b,a
; put select bits in 4..5 (selection)
        ani 03h 
        rlc
        rlc
        rlc
        rlc
; do NOT enable state-change interrupts (b1,b0)
; zero TBD bits
        ani 0B0h        ; b6,b3,b2,b1,b0=0 
        ora b           ; restore b7
; set selections
        out DAY_SENS_CTL_O
        pop b
        pop psw
        ret

;
; ==============================================
; Interrupt Service for Sun-Sensor state-changes
; ==============================================
DAY_ISR:
        push psw
        push b
        push d
        push h
;
; Day-Sensor interrupt is shared with the Filter Wheel Clock
; (actually, we don't get any Day-Sensor interrupts)

; see if FW Clock interrupt is enabled
; and an interrupt is pending.
        in FW_CTL_I
        ani 80h
        jz dismiss        ; no
; was FW counter bit 7 a 1?
        in FW_CNTR_I
        ani 80h
        jz dismiss      ; not the FW
; process FW Clock interrupt
; (which should set a new count, making 98 bit 7 a 0)
        call FILTER_ISR

; interrupt not caused by FW or DaySensor,
; ignore it
dismiss:
        pop h
        pop d
        pop b
        pop psw
        ei
        ret



