;*********************************************************************
;* counter
;*********************************************************************	
	
;******************************
;*
;* updateCounters
;* 
;*

updateCounters
	
	;prolog

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7

	;global div
	
	dexti 	>counterGlobal
	movei	r6,counterGlobal
	nop
	rqldi	r6,0
	nop
	nop
	ld	r0
	nop
	addi	r0,1
	movei	r1,0
	cmpeqi	r0,counterGlobalHitMax	;
	brtc	counterGlobalNotHit
	movets	r0,r1,r0	;delay slot
	nop			;delay slot
	st	r6,r0		;delay slot
	nop			;delay slot

	;
	
	dexti	>counters
	movei	r6,counters
	
	movei	r1,7	;we have 8 counters
	movei	r0,0	;counter
	
counterLoop	
	
	rqldi	r6,0
	nop
	addqi	r3,r0,2		;0..counter + 2
	ld	r2		;counter
	nop
	
	addi	r2,1
	movei	r4,0
	cmpeq	r2,r3
	movets	r2,r4,r2

	cmpeq	r0,r1
	brtc	counterLoop
	addi	r0,1		;delay slot		
	st	r6,r2		;delay slot
	addi	r6,1		;delay slot
	nop			;delay slot
	
counterGlobalNotHit	
	
	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	poparqp	r3
	pop	r2
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	
	
	
	.def	counterGlobalHitMax	8	
	
counterGlobal
	
	word	0	
	
counters	
	
count2	word	0	;0..1	2		0
count3	word	0	;0..2	3		1
count4	word	0	;0..3	4		2
count5	word	0	;0..4	5		3
count6	word	0	;0..5	6		4
count7	word	0	;0..6	7		5
count8	word	0	;0..7	8		6
count9	word	0	;0..8	9		7
