#####################
# Basic test code 6 #
#####################

# MEMORY MAP
# ----------

# ADDR		A1 A0 CS	WR						RD

# 0xFFF 	1  1  0		UART tx					UART rx
# 0xFFE     1  0  0		UART tx					UART status

# 0xFFD     0  1  1		GPIO A out				GPIO A out		  
# 0xFFC     0  0  1		GPIO A out  			GPIO A in 

# 0xFFB     0  1  2		NU     				    NU
# 0xFFA     1  0  2	    ROTARY_RESET			ROTARY_DATA
# 0xFF9     0  1  2		SNES_DATA               SNES_DATA
# 0xFF8     0  0  2		SNES_START    			SNES_STATUS

# 0xFF7     1  1  3		VGA 					VGA
# 0xFF6     1  0  3		VGA TILE_Y_REG
# 0xFF5     0  1  3		VGA TILE_X_REG
# 0xFF4     0  0  3		VGA CPU_DST_ADDR_REG
# 0xFF3     1  1  3		VGA CPU_COLOUR_REG		VGA TILE_POS
# 0xFF2     1  0  3 	VGA CPU_DATA_IN_REG		VGA CPU_DATA_OUT_REG
# 0xFF1     0  1  3		VGA CPU_SRC_ADDR_REG
# 0xFF0     0  0  3		VGA CPU_CTL_REG 		VGA CPU_STATUS_REG 

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

# 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

# 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
# ----

start:

    # draw_tile( COLOUR, X, Y, TILE )

	draw_tile( RED, eval( 0 + ( 8 * 6 ) ), 78, B_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 7 ) ), 78, o_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 8 ) ), 78, b_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 10 ) ), 78, G_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 11 ) ), 78, a_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 12 ) ), 78, m_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 13 ) ), 78, e_CHAR )
	draw_tile( RED, eval( 0 + ( 8 * 14 ) ), 78, s_CHAR )

	draw_tile( WHITE, eval( 0 + ( 8 * 1 ) ), 90, a_CHAR )

	draw_tile( WHITE, eval( 0 + ( 8 * 3 ) ), 90, M_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 4 ) ), 90, i_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 5 ) ), 90, k_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 6 ) ), 90, e_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 7 ) ), 90, F_CHAR )

	draw_tile( WHITE, eval( 0 + ( 8 * 9 ) ), 90, P_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 10 ) ), 90, r_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 11 ) ), 90, o_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 12 ) ), 90, d_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 13 ) ), 90, u_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 14 ) ), 90, c_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 15 ) ), 90, t_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 16 ) ), 90, i_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 17 ) ), 90, o_CHAR )
	draw_tile( WHITE, eval( 0 + ( 8 * 18 ) ), 90, n_CHAR )

	draw_tile( GREEN, eval( 0 + ( 8 * 5 ) ), 60, SPRITE_ALIEN_1_DOWN )
	draw_tile( GREEN, eval( 0 + ( 8 * 7 ) ), 60, SPRITE_ALIEN_2_DOWN )
	draw_tile( GREEN, eval( 0 + ( 8 * 9 ) ), 60, SPRITE_ALIEN_3_DOWN )
	draw_tile( GREEN, eval( 0 + ( 8 * 11 ) ), 60, SPRITE_ALIEN_4_DOWN )
	draw_tile( GREEN, eval( 0 + ( 8 * 13 ) ), 60, SPRITE_ALIEN_5_DOWN )
	draw_tile( GREEN, eval( 0 + ( 8 * 15 ) ), 60, SPRITE_ALIEN_6_DOWN )

	draw_tile( YELLOW, eval( 0 + ( 8 * 5 ) ), 50, SPRITE_ALIEN_1_UP )
	draw_tile( YELLOW, eval( 0 + ( 8 * 7 ) ), 50, SPRITE_ALIEN_2_UP )
	draw_tile( YELLOW, eval( 0 + ( 8 * 9 ) ), 50, SPRITE_ALIEN_3_UP )
	draw_tile( YELLOW, eval( 0 + ( 8 * 11 ) ), 50, SPRITE_ALIEN_4_UP )
	draw_tile( YELLOW, eval( 0 + ( 8 * 13 ) ), 50, SPRITE_ALIEN_5_UP )
	draw_tile( YELLOW, eval( 0 + ( 8 * 15 ) ), 50, SPRITE_ALIEN_6_UP )


	draw_tile( YELLOW, eval( -2 + ( 8 * 10 ) ), 40, SPRITE_ALIEN_6_UP )
	overlay_tile( GREEN, eval( 2 + ( 8 * 10 ) ), 40, SPRITE_ALIEN_6_UP )

trap:
    jump trap

####################
# TILE SUBROUTINES #
####################

# -------------
# - DRAW TILE -
# -------------

