# =============================================================================================================
# *
# * Copyright (c) Mike
# *
# * File Name: vgaTestCode.asm
# *
# * Version: V1.0
# *
# * Release Date:
# *
# * Author(s): M.Freeman
# *
# * Description: OXO test 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:
# *
# =============================================================================================================


start: 
    call init_game

init:
    call init_grid

loop:
    move RA 1
    store RA player

    run( make_move_x )
    run( check_win_condition )
    run( check_draw_condition )

    load RA finish
    and RA 0xFF
    jumpnz trap

    load RA win
    and RA 0xFF
    jumpnz trap

    load RA restart
    and RA 0xFF
    jumpnz trap

    move RA 0
    store RA player

    run( make_move_o )
    run( check_win_condition )
    run( check_draw_condition )

    load RA finish
    and RA 0xFF
    jumpnz trap

    load RA win
    and RA 0xFF
    jumpnz trap

    load RA restart
    and RA 0xFF
    jumpnz trap

    jump loop

trap:
    jump init

####################
# GAME SUBROUTINES #
####################

# ------------- 
# - INIT GAME -
# -------------

init_game:
    move RA 0
    store RA player_O_score
    store RA player_X_score
	store RA player
    store RA position
    store RA win
    store RA finish
    store RA restart

    #store RA player_O_try
    #store RA player_X_try

    load RA player_O_try
    add RA 1
    store RA player_O_try

    load RA player_X_try
    add RA 2
    store RA player_X_try

init_grid:
    move RA 0
    movea( RB, grid_state + 0 )
    store RA (RB)
    movea( RB, grid_state + 1 )
    store RA (RB)
    movea( RB, grid_state + 2 )
    store RA (RB)
    movea( RB, grid_state + 3 )
    store RA (RB)
    movea( RB, grid_state + 4 )
    store RA (RB)
    movea( RB, grid_state + 5 )
    store RA (RB)
    movea( RB, grid_state + 6 )
    store RA (RB)
    movea( RB, grid_state + 7 )
    store RA (RB)
    movea( RB, grid_state + 8 )
    store RA (RB)
    ret

# ---------------
# - MAKE MOVE O -
# ---------------
# 0000 0000 = empty  0000 0001 = O  0001 0000 = X  
#     0                  1              16

make_move_O:
    movea( RB, random_O )       # set random_O as base address

    load RA player_O_try        # load try count (0 - 63)
    add RA 1                    # inc
    and RA 0x3F                 # cap
    store RA player_O_try       # save  
    add RB RA                   # random base addr + try count
    load RA (RB)                # get random value
    store RA position

    movea( RB, grid_state )     # set grid_state
    add RB RA                   # offset by random value
    load RA (RB)                # get state
    and RA 0xFF                 # test if free i.e. 0
    jumpnz make_move_O          # if taken repeat

    move RA 0x01                # update grid
    store RA (RB)               
    ret

# ---------------
# - MAKE MOVE X -
# ---------------
# 0000 0000 = empty  0000 0001 = O  0001 0000 = X  
#     0                  1              16

make_move_X:
    movea( RB, random_X )       # set random_O as base address

    load RA player_X_try        # load try count (0 - 63)
    add RA 1                    # inc
    and RA 0x3F                 # cap
    store RA player_X_try       # save  
    add RB RA                   # random base addr + try count
    load RA (RB)                # get random value
    store RA position

    movea( RB, grid_state )     # set grid_state
    add RB RA                   # offset by random value
    load RA (RB)                # get state
    and RA 0xFF                 # test if free i.e. 0
    jumpnz make_move_X          # if taken repeat

    move RA 0x10                # update grid
    store RA (RB)              
    ret

# -----------------------
# - CHECK WIN CONDITION -
# -----------------------

# 0 | 1 | 2
# ----------
# 3 | 4 | 5
# ----------
# 6 | 7 | 8

# 012   345   678
# 036   147   258
# 048   246

# 0000 0000 = empty  0000 0001 = O  0001 0000 = X  
#     0                  1              16

