# =============================================================================================================
# *
# * Copyright (c) Mike
# *
# * File Name: clock.asm
# *
# * Version: V1.0
# *
# * Release Date:
# *
# * Author(s): M.Freeman
# *
# * Description: LCD clock code
# *
# * Conditions of Use: THIS CODE IS COPYRIGHT AND IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND, INCLUDING,
# *                    BUT NOT LIMITED TO, ANY IMPLIED WARRANTY OF MERCHANTABILITY AND FITNESS FOR A
# *                    PARTICULAR PURPOSE.
# *
# * Notes:
# *
# =============================================================================================================

##############
# MEMORY MAP #
##############

# ADDR		CS   	WR						RD

# 0xFFF 	3		IRQ COMMAND				IRQ COMMAND         
# 0xFFE     3		NU  					IRQ TRIGGER
# 0xFFD     3		IRQ STATUS  			IRQ STATUS
# 0xFFC     3       NU                      NU

# 0xFFB     2       GPIO A out              GPIO A out
# 0xFFA     2       GPIO A out              GPIO A in
# 0xFF9     2       GPIO A out              GPIO A out
# 0xFF8     2       GPIO A out              GPIO A in

# 0xFF7     1       UART TX                 UART RX
# 0xFF6     1       UART TX                 UART STATUS
# 0xFF5     1       UART TX                 UART RX
# 0xFF4     1       UART TX                 UART STATUS

# 0xFF3     0       NU                      NU
# 0xFF2     0       NU                      TIMER STATUS
# 0xFF1     0       TIMER COMMAND           TIMER COMMAND
# 0xFF0     0       TIMER COUNT             TIME COUNT

# 0xFEF     MEM	    RAM						RAM
# ...				...						...
# 0x000		MEM     RAM						RAM

#################
# IRQ REGISTERS #
#################

# COMMAND REGISTER
# ----------------
# B7 : irq_7 enable (1=enable, 0=disable)
# B6 : irq_6 enable (1=enable, 0=disable)
# B5 : irq_5 enable (1=enable, 0=disable)
# B4 : irq_4 enable (1=enable, 0=disable)
# B3 : irq_3 enable (1=enable, 0=disable)
# B2 : irq_2 enable (1=enable, 0=disable)
# B1 : irq_1 enable (1=enable, 0=disable)
# B0 : irq_0 enable (1=enable, 0=disable)

# TRIGGER REGISTER
# ----------------
# when read returns one-hot representation of highest priority IRQ (0 high, 7 low)
# with read complete IRQ flag reset. In the case of multiple IRQ set the next highest
# IRQ is selected as the new trigger source.

# STATUS REGISTER
# ---------------
# when read returns a binary value of all IRQ states, this is cleared when written to.

# IRQ
# ---
# B7 : NU
# B6 : NU
# B5 : NU
# B4 : NU
# B3 : NU
# B2 : TIMER
# B1 : UART
# B0 : GPIO

##################
# GPIO REGISTERS #
##################

# OUTPUT PINS
# -----------
# 128  B7 : NU
# 64   B6 : RW (0)
# 32   B5 : E
# 16   B4 : RS
# 8    B3 : D7
# 4    B2 : D6
# 2    B1 : D5
# 1    B0 : D4

# INPUT PINS
# ----------
# 128  B7 : BUT3
# 64   B6 : BUT2
# 32   B5 : BUT1
# 16   B4 : BUT0
# 8    B3 : SW3
# 4    B2 : SW2
# 2    B1 : SW1
# 1    B0 : SW0

# Note, input port pins ORed together to drive IRQ out.

##################
# UART REGISTERS #
##################

# TX : B7 - B0 data
# RX : B7 - B0 data 

# STATUS REGISTER
# ---------------
# B7 : NU
# B6 : NU
# B5 : NU
# B4 : NU
# B3 : NU
# B2 : TX Idle
# B1 : RX Idle
# B0 : RX Valid

# Note, IRQ out set by RX_VALID

###################
# TIMER REGISTERS #
###################

# COMMAND REGISER
# ---------------
# B7 : clock select msb 
# B6 : clock select
# B5 : clock select
# B4 : clock select lsb 
# B3 : nu
# B2 : nu
# B1 : single shot (0) or auto reload (1)
# B0 : enable count (1=true, 0=false)

# CLOCK SELECT BITS
# -----------------
# 0000 : clock_div_2        5MHz            0.0000002       200 ns     
# 0001 : clock_div_4        2.5MHz          0.0000004       400 ns
# 0010 : clock_div_8        1.25MHz         0.0000008       800 ns
# 0011 : clock_div_16       625KHz          0.0000016       1.6 us
# 0100 : clock_div_32       312.5KHz        0.0000032       3.2 us
# 0101 : clock_div_64       156.25KHz       0.0000064       6.4 us
# 0110 : clock_div_128      78.125KHz       0.0000128       12.8 us
# 0111 : clock_div_256      39.0625KHz      0.0000256       25.6 us     
# 1000 : clock_div_512      19.53125KHz     0.0000512       51.2 us

# STATUS REGISTER
# ---------------
# B7 : nu
# B6 : nu
# B5 : nu
# B4 : nu 
# B3 : counter enabled
# B2 : counter terminal count
# B1 : counter zero
# B0 : zero (counter zero & count enabled)

# Note, clock speed is 10MHz
# 1 / 0.0000512 = 19531   error = 0.25 * 51.2 us = 12.8 us
# error in 24 hours = 24 * 60 * 60 * 12.8us = 1.10592 seconds   

# ASCII CODES
# -----------
#
# Dec Hex Char                           Dec Hex Char      Dec Hex Char     Dec Hex Char
# ------------                           ------------      ------------     ------------
#  0  00  NUL (null)                      32  20  SPACE     64  40  @        96   60  `
#  1  01  SOH (start of heading)          33  21  !         65  41  A        97   61  a
#  2  02  STX (start of text)             34  22  "         66  42  B        98   62  b
#  3  03  ETX (end of text)               35  23  #         67  43  C        99   63  c
#  4  04  EOT (end of transmission)       36  24  $         68  44  D        100  64  d
#  5  05  ENQ (enquiry)                   37  25  %         69  45  E        101  65  e
#  6  06  ACK (acknowledge)               38  26  &         70  46  F        102  66  f
#  7  07  BEL (bell)                      39  27  '         71  47  G        103  67  g
#  8  08  BS  (backspace)                 40  28  (         72  48  H        104  68  h
#  9  09  TAB (horizontal tab)            41  29  )         73  49  I        105  69  i
# 10  0A  LF  (NL line feed, new line)    42  2A  *         74  4A  J        106  6A  j
# 11  0B  VT  (vertical tab)              43  2B  +         75  4B  K        107  6B  k
# 12  0C  FF  (NP form feed, new page)    44  2C  ,         76  4C  L        108  6C  l
# 13  0D  CR  (carriage return)           45  2D  -         77  4D  M        109  6D  m
# 14  0E  SO  (shift out)                 46  2E  .         78  4E  N        110  6E  n
# 15  0F  SI  (shift in)                  47  2F  /         79  4F  O        111  6F  o
# 16  10  DLE (data link escape)          48  30  0         80  50  P        112  70  p
# 17  11  DC1 (device control 1)          49  31  1         81  51  Q        113  71  q
# 18  12  DC2 (device control 2)          50  32  2         82  52  R        114  72  r
# 19  13  DC3 (device control 3)          51  33  3         83  53  S        115  73  s
# 20  14  DC4 (device control 4)          52  34  4         84  54  T        116  74  t
# 21  15  NAK (negative acknowledge)      53  35  5         85  55  U        117  75  u
# 22  16  SYN (synchronous idle)          54  36  6         86  56  V        118  76  v
# 23  17  ETB (end of trans. block)       55  37  7         87  57  W        119  77  w
# 24  18  CAN (cancel)                    56  38  8         88  58  X        120  78  x
# 25  19  EM  (end of medium)             57  39  9         89  59  Y        121  79  y
# 26  1A  SUB (substitute)                58  3A  :         90  5A  Z        122  7A  z
# 27  1B  ESC (escape)                    59  3B  ;         91  5B  [        123  7B  {
# 28  1C  FS  (file separator)            60  3C  <         92  5C  \        124  7C  |
# 29  1D  GS  (group separator)           61  3D  =         93  5D  ]        125  7D  }
# 30  1E  RS  (record separator)          62  3E  >         94  5E  ^        126  7E  ~
# 31  1F  US  (unit separator)            63  3F  ?         95  5F  _        127  7F  DEL

