;
;==================================================================================================
; HEATH H8 FRONT PANEL (DISPLAY AND KEYBOARD) ROUTINES
;==================================================================================================
;
; LED SEGMENTS (BIT VALUES)
;
;	+--02--+
;	40    04
;	+--01--+
;	20    08
;	+--10--+  80
;
;
H8PKEYNONE	.EQU	$FF		; NONE
H8PKEY0		.EQU	$FE
H8PKEY1		.EQU	$FC
H8PKEY2		.EQU	$FA
H8PKEY3		.EQU	$F8
H8PKEY4		.EQU	$F6
H8PKEY5		.EQU	$F4
H8PKEY6		.EQU	$F2
H8PKEY7		.EQU	$F0
H8PKEY8		.EQU	$EF
H8PKEY9		.EQU	$CF
H8PKEYPLUS	.EQU	$AF		; PLUS
H8PKEYMINUS	.EQU	$8F		; MINUS
H8PKEYMUL	.EQU	$6F		; MULTIPLY
H8PKEYDIV	.EQU	$4F		; DIVIDE
H8PKEYNUM	.EQU	$2F		; NUMBER
H8PKEYDOT	.EQU	$0F		; DOT
;
H8P_TICCNT	.EQU	$FFEA		; TICCNT AT $FFEA IS COPIED DOWN TO $000B FOR HEATH COMPATIBILITY
H8P_SPEED	.EQU	$FFEC		; SPEED CONTROL VALUE IS STORED HERE
H8P_SPDIO	.EQU	$30
H8FPIO		.EQU	$F0
;
	DEVECHO	"H8P: IO="
	DEVECHO	H8FPIO
	DEVECHO	"\n"
;
;__H8P_PREINIT_______________________________________________________________________________________
;
;  CONFIGURE AND RESET PANEL
;____________________________________________________________________________________________________
;
; HARDWARE RESET PRIOR TO ROMWBW CONSOLE INITIALIZATION
;
H8P_PREINIT:
	; HEATH H8 PLATFORM IMPLEMENTS A CPU SPEED DIVISOR AS SET BY THE
	; 2 LS BITS OF PORT H8PSPDIO.  THE OSCILLATOR IS NORMALLY 16MHZ.
	; 0=FULL (16MHZ), 1=1/2 (8MHZ), 2=1/4 (4 MHZ), 3=1/8 (2 MHZ)
	; FOR BOOT, WE SET THE DIVISOR TO HALF (8MHZ)WHICH IS THE FASTEST
	; SPEED WE CAN USE THAT STILL ENSURES ALL HARDWARE CAN BE DETECTED.
	; E.G., MSX BOARD MAXES OUT AT 8MHZ.
	LD	A,$01			; 8 MHZ OPERATION
	OUT	(H8P_SPDIO),A		; IMPLEMENT IT
	LD	(H8P_SPEED),A		; UPDATE FP SHADOW
;
	; HOOK INTERRUTPS FOR FRONT PANEL OPERATION
	LD	HL,H8P_INTR
	CALL	HB_ADDIM1
;
	RET
;
;__H8P_INIT__________________________________________________________________________________________
;
;  DISPLAY H8 FRONT PANEL INFO ON ROMWBW CONSOLE
;____________________________________________________________________________________________________
;
H8P_INIT:
	CALL	NEWLINE			; FORMATTING
	PRTS("H8FP:$")			; FORMATTING
;
	PRTS(" IO=0x$")			; FORMATTING
	LD	A,H8FPIO		; GET BASE PORT
	CALL	PRTHEXBYTE		; PRINT BASE PORT
;
	RET				; DONE
;
; H8 FRONT PANEL INTERRUPT
;
H8P_INTR:
	LD	(H8P_BCVAL),BC
	LD	(H8P_DEVAL),DE
	LD	(H8P_HLVAL),HL
	LD	HL,(H8P_TICCNT)		; 2MS TIC COUNTER
	INC	HL
	LD	(H8P_TICCNT),HL
	CALL	H8P_TIMER		; UP TIMER
	CALL	H8P_STAT		; CHECK KEYPAD PRESS
	CALL	H8P_GETSEGIDX		; SEGMENT INDEX IN (C)
	CALL	H8P_HORN
	OR	C
	OUT	(H8FPIO),A		; CLEAR INTERRUPT AND SET LED IDX
	LD	C,A
	LD	A,(H8P_FPENA)
	OR	A
	LD	A,$FF
	CALL	NZ,H8P_GETSEGPAT
	OUT	(H8FPIO+1),A		; SET LED PATTERN
