RAMStart     EQU  $0040
RomStart     EQU  $E000
VectorStart  EQU  $FFDC

$Include 'gpgtregs.inc'

        org     RomStart

gpio_init:

        mov #$FE,DDRA
        MOV #$00,PTA
        mov #$00,$30            ; stores the last number displayed on 7 segment
                                ; at memory location $30
        rts


;- TIMER_INIT -------------------------------------------------------------------------------
; timer_init: initializes timer to free run, no o.c., no i.c., leaves timer stopped
timer_init:
        mov     #$76,T1SC        ; Stop & reset, overflow interrupt enable, prescaler=64
        clr     T1SC0            ; Inhibit all capture/compare functions
        clr     T1SC1
        mov     #$4A,T1MODH      ; Modulo count = 19200 (4B00H)
        mov     #$FF,T1MODL
				 ; Bus clock speed = 2457600 Hz
				 ; Prescaler = 64
				 ; Interrupt period = 1 / 2457600 * 64 * 19200 = 0.5 second 

        bclr    4,T1SC           ; Un-reset TIMer for T1SC
        rts

Main:
        rsp                     ; stack pointer reset
        clra                    ; register init
        clrx
        mov     #$31,CONFIG1    ; MCU runs w/o LVI and COP support
        bsr     gpio_init       ; GPIO initialization
        bsr     timer_init      ; TIM initialization

        bclr    5,T1SC          ; start timer

main_loop:
        wait                    ; reduce power consumption
        bra     main_loop       ; runs infinitely until both buttons pressed

timer_isr:
         bclr 4,PTD
         psha
         lda     $30
         cmp     #$00
         ble     show0
         cmp     #$01
         ble     show1
         cmp     #$02
         ble     show2
         cmp     #$03
         ble     show3
         cmp     #$04
         ble     show4
         cmp     #$05
         ble     show5
         cmp     #$06
         ble     show6
         cmp     #$07
         ble     show7
         cmp     #$08
         ble     show8
         cmp     #$09
         ble     show9

exitint:
         inca
         sta     $30
         pula
         bclr    7,T1SC           ; clear TOF in TSC - handler is finishing
         rti

dummy_isr:
         rti

SHOW0:   MOV #$7E,PTA
         bra exitint
SHOW1:   MOV #$0C,PTA
         bra exitint
SHOW2:   MOV #$B6,PTA
         bra exitint
SHOW3:   MOV #$9E,PTA
         bra exitint
SHOW4:   MOV #$CC,PTA
         bra exitint
SHOW5:   MOV #$DA,PTA
         bra exitint
SHOW6:   MOV #$FA,PTA
         bra exitint
SHOW7:   MOV #$0E,PTA
         bra exitint
SHOW8:   MOV #$FE,PTA
         bra exitint
SHOW9:   MOV #$DE,PTA
         lda #$FF
         bra exitint

;- INTERRUPT VECTOR TABLE -------------------------------------------------------------------
        org  VectorStart
        dw  dummy_isr    ; Time Base Vector
        dw  dummy_isr    ; ADC Conversion Complete
        dw  dummy_isr    ; Keyboard Vector
        dw  dummy_isr    ; SCI Transmit Vector
        dw  dummy_isr    ; SCI Receive Vector
        dw  dummy_isr    ; SCI Error Vector
        dw  dummy_isr    ; SPI Transmit Vector
        dw  dummy_isr    ; SPI Receive Vector
        dw  dummy_isr    ; TIM2 Overflow Vector
        dw  dummy_isr    ; TIM2 Channel 1 Vector
        dw  dummy_isr    ; TIM2 Channel 0 Vector
        dw  timer_isr    ; TIM1 Overflow Vector
        dw  dummy_isr    ; TIM1 Channel 1 Vector
        dw  dummy_isr    ; TIM1 Channel 0 Vector
        dw  dummy_isr    ; PLL Vector
        dw  dummy_isr    ; ~IRQ1 Vector
        dw  dummy_isr    ; SWI Vector
        dw  main         ; Reset Vector
;- INTERRUPT TABLE --------------------------------------------------------------------------