draw_tile_subroutine:
#   cpu_colour_reg
    load RA block_colour
    store RA CPU_COLOUR_REG         # colour

#   xy_position:
    load RA block_x_top_pos         # x
    store RA TILE_X_REG
    load RA block_y_top_pos         # y
    store RA TILE_Y_REG

#   set tile address
    move RA RD
    store RA CPU_SRC_ADDR_REG       # tile addr in video memory  

#   ctl reg
    move RA DRAW_TILE
    store RA CPU_CTL_REG            # start 

draw_tile_wait:
    movea( RA, CPU_STATUS_REG )
    load RB (RA)
    and RB CTL_MASK
    jumpz draw_tile_wait
    ret

# ----------------
# - OVERLAY TILE -
# ----------------

overlay_tile_subroutine:
#   cpu_colour_reg
    load RA block_colour
    store RA CPU_COLOUR_REG         # colour

#   xy_position:
    load RA block_x_top_pos         # x
    store RA TILE_X_REG
    load RA block_y_top_pos         # y
    store RA TILE_Y_REG

#   set tile address
    move RA RD
    store RA CPU_SRC_ADDR_REG       # tile addr in video memory  

#   ctl reg
    move RA OVERLAY_TILE
    store RA CPU_CTL_REG            # start 

overlay_tile_wait:
    movea( RA, CPU_STATUS_REG )
    load RB (RA)
    and RB CTL_MASK
    jumpz overlay_tile_wait
    ret

# -------------------
# - DRAW TILE DIGIT -
# -------------------

# draw digit character at XY position, tile selected by NIBBLE
# colour              : block_colour
# X pos               : block_x_top_pos
# Y pos               : block_y_top_pos 
# offset              : nibble 

draw_tile_digit:
    moved( RD, zero_CHAR )            # base address
    load RA nibble                    # get offset
    and RA 0x0F
    move RB RA
    sub RB 9                          # <= 9 
    jumpc draw_tile_digit_exit

    asl RA                            # x 4
    asl RA
    add RD RA                         # add offset to base address
    
    call draw_tile_subroutine

draw_tile_digit_exit:
    ret

# -------------------
# - DRAW TILE ASCII -
# -------------------

# draw ASCII character at XY position, tile selected by BYTE (0x20 - 0x7F)
# colour              : block_colour
# X pos               : block_x_top_pos
# Y pos               : block_y_top_pos 
# offset              : byte 

draw_tile_ascii:
    moved( RD, sp_CHAR )        # base address SPACE (0x20)
    load RA byte                # get ASCII value
    and RA 0xFF     
    move RB RA 
    sub RB 0x7F                 # <= 0x7F
    jumpc draw_tile_ascii_exit

    asl RA                      # x 4
    asl RA
    add RD RA                   # add offset to base address
    
    call draw_tile_subroutine

draw_tile_ascii_exit:
    ret

# ------------------
# - DRAW TILE CHAR -
# ------------------

# draw CHAR character at XY position, tile selected by WORD address
# colour              : block_colour
# X pos               : block_x_top_pos
# Y pos               : block_y_top_pos 
# address             : word

draw_tile_char:
    load RA word                # get ASCII value
    move RD RA                   
    
    call draw_tile_subroutine

    ret


#####################
# BLOCK SUBROUTINES #
#####################

# --------------
# - BLOCK WAIT -
# --------------

block_wait:
    movea( RA, CPU_STATUS_REG )
    load RB (RA)
    and RB CTL_MASK
    jumpz block_wait
    ret

# ---------------
# - BLOCK WRITE -
# ---------------

# write data directly to frame buffer
# colour        : block_colour
# start address : block_start_address
# end address   : block_start_address + block_size
#

block_write_subroutine:
#   cpu_colour_reg
    movea( RB, CPU_COLOUR_REG )
    load RA block_colour
    store RA (RB)

#   cpu_data_in_reg - end src addr
    movea( RB, CPU_DATA_IN_REG )
    load RA block_start_address
    addm RA block_width
    store RA (RB)

#   cpu_addr_reg
    movea( RB, CPU_DST_ADDR_REG )
    load RA block_start_address
    store RA (RB)

#   ctl reg 
    movea( RA, CPU_CTL_REG )
    move RB BLOCK_WRITE
    store RB (RA)

block_write_wait:
    call block_wait
    ret

# ------------
# - DRAW BOX -
# ------------

#             1   2  3  4  5 
#  draw_box( RED, 6, 2, 7, 3 )
#             |   |  |  |  |
# 1 : COLOUR --   |  |  |  |
# 2 : X_TOP -------  |  |  |
# 3 : Y_TOP ----------  |  |
# 4 : X_BOTTOM ----------  |
# 5 : Y_BOTTOM -------------