check_win_condition:
    move RA 0
    store RA win

    move RC 0

    movea( RA, grid_state )     # access 0
    load RB (RA)
    add RC RB

    add RA 1                    # access 1
    load RB (RA)
    add RC RB

    add RA 1                    # access 2
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_0
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_0

    move RC 0

    movea( RA, grid_state )     
    add RA 3
    load RB (RA)                # access 3
    add RC RB

    add RA 1                    # access 4
    load RB (RA)
    add RC RB

    add RA 1                    # access 5
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_1
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_1

    move RC 0

    movea( RA, grid_state )     
    add RA 6
    load RB (RA)                # access 6
    add RC RB

    add RA 1                    # access 7
    load RB (RA)
    add RC RB

    add RA 1                    # access 8
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_2
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_2  

    move RC 0

    movea( RA, grid_state )     
    load RB (RA)                # access 0
    add RC RB

    add RA 3                    # access 3
    load RB (RA)
    add RC RB

    add RA 3                    # access 6
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_3
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_3 

    move RC 0

    movea( RA, grid_state )    
    add RA 1 
    load RB (RA)                # access 1
    add RC RB

    add RA 3                    # access 4
    load RB (RA)
    add RC RB

    add RA 3                    # access 7
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_4
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_4

    move RC 0

    movea( RA, grid_state )    
    add RA 2 
    load RB (RA)                # access 2
    add RC RB

    add RA 3                    # access 5
    load RB (RA)
    add RC RB

    add RA 3                    # access 8
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_5
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_5

    move RC 0

    movea( RA, grid_state )    
    load RB (RA)                # access 0
    add RC RB

    add RA 4                    # access 4
    load RB (RA)
    add RC RB

    add RA 4                    # access 8
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_6
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_6

    move RC 0

    movea( RA, grid_state )   
    add RA 2 
    load RB (RA)                # access 2
    add RC RB

    add RA 2                    # access 4
    load RB (RA)
    add RC RB

    add RA 2                    # access 6
    load RB (RA)
    add RC RB

    move RD RC
    and RC 0x0F
    sub RC 0x03
    jumpz check_win_condition_7
    move RC RD
    and RC 0xF0
    sub RC 0x30
    jumpz check_win_condition_7
    ret

check_win_condition_0:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_1:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_2:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_3:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_4:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_5:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_6:
    move RA 1
    store RA win

    call inc_player_score
    ret

check_win_condition_7:
    move RA 1
    store RA win

    call inc_player_score
    ret


# ------------------------
# - CHECK DRAW CONDITION -
# ------------------------

# 0 | 1 | 2
# ----------
# 3 | 4 | 5
# ----------
# 6 | 7 | 8

# 0000 0000 = empty  0000 0001 = O  0001 0000 = X  
#     0                  1              16

check_draw_condition:
    move RA 0
    store RA finish
    
    movea( RB, grid_state + 0 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 1 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 2 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 3 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 4 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 5 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 6 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 7 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

    movea( RB, grid_state + 8 )
    load RA (RB)
    and RA 0xFF
    jumpz check_draw_condition_false

check_draw_condition_true:
    move RA 1
    store RA finish

check_draw_condition_false:
    ret

# --------------------------
# - INCREMENT PLAYER SCORE -
# --------------------------

inc_player_score:
    ret

###########################
# CONSTANTS AND VARIABLES #
###########################
#<<
negative_sign_extend:
	.data 0xFF00

random_mask:
    .data 0xFFFC

msb_mask:
    .data 0x8000

count:
    .data 0

line:
    .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

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

pixel_colour:
    .data 0

pixel_x_pos:
    .data 0

pixel_y_pos:
    .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

# 64 random 0-8 values

random_O:
    .data 8
    .data 0
    .data 2
    .data 1
    .data 4
    .data 2
    .data 8
    .data 2
    .data 7
    .data 5
    .data 5
    .data 2
    .data 0
    .data 4
    .data 5
    .data 5
    .data 8
    .data 7
    .data 4
    .data 6
    .data 1
    .data 2
    .data 5
    .data 1
    .data 3
    .data 6
    .data 1
    .data 4
    .data 7
    .data 1
    .data 0
    .data 8
    .data 8
    .data 5
    .data 3
    .data 0
    .data 8
    .data 8
    .data 4
    .data 0
    .data 5
    .data 4
    .data 8
    .data 3
    .data 6
    .data 2
    .data 7
    .data 8
    .data 3
    .data 8
    .data 2
    .data 2
    .data 8
    .data 5
    .data 3
    .data 0
    .data 1
    .data 0
    .data 8
    .data 3
    .data 2
    .data 2
    .data 6
    .data 0

# 64 random 0-8 values

random_X:
    .data 6
    .data 7
    .data 6
    .data 6
    .data 8
    .data 5
    .data 2
    .data 2
    .data 6
    .data 6
    .data 0
    .data 0
    .data 4
    .data 5
    .data 8
    .data 4
    .data 7
    .data 4
    .data 4
    .data 2
    .data 5
    .data 1
    .data 6
    .data 6
    .data 8
    .data 2
    .data 8
    .data 8
    .data 1
    .data 6
    .data 2
    .data 6
    .data 0
    .data 8
    .data 6
    .data 2
    .data 0
    .data 3
    .data 2
    .data 1
    .data 0
    .data 4
    .data 8
    .data 2
    .data 3
    .data 0
    .data 8
    .data 3
    .data 0
    .data 0
    .data 5
    .data 6
    .data 3
    .data 4
    .data 5
    .data 0
    .data 2
    .data 2
    .data 6
    .data 0
    .data 1
    .data 0
    .data 8
    .data 3

.addr 4000

grid_state:
    .data 0
    .data 0
    .data 0
    .data 0
    .data 0
    .data 0
    .data 0
    .data 0
    .data 0








	



