        TTL     "LCDTest (Lab 5)"

******************************************************************
* 1) HEADER block - overall info about the program
* Project name: LCDTest
* Author(s): Alex and Travis
* Date: 4 Oct 2012
* Description: Writes 'hi' to the screen
******************************************************************

******************************************************************
CODE    EQU     $2000       ;where our code can begin on EVB
VPUTCHAR EQU    $EE86       ;send character in B to output device
CR      EQU     $0D         ;Carriage Return
PORTA   EQU     $0000
tenths  EQU     $3000       ;reserve a memory byte for tenth of seconds
seconds EQU     $3001       ;reserve a memory byte for seconds
tens    EQU     $3002
******************************************************************


******************************************************************
* 4) CODE section - actual program code                          *
* This is all set-up.  Output welcome message to the terminal,   *
* Print "Ultimate Watch" to the LCD, and check for the initial   *
* Button press                                                   *
******************************************************************

        ORG     CODE        ;tell assembler where to begin object code
                            ;main program body follows
Main    ldaa    #LINIT      ;set accumulator to "LINIT" value 0
        jsr     LCD         ;initialize LCD
        ldx     #Title      ;load x with the string
        ldaa    #LWRSTR     ;load A with the address of the write string subroutine
        jsr     LCD         ;execute LWRSTR
        ldx     #Greet      ;Point X at the greeting message
        jsr     PUTS        ;Output the greeting message

        ldx     #256+0      ;Point to second line (256) + first column (0)
        ldaa    #LGOTO      ;
        jsr     LCD         ;Point the cursor there
        ldab    #0          ;
        ldaa    #LWRDEC     ;
        jsr     LCD         ;Output 0
        jsr     LCD         ;Output another 0
        ldab    #':'        ;Load B with a colon to be output
        ldaa    #LWRCHR     ;Output colon to the screen
        jsr     LCD         ;Do it
        ldab    #0          ;
        ldaa    #LWRDEC     ;
        jsr     LCD         ;Output 1 more 0
        clr     tens        ;
        clr     seconds     ;Clear each variable
        clr     tenths      ;
        

Loop    movb    PORTA, $3100       ;Load Accumulator A with the values of Port A
        ldaa    $3100
	ANDA    #%10000000                    ;Mask with $80 to watch only PA7
        bne     Loop        ;If it is still high, loop.
Debounc cmpb    #20         ;Wait 20ms to ensure proper debouncing
        beq     Clock       ;When debounced, start the clock
dbl     ldx     #0          ;1ms loop
        dex                 ;
        cpx     #10000      ;
        beq     dbl         ;
        incb                ;Increment debounce counter
        bra     Debounc     ;

****************************************************************************
* The code following this is all run after the button is pushed initially  *
* but before it starts the stopwatch part of the program                   *
****************************************************************************

        
Clock   movb    PORTA, $3100       ;Load Accumulator A with the values of Port A
        ldaa    $3100
        ANDA    #%10000000  ;Is it still pushed?
        bne     Clock       ;If not, check again


        
        ldx     #256+0      ;Point to second line (256) + first column (0)
        ldaa    #LGOTO      ;
        jsr     LCD         ;Point to first character, second line

        ldab    tens        ;load b with tens counter
        ldaa    #LWRDEC     ;get ready to print tenths to fourth character, second row
        jsr     LCD         ;do it

        ldab    seconds     ;load b with seconds counter
        ldaa    #LWRDEC     ;get ready to print tenths to fourth character, second row
        jsr     LCD         ;do it
        
        ldab    #':'        ;Load B with a colon to be output
        ldaa    #LWRCHR     ;Output colon to the screen
        jsr     LCD         ;Do it
        
        ldab    tenths      ;load b with tenths counter
        ldaa    #LWRDEC     ;get ready to print tenths to fourth character, second row
        jsr     LCD         ;do it
        
        
        ldd     #99        ;If so, get ready to delay 100ms
        jsr     DelayN      ;Delay 100ms
        ldaa    tenths      ;Check the tenths counter
        cmpa    #9         ;Is it ten?
        beq     secondi     ;If so, branch to seconds incrementer
        
tenthsi ldaa    tenths      ;Increment tenths counter
	inca                ;
        staa    tenths      ;
        bra     Clock       ;Output and check button status
        
secondi clra
        staa    tenths      ;clear tenths counter
        ldaa    seconds
        cmpa    #9         ;Is seconds 10?
        beq     tensi       ;If so, branch to tens incrementer
        ldaa    seconds     ;
        inca                ;Otherwise, increment seconds
        staa    seconds     ;
        bra     Clock       ;Output and check button status

tensi   clra                ;Clear seconds
        staa    seconds     ;
        ldaa    tens        ;
        inca                ;Increment tens
        staa    tens        ;
        bra     Clock       ;Output and check button status






quit    ldx     #Exit
        jsr     PUTS
        swi                 ;return to  monitor on EVB







                
PUTS    ldab    0,x         ;get 1 char from string
        beq     PSDONE      ;done if terminating null
        jsr     PUTCHAR     ;output this char
        inx                 ;advance X to next char
        bra     PUTS        ;continue
PSDONE  rts                 ;done, return to caller

; single character (in B) output routine
PUTCHAR pshx                ;save caller's X
        ldx     VPUTCHAR    ;fetch address of monitor rtn
        jsr     0,X         ;call monitor rtn.
        pulx                ;restore caller's X
        rts

DelayN  bsr     Delay1      ;go delay 1 millisecond
        dbne    D,DelayN    ;dec 'N' & continue till zero
        rts

* Delay1: delay 1 millisecond via software,
* assumes F=24 MHz (and no interrupts)
* all registers preserved for caller

Delay1  pshx                ;2~ preserve registers used here
        ldx     #5997       ;2~ iterations of Dloop for 1ms.
Dloop   dex                 ;1~ ]
        bne     Dloop       ;3~ ] 5997 * 4~ = 23988~
        pulx                ;3~ recover used registers
        rts                 ;5~ 23988 + 12 = 24000~ = 1ms.
                
; constant data definitions (FCB,FDB,FCC)...
Title   FCC     "Ultimate Watch"
        FCB     0
Greet   FCC     "Welcome to the stopwatch program.  Press and hold the button to start the clock."
        FCB     0
Exit    FCC     "Thanks, have a good day."
        FCB     CR,CR,CR, 0
******************************************************************



#include 'LCDdriver.asm'
; tell assembler end of assembly source code
        END