# TERMINAL COMMANDS
# -----------------
#
# Common serial port speeds 
# Bit rate (bit/s)  Time per bit (μs)  Common applications 
# ----------------  -----------------  -------------------
# 75 	            13333.3 	 	
# 110 	            9090.9 	     	   Bell 101 modem
# 134.5 	        7434.9 	  	
# 150 	            6666.6 	 	
# 300 	            3333.3 	       	   Bell 103 modem or V.21 modem
# 600 	            1666.7 	  	
# 1,200 	        833.3 	  	       Bell 202, Bell 212A, or V.22 modem
# 1,800 	        555.6 	  	
# 2,400 	        416.7 	  	       V.22bis modem
# 4,800 	        208.3 	     	   V.27ter modem
# 7,200 	        138.9 	  	
# 9,600 	        104.2 	  	       V.32 modem
# 14,400 	        69.4 	     	   V.32bis modem
# 19,200 	        52.1 	  	
# 31,250 	        32 	  	           MIDI port
# 38,400 	        26.0 	  	
# 56,000 	        17.9 	  	       V.90/V.92 modem
# 57,600 	        17.4 	  	       V.32bis modem with V.42bis compression   ****
# 76,800 	        13.0 	  	       BACnet MS/TP networks[20]
# 115,200 	        8.68 	  	       V.34 modem with V.42bis compression, 
#                                      low cost serial V.90/V.92 modem with V.42bis or V.44 compression 
#
# screen /dev/ttyS0 19200,cs8
# screen /dev/ttyUSB0 57600
#
# Screen Command 	  Task
# --------------      ----
# Ctrl+a c 	          Create new window
# Ctrl+a k 	          Kill the current window / session
# Ctrl+a w            List all windows
# Ctrl+a 0-9 	      Go to a window numbered 0 9, use Ctrl+a w to see number
# Ctrl+a Ctrl+a 	  Toggle / switch between the current and previous window
# Ctrl+a S 	          Split terminal horizontally into regions and press Ctrl+a c to create new window there
# Ctrl+a :resize 	  Resize region
# Ctrl+a :fit 	      Fit screen size to new terminal size. You can also hit Ctrl+a F for the the same task
# Ctrl+a :remove 	  Remove / delete region. You can also hit Ctrl+a X for the same taks
# Ctrl+a tab 	      Move to next region
# Ctrl+a D (Shift-d)  Power detach and logout
# Ctrl+a d 	          Detach but keep shell window open
# Ctrl-a Ctrl-\ 	  Quit screen
# Ctrl-a ? 	          Display help screen i.e. display a list of commands


################
# MAIN PROGRAM #
################

start:
    move RA 0
    store RA rxBufferIndex         # set serial RX pointer

    call lcd_init
    call lcd_line_2

    load ra count         
    store ra TIMER_COUNT           # init counter with 1 second delay count

    move ra 0x83                  
    store ra TIMER_COMMAND         # set clock/512, auto reload, enable timer.

    move ra 0x04             
    store ra IRQ_COMMAND           # enable TIMER IRQ
   
