Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data  
  2.     gameOver:       .asciiz     "Game Over.\n"
  3.     playTime1:      .asciiz     "The playing time was "
  4.     playTime2:      .asciiz     " ms.\n"
  5.     score1:         .asciiz     "The game score was "
  6.     score2:         .asciiz     " frogs.\n"
  7.     headX:          .word       7   # head X position
  8.     headY:          .word       31  # head Y position
  9.     tailX:          .word       0   # tail X position
  10.     tailY:          .word       31  # tail Y position
  11.     startTime:      .word       0
  12.     dir:            .asciiz     "000000000000000000000000000000000000000000"
  13.                             # 0 = right, 1 = left, 2 = up, 3 = down
  14.                             # 42 characters (32 frogs + 8 original + 2 for overflow protection)
  15.                             # starts all 0's (right) since snake is facing right
  16.     #REGISTER USES
  17.     #t0 TRASH TEMP
  18.     #t1 TRASH TEMP
  19.     #t2 TRASH TEMP
  20.     #t3 TRASH TEMP
  21.     #t4 TRASH TEMP: frog randX
  22.     #t5 TRASH TEMP: frog randY
  23.     #t6 TRASH TEMP: frogs generated
  24.     #t7 FULL TEMP: Score
  25.     #t8 FULL TEMP: Last Pressed Direction (0,1,2,3)
  26.     #t9 FULL TEMP: Tail Size
  27.     #a0 setLED/getLED X
  28.     #a1 setLED/getLED Y
  29.     #a2 setLED color
  30.     #a3 dir .asciiz tracker
  31. .text
  32. Initialize:             # initialize program (sets walls, starter colors, values, then frogs)  
  33.     jal randinit            # initialize random
  34.     li $t6, 0           # set wall counter
  35. wall_loop:              # do frogs first, they will be overwritten by snake or walls (eliminates checking)
  36.     la $a0, 64          # sets upper bound for random generator as 64 (width)
  37.     jal getRandomNumber     # generate random number (1-64)
  38.     la $t4, ($t1)           # random X
  39.     la $a0, 64          # sets upper bound for random generator as 64 (height)
  40.     jal getRandomNumber     # generate random number (1-64)
  41.     la $t5, ($t1)           # random Y
  42.     la $a0, ($t4)           # prepare random X for setLED
  43.     la $a1, ($t5)           # prepare random Y for setLED
  44.     li $a2, 1           # set red
  45.     jal setLED          # make wall
  46.     addi $t6, $t6, 1        # add one to wall counter
  47.     beq $t6, 32, wall_end       # branch if all walls generated (64)
  48.     j wall_loop         # loop to generate another frog
  49.     wall_end:
  50.     li $a0, 0xFFFF0008      # load LED address
  51.     li $a1, 0x0000AAAA      # load yellow starters
  52.     addi $a0, $a0, 496      # jump ahead in LED display to start location
  53.     sw  $a1, 0($a0)         # store yellow starters at proper LED location
  54.    
  55.     li $t7, 0           # set score to 0 (score = frogs found)
  56.     li $t8, 4           # load last pressed direction as 4 (nothing) to start (makes pause at start)
  57.     li $t9, 8           # load TailSize as 8 to start
  58.    
  59.     li $t6 0            # set frog counter
  60. frog_loop:              # do frogs first, they will be overwritten by snake or walls (eliminates checking)
  61.     la $a0, 64          # sets upper bound for random generator as 64 (width)
  62.     jal getRandomNumber     # generate random number (1-64)
  63.     la $t4, ($t1)           # random X
  64.     la $a0, 64          # sets upper bound for random generator as 64 (height)
  65.     jal getRandomNumber     # generate random number (1-64)
  66.     la $t5, ($t1)           # random Y
  67.     la $a0, ($t4)           # prepare random X for setLED
  68.     la $a1, ($t5)           # prepare random Y for setLED
  69.     jal getLED          # get LED at randomXY
  70.     bne $v0, 0, not_frog        # if LED at randomXY is not blank, skip making frog
  71.     li $a2, 3           # set green
  72.     jal setLED          # make frog
  73.     addi $t6, $t6, 1        # add one to frog counter
  74.     beq $t6, 32, frog_end       # branch if all frogs generated (32)
  75.     not_frog:
  76.     j frog_loop         # loop to generate another frog
  77. frog_end:
  78.    
  79. _poll:
  80.     jal sleep           # sleep for 200ms  
  81. ######################################## BONUS: MOVING FROGS HAS BEEN COMMENTED OUT BELOW ########################################
  82. ### Due to random being generated by lower bits of time, frog movement frequency would occur in waves
  83. ### Also, since there were no saved locations of frogs, it would find them by just randomely generating XY coords until it found one (SUPER ineffient)
  84. ### Also, since it generates XY until it finds one, the less frogs there were, the more frequently an individual frog would move
  85. ### And the less individual frogs there were, the longer it would take to find one at a random XY which sometimes slowed the game for slightly noticable amounts of time
  86. ### THEREFORE: This 120 lines has been commented out. Frogs will stay stationary for now
  87.  
  88. #   la $a0, 15          # sets upper bound for random generator as 15
  89. #   jal getRandomNumber     # generate random number
  90. #   bne $t1, 10, frog_move_done2    # if not 10 (which would be a 1/15 chance that it is, aka on average once every 3 seconds since 15*200ms=3s), then skip
  91. #frog_move:             # generates randomXY until frog is found
  92. #   la $a0, 64          # sets upper bound for random generator as 64 (width)
  93. #   jal getRandomNumber     # generate random number (1-64)
  94. #   la $t4, ($t1)           # random X
  95. #   la $a0, 64          # sets upper bound for random generator as 64 (height)
  96. #   jal getRandomNumber     # generate random number (1-64)
  97. #   la $t5, ($t1)           # random Y
  98. #   la $a0, ($t4)           # prepare random X for setLED
  99. #   la $a1, ($t5)           # prepare random Y for setLED
  100. #   jal getLED          # get LED at randomXY
  101. #   bne $v0, 3, frog_move       # if LED at randomXY is not green, go back and generate another randomXY
  102. #   li $a2, 0           # set blank
  103. #   jal setLED          # make blank
  104. #   la $a0, 8           # set upper bound for random generator to 4
  105. #   jal getRandomNumber     # generate random number (1-8)
  106. #   subi $t1, $t1, 1        # subtract random number by one so its 0-7 (for consistency) this determines direction (4-7 are diagonals)
  107. #   la $a0, ($t4)           # prepare random X for setLED
  108. #   la $a1, ($t5)           # prepare random Y for setLED
  109. #   beq $t1, 0, frog_right      # if right
  110. #   beq $t1, 1, frog_left       # if left
  111. #   beq $t1, 2, frog_up     # if up
  112. #   beq $t1, 3, frog_down       # if down
  113. #   beq $t1, 4, frog_rightUp    # if right-up
  114. #   beq $t1, 5, frog_leftUp     # if left-up
  115. #   beq $t1, 6, frog_rightDown  # if right-down
  116. #   beq $t1, 7, frog_leftDown   # if left-down
  117. #   j frog_right            # catch
  118. #  
  119. #frog_right:
  120. #   addi $a0, $a0, 1        # move right 1
  121. #   bge $a0, 64, frog_RIGHT_SIDE    # check if we go beyond the edge
  122. #   j frog_move_done        # move done
  123. #   frog_RIGHT_SIDE:
  124. #   li $a0, 0           # set x location to left side
  125. #   j frog_move_done        # move done
  126. #
  127. #frog_left:
  128. #   subi $a0, $a0, 1        # move left 1
  129. #   blt $a0, $0, frog_LEFT_SIDE # check if we go beyond the edge
  130. #   j frog_move_done        # move done
  131. #   frog_LEFT_SIDE:
  132. #   li $a0, 63          # set x location to right side
  133. #   j frog_move_done        # move done
  134. #
  135. #frog_up:
  136. #   subi $a1, $a1, 1        # move up 1
  137. #   blt $a1, $0, frog_DOWN_SIDE # check if we go beyond the top
  138. #   j frog_move_done        # move done
  139. #   frog_DOWN_SIDE:
  140. #   li $a1, 63          # set y location to bottom row
  141. #   j frog_move_done        # move done
  142.  
  143. #frog_down:
  144. #   addi $a1, $a1, 1        # move down 1
  145. #   bge $a1, 64, frog_UP_SIDE   # check if we go beyond the bottom
  146. #   j frog_move_done        # move done
  147. #   frog_UP_SIDE:
  148. #   li $a1, 0           # set y location to top row
  149. #   j frog_move_done        # move done
  150. #  
  151. #frog_rightUp:
  152. #   addi $a0, $a0, 1        # move right 1
  153. #   bge $a0, 64, frog_RIGHT_SIDE1   # check if we go beyond the edge
  154. #   j skip1             # skip 
  155. #   frog_RIGHT_SIDE1:
  156. #   li $a0, 0           # set x location to left side
  157. #   skip1:
  158. #   subi $a1, $a1, 1        # move up 1
  159. #   blt $a1, $0, frog_DOWN_SIDE1    # check if we go beyond the top
  160. #   j frog_move_done        # move done
  161. #   frog_DOWN_SIDE1:
  162. #   li $a1, 63          # set y location to bottom row
  163. #   j frog_move_done        # move done
  164. #
  165. #frog_leftUp:
  166. #   subi $a0, $a0, 1        # move left 1
  167. #   blt $a0, $0, frog_LEFT_SIDE2    # check if we go beyond the edge
  168. #   j skip2             # skip 
  169. #   frog_LEFT_SIDE2:
  170. #   li $a0, 63          # set x location to right side
  171. #   skip2:
  172. #   subi $a1, $a1, 1        # move up 1
  173. #   blt $a1, $0, frog_DOWN_SIDE2    # check if we go beyond the top
  174. #   j frog_move_done        # move done
  175. #   frog_DOWN_SIDE2:
  176. #   li $a1, 63          # set y location to bottom row
  177. #   j frog_move_done        # move done
  178. #
  179. #frog_rightDown:
  180. #   addi $a0, $a0, 1        # move right 1
  181. #   bge $a0, 64, frog_RIGHT_SIDE3   # check if we go beyond the edge
  182. #   j skip3             # skip 
  183. #   frog_RIGHT_SIDE3:
  184. #   li $a0, 0           # set x location to left side
  185. #   skip3:         
  186. #   addi $a1, $a1, 1        # move down 1
  187. #   bge $a1, 64, frog_UP_SIDE3  # check if we go beyond the bottom
  188. #   j frog_move_done        # move done
  189. #   frog_UP_SIDE3:
  190. #   li $a1, 0           # set y location to top row
  191. #   j frog_move_done        # move done
  192. #
  193. #frog_leftDown:
  194. #   addi $a0, $a0, 1        # move right 1
  195. #   bge $a0, 64, frog_RIGHT_SIDE4   # check if we go beyond the edge
  196. #   j skip4             # skip
  197. #   frog_RIGHT_SIDE4:
  198. #   li $a0, 0           # set x location to left side
  199. #   skip4:
  200. #   addi $a1, $a1, 1        # move down 1
  201. #   bge $a1, 64, frog_UP_SIDE4  # check if we go beyond the bottom
  202. #   j frog_move_done        # move done
  203. #   frog_UP_SIDE4:
  204. #   li $a1, 0           # set y location to top row
  205. #   j frog_move_done        # move done
  206. #  
  207. #frog_move_done:            # frog position has been moved
  208. #   li $a2, 3           # prepare green
  209. #   jal setLED          # set new frog location to green
  210. #rog_move_done2:            # used as the skip from earlier
  211. ################ END OF FROG MOVEMENT CODE ################
  212.     skip:               # if a direction is already being travelled, skip it and the pause (pause has already been done)
  213.     la $t0, 0xFFFF0000      # address of key press signifier
  214.     lw $t0, 0($t0)          # load key press signifier
  215.     andi $t0, $t0, 1        # check if there was a keypress
  216.     bne $t0, $0, _keypress      # branch if there was a keypress
  217.     la $a3, dir         # load dir
  218.     beq $t8, 0, right_pressed   # branch if right was last pressed
  219.     beq $t8, 1, left_pressed    # branch if left was last pressed
  220.     beq $t8, 2, up_pressed      # branch if up was last pressed
  221.     beq $t8, 3, down_pressed    # branch if down was last pressed
  222.     j _poll             # loop poll
  223.    
  224. _keypress:
  225.     lw $t0, startTime       # load startTime
  226.     beq $t0, 0, get_start_time  # if startTime is 0 (hasn't been set yet) branch to get_start_time
  227.     j skip_start_time       # if it skips the branch, then skip get_start_time
  228.     get_start_time:
  229.     li $v0, 30          # prepare to get start time
  230.     syscall
  231.     sw $a0, startTime       # store start time
  232.     skip_start_time:
  233.    
  234.     la $t0, 0xFFFF0004      # address of which key was pressed
  235.     lw $t0, 0($t0)          # load the word that was stored
  236.     la $a3, dir         # load dir
  237.    
  238.     subi $t1, $t0, 66       # center?
  239.     beq $t1, $0, center_pressed # branch if center key pressed
  240.    
  241.     subi $t1, $t0, 227      # right?
  242.     beq $t1, $0, right_pressed  # branch if right was pressed
  243.  
  244.     subi $t1, $t0, 226      # left?
  245.     beq $t1, $0, left_pressed   # branch if left was pressed
  246.    
  247.     subi $t1, $t0, 224      # up?
  248.     beq $t1, $0, up_pressed     # branch if up was pressed
  249.  
  250.     subi $t1, $t0, 225      # down?
  251.     beq $t1, $0, down_pressed   # branch if down was pressed
  252.    
  253.     j _poll             # start the polling loop again
  254.  
  255.  
  256. right_pressed:
  257.     beq $t8, 1, skip        # if travelling left, skip right
  258.    
  259.     lw $a0, headX           # load headX
  260.     lw $a1, headY           # load headY
  261.     addi $a0, $a0, 1        # move right 1
  262.     bge $a0, 64, RIGHT_SIDE_wall    # check if we go beyond the edge
  263.     j RIGHT_SIDE_wall_skip      # skip side exception  
  264.     RIGHT_SIDE_wall:
  265.     li $a0, 0           # set x location to left side
  266.     RIGHT_SIDE_wall_skip:
  267.     jal getLED
  268.     beq $v0, 1, down_pressed    # if LED is red, branch to go down
  269.    
  270.     li $t8, 0           # set last pressed key to right
  271.     li $t4, 0           # character counter (for backtrace)
  272.     li $t3, 0           # load "right" into temp3
  273. right_loop:
  274.     addi $a3, $a3, 1        # increment to next character
  275.     addi $t4, $t4, 1        # increment counter
  276.     lb $t2, ($a3)           # load character into temp2
  277.     sb $t3,($a3)            # load temp3 into character location
  278.     la $t3, ($t2)           # load temp2 into temp3
  279.     beq $t4, 42, right_end      # if end of character line is reached, goto end
  280.     j right_loop            # loop 
  281. right_end:
  282.     sub $a3,$a3,42          # backtrace
  283.     lw $a0, headX           # load headX
  284.     lw $a1, headY           # load headY
  285.     addi $a0, $a0, 1        # move right 1
  286.     bge $a0, 64, RIGHT_SIDE     # check if we go beyond the edge
  287.     j head_move_done        # move done
  288.    
  289.     RIGHT_SIDE:
  290.     li $a0, 0           # set x location to left side
  291.     j head_move_done        # move done
  292.  
  293. left_pressed:  
  294.     beq $t8, 0, skip        # if travelling right, skip left
  295.     beq $t8, 4, skip        # if just started (facing right), skip left
  296.    
  297.     lw $a0, headX           # load headX
  298.     lw $a1, headY           # load headY
  299.     subi $a0, $a0, 1        # move right 1
  300.     blt $a0, $0, LEFT_SIDE_wall # check if we go beyond the edge
  301.     j LEFT_SIDE_wall_skip       # skip side exception  
  302.     LEFT_SIDE_wall:
  303.     li $a0, 63          # set x location to right side
  304.     LEFT_SIDE_wall_skip:
  305.     jal getLED
  306.     beq $v0, 1, up_pressed      # if LED is red, branch to go up
  307.    
  308.     li $t8, 1           # set last pressed key to left
  309.     li $t4, 0           # character counter (for backtrace)
  310.     li $t3, 1           # load "left" into temp3
  311. left_loop:
  312.     addi $a3, $a3, 1        # increment to next character
  313.     addi $t4, $t4, 1        # increment counter
  314.     lb $t2, ($a3)           # load character into temp2
  315.     sb $t3,($a3)            # load temp3 into character location
  316.     la $t3, ($t2)           # load temp2 into temp3
  317.     beq $t4, 42, left_end       # if end of character line is reached, goto end
  318.     j left_loop         # loop 
  319. left_end:
  320.     sub $a3,$a3,42          # backtrace
  321.     lw $a0, headX           # load headX
  322.     lw $a1, headY           # load headY
  323.     subi $a0, $a0, 1        # move left 1
  324.     blt $a0, $0, LEFT_SIDE      # check if we go beyond the edge
  325.     j head_move_done        # move done
  326.    
  327.     LEFT_SIDE:
  328.     li $a0, 63          # set x location to right side
  329.     j head_move_done        # move done
  330.  
  331. up_pressed:
  332.     beq $t8, 3, skip        # if travelling down, skip up
  333.    
  334.     lw $a0, headX           # load headX
  335.     lw $a1, headY           # load headY
  336.     subi $a1, $a1, 1        # move up 1
  337.     blt $a1, $0, DOWN_SIDE_wall # check if we go beyond the top
  338.     j DOWN_SIDE_wall_skip       # skip side exception  
  339.     DOWN_SIDE_wall:
  340.     li $a1, 63          # set y location to bottom row
  341.     DOWN_SIDE_wall_skip:
  342.     jal getLED
  343.     beq $v0, 1, right_pressed   # if LED is red, branch to go right
  344.    
  345.     li $t8, 2           # set last pressed key to up
  346.     li $t4, 0           # character counter (for backtrace)
  347.     li $t3, 2           # load "up" into temp3
  348. up_loop:
  349.     addi $a3, $a3, 1        # increment to next character
  350.     addi $t4, $t4, 1        # increment counter
  351.     lb $t2, ($a3)           # load character into temp2
  352.     sb $t3,($a3)            # load temp3 into character location
  353.     la $t3, ($t2)           # load temp2 into temp3
  354.     beq $t4, 42, up_end     # if end of character line is reached, goto end
  355.     j up_loop           # loop 
  356. up_end:
  357.     sub $a3,$a3,42          # backtrace
  358.     lw $a0, headX           # load headX
  359.     lw $a1, headY           # load head Y
  360.     subi $a1, $a1, 1        # move up 1
  361.     blt $a1, $0, DOWN_SIDE      # check if we go beyond the top
  362.     j head_move_done        # move done
  363.    
  364.     DOWN_SIDE:
  365.     li $a1, 63          # set y location to bottom row
  366.     j head_move_done        # move done
  367.  
  368. down_pressed:
  369.     beq $t8, 2, skip        # if travelling up, skip down
  370.    
  371.     lw $a0, headX           # load headX
  372.     lw $a1, headY           # load headY
  373.     addi $a1, $a1, 1        # move down 1
  374.     bge $a1, 64, UP_SIDE_wall   # check if we go beyond the bottom
  375.     j UP_SIDE_wall_skip     # skip side exception
  376.     UP_SIDE_wall:
  377.     li $a1, 0           # goto top
  378.     UP_SIDE_wall_skip:
  379.     jal getLED
  380.     beq $v0, 1, left_pressed    # if LED is red, branch to go left
  381.    
  382.     li $t8, 3           # set last pressed key to down
  383.     li $t4, 0           # character counter (for backtrace)
  384.     li $t3, 3           # load "down" into temp3
  385. down_loop:
  386.     addi $a3, $a3, 1        # increment to next character
  387.     addi $t4, $t4, 1        # increment counter
  388.     lb $t2, ($a3)           # load character into temp2
  389.     sb $t3,($a3)            # load temp3 into character location
  390.     la $t3, ($t2)           # load temp2 into temp3
  391.     beq $t4, 42, down_end       # if end of character line is reached, goto end
  392.     j down_loop         # loop 
  393. down_end:
  394.     sub $a3,$a3,42          # backtrace
  395.     lw $a0, headX           # get headX
  396.     lw $a1, headY           # get headY
  397.     addi $a1, $a1, 1        # move down 1
  398.     bge $a1, 64, UP_SIDE        # check if we go beyond the bottom
  399.     j head_move_done        # move done
  400.    
  401.     UP_SIDE:
  402.     li $a1, 0           # goto top
  403.     j head_move_done        # move done
  404.  
  405. center_pressed:
  406.     j skip
  407.  
  408. head_move_done:
  409.     sw $a0, headX           # store new headX
  410.     sw $a1, headY           # store new headY
  411.     jal getLED
  412.     beq $v0, 2, game_over       # if color is yellow (snake), branch to game_over
  413.     beq $v0, 3, hit_frog        # if color is green (frog), branch to hit_frog
  414.     beq $v0, 0, hit_nothing     # if color is blank, branch hit_nothing
  415.     j hit_nothing           # catches in case of exceptions
  416.    
  417. hit_frog:
  418.     lw $a0, headX           # get headX
  419.     lw $a1, headY           # get headY
  420.     li $a2, 2           # get yellow
  421.     jal setLED          # set new head LED
  422.     addi $t7, $t7, 1        # add one to score
  423.     beq $t7, 32, game_over      # if score = 32 (all 32 frogs found) branch to game_over
  424.     addi $t9, $t9, 1        # add one to tail length and do not move tail
  425.     j _poll             # go back to poll
  426.    
  427. hit_nothing:
  428.     lw $a0, headX           # load headX
  429.     lw $a1, headY           # load headY
  430.     li $a2, 2           # get yellow
  431.     jal setLED          # set new head LED
  432.     lw $a0, tailX           # load tailX
  433.     lw $a1, tailY           # load tailY
  434.     li $a2, 0           # get blank
  435.     jal setLED          # remove tail LED
  436.     la $a3, dir         # reload
  437.     la $t1, 0($t9)          # get tailSize 
  438.     add $a3, $a3, $t1       # increment to number at tailSize to find direction
  439.     lb $t0, ($a3)           # get number
  440.     beq $t0, 0, tail_right_pressed  # if right
  441.     beq $t0, 1, tail_left_pressed   # if left
  442.     beq $t0, 2, tail_up_pressed # if up
  443.     beq $t0, 3, tail_down_pressed   # if down
  444.     j tail_right_pressed        # catches it from somehow running the j _poll in hit_frog
  445.    
  446.  
  447.  
  448. tail_right_pressed:
  449.     lw $a0, tailX           # load tailX
  450.     lw $a1, tailY           # load tailY
  451.     addi $a0, $a0, 1        # move right 1
  452.     bge $a0, 64, TAIL_RIGHT_SIDE    # check if we go beyond the edge
  453.     j tail_move_done        # move done
  454.    
  455.     TAIL_RIGHT_SIDE:
  456.     li $a0, 0           # set x location to left side
  457.     j tail_move_done        # move done
  458.  
  459. tail_left_pressed:
  460.     lw $a0, tailX           # load tailX
  461.     lw $a1, tailY           # load tailY
  462.     subi $a0, $a0, 1        # move left 1
  463.     blt $a0, $0, TAIL_LEFT_SIDE # check if we go beyond the edge
  464.     j tail_move_done        # move done
  465.    
  466.     TAIL_LEFT_SIDE:
  467.     li $a0, 63          # set x location to right side
  468.     j tail_move_done        # move done
  469.  
  470. tail_up_pressed:
  471.     lw $a0, tailX           # load tailX
  472.     lw $a1, tailY           # load tailY
  473.     subi $a1, $a1, 1        # move up 1
  474.     blt $a1, $0, TAIL_DOWN_SIDE # check if we go beyond the top
  475.     j tail_move_done        # move done
  476.    
  477.     TAIL_DOWN_SIDE:
  478.     li $a1, 63          # set y location to bottom row
  479.     j tail_move_done        # move done
  480.  
  481. tail_down_pressed:
  482.     lw $a0, tailX           # load tailX
  483.     lw $a1, tailY           # load tailY
  484.     addi $a1, $a1, 1        # move down 1
  485.     bge $a1, 64, TAIL_UP_SIDE   # check if we go beyond the bottom
  486.     j tail_move_done        # move done
  487.    
  488.     TAIL_UP_SIDE:
  489.     li $a1, 0           # set y location to top row
  490.     j tail_move_done        # move done
  491.    
  492. tail_move_done:
  493.     sub $a3, $a3, $t1       # backtrace
  494.     sw $a0, tailX           # store new tailX
  495.     sw $a1, tailY           # store new tailY
  496.     j _poll             # go back to poll
  497.    
  498. game_over:
  499.     li $v0, 4
  500.     la $a0, gameOver        # "Game Over" text
  501.     syscall
  502.     la $a0, playTime1       # first part of "Play Time" text
  503.     syscall
  504.     li $v0, 30          # prepare to get system time
  505.     syscall
  506.     lw $a1, startTime           # load start time
  507.     sub $a0, $a0, $a1       # subtract start time from end time
  508.     li $v0, 1
  509.     syscall             # call end time
  510.     li $v0, 4
  511.     la $a0, playTime2       # second part of "Play Time" text
  512.     syscall
  513.     la $a0, score1          # first part of score text
  514.     syscall
  515.     li $v0, 1
  516.     la $a0, ($t7)           # score
  517.     syscall
  518.     li $v0, 4
  519.     la $a0, score2          # second part of score text
  520.     syscall
  521.     li $v0, 10
  522.     syscall
  523.    
  524. setLED: # USED GIVEN CODE
  525.     # void _setLED(int x, int y, int color)
  526.     # 03/11/2012: this version is for the 64x64 LED
  527.     # sets the LED at (x,y) to color
  528.     # color: 0=off, 1=red, 2=orange, 3=green
  529.     # warning:   x, y and color are assumed to be legal values (0-63,0-63,0-3)
  530.     # arguments: $a0 is x, $a1 is y, $a2 is color
  531.     # trashes:   $t0-$t3
  532.     # returns:   none
  533.  
  534.     # byte offset into display = y * 16 bytes + (x / 4)
  535.     sll $t0,$a1,4           # y * 16 bytes
  536.     srl $t1,$a0,2           # x / 4
  537.     add $t0,$t0,$t1         # byte offset into display
  538.     li  $t2,0xffff0008      # base address of LED display
  539.     add $t0,$t2,$t0         # address of byte with the LED
  540.     # now, compute led position in the byte and the mask for it
  541.     andi    $t1,$a0,0x3         # remainder is led position in byte
  542.     neg $t1,$t1             # negate position for subtraction
  543.     addi    $t1,$t1,3           # bit positions in reverse order
  544.     sll $t1,$t1,1           # led is 2 bits
  545.     # compute two masks: one to clear field, one to set new color
  546.     li  $t2,3      
  547.     sllv    $t2,$t2,$t1
  548.     not $t2,$t2             # bit mask for clearing current color
  549.     sllv    $t1,$a2,$t1         # bit mask for setting color
  550.     # get current LED value, set the new field, store it back to LED
  551.     lbu $t3,0($t0)          # read current LED value   
  552.     and $t3,$t3,$t2         # clear the field for the color
  553.     or  $t3,$t3,$t1         # set color field
  554.     sb  $t3,0($t0)          # update display
  555.     jr  $ra
  556.    
  557.  
  558. getLED: # USED GIVEN CODE
  559.     # int _getLED(int x, int y)
  560.     # 03/11/2012: this version is for the 64x64 LED
  561.     # returns the value of the LED at position (x,y)
  562.     # warning:   x and y are assumed to be legal values (0-63,0-63)
  563.     # arguments: $a0 holds x, $a1 holds y
  564.     # trashes:   $t0-$t2
  565.     # returns:   $v0 holds the value of the LED (0, 1, 2, 3)
  566.    
  567.     # byte offset into display = y * 16 bytes + (x / 4)
  568.     sll  $t0,$a1,4              # y * 16 bytes
  569.     srl  $t1,$a0,2              # x / 4
  570.     add  $t0,$t0,$t1            # byte offset into display
  571.     la   $t2,0xffff0008
  572.     add  $t0,$t2,$t0            # address of byte with the LED
  573.     # now, compute bit position in the byte and the mask for it
  574.     andi $t1,$a0,0x3            # remainder is bit position in byte
  575.     neg  $t1,$t1                # negate position for subtraction
  576.     addi $t1,$t1,3              # bit positions in reverse order
  577.         sll  $t1,$t1,1              # led is 2 bits
  578.     # load LED value, get the desired bit in the loaded byte
  579.     lbu  $t2,0($t0)
  580.     srlv $t2,$t2,$t1            # shift LED value to lsb position
  581.     andi $v0,$t2,0x3            # mask off any remaining upper bits
  582.     jr   $ra
  583.    
  584. sleep: 
  585.     li $v0, 32          # Prepare syscall sleep
  586.     li $a0, 200         # For this many milliseconds
  587.     syscall
  588.     jr $ra
  589.    
  590. randinit:               # initialize random
  591.     subi $sp, $sp, 8
  592.     sw $ra, 0($sp)
  593.     sw $s0, 4($sp)
  594.     li $v0, 30
  595.     syscall
  596.     move $s0, $a0
  597.     move $a1, $s0
  598.     li $a0, 1
  599.     li $v0, 40
  600.     syscall
  601.     lw $ra, 0($sp)
  602.     lw $s0, 4($sp)
  603.     addi $sp, $sp, 8
  604.     jr $ra
  605. getRandomNumber:            # get random (I:$a0, O: $t1)
  606.     subi $sp, $sp, 4
  607.     sw $ra, 0($sp)
  608.     move $a1, $a0
  609.     li $a0, 1
  610.     li $v0, 42
  611.     syscall
  612.     move $t1, $a0
  613.     lw $ra, 0($sp)
  614.     addi $sp, $sp, 4
  615.     jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement