COMP1521 pacman MIPS

########################################################################
# COMP1521 23T2 — Assignment 1 — Pacman!
# !!! IMPORTANT !!!
# Before starting work on the assignment, make sure you set your tab-width to 8!
# It is also suggested to indent with tabs only.
# Instructions to configure your text editor can be found here:
# https://cgi.cse.unsw.edu.au/~cs1521/23T2/resources/mips-editors.html
# !!! IMPORTANT !!!
# This program was written by YOUR-NAME-HERE (z5555555)
# on INSERT-DATE-HERE. INSERT-SIMPLE-PROGRAM-DESCRIPTION-HERE
# Version 1.0 (12-06-2023): Team COMP1521
########################################################################

#![tabsize(8)]

# Constant definitions.
# !!! DO NOT ADD, REMOVE, OR MODIFY ANY OF THESE DEFINITIONS !!!

# Directions
TOTAL_DIRECTIONS = 4

MAP_WIDTH = 13
MAP_HEIGHT = 10
MAP_DOTS = 53
NUM_GHOSTS = 3

WALL_CHAR = ‘#’
DOT_CHAR = ‘.’
PLAYER_CHAR =
GHOST_CHAR = ‘M’
EMPTY_CHAR = ‘ ‘

# Other helpful constants
GHOST_T_X_OFFSET = 0
GHOST_T_Y_OFFSET = 4
GHOST_T_DIRECTION_OFFFSET = 8
SIZEOF_GHOST_T = 12
SIZEOF_INT = 4

########################################################################
# DATA SEGMENT
# !!! DO NOT ADD, REMOVE, MODIFY OR REORDER ANY OF THESE DEFINITIONS !!!

.byte ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’
.byte ‘#’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘#’, ‘ ‘, ‘#’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘.’, ‘#’, ‘#’, ‘#’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘#’, ‘.’, ‘#’, ‘.’, ‘.’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘.’, ‘#’, ‘#’, ‘#’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘#’, ‘.’, ‘#’, ‘.’, ‘.’, ‘.’, ‘#’, ‘.’, ‘.’, ‘.’, ‘#’
.byte ‘#’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘#’, ‘.’, ‘.’, ‘.’, ‘#’, ‘.’, ‘#’
.byte ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’

.word 3, 3, UP # ghosts[0]
.word 4, 5, RIGHT # ghosts[1]
.word 9, 7, LEFT # ghosts[2]

.space MAP_HEIGHT * MAP_WIDTH

valid_directions:
.space 4 * TOTAL_DIRECTIONS
dots_collected:

lfsr_state:

# print_welcome strings
welcome_msg:
.asciiz “Welcome to 1521 Pacman!\n”
welcome_msg_wall:
.asciiz ” = wall\n”
welcome_msg_you:
.asciiz ” = you\n”
welcome_msg_dot:
.asciiz ” = dot\n”
welcome_msg_ghost:
.asciiz ” = ghost\n”
welcome_msg_objective:
.asciiz “\nThe objective is to collect all the dots.\n”
welcome_msg_wasd:
.asciiz “Use WASD to move.\n”
welcome_msg_ghost_move:
.asciiz “Ghosts will move every time you move.\nTouching them will end the game.\n”

# get_direction strings
choose_move_msg:
.asciiz “Choose next move (wasd): ”
invalid_input_msg:
.asciiz “Invalid input! Use the wasd keys to move.\n”

# main strings
dots_collected_msg_1:
.asciiz “You’ve collected ”
dots_collected_msg_2:
.asciiz ” out of ”
dots_collected_msg_3:
.asciiz ” dots.\n”

