COMBO_DEFAULT_DELAY=12

	.bss

combo_delay      .ds 1
combo_counter    .ds 1
combo_node       .ds 1
combo_child      .ds 2
combo_tree_size  .ds 1
combo_default    .ds 2
combo_tree       .ds 256
combo_keys:      .ds 256
combo_jumpTable: .ds 256

	.code
	
;----------------------------------------------------------------------
; name : comboHandler
;
; description:
; Traverse the combo_tree to check for a valid combo.
; If a combo is valid the prog will jump to the address specified
; in the jump table.
;
comboHandler:
	; Save registers
    pha
    phx
    phy

	ldx    combo_node
	beq    .l0
		lda    combo_keys,X
		cmp    joy
		bne    .l0
			dec    combo_counter
			beq    .timeout
				jmp    .combo_done
.timeout:
.l0:
		lda    combo_tree, X
		bpl    .l1
			asl    A
			tax
			jsr    .combo_jump
			jmp    .combo_reset
.l1:
		sta    combo_child

		inx
		lda    combo_tree, X
		bpl    .l2
			lda    combo_tree_size
.l2:
		sta    combo_child+1
	
		ldx    combo_child
.check_child:
		lda    combo_keys, X
		cmp    joy
		bne    .l3
			stx    combo_node
			jmp    .combo_reset_counter
.l3:
		inx
		cpx   combo_child+1
		bcc   .check_child
	
.combo_reset:
	jsr    .combo_default
	stz    combo_node

.combo_reset_counter:
	lda    combo_delay
	sta    combo_counter

.combo_done:
	ply
	plx
	pla

	rts

.combo_jump:

	jmp    [combo_jumpTable,X]

.combo_default:
	jmp    [combo_default]
	
	; combo editor

;----------------------------------------------------------------------
; name : dummyComboFnctl
;
; description:
; Dummy combo callback.
;
; in  : X = index in the jump table
;
; out : (nothing)
;
dummyComboFnctl:

	txa
	lsr    A
	sta    <_al
	stz    <_ah
	ldx    #20
	lda    #18
	jsr put_number

	rts