wait:
    call readSerialPort            # test serial

    load ra currentSeconds         # has seconds been updated
    subm ra seconds
    jumpz wait

    load ra seconds                # yes, update seconds
    store ra currentSeconds

update:
    call lcd_line_2                # update display
    call displayTime

    jump wait                  

trap:
    move RA 0
    jump trap


#######
# ISR #
#######

.addr 100

isr:
    store RA checkpoint
    load RA IRQ_TRIGGER         # reset IRQ

    load RA seconds             # inc seconds
    add RA 1
    store RA seconds
    sub RA 60
    jumpnz isr_exit

    move RA 0                   # inc minutes
    store RA seconds
    load RA minutes
    add RA 1
    store RA minutes
    sub RA 60
    jumpnz isr_exit

    move RA 0                   # inc hours
    store RA minutes
    load RA hours
    add RA 1
    store RA hours
    sub RA 24
    jumpnz isr_exit

    move RA 0
    store RA hours

isr_exit:
    load RA seconds             # debug display 
    move RD RA
    load RA minutes
    move RD RA
    load RA hours
    move RD RA

    load RA checkpoint
    reti

#####################
# CLOCK SUBROUTINES #
#####################

# --------------------
# - Read serial port -
# --------------------
# serial packet format: HHMMSS<CR>
# 1/9600 = 0.000104167 = 104us = 1ms per char = 10ms for packet

readSerialPort:
    load RA UART_STATUS             # test status bit
    and RA 1
    jumpz readSerialPort_exit

    load RA UART_RX                 # read char, buffer in RB
    move RB RA
    #store RA UART_TX
    
    movea( RC, rxBuffer )           # set pointer to rxBuf
    load RA rxBufferIndex           # calc offset
    add RC RA
    store RB (RC)                   # store data

    add RA 1
    store RA rxBufferIndex          # inc pntr
    
    sub RA 10
    jumpz readSerialPort_update     # have 10 chars been entered?

    sub RB 0x0D                 
    jumpnz readSerialPort_exit      # has cr been RX? 

readSerialPort_update:
    movea( RB, rxBuffer )           # debug, dump string back to terminal
    load RA rxBufferIndex           # comment out in final design
    add RA RB
    move RC 0
    store RC (RA) 
    call tx_loop

    call updateTime                 # update hours, minutes and seconds variables

    move RA 0                       # reset pntr
    store RA rxBufferIndex

readSerialPort_exit:
    ret

# --------------------------
# - Update time (HH:MM:SS) -
# --------------------------

updateTime:
    move RA 0
    store RA rxBufferIndex
    movea( RB, rxBuffer )           # string address, first char

    call updateDigits               # convert char to int

    load RA digit1                  # add digits
    addm RA digit0
    store RA hours                  # update hours 

    movea( RB, rxBuffer )           # string address, third char
    load RA rxBufferIndex   
    add RB RA

    call updateDigits               # convert char to int

    load RA digit1                  # add digits
    addm RA digit0
    store RA minutes                # update minutes

    movea( RB, rxBuffer )           # string address, fifth char
    load RA rxBufferIndex   
    add RB RA

    call updateDigits               # convert char to int 

    load RA digit1                  # add digits       
    addm RA digit0
    store RA seconds                # update seconds
 
    ret

# Convert two CHARs into digits
# -----------------------------

updateDigits:
    load RA (RB)            # addr of char passed in RB i.e. 10s digit
    sub RA 0x30             # sub 0x30 to convert to int

    store RA digit1
    rol RA                  # x2
    rol RA                  # x4
    rol RA                  # x8
    addm RA digit1          # x9
    addm RA digit1          # x10
    store RA digit1

    add RB 1                # addr of second char
    load RA (RB)            # addr of second char in RB i.e. 1s digit
    sub RA 0x30             # sub 0x30 to convert to int           
    store RA digit0

    load RA rxBufferIndex   # inc pntr
    add RA 2
    store RA rxBufferIndex
    ret

# ----------------
# - Display time -
# ----------------

displayTime:
    movea( RC, lcd_time )   

    load RA hours
    call byte2Dec_digit
    add RC 1

    move RA 0x3A
    store RA (RC)
    add RC 1

    load RA minutes
    call byte2Dec_digit
    add RC 1

    move RA 0x3A
    store RA (RC)
    add RC 1

    load RA seconds
    call byte2Dec_digit
    add RC 1

    move RA 0
    store RA (RC)

    move ra 0
    store RA lcd_config
    movea( RA, lcd_time )
    store RA lcd_pntr

    call lcdTxString
    ret

# convert int to char
# -------------------

byte2Dec_digit:
    move RB 0
 
byte2Dec_digit_loop:
    sub RA 10                      # sub 10
    jumpc byte2Dec_digit_exit      # yes, exit
    add RB 1                       # inc count 
    jump byte2Dec_digit_loop       # repeat

byte2Dec_digit_exit:
    add RB 0x30
    store RB (RC)
    move RD RB
    add RC 1
    
    add RA 10                    # undo last sub
    add RA 0x30
    move RD RA
    store RA (RC)

    ret

###########################
# SERIAL PORT SUBROUTINES #
###########################

# ------------------------------------------
# - Copy string (must terminate with a \0) -
# ------------------------------------------
# Source / destination addresses passed in SRCADDR / DESTADDR

strcpy:
    load RA srcAddr          # get source address
    move RB RA
    load RA destAddr         # get destination address
    move RC RA 

strcpy_loop:              
    load RA (RB)             # load  char
    and RA 0xFF
    jumpz strcpy_exit        # exit if 0
    store RA (RC)            # copy
    add RB 1
    add RC 1
    jump strcpy_loop         # repeat

strcpy_exit:
    ret

# ----------------------------------------
# - TX String (must terminate with a \0) -
# ----------------------------------------
# data in txBuffer written to serial port, blocking 

txString:
    movea( RB, txBuffer )  # get string address

tx_loop:              
    load RA (RB)             # load  char
    and RA 0xFF
    jumpz tx_exit            # exit if 0
    store RA UART_TX         # tx

    call waitTX

    add RB 1                 # inc address
    jump tx_loop             # repeat

tx_exit:
    move RA 0
    store RA txBufferIndex
    ret

waitTX:
    load RA UART_STATUS      # test TX status wait till 1
    and RA 0x04          
    jumpz waitTX   
    ret

# --------------------------------
# - Add NULL to tx string buffer -
# --------------------------------

addNULL:
    load RA txBufferIndex 
    movea( RC, txBuffer )
    add RC RA 
    move RA 0
    store RA (RC)
    ret

# ---------------------------------
# - Add LF CR to tx string buffer -
# ---------------------------------

addLFCR:
    load RA txBufferIndex 
    movea( RC, txBuffer )
    add RC RA 
    add RA 2
    store RA txBufferIndex

    move RA 0x0A
    store RA (RC)
    add RC 1
    move RA 0x0D
    store RA (RC)
    ret

# ---------------------------------
# - Add SPACE to tx string buffer -
# ---------------------------------

addSPACE:
    load RA txBufferIndex 
    movea( RC, txBuffer )
    add RC RA 
    add RA 1
    store RA txBufferIndex

    move RA 0x20
    store RA (RC)
    ret

# --------------------------------------------
# - Convert word into hexidecimal characters -
# --------------------------------------------
# data to be convert in variable "word", characters written to txBuffer

word2Hex:
    load RA word
    asr RA
    asr RA
    asr RA
    asr RA
    asr RA
    asr RA
    asr RA
    asr RA
    store RA byte
    call byte2Hex

    load RA word
    and RA 0xFF
    store RA byte
    call byte2Hex

    ret

# --------------------------------------------
# - Convert byte into hexidecimal characters -
# --------------------------------------------
# data to be convert in variable "byte", characters written to txBuffer

byte2Hex:
    load RA txBufferIndex 
    movea( RC, txBuffer )
    add RC RA
    add RA 2
    store RA txBufferIndex

    movea( RB, nibble2HexTable )
    load RA byte
    and RA 0xF0
    asr RA
    asr RA
    asr RA
    asr RA
    add RB RA

    load RA (RB)
    store RA (RC)
    add RC 1

    movea( RB, nibble2HexTable )

    load RA byte
    and RA 0x0F
    add RB RA

    load RA (RB)
    store RA (RC)

    ret

# ----------------------------------------
# - Convert byte into decimal characters -
# ----------------------------------------
# data to be convert in variable "byte", characters written to txBuffer

byte2Dec:
    load RA txBufferIndex 
    movea( RC, txBuffer )
    add RC RA
    add RA 3
    store RA txBufferIndex

    load RA byte
    and RA 0xFF
    move RB 0

byte2Dec_100s_loop:
    sub RA 100                   # sub 100
    jumpc byte2Dec_100s_exit     # yes, exit
    add RB 1                     # inc count 
    jump byte2Dec_100s_loop      # repeat

byte2Dec_100s_exit:
    add RB 0x30
    store RB (RC)
    add RC 1
    
    add RA 100                   # undo last sub
    move RB 0
 
byte2Dec_10s_loop:
    sub RA 10                    # sub 10
    jumpc byte2Dec_10s_exit      # yes, exit
    add RB 1                     # inc count 
    jump byte2Dec_10s_loop       # repeat

byte2Dec_10s_exit:
    add RB 0x30
    store RB (RC)
    add RC 1
    
    add RA 10                    # undo last sub
    add RA 0x30
    store RA (RC)

    ret

# -------------
# - RX String -
# -------------

rxString:
    move RA 0
    store RA rxBufferIndex
    movea( RB, rxBuffer )       # get string address

rx_loop:
    load RA UART_STATUS         # has ASCII char been RX?
    and RA 0x01
    jumpz rx_loop               # no check again

    load RA UART_RX
    store RA rxChar
    sub RA 0x0D              
    jumpz rx_exit               # exit if char is CR

    load RA rxChar
    store RA (RB)

    load RA rxBufferIndex
    add RA 1
    store RA rxBufferIndex

    add RB RA
    jump rx_loop

rx_exit:
    move RA 0
    store RA (RB)
    ret 

# ------------------ 
# - Software Delay -
# ------------------
# 0.3 x FFF x 2 = 3.6ms

delay:
    movea( RA, 0xFFF )

delay_loop:
    sub RA 1
    jumpnz delay_loop
    ret

###########################
# LCD DISPLAY SUBROUTINES #
###########################

# ------------
# - LCD INIT -
# ------------

lcd_init:
    movea( RA, lcd_init_string )
    store RA lcd_pntr

    move RA 1
    store RA lcd_config
    call lcdTxString

    call lcd_line_1

    movea( RA, lcd_message_string )
    store RA lcd_pntr

    move RA 0
    store RA lcd_config
    call lcdTxString

    ret

# ---------------
# - LCD PULSE E -
# ---------------

# GPIO PORT
# ---------
# 128  bit 7 : NU
# 64   bit 6 : RW (0)
# 32   bit 5 : E
# 16   bit 4 : RS (1=text, 0=config)
# 8    bit 3 : D7
# 4    bit 2 : D6
# 2    bit 1 : D5
# 1    bit 0 : D5

# ------------------
# - LCD SET LINE 1 -
# ------------------
# LCD_LINE_1 = 0x80 LCD RAM address for the 1st line

lcd_line_1:
    move RA 0x08
    store RA GPIO_A_OUT
    call lcd_delay

    lcd_pulse_pin( 32 )

    move RA 0x00
    store RA GPIO_A_OUT
    call lcd_delay

    lcd_pulse_pin( 32 )
    ret

