;************************************************************
; Names: Eric Garrison and Andrew Goldie
; Assignment: Lab 8 - Program 4
; Class: TECH-3233 - Microprocessor Technology
; Date: March 24, 2011
; Description: Write a subroutine that will convert an
;   unsigned value in register A into 3 digits representing
;   the 100's place, 10's place and 1's place of the decimal
;   number.  Store these values, in order, in 3 consecutive
;   memory locations called "cd" for Coded Decimal.  Upon
;   exit of the routine, all registers should be restored to
;   their previous state.  (Hint: The largest number is 255.)
;************************************************************

MAIN:   ORG $D000       ;Need to set the start of the program to D000

BUFFALO EQU $E000       ;Used as a jump to end the program
OUTA    EQU $FFB8       ;Must set the location of the print to terminal command before it can be used
LCD_INI EQU $FF70       ;Needed to initialize the LCD on the Fox11 board
LCD_LINE1 EQU $FF73     ;Needed to display Ascii values on the Fox11 board's LCD display

        LDAA #213       ;Load register A with a default value (213 in Decimal, D5 in Hex)
        
        JSR GETBCD      ;Need to call the subroutine to convert the value in register A into BCD
        JSR CNVASC      ;Need to call the subroutine to convert the value CD into Ascii
        JSR DISPLY      ;Need to display the value stored in ASCII to the terminal
        JSR SHWLCD      ;Need to display the value stored in ASCII to the Fox11 board's LCD display
        
        JMP MAIN        ;Instead of ending the program, we jump to the beginning to keep the message on the LCD display
        
;************************************************************
; Get BCD Converts the value stored in register A into a BCD
; number, and stores the value in memory location cd.
;************************************************************

CD      RMB 3           ;Must create a space in memory that is 3 bytes long to store the BCD

GETBCD: PSHA            ;Need to move what's in register A onto the stack
        PSHB            ;Need to move what's in register B onto the stack
        PSHX            ;Need to move what's in register X onto the stack
        PSHY            ;Need to move what's in register Y onto the stack
        LDX #100        ;Load register X with the decimal value of 100 (for the 100's place)
        TAB             ;Must move the value in register A into B because we divide using register D
        LDAA #0         ;Since A was moved to B, A needs to be set to 0 so the number isn't bigger
        IDIV            ;Need to divide the first part of register D to get the first part of the BCD
        XGDX            ;In order to save the least significant bit, we need to temporarily swap D and X
        STAB CD         ;The first quotient needs to be stored in the saved memory location
        XGDX            ;After storing B in the saved memory location, we need to swap D and X back
        LDX #10         ;Need to load register X with the decimal value of 10 (for the 10's place)
        IDIV            ;Need to divide the remainder in register D to get the second part of the BCD
        XGDX            ;In order to save the least significant bit, we need to temporarily swap D and X
        STAB CD+1       ;The second quotient needs to be stored in the next byte in the saved memory location
        XGDX            ;After storing B in the saved memory location, we need to swap D and X back
        STAB CD+2       ;The last remainder is the last part of the BCD number to be saved in the saved memory location
        PULY            ;Need to restore register Y
        PULX            ;Need to restore register X
        PULB            ;Need to restore register B
        PULA            ;Need to restore register A
        RTS             ;After storing this data, the subroutine needs to end
        
;************************************************************
; Convert Ascii Converts the value stored CD into the ASCII
; characters and stores those values in ASCII.
;************************************************************

SHOWME  FCC "Value = " 	;We need to set the first part of the message shown on the LCD display (Called from SHWLCD)
ASCII   RMB 3           ;Must create a space in memory that is 3 bytes long to store the ASCII characters
                        ;When SHOWME is called, this will be picked up between the 2 FCC instructions and displayed on the LCD
        FCC "     "     ;Need to fill in the rest of LINE 1 with blank spaces so garbage is not shown
        
CNVASC: PSHA            ;Need to move what's in register A onto the stack
        PSHB            ;Need to move what's in register B onto the stack
        PSHX            ;Need to move what's in register X onto the stack
        PSHY            ;Need to move what's in register Y onto the stack
        LDAA #0         ;Need to clear out register A with 0
        LDAB CD         ;Need to load register B with the first byte from CD
        ADDB #$30       ;Need to add 30 hex to B to get the correct Ascii value from the Ascii table
        STAB ASCII      ;Need to store the Ascii character in register B in the first byte of ASCII
        LDAB CD+1       ;Need to load register B with the second byte from CD
        ADDB #$30       ;Need to add 30 hex to B to get the correct Ascii value from the Ascii table
        STAB ASCII+1    ;Need to store the Ascii character in register B in the second byte of ASCII
        LDAB CD+2       ;Need to load register B with the third byte from CD
        ADDB #$30       ;Need to add 30 hex to B to get the correct Ascii value from the Ascii table
        STAB ASCII+2    ;Need to store the Ascii character in register B in the third byte of ASCII
        PULY            ;Need to restore register Y
        PULX            ;Need to restore register X
        PULB            ;Need to restore register B
        PULA            ;Need to restore register A
        RTS             ;After storing this data, the subroutine needs to end
        
;************************************************************
; Prints the values stored in ASCII to the terminal using
; the OUTA Buffalo command.
;************************************************************

DISPLY: PSHA            ;Need to move what's in register A onto the stack
        LDAA ASCII      ;Loads register A with the first byte from ASCII
        JSR OUTA        ;Prints the value from register A to the terminal
        LDAA ASCII+1    ;Loads register A with the second byte from ASCII
        JSR OUTA        ;Prints the value from register A to the terminal
        LDAA ASCII+2    ;Loads register A with the third byte from ASCII
        JSR OUTA        ;Prints the value from register A to the terminal
        PULA            ;Need to restore register A
        RTS             ;After storing this data, the subroutine needs to end
        
;************************************************************
; Prints Ascii values stored in ASCII to the Fox11 Board's
; LCD display.
;************************************************************
        
SHWLCD: PSHA            ;Need to move what's in register A onto the stack
        PSHB            ;Need to move what's in register B onto the stack
        PSHX            ;Need to move what's in register X onto the stack
        PSHY            ;Need to move what's in register Y onto the stack
        JSR LCD_INI     ;Need to initialize the Fox11 board's LCD before displaying to it
        LDX #SHOWME     ;Loads register X with the user message along with the value from ASCII
        JSR LCD_LINE1   ;Needed to display the message on the Fox11 board's LCD with the value from ASCII
	PULY            ;Need to restore register Y
        PULX            ;Need to restore register X
        PULB            ;Need to restore register B
        PULA            ;Need to restore register A
        RTS             ;After storing this data, the subroutine needs to end