-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakout.asm
1784 lines (1462 loc) · 44.9 KB
/
breakout.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################ CSC258H1F Fall 2022 Assembly Final Project ##################
# This file contains our implementation of Breakout.
#
# Student 1: Chris Wangzheng Jiang, 1008109574
# Student 2: Yahya Elgabra, 1008553030
######################## Bitmap Display Configuration ########################
# - Unit width in pixels: 4
# - Unit height in pixels: 4
# - Display width in pixels: 256
# - Display height in pixels: 256
# - Base Address for Display: 0x10008000 ($gp)
#
# Note: I wanted to use units of 2, or even 1, but doing so led to memory leaks,
# since the bitmap display was only allocated memory for around 8192 pixels
# before the addresses would overlap with existing data storage.
#
##############################################################################
.data
##############################################################################
# Immutable Data
##############################################################################
# The address of the bitmap display. Don't forget to connect it!
ADDR_DSPL:
.word 0x10008000
# The address of the keyboard. Don't forget to connect it!
ADDR_KBRD:
.word 0xffff0000
# The color of the walls (and top bar)
COLOR_WALLS:
.word 0x00888888
# The thickness (in units) of the top bar
TOP_BAR_THICKNESS:
.word 8
# The thickness (in units) of the side wall
SIDE_WALL_THICKNESS:
.word 2
# The amount of gap (unis) between the top bar and the top-most row of bricks
TOP_GAP_THICKNESS:
.word 8
# The thickness (in units) of a row of bricks
BRICK_ROW_THICKNESS:
.word 2
# The number of brick rows
BRICK_ROW_AMOUNT:
.word 7
# An array containg the possible color that a row of bricks can have,
# is cycled through when drawing the bricks row by row
BRICK_COLORS:
.word 0x007962e0
.word 0x007073c6
.word 0x006184ac
.word 0x00529592
.word 0x0043a678
.word 0x0034b75e
.word 0x0025c844
# Y position of the paddle , this is constant
# (the paddle is 1 unit thick)
PADDLE_Y:
.word 61
# Y position of the second paddle , this is constant
# (the paddle is 1 unit thick)
PADDLE_2_Y:
.word 58
# The width of a single brick: (hardcoded)
# Note that a row of bricks has width of 60 units.
BRICK_WIDTH:
.word 6
##############################################################################
# Mutable Data
##############################################################################
# The movement vector for the ball.
# For each loop, BALL_X += VEC_X and BALL_Y += VEC_Y
# As of now, VEC_X and VEC_Y can only be 1 or -1.
VEC_X:
.word 1
VEC_Y:
.word 1
# The position of the ball (1 unit by 1 unit). Initial value is initial position.
# These 2 variables are dynamic.
BALL_X:
.word 31
BALL_Y:
.word 56
# X position of the paddle, this is dynamic, 2 variables helps with
# collision detection, this also means the length of the paddle is adjustable
PADDLE_X_LEFT:
.word 26
PADDLE_X_RIGHT:
.word 36
# X position of the second paddle, this is dynamic, 2 variables helps with
# collision detection, this also means the length of the paddle is adjustable
PADDLE_2_X_LEFT:
.word 26
PADDLE_2_X_RIGHT:
.word 36
# The player's score, each time a brick is hit the score increments by 1
SCORE:
.word 0
# Keeps track of the players' lives
LIVES:
.word 0x00FF0000
.word 0x00FF0000
.word 0x00FF0000
##############################################################################
# Code
##############################################################################
.text
.globl main # Run the Brick Breaker game.
main:
# Initializing the game:
# Step 1: Draw the top bar and 2 side walls in the game
jal draw_walls # draw_walls() : draw the walls (and top bar) of the game
# Step 2: Draw the bricks (a few colored rows)
jal draw_bricks
# Step 3: Draw the paddle in the initial position
# Step 4: Draw the ball in the initial position
# Step 5: Draw lives and initialize score
la $t0, SCORE
sw $zero, 0($t0)
jal reinitialize
game_loop:
# 1. Check if key has been pressed & which one has been pressed
lw $t0, ADDR_KBRD # t0 = address of the keyboard
lw $t9, 0($t0) # bool t9 = keyboard.isPressed();
beq $t9, 1, keyboard_input # if (t9 == 1): goto keyboard_input
j no_keyboard_input # else: goto no_keyboard_input
keyboard_input: # keyboard input detected
lw $t9, 4($t0) # t9 = ASCII(keyboard.keyPressed());
beq $t9, 113, respond_to_q # key is q: quit
beq $t9, 97, respond_to_a # key is a: move paddle left by 1 unit
beq $t9, 100, respond_to_d # key is d: move paddle right by 1 unit
beq $t9, 44, respond_to_comma # key is ,: move paddle_2 left by 1 unit
beq $t9, 46, respond_to_period # key is .: move paddle_2 right by 1 unit
beq $t9, 112, respond_to_p # key is p: pause game
beq $t9, 114, respond_to_r # key is r: reset game
j end_key_responding # key is invalid, continue as usual
respond_to_q: # Exit the game shamefully
li $v0, 10
syscall
respond_to_a: # Move paddle left by 1 unit (if not at leftmost edge)
jal move_paddle_left
j end_key_responding
respond_to_d: # Move paddle right by 1 unit (if not at rightmost edge)
jal move_paddle_right
j end_key_responding
respond_to_comma: # Move paddle_2 left by 1 unit (if not at leftmost edge)
jal move_paddle_2_left
j end_key_responding
respond_to_period: # Move paddle_2 right by 1 unit (if not at rightmost edge)
jal move_paddle_2_right
j end_key_responding
respond_to_p: # Pause game
jal pause
j end_key_responding
respond_to_r: # Reset game
la $t0, LIVES
li $t1, 0x00FF0000
sw $t1, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
b main
end_key_responding:
nop
no_keyboard_input: # no key pressed, continue as usual
# 2a. Check for collisions (of ball), and, if bump into brick, delete brick
# Method: adjust directional vectors of ball if ball touches edges
# Step 1: Check top of ball : (BALL_X, BALL_Y - 1)
jal collision_top
# Step 2: Check left of ball: (BALL_X - 1, BALL_Y)
jal collision_left
# Step 3: Check right of ball: (BALL_X + 1, BALL_Y)
jal collision_right
# Step 4: Check bottom of ball: (BALL_X, BALL_Y + 1); also check for game-over
jal collision_bottom
# 2b. Update locations (ball)
jal redraw_ball
# 3. Draw the screen (misc updates - do we even have any?)
lw $t8, SCORE
beq $t8, 280, game_over
# 4. Sleep
# eMARS stuff
lw $t8, ADDR_DSPL
li $t9, 0x00888888
sw $t9, 0($t8)
li $v0, 32
li $a0, 50
syscall
#5. Go back to 1
b game_loop
# void pause();
#
# Pauses the game.
# This function uses t0, t9. (unop)
pause:
lw $t0, ADDR_KBRD # t0 = address of the keyboard
lw $t9, 0($t0) # bool t9 = keyboard.isPressed();
beq $t9, 1, pause_input # if (t9 == 1): goto pause_input
j no_pause_input # else: goto no_pause_input
pause_input:
lw $t9, 4($t0) # t9 = ASCII(keyboard.keyPressed());
beq $t9, 112, unpause # key is p: unpause game
beq $t9, 113, respond_to_q # key is q: quit game
beq $t9, 114, reset # key is r: reset game
no_pause_input:
b pause
unpause:
b game_loop
# pauses the game but allows the players to move their paddles and the ball horizontally
launch_pause:
lw $t0, ADDR_KBRD # t0 = address of the keyboard
lw $t9, 0($t0) # bool t9 = keyboard.isPressed();
beq $t9, 1, launch_input # if (t9 == 1): goto launch_input
j no_launch_input # else: goto no_launch_input
launch_input:
lw $t9, 4($t0) # t9 = ASCII(keyboard.keyPressed());
beq $t9, 112, launch # key is p: unpause game
beq $t9, 97, launch_pause_left # key is a: move paddle and ball left by 1 unit
beq $t9, 100, launch_pause_right # key is d: move paddle and ball right by 1 unit
beq $t9, 44, launch_pause_left_2 # key is ,: move paddle_2 and ball left by 1 unit
beq $t9, 46, launch_pause_right_2 # key is .: move paddle_2 and ball right by 1 unit
no_launch_input:
b launch_pause
launch_pause_left:
jal move_ball_left
jal move_paddle_left
b launch_repause
launch_pause_right:
jal move_ball_right
jal move_paddle_right
b launch_repause
launch_pause_left_2:
jal move_ball_left
jal move_paddle_2_left
b launch_repause
launch_pause_right_2:
jal move_ball_right
jal move_paddle_2_right
b launch_repause
launch_repause:
jal erase_ball_paddles
jal draw_paddle
jal draw_paddle_2
jal draw_ball
# eMARS stuff
lw $t8, ADDR_DSPL
li $t9, 0x00888888
sw $t9, 0($t8)
b launch_pause
launch:
b game_loop
# =======================================================================================
# void redraw_ball();
#
# Erases the ball at (BALL_X, BALL_Y) and redraws it at (BALL_X + VEC_X, BALL_Y + VEC_Y)
#
# This function uses t0, t1, t9.
redraw_ball:
# PROLOGUE:
nop
# BODY
lw $t0, BALL_X
lw $t1, BALL_Y
# Get the address of (BALL_X, BALL_Y) using get_address_from_coords
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
move $a0, $t0
move $a1, $t1
jal get_address_from_coords
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ---------------------------------------------
# Now we erase the ball:
li $t9, 0x00000000
sw $t9, 0($v0)
# Now, update the addresses of BALL_X and BALL_Y
lw $t9, VEC_X
la $t2, BALL_X
add $t0, $t0, $t9 # t0 = BALL_X + VEC_X
sw $t0, 0($t2) # BALL_X = BALL_X + VEC_X
lw $t9, VEC_Y
la $t2, BALL_Y
add $t1, $t1, $t9 # t1 = BALL_Y + VEC_Y
sw $t1, 0($t2) # BALL_Y = BALL_Y + VEC_Y
# Get the address of the new (BALL_X, BALL_Y)
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
move $a0, $t0
move $a1, $t1
jal get_address_from_coords
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ---------------------------------------------
# Now we draw the new ball:
li $t9, 0x00ffffff
sw $t9, 0($v0)
# EPILOGUE
jr $ra
# move_ball_left();
# changes the ball to the left by 1 unless it is touching the wall (DOES NOT DRAW)
move_ball_left:
la $t0, BALL_X
lw $t1, 0($t0)
la $t2, SIDE_WALL_THICKNESS
lw $t2, 0($t2) # find where the wall is
beq $t1, $t2, move_ball_left_end # don't subtract 1 if you are already at the edge
addi $t1, $t1, -1
sw $t1, 0($t0)
move_ball_left_end:
jr $ra
# move_ball_right();
# changes the ball to the right by 1 unless it is touching the wall (DOES NOT DRAW)
move_ball_right:
la $t0, BALL_X
lw $t1, 0($t0)
la $t2, SIDE_WALL_THICKNESS
lw $t2, 0($t2)
li $t3, 63
sub $t2, $t3, $t2 # find where the wall is
beq $t1, $t2, move_ball_right_end # don't add 1 if you are already at the edge
addi $t1, $t1, 1
sw $t1, 0($t0)
move_ball_right_end:
jr $ra
# =======================================================================================
# void game_over();
#
# Game over! Play sound, screen turns to black, waiting on reset
game_over:
# sound stuff
li $a0, 63
li $a1, 1000
li $a2, 120
li $a3, 100
li $v0, 31
syscall
# black screen
li $a0, 0
addi $sp, $sp, -16
sw $zero, 0($sp)
sw $zero, 4($sp)
li $t0, 63
sw $t0, 8($sp)
sw $t0, 12($sp)
jal draw_rectangle
# eMARS stuff
lw $t8, ADDR_DSPL
li $t9, 0
sw $t9, 0($t8)
wait_for_reset:
lw $t0, ADDR_KBRD # t0 = address of the keyboard
lw $t9, 0($t0) # bool t9 = keyboard.isPressed();
beq $t9, 1, reset_input # if (t9 == 1): goto reset_input
j no_reset_input # else: goto no_reset_input
reset_input:
lw $t9, 4($t0) # t9 = ASCII(keyboard.keyPressed());
beq $t9, 114, reset # key is r: reset game
no_reset_input:
b wait_for_reset
reset:
b main
# void reduce_hp();
#
# remove 1 hp, retry if more than 1 hp, else, game over
# mutates $t0, $t1, $t8, $t9
reduce_hp:
la $t0, LIVES
lw $t1, 4($t0)
beq $t1, 0, third_death
lw $t1, 8($t0)
beq $t1, 0, second_death
first_death: # change the color of the 3rd heart to black then reinitialize
li $t1, 0
sw $t1, 8($t0)
b reinitialize
second_death: # change the color of the 2nd heart to black then reinitialize
li $t1, 0
sw $t1, 4($t0)
reinitialize: # resets ball and paddles positions, redraw them and lives
la $t0, BALL_X
li $t1, 31
sw $t1, 0($t0)
la $t0, BALL_Y
li $t1, 56
sw $t1, 0($t0)
la $t0, PADDLE_X_LEFT
li $t1, 26
sw $t1, 0($t0)
la $t0, PADDLE_X_RIGHT
li $t1, 36
sw $t1, 0($t0)
la $t0, PADDLE_2_X_LEFT
li $t1, 26
sw $t1, 0($t0)
la $t0, PADDLE_2_X_RIGHT
li $t1, 36
sw $t1, 0($t0)
jal erase_ball_paddles
jal draw_paddle
jal draw_paddle_2
jal draw_ball
jal draw_lives
# eMARS stuff
lw $t8, ADDR_DSPL
li $t9, 0x00888888
sw $t9, 0($t8)
b launch_pause # prepare launch
third_death: # change the color of the 1st heart to black, game over
li $t1, 0
sw $t1, 0($t0)
jal draw_lives
# eMARS stuff
lw $t8, ADDR_DSPL
li $t9, 0x00888888
sw $t9, 0($t8)
b game_over
# void erase_ball_paddles();
#
# cleans the area between the walls and under the bricks
# mutates $a0, $t0, $1, $t2, $t3, $t4, $t5
erase_ball_paddles:
# PROLOGUE:
nop
# BODY:
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
li $a0, 0
la $t0, SIDE_WALL_THICKNESS
lw $t0, 0($t0) # x_start = SIDE_WALL_THICKNESS
li $t3, 63 # y_end = 63
sub $t2, $t3, $t0 # x_end = 63 - SIDE_WALL_THICKNESS
la $t4 TOP_BAR_THICKNESS
lw $t5, 0($t4)
move $t1, $t5
la $t4 TOP_GAP_THICKNESS
lw $t5, 0($t4)
add $t1, $t1, $t5
la $t4 BRICK_ROW_THICKNESS
lw $t5, 0($t4)
la $t4 BRICK_ROW_AMOUNT
lw $t4, 0($t4)
mult $t5, $t4
mflo $t4 # y_start = TOP_BAR_THICKNESS + TOP_GAP_THICKNESS +
add $t1, $t1, $t4 # BRICK_ROW_THICKNESS * BRICK_ROW_AMOUNT
# draw_rectangle parameters
addi $sp, $sp, -16
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
jal draw_rectangle
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ----------------------------------------------
# EPILOGUE:
jr $ra
# =======================================================================================
# void collision_bottom();
#
# Collision for the bottom of the ball (BALL_X, BALL_Y + 1).
# First, if BALL_Y + 1 == 64, then game over, otherwise:
# if it's a brick (i.e. not a paddle and not a wall), delete the corresponding brick.
#
# This function uses t0, t1, t9.
collision_bottom:
# PROLOGUE:
nop
# BODY:
lw $t0, BALL_X
lw $t1, BALL_Y
addi $t1, $t1, 1 # (t0,t1) = (BALL_X , BALL_Y + 1)
# Check for game-over conditions.
beq $t1, 64, reduce_hp
# Obtain the address for (BALL_X, BALL_Y + 1) with get_address_from_coords
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
move $a0, $t0
move $a1, $t1
jal get_address_from_coords
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ----------------------------------------------
lw $t9, 0($v0) # Get color from address
beq $t9, 0x00000000, collision_bottom_end
lw $t0, COLOR_WALLS
beq $t9, $t0, collision_bottom_bounce
beq $t9, 0x00ffffff, collision_bottom_bounce
j collision_bottom_brick # If not those colors, then it's a brick:
collision_bottom_bounce: # Flip the sign of VEC_Y
# Call function play_sound(): ----------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
la $t0, VEC_Y
lw $t1, VEC_Y
sub $t1, $0, $t1
sw $t1, 0($t0)
j collision_bottom_end
collision_bottom_brick:
# Call functions play_sound() and update_score(): ------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
jal update_score
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
# Flip the sign of VEC_Y
la $t0, VEC_Y
lw $t1, VEC_Y
sub $t1, $0, $t1
sw $t1, 0($t0)
# Call function change_brick_at_pos(BALL_X, BALL_Y + 1): ----------
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $a0, BALL_X
lw $a1, BALL_Y
addi $a1, $a1, 1 # (a0,a1) = (BALL_X, BALL_Y + 1)
jal change_brick_at_pos
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
collision_bottom_end:
# EPILOGUE:
jr $ra
# =======================================================================================
# void collision_right();
#
# Collision for the right of the ball (BALL_X + 1, BALL_Y).
# And if it's a brick (i.e. not a paddle and not a wall), delete the corresponding brick.
#
# This function uses t0, t1, t9.
collision_right:
# PROLOGUE:
nop
# BODY:
lw $t0, BALL_X
lw $t1, BALL_Y
addi $t0, $t0, 1 # (t0,t1) = (BALL_X + 1, BALL_Y)
# Obtain the address for (BALL_X + 1, BALL_Y) with get_address_from_coords
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
move $a0, $t0
move $a1, $t1
jal get_address_from_coords
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ----------------------------------------------
lw $t9, 0($v0) # Get color from address
beq $t9, 0x00000000, collision_right_end
lw $t0, COLOR_WALLS
beq $t9, $t0, collision_right_bounce
beq $t9, 0x00ffffff, collision_right_bounce
j collision_right_brick # If not those colors, then it's a brick:
collision_right_bounce: # Flip the sign of VEC_X
# Call function play_sound(): ------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
la $t0, VEC_X
lw $t1, VEC_X
sub $t1, $0, $t1
sw $t1, 0($t0)
j collision_right_end
collision_right_brick:
# Call functions play_sound() and update_score(): -----------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
jal update_score
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
# Flip the sign of VEC_X
la $t0, VEC_X
lw $t1, VEC_X
sub $t1, $0, $t1
sw $t1, 0($t0)
# Call function change_brick_at_pos(BALL_X + 1, BALL_Y): ----------
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $a0, BALL_X
lw $a1, BALL_Y
addi $a0, $a0, 1 # (a0,a1) = (BALL_X + 1, BALL_Y)
jal change_brick_at_pos
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
collision_right_end:
# EPILOGUE:
jr $ra
# =======================================================================================
# void collision_left();
#
# Collision for the left of the ball (BALL_X - 1, BALL_Y).
# And if it's a brick (i.e. not a paddle and not a wall), delete the corresponding brick.
#
# This function uses t0, t1, t9.
collision_left:
# PROLOGUE:
nop
# BODY:
lw $t0, BALL_X
lw $t1, BALL_Y
addi $t0, $t0, -1 # (t0,t1) = (BALL_X - 1, BALL_Y)
# Obtain the address for (BALL_X - 1, BALL_Y) with get_address_from_coords
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
move $a0, $t0
move $a1, $t1
jal get_address_from_coords
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ----------------------------------------------
lw $t9, 0($v0) # Get color from address
beq $t9, 0x00000000, collision_left_end
lw $t0, COLOR_WALLS
beq $t9, $t0, collision_left_bounce
beq $t9, 0x00ffffff, collision_left_bounce
j collision_left_brick # If not those colors, then it's a brick:
collision_left_bounce: # Flip the sign of VEC_X
# Call function play_sound(): ------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
la $t0, VEC_X
lw $t1, VEC_X
sub $t1, $0, $t1
sw $t1, 0($t0)
j collision_left_end
collision_left_brick:
# Call functions play_sound() and update_score(): -----------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
jal update_score
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
# Flip the sign of VEC_X
la $t0, VEC_X
lw $t1, VEC_X
sub $t1, $0, $t1
sw $t1, 0($t0)
# Call function change_brick_at_pos(BALL_X - 1, BALL_Y): ----------
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $a0, BALL_X
lw $a1, BALL_Y
addi $a0, $a0, -1 # (a0,a1) = (BALL_X - 1, BALL_Y)
jal change_brick_at_pos
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
collision_left_end:
# EPILOGUE:
jr $ra
# =======================================================================================
# void collision_top();
#
# Collision for the top of the ball (BALL_X, BALL_Y - 1).
# And if it's a brick (i.e. not a paddle and not a wall), delete the corresponding brick.
#
# This function uses t0, t1.
collision_top:
# PROLOGUE:
nop
# BODY:
lw $t0, BALL_X
lw $t1, BALL_Y
addi $t1, $t1, -1 # (t0,t1) = (BALL_X, BALL_Y - 1)
# Obtain the address for (BALL_X, BALL_Y - 1) with get_address_from_coords
# Function call: ------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
move $a0, $t0
move $a1, $t1
jal get_address_from_coords
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ----------------------------------------------
lw $t9, 0($v0) # Get color from address
beq $t9, 0x00000000, collision_top_end
lw $t0, COLOR_WALLS
beq $t9, $t0, collision_top_bounce
beq $t9, 0x00ffffff, collision_top_bounce
j collision_top_brick # If not those colors, then it's a brick:
collision_top_bounce: # Flip the sign of VEC_Y
# Call function play_sound(): ------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
la $t0, VEC_Y
lw $t1, VEC_Y
sub $t1, $0, $t1
sw $t1, 0($t0)
j collision_top_end
collision_top_brick:
# Call functions play_sound() and update_score(): -----------------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal play_sound
jal update_score
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
# Flip the sign of VEC_Y
la $t0, VEC_Y
lw $t1, VEC_Y
sub $t1, $0, $t1
sw $t1, 0($t0)
# Call function change_brick_at_pos(BALL_X, BALL_Y - 1): ----------
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $a0, BALL_X
lw $a1, BALL_Y
addi $a1, $a1, -1 # (a0,a1) = (BALL_X, BALL_Y - 1)
jal change_brick_at_pos
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
collision_top_end:
# EPILOGUE:
jr $ra
# =======================================================================================
# void play_sound();
# Produces sound when a collision is detected
# mutates a0, a1, a2, a3, v0
play_sound:
# PROLOGUE:
nop
# BODY:
li $a0, 63
li $a1, 500
li $a2, 121
li $a3, 100
li $v0, 31
syscall
# EPILOGUE:
jr $ra
# void update_score();
# Increments score by 1 when a brick is hit
# mutates $t0, $t1
update_score:
# PROLOGUE:
nop
# BODY:
la $t1, SCORE
lw $t0, 0($t1)
addi $t0, $t0, 1
sw $t0, 0($t1)
# EPILOGUE:
jr $ra
# =======================================================================================
# void change_brick_at_pos(int X, int Y);
#
# Changes the brick's color that contains the corresponding pixel (X,Y).
# Parameters:
# a0 = X ; a1 = Y
# This function uses t0.
change_brick_at_pos:
# PROLOGUE:
nop
# BODY
# Step 0: get next color (realized that this is large enough to be a helper function too late)
addi $sp, $sp, -16
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
srl $t1, $t9, 16
sll $t1, $t1, 16 # $t1 is R
srl $t2, $t9, 8
sll $t2, $t2, 24
srl $t2, $t2, 16 # $t2 is G
sll $t3, $t9, 24
srl $t3, $t3, 24 # $t3 is B
li $t0, 0x000F0000
sub $t1, $t1, $t0 # $t1 is next R
addi $t2, $t2, 0x00001100 # $t2 is next G
li $t0, 0x0000001a
sub $t3, $t3, $t0 # $t3 is next B
add $a2, $t1, $t2
add $a2, $a2, $t3 # $a2 is next color
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
addi $sp, $sp, 16
# Call function check_color(ignore, ignore, color COLOR): --------
addi $sp, $sp, -4
sw $ra, 0($sp)
jal check_color
lw $ra, 0($sp)
add $sp, $sp, 4
# Function call complete ------------------------------------------
# Step 1: change X, Y to their relative positions
# (X - SIDE_WALL_TKNS, Y - TOP_BAR_TKNS - TOP_GAP_TKNS)
lw $t0, SIDE_WALL_THICKNESS
sub $a0, $a0, $t0
lw $t0, TOP_BAR_THICKNESS
sub $a1, $a1, $t0
lw $t0, TOP_GAP_THICKNESS
sub $a1, $a1, $t0
# Step 2: X = (X / BRICK_WIDTH) * BRICK_WIDTH ;
# Y = (Y / BRICK_ROW_THICKNESS) * BRICK_ROW_THICKNESS ;