;
; ---------------------------------------------------------------
; Application definitions
; ---------------------------------------------------------------
;
; ------ event log definitions ----
;
evtentrysize	equ	8
evtentrycount	equ	256
evtlogsize	equ	evtentrysize*evtentrycount
;
; ------ cell structure --------
;
cell_next	equ	0
cell_prev	equ	2
cell_use	equ	4
cell_code	equ	5
cell_code2	equ	6
cell_code3	equ	7
cell_data	equ	8
cell_datalen	equ	8
cell_size	equ	cell_data+cell_datalen
;
; ------ cell pool dimensions ----
;
cellpoolnum	equ	128	; 128 cells in pool
cellpoolsize	equ	cellpoolnum*cell_size
;
; ------ cell queue structure ------
;
cque_head	equ 	0	; index of head entry pointer 
cque_tail	equ	2       ; index of tail entry pointer
cque_cbsize	equ	4	; cell queue control block size is 4 bytes
;
cque_num	equ	8	; 8 cell queues
;
; ------ cell use definitions
;
c_use_key	equ	080H
c_use_puls	equ	081H
c_use_to	equ	082H
;
; ------ event code definitions
;
c_evt_key	equ	1	; KEY event code
c_evt_to	equ	3	; TIMEOUT event code
;
; ------ key matrix definitions
;
keyrownum	equ	8	; 8 rows
keycolnum	equ	8	; 8 keys per row (= 1 byte)
;
keydrive	equ	0FF07H
keyreturn	equ	0FF07H
;
c_key_make      equ	040H	; key MAKE event mask
c_key_break	equ	000H	; key BREAK event mask
;
c_key_col_0	equ	0
c_key_col_1	equ	1
c_key_col_2	equ	2
c_key_col_3	equ	3
c_key_col_4	equ	4
c_key_col_5	equ	5
c_key_col_6	equ	6
c_key_col_7	equ	7
;
c_key_row_0	equ	0
c_key_row_1	equ	008H
c_key_row_2	equ	010H
c_key_row_3	equ	018H
c_key_row_4	equ	020H
c_key_row_5	equ	028H
c_key_row_6	equ	030H
c_key_row_7	equ	038H
;
; ------ relay driver definitions ----
;
relbase		equ	0FF00H
;
reldrivnum	equ	4	; 4 byte-wide driver ports
;
; --- PULSER definitions and macros --------------------------
;
; --- dual-plane definitions
;
c_plane_mask	EQU	080H
c_plane0_select	EQU	000H
c_plane1_select	EQU	080H
c_plane0_line	EQU	30	; toggle this line to select plane 0
c_plane1_line	EQU	31	; toggle this line to select plane 1
;
; --- PULSER states
;
psta_wevt	EQU	0
psta_delay	EQU	1
;
; PULSER events
;
pev_bron	EQU	1	; BI-relay ON
pev_broff	EQU	2	; BI-relay OFF
pev_brtog	EQU	3	; BI-relay TOGle
pev_mron	EQU	4	; MO-relay ON
pev_mroff	EQU	5	; MO-relay OFF
pev_mrtog	EQU	6	; MO-relay TOGgle
pev_mbon	EQU	7	; memorize BI-relay ON
pev_mboff	EQU	8	; memorize BI-relay OFF
pev_mmon	EQU	9	; memorize MO-relay ON
pev_mmoff	EQU	10	; memorize MO-relay OFF
;
; PULSER program commands
;
pc_end		EQU	0	; end of sequence
pc_c1be		EQU	8	; compare P1 to <BP1>, if equal, branch to <WP2>
pc_sdc		EQU	9       ; set delay counter to <WP1>
pc_nst		EQU	10	; set new state to <PP1> 
pc_delay	EQU	11	; delay for <BP1>*10ms
pc_wevt		EQU	12	; wait for event in queue 1
pc_plon		EQU	13	; set port line <P1> to ON
pc_ploff	EQU	14	; set port line <P1> to OFF
pc_bra		EQU	15	; branch to <WP1>
pc_cbro		EQU	16	; calc. BI-relay ON  line number from <P2> to <P1>
pc_cbrf         EQU	17      ; calc. BI-relay OFF line number from <P2> to <P1>
pc_mbro		EQU	18	; memorize BI-relay <P2> as ON
pc_mbrf		EQU	19	; memorize BI-relay <P2> as OFF
pc_mmro		EQU	20	; memorize MO-relay <P2> as ON
pc_mmrf		EQU	21	; memorize MO-relay <P2> as OFF
pc_cbtl		EQU	22	; calc BI-relay <P2> toggle line to <P2>
pc_mtog		EQU	23	; toggle MO-relay <P2>
pc_plon2	EQU	24	; set port line <P2> to ON
pc_ploff2	EQU	25	; set port line <P2> to OFF
pc_cbps		EQU	26	; calculate BI-relay plane select line number from <P2> to <P1>
;
; PULSER command macros
;
%*DEFINE(P_END)(
	DB	pc_end)
;
%*DEFINE(P_C1BE(COMPARE_VALUE,BRANCH_DESTINATION))(
	DB	pc_c1be,%COMPARE_VALUE
	DW	%BRANCH_DESTINATION)
;
%*DEFINE(P_SDC(DELAY_VALUE_MS))(
	DB	pc_sdc,%DELAY_VALUE_MS/10)
;
%*DEFINE(P_NST(NEW_STATE))(
	DB	pc_nst,%NEW_STATE)
;
%*DEFINE(P_DELAY(DELAY_VALUE_MS))(
	DB	pc_delay,%DELAY_VALUE_MS/10)
;
%*DEFINE(P_WEVT)(
	DB	pc_wevt)
;
%*DEFINE(P_PLON)(
	DB	pc_plon)
;
%*DEFINE(P_PLOFF)(
	DB	pc_ploff)
;
%*DEFINE(P_BRA(BRANCH_DESTINATION))(
	DB	pc_bra
	DW	%BRANCH_DESTINATION)
;
%*DEFINE(P_CBRO)(
	DB	pc_cbro)
;
%*DEFINE(P_CBRF)(
	DB	pc_cbrf)
;
%*DEFINE(P_MBRO)(
	DB	pc_mbro)
;
%*DEFINE(P_MBRF)(
	DB	pc_mbrf)
;
%*DEFINE(P_MMRO)(
	DB	pc_mmro)
;
%*DEFINE(P_MMRF)(
	DB	pc_mmrf)
;
%*DEFINE(P_CBTL)(
	DB	pc_cbtl)
;
%*DEFINE(P_MTOG)(
	DB	pc_mtog)
;
%*DEFINE(P_PLON2)(
	DB	pc_plon2)
;
%*DEFINE(P_PLOFF2)(
	DB	pc_ploff2)
;
%*DEFINE(P_CBPS)(
        DB	pc_cbps)
;
; -----------------------------------
;
%*DEFINE(LDIX(VAL,REGL,REGH))(
        MOV	%REGL,#LOW(%VAL)
	MOV	%REGH,#HIGH(%VAL))
;
%*DEFINE(MDPTR(REGL,REGH))(
	mov	dpl,%REGL
	mov	dph,%REGH)
;
%*DEFINE(PDPTR(REGL,REGH))(
	mov	%REGL,dpl
	mov	%REGH,dph)
;
; ------ basic tick timer in timer 0 ---------------------------
;
; timer 0 period is calculated as follows:
;
; reload := -((time_period in us * quartz frequency in MHz) / 12 )
;
timer_reload    equ     -9216   ; timer 0 for 10 ms tick at 11.0592 MHz    <===
;
t0divisor	equ	25	; generate 250ms from 10 ms cycle
;
; --- TIMEOUT definitions ---------------------------------------
;
c_to_divisor	equ	10	; divisor between basic tick and classes is 10
                                ; this results in
           			; class 0: 100 ms
				; class 1: 1   sec.
				; class 2: 10  sec.
				; class 3: 100 sec.
c_to_slots	equ	16	; 16 slots per class
c_to_classes	equ	4	; 4 classes, 0..3
c_no_timeouts	equ	(c_to_slots*c_to_classes)	; total number of timeouts
;
; --- TASKER definitions ----------------------------------------
;
tsktra_evt	EQU	0	; offset to event
tsktra_sev	EQU	1	; offset to subevent
tsktra_act	EQU	2	; offset to action
tsktra_sac	EQU	3	; offset to sub-action
tsktrasiz	EQU	4	; size of one task transition entry
;
tsktblsiz	EQU	256*tsktrasiz	; allocate for 256 transitions
;
; --- ACTION codes
;
c_act_puls	EQU	000H	; action 0: event to pulser
c_act_seq	EQU	010H	; action 1: perform sequence
;
; --- SEQUENCER definitions ------------------------------------------- 
;
c_seq_end	EQU	0	; end of sequence
c_seq_puls	EQU	1	; event <P1>,<P2> to pulser
c_seq_tostart   EQU	2	; start timeout <P1> for <P2> ticks
c_seq_tostop	EQU	3	; stop timeout <P1>
c_seq_ljmp	EQU	4	; goto sequence step <P1>*256+<P2>
c_seq_setvar	EQU	5	; set variable <P1> to value <P2>
c_seq_cgequ	EQU	6  	; compare variable <P1> against <P2>, if equal, jump to <W_P3+4>
c_seq_djnz	EQU	7	; decrement variable <P1>, if not zero, branch relative by <P2>
;
; --- SEQUENCER macros
;
; SEQ_END: end of sequence
;
%*DEFINE(SEQ_END)(
	db	c_seq_end)
;
; SEQ_PULS: send command PEVT with parameter PPAR to pulser
;
%*DEFINE(SEQ_PULS(PEVT,PPAR))(
	db	c_seq_puls,%PEVT,%PPAR
	)
;
; SEQ_TOSTART: start timeout running in slot TSLOT for TTICKs
;
%*DEFINE(SEQ_TOSTART(TSLOT,TTICK))(
	db	c_seq_tostart,%TSLOT,%TTICK
	)
;
; SEQ_TOSTOP: stop timeout running in slot TSLOT
;
%*DEFINE(SEQ_TOSTOP(TSLOT))(
	db	c_seq_tostop,%TSLOT
	)
;
%*DEFINE(SEQ_LJMP(JTARGET))(
	db	c_seq_ljmp
	dw	%JTARGET
	)
;
; SEQ_SETVAR: set variable number VNUM to BYTE value VVAL
;
%*DEFINE(SEQ_SETVAR(VNUM,VVAL))(
	db	c_seq_setvar,%VNUM,%VVAL)
;
; SEQ_CGEQU: compare variable VNUM against VCMP, if greater equal, jump to JTARGET
;
%*DEFINE(SEQ_CGEQU(VNUM,VCMP,JTARGET))(
	db	c_seq_cgequ,%VNUM,%VCMP
	dw	%JTARGET
	)
;
; SEQ_DJNZ: decrement variable VNUM, if not yet zero, jump to JTARGET
;
%*DEFINE(SEQ_DJNZ(VNUM,JTARGET))(
	db	c_seq_djnz,%VNUM
	dw	%JTARGET
	)
;
; ---- sequencer utility macros
;
%*DEFINE(SEQU_GETIP(RL,RH))(
	mov	dptr,#seq_exe_ptr
	movx	a,@dptr
	mov	%RH,a
	inc	dptr
	movx	a,@dptr
	mov	%RL,a)
;
; save register pair RH,RL to sequencer instruction pointer
;
%*DEFINE(SEQU_PUTIP(RL,RH))(
	mov	dptr,#seq_exe_ptr
	mov	a,%RH
	movx	@dptr,a
	inc	dptr
	mov	a,%RL
	movx	@dptr,a)
;
; ----- MACROS for using BASIC-52 API calls -------------------------------------
;
b52_entry	equ	00030H
;
ab52_puttxt	equ	006H
ab52_putcrlf	equ	007H
ab52_putchar	equ	080H
;
%*DEFINE(B52_PUTCHAR(VCHAR))(
	mov	r5,%VCHAR
	mov	a,#ab52_putchar
	lcall	b52_entry)
;
%*DEFINE(B52_PUTCRLF)(
	mov	a,#ab52_putcrlf
	lcall	b52_entry)
;
%*DEFINE(B52_PUTTXTC(VADR))(
	mov	r3,#HIGH(%VADR)
	mov	r1,#LOW(%VADR)
	clr	52
	mov	a,#ab52_puttxt
	lcall	b52_entry)