;
	CALL	H8P_KEYPAD
	LD	A,(H8P_STTIMER)
	INC	A
	AND	$1F			; UPDATE LEDS EVERY 32 TICKS
	LD	(H8P_STTIMER),A
	CALL	Z,H8P_HDLSTATE
	LD	A,(H8P_HBTICK)		; ROMWBW TIMER
	INC	A
	LD	(H8P_HBTICK),A
	CP	10
	RET	NZ
	CALL	HB_TICK
	XOR	A
	LD	(H8P_HBTICK),A
	INC	A			; INTERRUPT HANDLED
	RET
;
; HANDLE FRONT PANEL SPEAKER SOUNDS
;
H8P_HORN:
	LD	HL,(H8P_HORNDUR)
	LD	A,H
	OR	L
	LD	A,$D0			; HORN OFF
	RET	Z
	DEC	HL
	LD	(H8P_HORNDUR),HL
	LD	A,$50			; HORN ON
	RET
;
; HANDLE UP-TIME TIMER
;
H8P_TIMER:
	LD	HL,(H8P_ONESEC)
	DEC	HL
	LD	(H8P_ONESEC),HL
	LD	A,H
	OR	L
	RET	NZ
	LD	HL,(H8P_UPTIME)
	INC	HL
	LD	(H8P_UPTIME),HL
	LD	HL,500
	LD	(H8P_ONESEC),HL
	CALL	H8P_TIMER1
	LD	A,(H8P_STATE)
	OR	A
	CALL	Z,H8P_TIMER2
	RET
; ADVANCE DIGITS
H8P_TIMER1:
	LD	C,9
	LD	HL,H8P_UPTDIG+8
	CALL	H8P_ADVDIG
	RET
; SHOW DIGITS
H8P_TIMER2:
	LD	DE,H8P_UPTDIG+8
	LD	HL,H8P_SEGBUF+8
	LD	C,9
H8P_TIMER3:
	PUSH	BC
	PUSH	HL
	LD	A,C
	CP	7
	JR	Z,H8P_TIMER4
	CP	4
	JR	Z,H8P_TIMER4
	JR	H8P_TIMER5
H8P_TIMER4:
	LD	A,$80
	JR	H8P_TIMER6
H8P_TIMER5:
	LD	HL,H8P_DIGMAP
	LD	A,(DE)
	LD	C,A
	LD	B,0
	ADD	HL,BC
	LD	A,(HL)
H8P_TIMER6:
	POP	HL
	LD	(HL),A
	DEC	HL
	DEC	DE
	POP	BC
	DEC	C
	JR	NZ,H8P_TIMER3
	RET
H8P_ADVDIG:
	LD	A,(HL)			; 000.000.00X
	INC	A
	LD	(HL),A
	CP	10			; 0-9
	RET	NZ
	LD	(HL),0
	DEC	HL
	LD	A,(HL)			; 000.000.0X0
	INC	A
	LD	(HL),A
	CP	6			; 0-5
	RET	NZ
	LD	(HL),0
	DEC	HL
	DEC	HL
	LD	A,(HL)			; 000.00X.000
	INC	A
	LD	(HL),A
	CP	10			; 0-9
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YPAD2
	CP	H8PKEY1
	JP	Z,H8P_KEYPAD1
	CP	H8PKEY0
	JP	Z,H8P_KEYPAD0
	CP	H8PKEYPLUS
	JP	Z,H8P_KEYPADPLUS
	CP	H8PKEYMINUS
	JP	Z,H8P_KEYPADMINUS
	RET
; RESET TIMER
H8P_KEYPADALT:
	LD	A,(H8P_FPENA)
	OR	A
	RET	Z
	LD	A,(H8P_STATE)
	OR	A
	RET	NZ
	XOR	A
	LD	C,9
	LD	HL,H8P_UPTDIG
H8P_KEYPADALTL:
	LD	(HL),A
	INC	HL
	DEC	C
	JR	NZ,H8P_KEYPADALTL
	RET
; ENABLE FRONT PANEL DISPLAY
H8P_KEYPADCAN:
	LD	A,(H8P_FPENA)
	CPL
	LD	(H8P_FPENA),A
	RET
; SET MEM STATE
H8P_KEYPADMEM:
	LD	A,2
	LD	(H8P_STATE),A
	LD	(H8P_MEMENTER),A
	XOR	A
	LD	(H8P_MEMADRIDX),A
	CALL	H8P_UPDMEMLOC
	RET
