Routine: Clock
Function: Updates clock and displays it if the appropriate flag is selected
Called by: Keyb, Read
Calls: Reset
Entry: Clkflg=0 for no display, 129 for Hrs.Mins.Secs format, 130 for hrs mins format.
Exit: None
Preserved: E,IX,IY
CLOCK CALL RESET
; Call the Reset subroutine to execute a system reset if the Reset key is being pressed.
CALL CLK5
; Check the 50 Hz counter, and update the clock if necessary.
CP 25
JR C,CLK
; If 0.5 seconds have passed since the beginning of the second, clear Clkdot: this is used later to flash the decimal point between the Hours and Minutes in format A.
XOR A
LD (CLKDOT),A
CLK LD A,(CLKFLG)
BIT 7,A
RET Z
; Return if clock display is not required.
AND 3
CP 2
JR Z,CLK1
; Display in format B, if Clkflg is 130, otherwise branch to Clk1 and display in format B.
LD A,(CLKSEC)
OUT (8),A
LD A,(CLKSEC)
OUT (9),A
LD A,(CLKMIN)
OR 16
; Or 16 illuminates the decimal point of the digit.
OUT (10),A
LD A,(CLKMIN+1)
OUT (11),A
LD A,(CLKHRS)
OR 16
OUT (12),A
LD A,(CLKHRS+1)
OUT (13),A
OR A
LD A,254
JR Z,CLK0
INC A
CLK0 OUT (144),A
RET
CLK1 LD A,(CLKMIN)
OUT (8),A
LD A,(CLKMIN+1)
OUT (9),A
LD A,(CLKHRS)
LD B,A
LD A,(CLKDOT)
OR B
; Include a decimal point here only in the first half of each second.
OUT (11),A
LD A,(CLKHRS+1),A
OUT (12),A
OR A
LD A,52
JR Z,CLK2
INC A
INC A
CLK2 OUT (144),A
RET
CLK3 LD HL,CLKSEC
; This part of the subroutine increments the time by one second. Minutes and hours are also updates if the seconds overflow.
LD C,10
LD D,6
LD B,0
LD A,C
INC (HL)
CP (HL)
RET NZ
LD (HL),B
INC HL
INC (HL)
LD A,D
CP (HL)
RET NZ
LD (HL),B
INC HL
INC (HL)
LD A,C
CP (HL)
RET NZ
LD (HL),B
INC HL
INC (HL)
LD A,D
CP (HL)
RET NZ
LD (HL),B
INC HL
INC (HL)
LD A,C
CP (HL)
JR NZ,CLK4
LD (HL),B
INC HL
INC (HL)
RET
CLK4 LD A,4
CP (HL)
RET NZ
INC HL
LD A,2
CP (HL)
RET NZ
LD (HL),B
DEC HL
LD (HL),B
RET
CLK5 LD A,(CLK50)
LD D,A
IN A,(6)
; Read in the 50 Hz counter value and check it against the value calculated for the next second.
SUB D
JP P,CLK6
NEG
CLK6 CP 50
RET C
; Return if a second has not passed yet.
LD A,50
ADD D
LD (CLK50),A
; Calculate the time of the next second, and call Clk3 to increment the clock counters.
CALL CLK3
JR CLK5