Advertisement
Guest User

Untitled

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