; SET REG STATE
H8P_KEYPADREG:
	LD	A,1
	LD	(H8P_STATE),A
	XOR	A
	LD	(H8P_MEMENTER),A
	RET
; NOTHING
H8P_KEYPAD9:
;	RET
; TIMER
H8P_KEYPAD8:
	LD	A,(H8P_STATE)
	CP	2
	RET	Z
	LD	A,0
	LD	(H8P_STATE),A
	RET
; SPEED CONTROL
H8P_KEYPAD7:
	LD	A,(H8P_STATE)
	CP	2			; MEM MODE
	LD	A,7
	JP	Z,H8P_KEYPADDIG
	LD	A,3
	LD	(H8P_STATE),A
	RET
; PC (OUT)
H8P_KEYPAD6:
	LD	A,(H8P_STATE)
	CP	2			; MEM MODE
	LD	A,6
	JP	Z,H8P_KEYPADDIG
	LD	A,5
	LD	(H8P_REGNUM),A
	RET
; HL (IN)
H8P_KEYPAD5:
	LD	A,(H8P_STATE)
	CP	2			; MEM MODE
	LD	A,5
	JP	Z,H8P_KEYPADDIG
	LD	A,3
	LD	(H8P_REGNUM),A
	RET
; DE
H8P_KE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3
	CP	2
	RET	NZ
	LD	HL,(H8P_MEMLOC)
	INC	HL
	LD	(H8P_MEMLOC),HL
	JP	H8P_UPDMEMLOC
H8P_KEYPADPLUS3:
	LD	A,(H8P_SPEED)
	OR	A
	RET	Z
	DEC	A
	;;;LD	(H8P_SPEED),A
	;;;OUT	(H8P_SPDIO),A
	;;;RET
	; EXIT VIA HBIOS SPEED CHANGE, W/ L=NEW SPEED
	JP	H8P_SETSPD		; SEE HBIOS.ASM
; MEM/SPEED DECREASE
H8P_KEYPADMINUS:
	LD	A,(H8P_STATE)
	CP	3
	JR	Z,H8P_KEYPADMINUS3
	CP	2
	RET	NZ
	LD	HL,(H8P_MEMLOC)
	DEC	HL
	LD	(H8P_MEMLOC),HL
	JP	H8P_UPDMEMLOC
H8P_KEYPADMINUS3:
	LD	A,(H8P_SPEED)
	CP	3
	RET	Z
	INC	A
	;;;LD	(H8P_SPEED),A
	;;;OUT	(H8P_SPDIO),A
	;;;RET
	; EXIT VIA HBIOS H8P SPEED CHANGE, W/ A=NEW SPEED
	JP	H8P_SETSPD		; SEE HBIOS.ASM
;
; HANDLE FRONT PANEL STATE
;
H8P_HDLSTATE:
	LD	A,(H8P_STATE)
	OR	A
	RET	Z			; UP-TIME TIMER
	DEC	A
	JP	Z,H8P_HDLREG		; SHOWING REGISTER VALUES
	DEC	A
	JP	Z,H8P_HDLMEM		; SHOWING MEMORY LOCATION VALUE
	DEC	A
	JP	Z,H8P_HDLSPD		; MODIFYING SPEED
	RET
;
H8P_HDLREG:
	LD	A,(H8P_REGNUM)
	LD	HL,H8P_REGAF
	OR	A
	JP	Z,H8P_HDLREGAF
	DEC	A
	LD	HL,H8P_REGBC
	JP	Z,H8P_HDLREGBC
	DEC	A
	LD	HL,H8P_REGDE
	JP	Z,H8P_HDLREGDE
	DEC	A
	LD	HL,H8P_REGHL
	JP	Z,H8P_HDLREGHL
	DEC	A
	LD	HL,H8P_REGSP
	JP	Z,H8P_HDLREGSP
	DEC	A
	LD	HL,H8P_REGPC
	JP	Z,H8P_HDLREGPC
	RET
H8P_HDLREGAF:
	CALL	H8P_UPDLEDS
	LD	HL,HBX_INTSTK
	DEC	HL
	LD	A,(HL)			; (HL)=AF LOW
	LD	(H8P_AFVAL),A
	DEC	HL
	LD	A,(HL)			; (HL)=AF HIGH
	LD	(H8P_AFVAL+1),A
	LD	BC,H8P_AFVAL
	LD	HL,H8P_SEGBUF
	JP	H8P_FILLOCT
H8P_HDLREGBC:
	LD	BC,H8P_BCVAL
	CALL	H8P_FILLOCT
	JP	H8P_UPDLEDS
H8P_HDLREGDE:
	LD	BC,H8P_DEVAL
	CALL	H8P_FILLOCT
	JP	H8P_UPDLEDS
H8P_HDLREGHL:
	CALL	H8P_UPDLEDS
;
	;;;LD	HL,(HBX_INT_SP)
	;;;LD	A,(HL)
	;;;LD	(H8P_HLVAL),A
	;;;INC	HL
	;;;LD	A,(HL)
	;;;LD	(H8P_HLVAL+1),A
;
	LD	A,(HB_CURBNK)		; GET PRE-INT BANK
	LD	D,A			; PUT IN D
;
	; HORRIBLE HACK TO MAKE PEEK RETURN TO OUR BANK!!!
	PUSH	AF			; SAVE ORIG HB_CURBNK
	LD	A,BID_BIOS		; MAKE IT OUR BANK
	LD	(HB_CURBNK),A		; OVERRIDE FOR PEEK
;
	; FOLLOW STACK TO GET PC, USE PEEK IN CASE STACK IN ALT BANK
	LD	HL,(HBX_INT_SP)		; PRE-INT SP HAS HL AT TOS
	CALL	HBX_PEEK		; GET LSB TO E
	LD	C,E			; PUT IN C
	INC	HL			; POINT TO MSB
	CALL	HBX_PEEK		; GET MSB TO E
	LD	B,E			; PUT IN B
	LD	(H8P_HLVAL),BC		; SAVE FOR DISPLAY
;
	; UNDO HACK!!!
	POP	AF			; RECOVER HB_CURBNK
	LD	(HB_CURBNK),A		; SAVE RESTORE IT
;
	LD	BC,H8P_HLVAL
	LD	HL,H8P_SEGBUF
	JP	H8P_FILLOCT
H8P_HDLREGSP:
	CALL	H8P_UPDLEDS
	LD	HL,(HBX_INT_SP)		; GET INT SAVED SP
	INC	HL			; BUMP TO VAL
	INC	HL			; ... OF SP
	INC	HL			; ... AT TIME
	INC	HL			; ... OF INTERRUPT
	LD	(H8P_SPVAL),HL		; SAVE FOR DISPLAY
	LD	BC,H8P_SPVAL
	LD	HL,H8P_SEGBUF
	JP	H8P_FILLOCT
H8P_HDLREGPC:
	CALL	H8P_UPDLEDS
;
	;;;LD	HL,(HBX_INT_SP)		; (HL)=HL LOW
	;;;INC	HL			; (HL)=HL HIGH
	;;;INC	HL			; (HL)=PC LOW
	;;;LD	A,(HL)
	;;;LD	(H8P_PCVAL),A
	;;;INC	HL			; (HL)=PC HIGH
	;;;LD	A,(HL)
	;;;LD	(H8P_PCVAL+1),A
;
	LD	A,(HB_CURBNK)		; GET PRE-INT BANK
	LD	D,A			; PUT IN D
;
	; HORRIBLE HACK TO MAKE PEEK RETURN TO OUR BANK!!!
	PUSH	AF			; SAVE ORIG HB_CURBNK
	LD	A,BID_BIOS		; MAKE IT OUR BANK
	LD	(HB_CURBNK),A		; OVERRIDE FOR PEEK
;
	; FOLLOW STACK TO GET PC, USE PEEK IN CASE STACK IN ALT BANK
	LD	HL,(HBX_INT_SP)		; PRE-INT SP
	INC	HL			; REMOVE 1 LEVEL TO
	INC	HL			; ... POINT TO PRE-INT PC
	CALL	HBX_PEEK		; GET LSB TO E
	LD	C,E			; PUT IN C
	INC	HL			; POINT TO MSB
	CALL	HBX_PEEK		; GET MSB TO E
	LD	B,E			; PUT IN B
	LD	(H8P_PCVAL),BC		; SAVE FOR DISPLAY
;
	; UNDO HACK!!!
	POP	AF			; RECOVER HB_CURBNK
	LD	(HB_CURBNK),A		; SAVE RESTORE IT
;
	LD	BC,H8P_PCVAL
	LD	HL,H8P_SEGBUF
	JP	H8P_FILLOCT
H8P_HDLMEM:
	CALL	H8P_SETDIGD
	RET
H8P_HDLSPD:
	LD	HL,H8P_SPD16
	LD	A,(H8P_SPEED)
	OR	A
	JR	Z,H8P_UPDLE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:
	AND	$C0
	RRA
	RRA
	RRA
	RRA
	RRA
	RRA
	JR	H8P_GETOCTX
; MEDIUM OCTAL BITS
H8P_GETOCTM:
	AND	$38
	RRA
	RRA
	RRA
	JR	H8P_GETOCTX
; LOW OCTAL BITS
H8P_GETOCTL:
	AND	$07
H8P_GETOCTX:
	PUSH	HL
	LD	E,A
	LD	D,0
	LD	HL,H8P_DIGMAP
	ADD	HL,DE
	LD	A,(HL)			; VALUE CONVERTED TO LED SEGMENT PATTERN
	POP	HL
	RET
; B=CURVAL, C=NEWVAL
H8P_SETOCTH:
	LD	A,C
	AND	$03
	RLA
	RLA
	RLA
	RLA
	RLA
	RLA
	LD	C,A
	LD	A,B
	AND	$3F
	OR	C
	RET
H8P_SETOCTM:
	LD	A,C
	AND	$07
	RLA
	RLA
	RLA
	LD	C,A
	LD	A,B
	AND	$C7
	OR	C
	RET
H8P_SETOCTL:
	LD	A,C
	AND	$07
	LD	C,A
	LD	A,B
	AND	$F8
	OR	C
	CALL	H8P_BEEP
	RET
;
H8P_BEEP:
	PUSH	HL
	LD	HL,16
	LD	(H8P_HORNDUR),HL
	POP	HL
	RET
;
; DEVICE INFORMATION
;
H8P_DEVICE:
;	LD	D,DSKYDEV_H8P		; D := DEVICE TYPE
	LD	E,0			; E := PHYSICAL DEVICE NUMBER
	LD	H,0			; H := MODE
	LD	L,H8FPIO		; L := BASE I/O ADDRESS
	XOR	A			; SIGNAL SUCCESS
	RET
;
; DIGITS TO LED PATTERNS
;	+--02--+
;	40    04
;	+--01--+
;	20    08
;	+--10--+  80
;
H8P_DIGMAP:
	;	  0    1    2    3    4    5    6    7
	.DB	$7E, $0C, $37, $1F, $4D, $5B, $7B, $0E
	;	  8    9    A    B    C    D    E    F
	.DB	$7F, $5F, $6F, $79, $72, $3D, $73, $63
;
H8P_REGNUM:
	.DB	0
H8P_REGAF:
	.DB	$00, $00, $00, $00, $00, $00, $00, $6F, $63
H8P_REGBC:
	.DB	$00, $00, $00, $00, $00, $00, $00, $79, $72
H8P_REGDE:
	.DB	$00, $00, $00, $00, $00, $00, $00, $3D, $73
H8P_REGHL:
	.DB	$00, $00, $00, $00, $00, $00, $00, $6D, $70
H8P_REGSP:
	.DB	$00, $00, $00, $00, $00, $00, $00, $5B, $67
H8P_REGPC:
	.DB	$00, $00, $00, $00, $00, $00, $00, $67, $72
H8P_SPD16:
	.DB	$00, $00, $00, $00, $0C, $7B, $5B, $67, $3D
H8P_SPD08:
	.DB	$00, $00, $00, $00, $00, $7F, $5B, $67, $3D
H8P_SPD04:
	.DB	$00, $00, $00, $00, $00, $4D, $5B, $67, $3D
H8P_SPD02:
	.DB	$00, $00, $00, $00, $00, $37, $5B, $67, $3D
H8P_MEMADRIDX:
	.DB	0
H8P_MEMENTER:
	.DB	0
H8P_SPVAL:
	.DW	0
H8P_AFVAL:
	.DW	0
H8P_BCVAL:
	.DW	0
H8P_DEVAL:
	.DW	0
H8P_HLVAL:
	.DW	0
H8P_PCVAL:
	.DW	0
;
H8P_STATE:
	.DB	00
H8P_STTIMER:
	.DB	00
H8P_FPENA:
	.DB	$FF
H8P_SEGIDX:
	.DB	09
H8P_HBTICK:
	.DB	00
H8P_KEYBUF:
	.DB	00
H8P_LSTKEY:
	.DB	00
H8P_SEGBUF:
	.DB	$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
H8P_HORNDUR:
	.DW	$0080
H8P_ONESEC:
	.DW	500
H8P_UPTIME:
	.DW	0
H8P_UPTDIG:
	.DB	0,0,0,0,0,0,0,0,0