# ------------------
# - LCD SET LINE 2 -
# ------------------
# LCD_LINE_2 = 0xC0 LCD RAM address for the 2nd line

lcd_line_2:
    move RA 0x0C
    store RA GPIO_A_OUT
    call lcd_delay

    lcd_pulse_pin( 32 )

    move RA 0x00
    store RA GPIO_A_OUT
    call lcd_delay

    lcd_pulse_pin( 32 )
    ret

# -----------------
# - LCD TX STRING -
# -----------------

lcdTxString:
    move RA 0                 # clear all bits
    store RA GPIO_A_OUT

    load RA lcd_config
    and RA 0xFF
    jumpnz lcd_tx_continue

    load RA GPIO_A_DATA       # set RS bit (text)
    add RA 16
    store RA GPIO_A_OUT

lcd_tx_continue:
    load RA lcd_pntr
    move RB RA

    load RA (RB)              # exit if NULL
    and RA 0xFF
    jumpz lcd_tx_exit

    and RA 0xF0               # extract high nibble
    rotate_right( RA, 4 )
    addm RA GPIO_A_DATA
    store RA GPIO_A_OUT
    call lcd_delay

    lcd_pulse_pin( 32 )
 
    load RA (RB)
    and RA 0x0F              # extract low nibble
    move RB RA
    load RA GPIO_A_DATA
    and RA 0xF0
    add RA RB
    store RA GPIO_A_OUT
    call lcd_delay

    lcd_pulse_pin( 32 )

    move RA 0
    store RA GPIO_A_OUT

    call lcd_delay

    load RA lcd_pntr
    add RA 1
    store RA lcd_pntr
    jump lcdTxString

lcd_tx_exit:
    ret

# ------------------
# - LCD TIME DELAY -
# ------------------

lcd_delay:
    movea( RA, 0x200 )
    store RA lcd_delay_count

lcd_delay_loop:
    load RA lcd_delay_count
    sub RA 1
    store RA lcd_delay_count
    jumpnz lcd_delay_loop

    ret

###########################
# CONSTANTS AND VARIABLES #
###########################

negative_sign_extend:
	.data 0xFF00

msb_mask:
    .data 0x8000

# ---------------------
# - Convert Variables -
# ---------------------

word:
    .data 0

byte:
    .data 0

nibble:
    .data 0

bin_2_dec_1s_cnt:
	.data 0

bin_2_dec_10s_cnt:
	.data 0

bin_2_dec_100s_cnt:
	.data 0

# --------------------
# - String Variables -
# --------------------

srcAddr:
    .data 0x00

destAddr:
    .data 0x00

# -------------------------
# - Serial Port Variables -
# -------------------------

txBufferIndex:
    .data 0x00

rxBufferIndex:
    .data 0x00

keyPressed:
    .data 0x00

rxChar:
    .data 0x00

txBuffer:
    space( 64 )

rxBuffer:
    space( 64 )

# --------------------
# - Bin to Hex table -
# --------------------

nibble2HexTable:
    .data 0x30    # '0'
    .data 0x31    # '1' 
    .data 0x32    # '2'
    .data 0x33    # '3' 
    .data 0x34    # '4'
    .data 0x35    # '5' 
    .data 0x36    # '6'
    .data 0x37    # '7' 
    .data 0x38    # '8'
    .data 0x39    # '9' 
    .data 0x41    # 'A'
    .data 0x42    # 'B' 
    .data 0x43    # 'C'
    .data 0x44    # 'D' 
    .data 0x45    # 'E'
    .data 0x46    # 'F' 

# ---------------
# - Test String -
# ---------------

hello:
    .data 0x48    # H
    .data 0x65    # e
    .data 0x6C    # l
    .data 0x6C    # l
    .data 0x6F    # 0
    .data 0x20    # SP
    .data 0x57    # W
    .data 0x6F    # o
    .data 0x72    # r
    .data 0x6C    # l
    .data 0x64    # d
    .data 0x0A    # LF
    .data 0x0D    # CR
    .data 0x00    # NULL