# check_ghost_collision strings
game_over_msg:
.asciiz “You ran into a ghost, game over! :(\n”

# collect_dot_and_check_win strings
game_won_msg:
.asciiz “All dots collected, you won! :D\n”

# ——————————————————————————
# Text Segment
# ——————————————————————————

############################################################
#### ####
#### Your journey begins here, intrepid adventurer! ####
#### ####
############################################################

################################################################################
# Implement the following functions,
# and check these boxes as you finish implementing each function.
# SUBSET 0
# – [ ] print_welcome
# SUBSET 1
# – [ ] main
# – [ ] get_direction
# – [ ] play_tick
# SUBSET 2
# – [ ] copy_map
# – [ ] get_valid_directions
# – [ ] print_map
# – [ ] try_move
# SUBSET 3
# – [ ] check_ghost_collision
# – [ ] collect_dot_and_check_win
# – [ ] do_ghost_logic
# (and also the ghosts part of print_map)
# PROVIDED
# – [X] get_seed
# – [X] random_number

################################################################################
# .TEXT print_welcome:
# Subset: 0
# Args: void
# Returns: void
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# print_welcome
# -> [prologue]
# -> body
# -> [epilogue]

print_welcome__prologue:

print_welcome__body:
# TODO: put your implementation of print_welcome here

print_welcome__epilogue:

################################################################################
# .TEXT


# Subset: 1
# Args: void
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# -> [prologue]
# -> body
# -> [epilogue]

main__prologue:
# TODO: put your prologue for main here

main__body:
# TODO: put your body for main here

main__epilogue:
# TODO: put your epilogue for main here

################################################################################
# .TEXT
get_direction:
# Subset: 1
# Args: void
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# get_direction
# -> [prologue]
# -> body
# -> [epilogue]

get_direction__prologue:

get_direction__body:

get_direction__epilogue:

################################################################################
# .TEXT play_tick:
# Subset: 1
# – $a0: int *dots_collected
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# play_tick
# -> [prologue]
# -> body
# -> [epilogue]

play_tick__prologue:

play_tick__body:

play_tick__epilogue:

################################################################################
# .TEXT
# Subset: 2
# – $a0: char dst[MAP_HEIGHT][MAP_WIDTH]
# – $a1: char src[MAP_HEIGHT][MAP_WIDTH]
# Returns: void
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# copy_map
# -> [prologue]
# -> body
# -> [epilogue]

copy_map__prologue:

copy_map__body:

copy_map__epilogue:

################################################################################
# .TEXT
get_valid_directions:
# Subset: 2
# – $a0: int x
# – $a1: int y
# – $a2: int dir_array[TOTAL_DIRECTIONS]
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# get_valid_directions
# -> [prologue]
# -> body
# -> [epilogue]

get_valid_directions__prologue:

get_valid_directions__body:

get_valid_directions__epilogue:

################################################################################
# .TEXT print_map:
# Subset: 2
# Args: void
# Returns: void
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# print_map
# -> [prologue]
# -> body
# -> [epilogue]

print_map__prologue:

print_map__body:

print_map__epilogue:

################################################################################
# .TEXT # Subset: 2
# – $a0: int *x
# – $a1: int *y
# – $a2: int directions
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# try_move
# -> [prologue]
# -> body
# -> [epilogue]

try_move__prologue:

try_move__body:

try_move__epilogue:

################################################################################
# .TEXT
check_ghost_collision:
# Subset: 3
# Args: void
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# check_ghost_collision
# -> [prologue]
# -> body
# -> [epilogue]

check_ghost_collision__prologue:

check_ghost_collision__body:

check_ghost_collision__epilogue:

################################################################################
# .TEXT collect_dot_and_check_win:
# Subset: 3
# – $a0: int *dots_collected
# Returns:
# – $v0: int
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# collect_dot_and_check_win
# -> [prologue]
# -> body
# -> [epilogue]

collect_dot_and_check_win__prologue:

collect_dot_and_check_win__body:

collect_dot_and_check_win__epilogue:

################################################################################
# .TEXT
do_ghost_logic:
# Subset: 3
# Args: void
# Returns: void
# Frame: […]
# Uses: […]
# Clobbers: […]
# Structure:
# do_ghost_logic
# -> [prologue]
# -> body
# -> [epilogue]

do_ghost_logic__prologue:

do_ghost_logic__body:

do_ghost_logic__epilogue:

################################################################################
################################################################################
### PROVIDED FUNCTIONS — DO NOT CHANGE ###
################################################################################
################################################################################

get_seed_prompt_msg:
.asciiz “Enter a non-zero number for the seed: ”
invalid_seed_msg:
.asciiz “Seed can’t be zero.\n”

################################################################################
# .TEXT
# Args: void
# Returns: void
# Frame: [$ra]
# Uses: [$v0, $a0]
# Clobbers: [$v0, $a0]
# – $v0: copy of lfsr_state
# Structure:
# get_seed
# -> [prologue]
# -> body
# -> loop_start
# -> loop_end
# -> [epilogue]
# PROVIDED FUNCTION — DO NOT CHANGE

get_seed__prologue:

get_seed__body:
get_seed__loop: # while (TRUE) {
li $v0, 4 # syscall 4: print_string
la $a0, get_seed_prompt_msg
syscall # printf(“Enter a non-zero number for the seed: “);

li $v0, 5 # syscall 5: read_int
sw $v0, lfsr_state # scanf(“%u”, &lfsr_state);

bnez $v0, get_seed__loop_end # if (lfsr_state != 0) break;

li $v0, 4 # syscall 4: print_string
la $a0, invalid_seed_msg
syscall # printf(“Seed can’t be zero.\n”);

b get_seed__loop # }

get_seed__loop_end:
get_seed__epilogue:

################################################################################
# .TEXT
random_number:
# Args: void
# Returns:
# – $v0: uint32_t
# Frame: [$ra]
# Uses: [$t0, $t1, $t2, $v0]
# Clobbers: [$t0, $t1, $t2, $v0]
# – $t0: uint32_t bit
# – $t1: copy of lfsr_state
# – $t2: temporary shift result
# Structure:
# random_number
# -> [prologue]
# -> body
# -> [epilogue]
# PROVIDED FUNCTION — DO NOT CHANGE

random_number__prologue:

random_number__body:
lw $t1, lfsr_state # load lfsr_state
move $t0, $t1 # uint32_t bit = lfsr_state;

srl $t2, $t1, 10 # lfsr_state >> 10
xor $t0, $t0, $t2 # bit ^= lfsr_state >> 10;

srl $t2, $t1, 30 # lfsr_state >> 30
xor $t0, $t0, $t2 # bit ^= lfsr_state >> 30;

srl $t2, $t1, 31 # lfsr_state >> 31
xor $t0, $t0, $t2 # bit ^= lfsr_state >> 31;

andi $t0, $t0, 1 # bit &= 0x1u;

sll $t1, $t1, 1 # lfsr_state <<= 1; or $t1, $t1, $t0 # lfsr_state |= bit; sw $t1, lfsr_state # store lfsr_state move $v0, $t1 # return lfsr_state; random_number__epilogue: