; This is the example done in class on 10/22/2010
;Jonathan Valvano
PTP      equ $0258  ; Port P I/O Register
PTIP     equ $0259  ; Port P Input
DDRP     equ $025A  ; Port P Data Direction Register

PTS      equ $0248  ; Port S I/O Register
PTIS     equ $0249  ; Port S Input
DDRS     equ $024A  ; Port S Data Direction Register

PTT      equ $0240  ; Port T I/O Register
PTIT     equ $0241  ; Port T Input
DDRT     equ $0242  ; Port T Data Direction Register

;Timer
OC7D     equ $0043  ; Output Compare 7 Data Register
OC7M     equ $0042  ; Output Compare 7 Mask Register
TC0      equ $0050  ; Timer Input Capture/Output Compare Register 0
TC1      equ $0052  ; Timer Input Capture/Output Compare Register 1
TC2      equ $0054  ; Timer Input Capture/Output Compare Register 2
TC3      equ $0056  ; Timer Input Capture/Output Compare Register 3
TC4      equ $0058  ; Timer Input Capture/Output Compare Register 4
TC5      equ $005A  ; Timer Input Capture/Output Compare Register 5

TC6      equ $005C  ; Timer Input Capture/Output Compare Register 6
TC7      equ $005E  ; Timer Input Capture/Output Compare Register 7
TCNT     equ $0044  ; Timer Count Register
TCTL1    equ $0048  ; Timer Control Register 1
TCTL2    equ $0049  ; Timer Control Register 2
TCTL3    equ $004A  ; Timer Control Register 3
TCTL4    equ $004B  ; Timer Control Register 4
TFLG1    equ $004E  ; Main Timer Interrupt Flag 1
TFLG2    equ $004F  ; Main Timer Interrupt Flag 2
TIE      equ $004C  ; Timer Interrupt Enable Register

TIOS     equ $0040  ; Timer Input Capture/Output Compare Select
TSCR1    equ $0046  ; Timer System Control Register1
TSCR2    equ $004D  ; Timer System Control Register 2
PACTL    equ $0060  ; Pulse accumulator control register
   org $2000
NumberOfOC0interrupts rmb  2
   org $4000
main lds #$4000
     bsr Init
     cli
loop
     ldaa PTT   ;toggles when main loop runs
     eora #$80
     staa PTT

    bra loop
Init movb #$80,TSCR1
     movb #3,TSCR2  ;1us
     bset TIOS,#$01
     bset TIE,#$01
     ldd  TCNT
     addd #100
     std  TC0  ;interrupt in 100us
     movw #0,NumberOfOC0interrupts ;debugging
     bset DDRP,#$80  ;debugging monitor
     bset DDRT,#$80  ;debugging monitor

     rts

OC0InterruptHandler  ;ISR Interrupt Service Routine
     ldaa #1
     staa TFLG1
     ldd  TC0
     addd #1000
     std  TC0  ;interrupt again in 1ms
     ldaa PTP  ;PP7 toggles when interrupt occurs
     eora #$80
     staa PTP
     ldd  NumberOfOC0interrupts 
     addd #1
     std  NumberOfOC0interrupts 
     rti
     org $FFEE
     fdb OC0InterruptHandler  
     org $FFFE
     fdb main