#include <v2_18g3.asm>

#define	PACTL	REGBASE+0x26
#define	PACNT	REGBASE+0x27 
#define	TMSK2	REGBASE+0x24
#define	TFLG2	REGBASE+0x25


	.sect .data
msg:	.asciz	"Pulse Accumulator Test Program\n\n"
	

	.sect .text
main:
	ldx	#msg		// pointer to output message
	jsr	OUTSTRING	// write program name out

	ldx     #BUTTONISR	// interrupt for interrupt button press
	stx     ISR_JUMP15

	ldx     #PA_OVRISR	// interrupt for pulse accumulator overflow
	stx     ISR_JUMP4	// called when PACNT goes from 255 to 0 

	ldaa    #0xf0		// Turn on pulse accumulator
	staa    PACTL
	ldaa    TMSK2		// Turn on overflow interrupts
	oraa	#0x20
	staa    TMSK2

/* Event spin loop */
loop:
	// every time PACNT is 0 (every 256 ticks), we print '.'
	// Gives you an idea of how fast the pulse accumulator is.
	ldaa PACNT
	bne  loop
	
	ldaa #'.'		 			
	jsr  OUTCHAR
	bra  loop

/* Interrupt Service Routine called each time the pulse accumlator
overflows or about every 10 ms. */
PA_OVRISR:	
// if you don't reset these flags (see description in HC11 manual), 
// nasty stuff happens, e.g. Ctrl-X reset can get disabled
	ldaa TFLG2
	oraa #0xc0		// clear PAOVF
	staa TFLG2
	rti

/* ISR called each time the IRQ pin is pulled low (button pushed). Do
as little as possible in this interrupt; LCD functions called here may
crash. Could use this interrupt to save current value of PACNT as a
"random" number. */
BUTTONISR:
    ldab PACNT
    clra
    jsr  CONSOLEINT	// output value of pulse accumulator count (PACNT)
    rti