# draw box directly to frame buffer, multiples of 2 pixel along X axis
# 
# colour                       : block_colour
# start addr in frame buffer   : block_start_address
# number of 2 pixel blocks     : block_width
# number of rows               : block_depth


draw_box_subroutine:
    call block_write_subroutine

    load RA block_depth
    sub RA 1
    store RA block_depth
    jumpz draw_box_exit

    load RA block_start_address
    add RA eval( VDC_MAX_X_LIMIT >> 1 )
    store RA block_start_address

    jump draw_box_subroutine

draw_box_exit:
    ret

# ------------------
# - DRAW RECTANGLE -
# ------------------

# draw rectangle directly to frame buffer, multiples of 2 pixel along X axis

# colour                  : block_colour
# top left x location     : block_x_top_pos
# bottom right x location : block_x_bottom_pos
# top left y location     : block_y_top_pos
# bottom right y location : block_y_bottom_pos

draw_rectangle:
#   calc start address
    load RA block_x_top_pos
    store RA TILE_X_REG
    load RA block_y_top_pos
    store RA TILE_Y_REG
    load RA TILE_POS
	store RA block_start_address

#   calc width
    load RA block_x_bottom_pos 
    subm RA block_x_top_pos
    asr RA
    store RA block_width

#   draw line
    call block_write_subroutine

#   check if all rows drawn
    load RA block_y_bottom_pos
    subm RA block_y_top_pos
    jumpz draw_rectangle_exit

#   increment Y
    load RA block_y_top_pos
    add RA 1
    store RA block_y_top_pos
    jump draw_rectangle

draw_rectangle_exit:
    ret

# --------------
# - BLOCK COPY -
# --------------

# write data directly to frame buffer
# colour              : block_colour
# start address       : block_start_address
# end address         : block_start_address + block_width
# destination address : block_destination_address 
#

block_copy_subroutine:
#   cpu_data_in_reg - end src addr
    movea( RB, CPU_DATA_IN_REG )
    load RA block_start_address
    addm RA block_width
    store RA (RB)

#   cpu_addr_reg
    movea( RB, CPU_SRC_ADDR_REG )
    load RA block_start_address
    store RA (RB)

#   cpu_dest_reg
    movea( RB, CPU_DST_ADDR_REG )
    load RA block_destination_address
    store RA (RB)

#   ctl reg 
    movea( RA, CPU_CTL_REG )
    move RB BLOCK_COPY
    store RB (RA)

block_copy_wait:
    call block_wait
    ret

# ------------
# - COPY BOX -
# ------------

#                1  2  3  4  5  6
#      copy_box( 0  0  5  5  6  6 )
#                |  |  |  |  |  | 
# 1 : SRC_X_TOP --  |  |  |  |  |
# 2 : SRC_Y_TOP -----  |  |  |  |
# 3 : SRC_X_BOTTOM -----  |  |  |
# 4 : SRC_Y_BOTTOM --------  |  |
# 5 : DEST_X_TOP -------------  |
# 6 : DEST_Y_TOP ----------------

# draw box directly to frame buffer, multiples of 2 pixel along X axis
# 
# colour                       : block_colour
# start addr in frame buffer   : block_start_address
# destination address          : block_destination_address 
# number of 2 pixel blocks     : block_width
# number of rows               : block_depth

copy_box_subroutine:
    call block_copy_subroutine

    load RA block_depth
    sub RA 1
    store RA block_depth
    jumpz copy_box_exit

    load RA block_start_address
    add RA eval( VDC_MAX_X_LIMIT >> 1 )
    store RA block_start_address

    load RA block_destination_address
    add RA eval( VDC_MAX_X_LIMIT >> 1 )
    store RA block_destination_address

    jump copy_box_subroutine

copy_box_exit:
    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

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

    add RB 1                 # inc address
    jump tx_loop             # repeat

tx_exit:
    move RA 0
    store RA txBufferIndex
    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 TX Test -
# --------------

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

    load RA UART_RX
    store RA UART_TX

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

    jump rx_tx_test          # repeat

# ------------------ 
# - Software Delay -
# ------------------

delay:
    move RB 3 

delay_start:
    movea( RA, 0xFFF )

delay_loop:
    sub RA 1
    jumpnz delay_loop
    sub RB 1
    jumpnz delay_start

    ret



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

negative_sign_extend:
	.data 0xFF00

msb_mask:
    .data 0x8000

count:
    .data 0

line:
    .data 0

# -------------------
# - Block Variables -
# -------------------

block_colour:
    .data 0

block_x_top_pos:
    .data 0

block_x_bottom_pos:
    .data 0

block_y_top_pos:
    .data 0

block_y_bottom_pos:
    .data 0

block_start_address:
    .data 0

block_width:
    .data 0

block_depth:
    .data 0