# ------------------
# - Welcome string -
# ------------------

welcome:
    .data 0x0A    # LF
    .data 0x0D    # CR
    .data 0x53    # S
    .data 0x69    # i
    .data 0x6D    # m
    .data 0x70    # p
    .data 0x6C    # l
    .data 0x65    # e
    .data 0x43    # C
    .data 0x50    # P
    .data 0x55    # U
    .data 0x20    # SP
    .data 0x44    # D
    .data 0x65    # e
    .data 0x62    # b
    .data 0x75    # u
    .data 0x67    # g
    .data 0x0A    # LF
    .data 0x0D    # CR
    .data 0x00    # NULL

# ---------------
# - Time string -
# ---------------

timeMessage:
    .data 0x0A    # LF
    .data 0x0D    # CR
    .data 0x53    # S
    .data 0x69    # i
    .data 0x6D    # m
    .data 0x70    # p
    .data 0x6C    # l
    .data 0x65    # e
    .data 0x43    # C
    .data 0x50    # P
    .data 0x55    # U
    .data 0x20    # SP
    .data 0x43    # C
    .data 0x6C    # l
    .data 0x6F    # o
    .data 0x63    # c
    .data 0x6B    # k
    .data 0x0A    # LF
    .data 0x0D    # CR
    .data 0x00    # NULL


# ------------------
# - Software delay -
# ------------------

delay_count:
	.data 0

# --------------- 
# - LCD Display -
# ---------------

lcd_delay_count:
	.data 0

lcd_pntr:
    .data 0

lcd_config:
    .data 0

# 0011 0011 Initialise
# 0011 0010 Initialise
# 0000 0110 Cursor move direction
# 0000 1100 Display On,Cursor Off, Blink Off
# 0010 1000 Data length, number of lines, font size
# 0000 0001 Clear display
# 1000 0000 LCD RAM address for the 1st line

lcd_init_string:
    .data 0x33
    .data 0x32
    .data 0x06
    .data 0x0C
    .data 0x28
    .data 0x01
    .data 0x80
    .data 0

lcd_message_string:
    .data 0x54 # T
    .data 0x69 # i
    .data 0x6D # m
    .data 0x65 # e
    .data 0x20 # SP
    .data 0x28 # (
    .data 0x48 # H
    .data 0x3A # COL
    .data 0x4D # M
    .data 0x3A # COL
    .data 0x53 # S
    .data 0x29 # )
    .data 0

###################
# CLOCK VARIABLES #
###################

count:
#   .data 300
    .data 19531

seconds:
    .data 0     # seconds

minutes:
    .data 0     # minutes

hours:
    .data 12     # hours

checkpoint:
    .data 0

digit1:
    .data 0

digit0:
    .data 0

currentSeconds:
    .data 0

currentSwitch:
    .data 0

lcd_time:
    .data 0     # H
    .data 0     # H
    .data 0     # COL
    .data 0     # M
    .data 0     # M
    .data 0     # COL
    .data 0     # S
    .data 0     # S
    .data 0     # NULL

#############
# TEST CODE #
#############

    movea( RC, rxBuffer )       # set pointer to rxBuf
    move RA 0x31
    store RA (RC)               # 
    add RC 1
    move RA 0x32
    store RA (RC)               # 
    add RC 1
    move RA 0x33
    store RA (RC)               # 
    add RC 1
    move RA 0x34
    store RA (RC)               # 
    add RC 1
    move RA 0x35
    store RA (RC)               # 
    add RC 1
    move RA 0x36
    store RA (RC)               # 
    add RC 1

    call updateTime
    jump trap


    load RA GPIO_A_IN
    store RA byte
    call byte2Dec
    call addLFCR
    call addNULL
    call txString
    call delay
















	














