; ****************************************************************
; Lab 2 Assembly Counter
; Purpose:  This is an assmebly version of the Lab2 4-bit Counter program.
;           If the push button connected to portE is pressed, the 
;           value of the LEDs connected to port T is incremented.
;           
;           For this code to work properly with the project board,
;           the PB and LED jumpers need to be set.
; ****************************************************************





;*****************************************************************
;* This stationery serves as the framework for a                 *
;* user application (single file, absolute assembly application) *
;* For a more comprehensive program that                         *
;* demonstrates the more advanced functionality of this          *
;* processor, please see the demonstration applications          *
;* located in the examples subdirectory of the                   *
;* Freescale CodeWarrior for the HC12 Program directory          *
;*****************************************************************

; export symbols
            XDEF Entry            ; export 'Entry' symbol
            ABSENTRY Entry        ; for absolute assembly: mark this as application entry point

; include derivative specific macros
            INCLUDE 'mc9s12c32.inc'

ROMStart    EQU  $4000  ; absolute address to place my code/constant data

; variable/data section

 ifdef _HCS12_SERIALMON
            ORG $3FFF - (RAMEnd - RAMStart)
 else
            ORG RAMStart
 endif
 ; Insert here your data definition.
Counter     DS.W 1
FiboRes     DS.W 1


; code section
            ORG   ROMStart
Entry:
            ; remap the RAM & EEPROM here. See EB386.pdf
 ifdef _HCS12_SERIALMON
            ; set registers at $0000
            CLR   $11                  ; INITRG= $0
            ; set ram to end at $3FFF
            LDAB  #$39
            STAB  $10                  ; INITRM= $39

            ; set eeprom to end at $0FFF
            LDAA  #$9
            STAA  $12                  ; INITEE= $9


            LDS   #$3FFF+1        ; See EB386.pdf, initialize the stack pointer
 else
            LDS   #RAMEnd+1       ; initialize the stack pointer
 endif
            CLI                   ; enable interrupts



;********************************Start of Program Code**********************************
            LDAA  #$F0             
            STAA  DDRAD            ; Setup port AD
            
            LDAA  #$FF
            STAA  DDRT             ; Setup port T
            
            LDAA  #$00
            STAA  DDRE             ; Setup port E
            
            LDAA  #$CF
            STAA  PTT              ; Set output of port T to electronically enable
                                   ; the push buttons and LEDs
            LDAA  #$00
            STAA  PTAD             ; Set counter value to 0
            
mainLoop:
            LDAA PORTE             ; Load the value of Port E
            ANDA #$01              ; Remove all other bits but what we want to check
            CMPA #$01              ; Check PB2 has been pressed
            BEQ Count              ; If pressed, increment counter
            BRA mainLoop           ; else, loop back.


Count:
            LDAA PORTE
            ANDA #$01
            CMPA #$01
            BEQ Count             ; Loop back to Count, until button is released
            LDAA PTAD             ; Load the current value of port AD
            ADDA #$10             ; Increment the counter by 1
            STAA PTAD             ; Output back to port AD
            BRA mainLoop
 
;*******************************End of counter code************************************




;**************************************************************
;*                 Interrupt Vectors                          *
;**************************************************************
            ORG   $FFFE
            DC.W  Entry           ; Reset Vector