block_destination_address:
    .data 0

# ---------------------
# - 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

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    # SPACE
    .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    # SPACE
    .data 0x44    # D
    .data 0x65    # e
    .data 0x62    # b
    .data 0x75    # u
    .data 0x67    # g
    .data 0x0A    # LF
    .data 0x0D    # CR
    .data 0x00    # NULL

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

delay_low_count:
	.data 0

delay_high_count:
	.data 0

# ---------
# - Pixel -
# ---------

pixel_colour:
    .data 0

pixel_x_pos:
    .data 0

pixel_y_pos:
    .data 0

# ---------
# - Line  -
# ---------

line_colour:
    .data 0

line_x:
    .data 0

line_y:
    .data 0

line_x0:
    .data 0

line_y0:
    .data 0

line_x1:
    .data 0

line_y1:
    .data 0

line_dx:
    .data 0

line_dy:
    .data 0

line_error:
    .data 0

line_step:
    .data 0

line_slope:
    .data 0

# ----------
# - Circle -
# ----------

circle_colour:
    .data 0

circle_filled:
    .data 0

circle_x:
    .data 0

circle_y:
    .data 0

circle_x0:
    .data 0

circle_y0:
    .data 0

circle_r:
    .data 0

circle_error:
    .data 0 

circle_A_x:
    .data 0 

circle_A_y:
    .data 0 

circle_B_x:
    .data 0 

circle_B_y:
    .data 0 

circle_C_x:
    .data 0 

circle_C_y:
    .data 0 

circle_D_x:
    .data 0 

circle_D_y:
    .data 0 

circle_E_x:
    .data 0 

circle_E_y:
    .data 0 

circle_F_x:
    .data 0 

circle_F_y:
    .data 0 

circle_G_x:
    .data 0 

circle_G_y:
    .data 0 

circle_H_x:
    .data 0 

circle_H_y:
    .data 0 

# -----------
# - Sprites -
# -----------

sprite:
    .data 0
    
sprite_command:
    .data 0

sprite_pntr:
    .data 0

sprite_pntr_1:
    .data 0

sprite_collision:
    .data 0

sprite_tile:
    .data 0

sprite_x_offset:
    .data 0

sprite_y_offset:
    .data 0

sprite_x_pos:
    .data 0

sprite_x_pos_1:
    .data 0

sprite_y_pos:
    .data 0

sprite_y_pos_1:
    .data 0

sprite_hitbox_x_min:
    .data 0

sprite_hitbox_x_min_1:
    .data 0

sprite_hitbox_x_max:
    .data 0

sprite_hitbox_x_max_1:
    .data 0

sprite_hitbox_y_min:
    .data 0

sprite_hitbox_y_min_1:
    .data 0

sprite_hitbox_y_max:
    .data 0

sprite_hitbox_y_max_1:
    .data 0

sprite_limit_x_min:
    .data 0

sprite_limit_x_max:
    .data 0

sprite_limit_y_min:
    .data 0

sprite_limit_y_max:
    .data 0

sprite_edge:
    .data 0

sprite_number:
    .data 0

# -------------- 
# - Controller -
# --------------

rotary_switch:
	.data 0
	
fire_switch:
	.data 0
	
snes_switch:
	.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

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

message_string:
    .data 0x4F # O
    .data 0x58 # X
    .data 0x4F # O
    .data 0x20 # space
    .data 0x44 # D
    .data 0x65 # e
    .data 0x6D # m
    .data 0x6F # o
    .data 0


##################
# GAME VARIABLES #
##################

player_O_score:
	.data 0

player_X_score:
	.data 0

player:
    .data 0

position:
    .data 0

win:
    .data 0

finish:
    .data 0

restart:
    .data 0

player_O_try:
    .data 0

player_X_try:
    .data 0

###############
# SPRITE DATA #
###############

alien_sprite_data:
    initialize_sprite( SPRITE_SET_VISIBLE, SPRITE_SET_AUTO, SPRITE_SET_TILE_INC, SPRITE_TILE_NUMBER_0, 
                       SPRITE_ALIEN_1_DOWN, SPRITE_ALIEN_1_UP, SPRITE_ALIEN_1_DOWN, SPRITE_ALIEN_1_UP, 
                       SPRITE_ALIEN_1_DOWN, SPRITE_ALIEN_1_UP, SPRITE_ALIEN_1_DOWN, SPRITE_ALIEN_1_UP, 
                       BLACK, GREEN, 
                       82,  15, RIGHT, DOWN, 7, 0, 7, 0, 
                       VDC_MAX_X_LIMIT, eval( VDC_MIN_X_LIMIT + 2 ), eval( VDC_MAX_Y_LIMIT - 18 ), eval( VDC_MIN_Y_LIMIT + 2 ) )








