;**************************************************************************
;ECE348, Spring 2015
;Group ##
;<name1> <andrewid1>
;<name2> <andrewid2>
;**************************************************************************
; Carnegie Mellon 18-348   lab 01  program 3 -- assembler version
; This is an example assembly program that uses PushButtons and LEDs on the
; project board. When PushButton 1 is pressed, LED1 turns on and LED2 
; turns off until PushButton 2 is pressed. When PushButton 2 is pressed, 
; LED1 turns off and LED2 turns on until PushButton 1 is pressed.

; This program uses bit 1 and bit 0 of Port T for output and bit 1 and bit
; 0 of Port P for input.

; For this example to work, the PushButtons and LEDs on the project board
; need to be wired to the correct MCU pins. Typically, the CSM12C32 module
; is connected to J5 on the project board.  Connectors J5, J6 and J7 are
; all connected together, so you can use the corresponding pin on J6 or J7
; to wire things to the MCU.
;---------------------
;WIRING
;---------------------
; PB1 to PIN 9 (Port P bit 0)
; PB2 to PIN 11(Port P bit 1)
; LED1 to PIN 13 (Port T bit 0)
; LED2 to PIN 15 (Port T bit 1)
;---------------------
;
;**************************************************************************



; export symbols
            XDEF Entry            ; export 'Entry' symbol
            ABSENTRY Entry        ; for absolute assembly: mark this as application entry point

; include derivative specific macros
            INCLUDE 'mc9s12c128.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.
LED_1_ON    EQU  $01
LED_2_ON    EQU  $02


; code section
            ORG   ROMStart
Entry:
            ; remap the RAM &amp; 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


;see Section 2.3.2 for full description of control registers
;configure port T for output
            CLR   RDRT            ; full strength drive
            CLR   MODRR           ; make sure pins are routed to port t
            LDAA  #$FF            ; Load RegA with 1 in each bit
            STAA  DDRT            ; set Port T to output
;configure port P as input
            CLR   PERP            ; disable pullup/pulldown
            CLR   DDRP            ; set Port P to input

EndlessLoop:
            LDAA  PTP             ; Load RegA with value stored at PTM
            ANDA  #$03            ; Look at bit 1 and bit 0 of RegA
            
; NOTE: For PushButtons on Project Board, a 0 means the button is pressed
;       and a 1 means the button is not pressed, so for bit 1 and bit 0
;       of RegA we change the 0's to 1's and the 1's to 0's using an
;       exclusive or with 1's in bit 1 and bit 0
            EORA  #$03            ;
Button2:
            CMPA  #$02            ; see if button 2 is pressed
            BNE   Button1         ; if not, branch to Button1
            LDAA  #LED_2_ON       ; if so, turn on LED2 and turn off LED1
            STAA  PTT             ;
            BRA   EndlessLoop     ; restart loop
Button1:
            CMPA  #$01            ; see if button 1 is pressed
            BNE   EndlessLoop     ; if not, restart loop
            LDAA  #LED_1_ON       ; if so, turn on LED1 and turn off LED2
            STAA  PTT             ;
            BRA   EndlessLoop     ; restart loop

;**************************************************************
;*                 Interrupt Vectors                          *
;**************************************************************
            ORG   $FFFE
            DC.W  Entry           ; Reset Vector
