;********************************************************************
;* This stationery serves as the framework for a                    *
;* user application (single file, absolute assembly application     *
;* for 18-348)                                                      *
;********************************************************************

; 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'

; variable/data section
            INCLUDE 'RAMLocation.inc'
            
;****************************************************
; ADD GLOBAL VARIABLES HERE
;****************************************************

ROMStart    EQU  $4000  ; absolute address to place my code/constant data

; code section
            ORG   ROMStart
Entry:
            INCLUDE 'StackInfo.inc'

            CLI                   ; enable interrupts

;*****************************************************
; ADD CODE HERE
;*****************************************************

;This main code loops through every value from $0 to $FFFF
;and test the operation of divByTwo against the IDIVS instruction.
;If the program reaches BP2 without ever reaching BP1, then all
;the loop values divded correctly.

;Beginning here, do not modify the code
;Any modication of the code from here to the "end do not modify" comment
;will result in no credit being given for this part of the assignment

            LDX #$0;
divLoop:
            PSHX            ;save the counter
            TFR X, D        ;copy the counter to D

            BSR divByTwo    ;divide the value in D by 2
            TFR D, Y        ;save result in Y

            TFR X, D        ;counter value in D
            LDX #2          ;2 into X
            IDIVS           ;D / X -> X

            TFR X, D        ;compare the division results
            PSHY
            SUBD SP
            BEQ noError
            ;set a breakpoint
BP1:        NOP ;if the program reaches this point, there was an error!
noError:
            PULD ;cleanup stack
            ;loop
            PULX
            IBNE X, divLoop
            ;set a breakpoint
BP2:        NOP ;if the program reaches this point, then all the loop values were verified
            

;end do not modify

loopForever:
            BRA loopForever

;this function should divide the value in D by two using shift operations
;no DIV instructions are allowed (EDIV, EDIVS, FDIV, IDIV, IDIVS).
divByTwo:
            ;TODO implement this function
            RTS


;*****************************************************
; DO NOT MODIFY OR ADD ANYTHING AFTER THIS POINT
;*****************************************************

;**************************************************************
;*                 Interrupt Vectors                          *
;**************************************************************
            ORG   $FFFE
            DC.W  Entry           ; Reset Vector
