-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetris.asm
3541 lines (2836 loc) · 93.3 KB
/
tetris.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
# UnB - Universidade de Brasília
# ------------------------------
# Departamento de Ciência da Computação - 2016/2
# Organização e Arquitetura de Computadores - Turma "A"
# Prof. Marcos Vinicius Lamar
# Grupo 3: Breno Xavier Pinto - 13/0103845
# Jefferson Viana Fonseca Abreu - 13/0028959
# Miguel Angelo Montagner Filho - 13/0127302
# Paulo Victor Gonçalves Farias - 13/0144754
# --------------------------------------------------------------------------------------------------------
# Projeto Aplicativo: MIPS Tetris Multiplayer
# -------------------------------------------
# Objetivo: Implementação do clássico jogo Tetris para até 4 jogadores. Usando o kit de desenvolvimento
# DE2-70 e a interface infravermelho.
#
# Conteúdo do arquivo: Grupo3_OAC20162_projeto.zip
# > tetris.asm
# > tetris_code.mif
# > tetris_data.mif
# > relatorioImplementacao.pdf
# > MIPS-PUM-v4.5 (processador uniciclo)
#
# --------------------------------------------------------------------------------------------------------
# tetris.asm
# -------------------------------
# Padrão: O programa, escrito em Assembly MIPS 32, está em conformidade com o uso de registradores du-
# rante a implementação.
#
# Saída: Jogo Tetris implementado para um número de jogadores específico.
#
# Limitações: As peças não rotacionam.
#
# Como compilar e executar: ( na MAIN se $s0 = 0, TECLADO, simulação no MARS, $s0 = 1, simulação na placa)
# No MARS:
# - Assemble file
# - Tools > Bitmap Display Tool
# - Run
# Na DE2-70:
# - Com o Quartus aberto e a placa funcionando com o processador uniciclo.
# - Tools > In-System-Memory Content Editor
# - Atualize UseC e UseD para os arquivos tetris_code e tetris_data
# - Inicie a simulação na placa
# Versão do Mars: 4.5
#
# Foram feitos testes no sistema Linux Mint(64 bits), sendo que a versão entregue compilou na IDE e
# também funcionou na placa.
#########################################################################################################
## CONSTANTS ##
#########################################################################################################
.eqv VGA 0xFF000000 # VGA beginning address
.eqv TAMX 320 # Display width in pixels
.eqv TAMY 240 # Display height in pixels
.eqv PLAYERS_1_MENU_PY 100 # String position on menu for P1
.eqv PLAYERS_2_MENU_PY 130 # String position on menu for P2
.eqv PLAYERS_3_MENU_PY 160 # String position on menu for P3
.eqv PLAYERS_4_MENU_PY 190 # String position on menu for P4
#########################################################################################################
## MEMORY OFFSETS ##
#########################################################################################################
.eqv OFFSET_NUMBER_OF_PLAYERS 0 # 000 - 004
.eqv OFFSET_BOARD_POSITIONS 68 # 068 - 084
.eqv OFFSET_MATRICES 84 # 084 - 5084
.eqv OFFSET_SCORES 5084 # 5084 - 5100
.eqv OFFSET_SPEED_DOWN 5100 # 5100 - 5104
.eqv OFFSET_USER_CLOCK 5104 # 5104 - 5120
.eqv OFFSET_TYPE_CURRENT_PIECE 5120 # 5120 - 5136
.eqv OFFSET_INFO_PIECE 5236 # 5136 - 5456 ({X, Y, Type, Rotation, [4X4]}) * 4 PLAYERS
.eqv OFFSET_INFO_AUX_PIECE 5556 # 5456 - 5536 ({X, Y, Type, Rotation, [4X4]})
.eqv OFFSET_REGISTERED_KEYS 5636 # 5536 - 5600
.eqv OFFSET_PLAYER_ALIVE 5700 # 5700 - 5716
.eqv OFFSET_OF_NEW_SP 5716
#########################################################################################################
## PIECE TYPE ##
#########################################################################################################
# Info piece type
# 0 = Straight Polyomino
# 1 = Square Polyomino
# 2 = T-Polyomino
# 3 = J
# 4 = L
# 5 = S
# 6 = Z
#
# See https://en.wikipedia.org/wiki/Tetromino#One-sided_tetrominoes
#########################################################################################################
## IRDA ##
#########################################################################################################
.eqv IRDA_READ_ADDRESS 0xFFFF0504
#########################################################################################################
## DATA ##
#########################################################################################################
.data
NUM: .float 160.0
SEED: .word 0x00001015 # Used in random function
TETRIS_STRING: .asciiz "TETRIS\n"
PLAYERS_1: .asciiz "1 Jogador\n"
PLAYERS_2: .asciiz "2 Jogadores\n"
PLAYERS_3: .asciiz "3 Jogadores\n"
PLAYERS_4: .asciiz "4 Jogadores\n"
SELECT_OPTION: .asciiz "->\n"
CONFIG_P: .asciiz "Config. das Keys do Jogador "
CONFIG_T0: .asciiz "Escolha a tecla <<"
CONFIG_T1: .asciiz "Escolha a tecla >>"
CONFIG_T2: .asciiz "Escolha a tecla v"
CONFIG_T3: .asciiz "Escolha a tecla rotacao."
NL: .asciiz "\n"
SPACE: .asciiz " "
SCORE_P1: .asciiz "SCORE P1:"
SCORE_P2: .asciiz "SCORE P2:"
SCORE_P3: .asciiz "SCORE P3:"
SCORE_P4: .asciiz "SCORE P4:"
#########################################################################################################
## TETRIS ##
#########################################################################################################
.text
# <-------------------------------------- MAIN: BEGIN -------------------------------------------------->
MAIN:
# Save initial $sp value into $s7, this value will remain unchanged through the game
move $s7, $sp
# Update $sp to its new offset
subi $sp, $sp, OFFSET_OF_NEW_SP
# Save number of users (Default 1)
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
li $t1, 1
sw $t1, ($t0)
# Configura os controles escolhidos se for IrDA
li $s1, 0
li $s2, 1 # indica que esta acontecendo antes da escolha dos jogadores
#jal CONTROL_CONFIG
# Inicializa a tela
la $t0, VGA
li $t1, TAMX
li $t5, 0x00 # cor 0x00000000
# Printa o menu e l� a op��o escolhida, se $s1 = 0 => TECLADO, $s1 = 1 => IRDA
li $s1, 0
# li $s1, 1 <= IRDA
jal PRINT_MENU
jal READ_MENU_OPTION_INPUT
# Erase Screen
li $a0, 0x00
li $v0, 48
syscall
# Configura os controles escolhidos se for IrDA
li $s1, 0
li $s2, 0 # indica que esta acontecendo depois da escolha dos jogadores
#jal CONTROL_CONFIG
jal INIT_MAIN_LOOP
jal PRINT_END_SCORE
li $v0 10
syscall
# <-------------------------------------- MAIN: END -------------------------------------------------->
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#########################################################################################################
## MENU SCREEN ##
#########################################################################################################
# <-------------------------------------- PRINT MENU BEGIN --------------------------------------------->
PRINT_MENU:
# Saves $ra value
addi $sp, $sp, -4
sw $ra, 0($sp)
# Clear screen
li $v0, 48
li $a0, 0x0000
syscall
# Printing strings of init screen
li $v0, 104
la $a0, TETRIS_STRING
li $a1, 135
li $a2, 30
li $a3, 0x00FF
syscall
la $a0, PLAYERS_1
li $a1, 120
li $a2, PLAYERS_1_MENU_PY
syscall
la $a0, PLAYERS_2
li $a1, 120
li $a2, PLAYERS_2_MENU_PY
syscall
la $a0, PLAYERS_3
li $a1, 120
li $a2, PLAYERS_3_MENU_PY
syscall
la $a0, PLAYERS_4
li $a1, 120
li $a2, PLAYERS_4_MENU_PY
syscall
li $a3, 0x00FF
# Prints '->' = option selected
jal PRINT_MENU_OPTION
# Loads $ra and returns
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# <-------------------------------------- PRINT MENU END -------------------------------------------------->
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# <-------------------------------------- PRINT MENU OPTION BEGIN ------------------------------------------->
## Imprime seta que seleciona o jogador
## OFFSET_NUMBER_OF_PLAYERS : posicao Y da seta
## $a3 : Color
PRINT_MENU_OPTION:
addi $sp, $sp, -4
sw $ra, 0($sp)
li $v0, 104
la $a0, SELECT_OPTION
li $a1, 90
# Load number of users
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t1, ($t0)
bne $t1, 1, MENU_OPT_2
li $a2, PLAYERS_1_MENU_PY
j MENU_OUT
MENU_OPT_2:
bne $t1, 2, MENU_OPT_3
li $a2, PLAYERS_2_MENU_PY
j MENU_OUT
MENU_OPT_3:
bne $t1, 3, MENU_OPT_4
li $a2, PLAYERS_3_MENU_PY
j MENU_OUT
MENU_OPT_4:
bne $t1, 4, MENU_OUT
li $a2, PLAYERS_4_MENU_PY
MENU_OUT:
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
## Fim imprime seta que seleciona o jogador
# <-------------------------------------- PRINT MENU OPTION END ------------------------------------------->
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# <-------------------------------------- PRINT MENU OPTION INPUT BEGIN------------------------------------>
### $s0 = 0 => KEYBOARD, $s1 = 1 => IRDA
READ_MENU_OPTION_INPUT:
# Saves $ra
addi $sp, $sp, -4
sw $ra, 0($sp)
# checks to see if using keyboard
beq $s1, $zero, USE_KEYBOARD
# not using keyboard -> using irda
# loads irda address
la $s6, IRDA_READ_ADDRESS
# Pointer to keys allocation in memory
subi $s5, $s7, OFFSET_REGISTERED_KEYS
USE_IRDA:
# Keeps IrDA on loop until it reads something from the receiver address
# receives output at $v1
jal IRDA_GET_KEY_IMM
# Ve se eh um up down ou escolha
DECODE_KEY:
# se for um down
lw $t7,($s5)
bne $t7, $v1, NOT_DOWN_IR
move $v1, $zero
jal PRESS_MENU_DOWN
NOT_DOWN_IR:
# se for um up
lw $t7,-4($s5)
bne $t7, $v1, NOT_UP_IR
move $v1, $zero
jal PRESS_MENU_UP
NOT_UP_IR:
# se for um enter
lw $t7,-8($s5)
beq $t7, $v1, ENTER_IR
move $v1, $zero
#nao eh enter, volta
j USE_IRDA
ENTER_IR:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# Keeps IrDA on loop until it reads something from the receiver addres
# $s6 = Irda_read_address
IRDA_GET_KEY_IMM:
lw $v1, 0($s6) # $s6 = IRDA_READ_ADDRESS
beq $v1, $zero, IRDA_GET_KEY_IMM
jr $ra
# <-------------------------------------- PRINT MENU OPTION INPUT END ------------------------------------>
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# <-------------------------------------- USE KEYBOARD BEGIN ------------------------------------------->
USE_KEYBOARD:
li $v0, 12
syscall # Read Character
addi $a0, $v0, 0
li $t1, 115
bne $t1, $v0, NOT_AN_S
jal PRESS_MENU_DOWN
NOT_AN_S:
li $t1, 119
bne $t1, $v0, NOT_AN_W
jal PRESS_MENU_UP
NOT_AN_W:
li $t1, 10
bne $t1, $v0, NOT_AN_ENTER
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
NOT_AN_ENTER:
j PRESS_MENU_NOTHING
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# <-------------------------------------- USE KEYBOARD END ------------------------------------------->
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# <-------------------------------------- PRESS MENU DOWN BEGIN--------------------------------------->
PRESS_MENU_DOWN:
addi $sp, $sp, -4
sw $ra, 0($sp)
li $a3, 0x0000
jal PRINT_MENU_OPTION
# Load number of users
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t1, ($t0)
move $t0, $t1
bne $t1, 1, PRESS_MENU_DOWN_OP2
li $t0, 2
j PRESS_MENU_DOWN_OUT
PRESS_MENU_DOWN_OP2:
bne $t1, 2, PRESS_MENU_DOWN_OP3
li $t0, 3
j PRESS_MENU_DOWN_OUT
PRESS_MENU_DOWN_OP3:
bne $t1, 3, PRESS_MENU_DOWN_OUT
li $t0, 4
PRESS_MENU_DOWN_OUT:
subi $t1, $s7, OFFSET_NUMBER_OF_PLAYERS
sw $t0, ($t1)
li $a3, 0x00FF
jal PRINT_MENU_OPTION
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# <-------------------------------------- PRESS MENU DOWN END --------------------------------------->
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# <-------------------------------------- PRESS MENU UP BEGIN --------------------------------------->
PRESS_MENU_UP:
addi $sp, $sp, -4
sw $ra, 0($sp)
li $a3, 0x0000
jal PRINT_MENU_OPTION
# Load number of users
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t1, ($t0)
move $t0, $t1
bne $t1, 2, PRESS_MENU_UP_OP2
li $t0, 1
j PRESS_MENU_UP_OUT
PRESS_MENU_UP_OP2:
bne $t1, 3, PRESS_MENU_UP_OP3
li $t0, 2
j PRESS_MENU_UP_OUT
PRESS_MENU_UP_OP3:
bne $t1, 4, PRESS_MENU_UP_OUT
li $t0, 3
PRESS_MENU_UP_OUT:
subi $t1, $s7, OFFSET_NUMBER_OF_PLAYERS
sw $t0, ($t1)
li $a3, 0x00FF
jal PRINT_MENU_OPTION
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# <-------------------------------------- PRESS MENU UP END --------------------------------------->
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# <-------------------------------------- PRESS MENU NOTHING --------------------------------------->
PRESS_MENU_NOTHING:
li $v0, 11
syscall # Write Character
j READ_MENU_OPTION_INPUT
nop
# <-------------------------------------- PRESS MENU NOTHING END ------------------------------------>
#########################################################################################################
## END MENU SCREEN ##
#########################################################################################################
#########################################################################################################
## CONTROL CONFIG ##
#########################################################################################################
# Configures the IrDA keys based on the number of players
CONTROL_CONFIG:
beq $s2, 1, OUT_CONTROL
# savers $ra
addi $sp, $sp, -4
sw $ra, 0($sp)
# Load number of users
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t1, ($t0)
move $s0, $t1
# IrDA Read Address
la $s6, IRDA_READ_ADDRESS
# Pointer to keys allocation in memory
subi $s5, $s7, OFFSET_REGISTERED_KEYS
# Number of players counter
addi $t3, $zero, 1
# Loop: while(players): get keys
LOOP_CONTROL:
# Limpa a tela
li $v0, 48
li $a0, 0x0000
syscall
# PRINT CONFIG STRING + PLAYER NUMBER
li $v0, 104
la $a0, CONFIG_P
li $a1, 10
li $a2, 30
li $a3, 0x00FF
syscall
li $v0, 101
move $a0, $t3
li $a1, 300
li $a2, 30
li $a3, 0x00BB
syscall
# GET KEY 0
li $v0, 104
la $a0, CONFIG_T0
li $a1, 120
li $a2, PLAYERS_1_MENU_PY
li $a3, 0x00FF
syscall
jal GET_KEY
subi $s5, $s5, -4
# GET KEY 1
li $v0, 104
la $a0, CONFIG_T1
li $a1, 120
li $a2, PLAYERS_2_MENU_PY
li $a3, 0x00FF
syscall
jal GET_KEY
subi $s5, $s5, -4
# GET KEY 2
li $v0, 104
la $a0, CONFIG_T2
li $a1, 120
li $a2, PLAYERS_3_MENU_PY
li $a3, 0x00FF
syscall
jal GET_KEY
subi $s5, $s5, -4
# GET KEY 3
li $v0, 104
la $a0, CONFIG_T3
li $a1, 120
li $a2, PLAYERS_4_MENU_PY
li $a3, 0x00FF
syscall
jal GET_KEY
subi $s5, $s5, -4
beq $s0, $t3, END_CONFIG_KEYS
addi $t3, $t3, 1
j LOOP_CONTROL
# Get key = 0 - Teclado
# Get key = 1 - IrDA
GET_KEY:
move $t7, $zero
beq $s1, $t7, TECLADO_GET_KEY
addi $t7, $t7, 1
beq $s1, $t7, IRDA_GET_KEY
# Read character and saves ASCII code
TECLADO_GET_KEY:
li $v0, 12
syscall
# saves value
sw $v0, ($s5)
OUT_CONTROL:
jr $ra
# Keeps IrDA on loop until it reads something from the receiver addres
# $s6 = Irda_read_address
# $s5 = Address to save key
IRDA_GET_KEY:
# Keeps searching until IrDA different than zero
lw $v1, 0($s6) # $s6 = IRDA_READ_ADDRESS
beq $v1, $zero, IRDA_GET_KEY
# IrDA different than zero, save value and return
sw $v1, 0($s5)
move $v1, $zero
jr $ra
END_CONFIG_KEYS:
# Limpa a tela
li $v0, 48
li $a0, 0x0000
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## END CONTROL CONFIG ##
#########################################################################################################
#########################################################################################################
## PRINT PIXEL ##
#########################################################################################################
# $a0 = X
# $a1 = Y
# $a2 = Color
PLOT_PIXEL:
li $v0, 45
syscall
jr $ra
#########################################################################################################
## END PIXEL ##
#########################################################################################################
#########################################################################################################
## PRINT STATIC BOARDS ##
#########################################################################################################
# $a0 = Amount of players
PRINT_STATIC_BOARDS:
addi $sp, $sp, -4
sw $ra, 0($sp)
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t1, ($t0)
li $a2, 0xFF
bne $t1, 1, IF_NOT_1_PLAYER
subi $t0, $s7, OFFSET_BOARD_POSITIONS
li $a0, 125
sw $a0, ($t0)
jal PRINT_ONE_BOARD
IF_NOT_1_PLAYER:
bne $t1, 2, IF_NOT_2_PLAYERS
subi $t0, $s7, OFFSET_BOARD_POSITIONS
li $a0, 65
sw $a0, ($t0)
jal PRINT_ONE_BOARD
subi $t0, $s7, OFFSET_BOARD_POSITIONS
subi $t0, $t0, 4
li $a0, 200
sw $a0, ($t0)
jal PRINT_ONE_BOARD
IF_NOT_2_PLAYERS:
bne $t1, 3, IF_NOT_3_PLAYERS
subi $t0, $s7, OFFSET_BOARD_POSITIONS
li $a0, 30
sw $a0, ($t0)
jal PRINT_ONE_BOARD
subi $t0, $s7, OFFSET_BOARD_POSITIONS
subi $t0, $t0, 4
li $a0, 130
sw $a0, ($t0)
jal PRINT_ONE_BOARD
subi $t0, $s7, OFFSET_BOARD_POSITIONS
subi $t0, $t0, 8
li $a0, 230
sw $a0, ($t0)
jal PRINT_ONE_BOARD
IF_NOT_3_PLAYERS:
bne $t1, 4, IF_NOT_4_PLAYERS
subi $t0, $s7, OFFSET_BOARD_POSITIONS
li $a0, 4
sw $a0, ($t0)
jal PRINT_ONE_BOARD
subi $t0, $s7, OFFSET_BOARD_POSITIONS
subi $t0, $t0, 4
li $a0, 84
sw $a0, ($t0)
jal PRINT_ONE_BOARD
subi $t0, $s7, OFFSET_BOARD_POSITIONS
subi $t0, $t0, 8
li $a0, 164
sw $a0, ($t0)
jal PRINT_ONE_BOARD
subi $t0, $s7, OFFSET_BOARD_POSITIONS
subi $t0, $t0, 12
li $a0, 244
sw $a0, ($t0)
jal PRINT_ONE_BOARD
IF_NOT_4_PLAYERS:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## END STATIC BOARD ##
#########################################################################################################
#########################################################################################################
## PRINT ONE BOARD ##
#########################################################################################################
# $a0 = X position
# $a2 = color
PRINT_ONE_BOARD:
addi $sp, $sp, -4
sw $ra, 0($sp)
li $a1, 50
#for 0 -> 140
li $t0, 0
PRINT_ONE_BOARD_LOOP_1:
jal PLOT_PIXEL
addi $a0, $a0, 72
jal PLOT_PIXEL
addi $a0, $a0, -72
addi $a1, $a1, 1
addi $t0, $t0, 1
bne $t0, 142, PRINT_ONE_BOARD_LOOP_1
#li $a1, 0
#for 0 -> 70
li $t0, 20
PRINT_ONE_BOARD_LOOP_2:
jal PLOT_PIXEL
addi $a1, $a1, -142
jal PLOT_PIXEL
addi $a1, $a1, 142
addi $a0, $a0, 1
addi $t0, $t0, 1
bne $t0, 93, PRINT_ONE_BOARD_LOOP_2
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## END PRINT ONE BOARD ##
#########################################################################################################
#########################################################################################################
## PRINT SQUARE ##
#########################################################################################################
# $a0 = X position
# $a1 = Y position
# $a2 = color
# $a3 = Player {0, 1, 2, 3}
PRINT_SQUARE:
addi $sp, $sp, -4
sw $ra, 0($sp)
mul $a3, $a3, 4
subi $t0, $s7, OFFSET_BOARD_POSITIONS
sub $t0, $t0, $a3
lw $t1, ($t0)
move $t2, $t1 # nesta linha $t2 tem o x indicando o incio do campo de jogo
# offset de x
mul $a0, $a0, 7
add $t2, $t2, $a0
addi $t2, $t2, 2 # Pula dois pixel para mostrar vazio entre dois quadrados
addi $t4, $t2, 6 # end x
#li $t3, 50 #inicio de y
#addi $t5, $t3, 6 # end y
li $t3, 50 # nesta linha $t3 tem o y indicando o incio do campo de jogo
# offset de y
mul $a1, $a1, 7
add $t3, $t3, $a1
addi $t3, $t3, 2 # Pula dois pixel para mostrar vazio entre dois quadrados
move $t6, $t3 #salva valor de $t3 em $t6
addi $t5, $t3, 6 # end y
PRINT_SQUARE_LOOP1:
move $t3, $t6
PRINT_SQUARE_LOOP2:
move $a0, $t2
move $a1, $t3
jal PLOT_PIXEL
addi $t3, $t3, 1
bne $t3, $t5, PRINT_SQUARE_LOOP2
addi $t2, $t2, 1
bne $t2, $t4, PRINT_SQUARE_LOOP1
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## PRINT SQUARE END ##
#########################################################################################################
#########################################################################################################
## INIT MATRICES ##
#########################################################################################################
INIT_MATRICES:
# $t0 position of memory to save color
# $t1 Amount of Players
# $t2 count 0 -> $t1
# $t4 end position of memory to save color
# $t5 color
# $t6 INIT MATRICES OFFSET
subi $t0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t1, ($t0)
li $t2, 0
subi $t0, $s7, OFFSET_MATRICES
INIT_MATRICES_MAIN_LOOP:
subi $t4, $t0, 160
INIT_MATRICES_LOOP2:
li $t5, 0x00
sw $t5, ($t0)
subi $t0, $t0, 1
bne $t0, $t4, INIT_MATRICES_LOOP2
subi $t4, $t0, 800
INIT_MATRICES_LOOP1:
li $t5, 0x00
sw $t5, ($t0)
subi $t0, $t0, 1
bne $t0, $t4, INIT_MATRICES_LOOP1
subi $t4, $t0, 40
INIT_MATRICES_LOOP3:
li $t5, 0xFF
sw $t5, ($t0)
subi $t0, $t0, 1
bne $t0, $t4, INIT_MATRICES_LOOP3
addi $t2, $t2, 1
bne $t1, $t2, INIT_MATRICES_MAIN_LOOP
jr $ra
#########################################################################################################
## END INIT MATRICES ##
#########################################################################################################
#########################################################################################################
## PRINT BOARDS ##
#########################################################################################################
PRINT_BOARDS:
addi $sp, $sp, -4
sw $ra, 0($sp)
addi $sp, $sp, -4
sw $s0, 0($sp)
addi $sp, $sp, -4
sw $s1, 0($sp)
subi $s0, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $s0, ($s0)
li $s1, 0
LOOP_PRINT_BOARDS:
move $a3, $s1
jal PRINT_BOARD
addi $s1, $s1, 1
bne $s1, $s0, LOOP_PRINT_BOARDS
lw $s1, 0($sp)
addi $sp, $sp, 4
lw $s0, 0($sp)
addi $sp, $sp, 4
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## END PRINT BOARDS ##
#########################################################################################################
#########################################################################################################
## PRINT BOARD ##
#########################################################################################################
# $a3 player = {0, 1, 2, 3}
PRINT_BOARD:
addi $sp, $sp, -4
sw $ra, 0($sp)
addi $sp, $sp, -4
sw $s0, ($sp)
addi $sp, $sp, -4
sw $s1, ($sp)
addi $sp, $sp, -4
sw $s2, ($sp)
addi $sp, $sp, -4
sw $s3, ($sp)
addi $sp, $sp, -4
sw $s4, ($sp)
move $s4, $a3
mul $s3, $a3, 1000
subi $s2, $s7, OFFSET_MATRICES
sub $s2, $s2, $s3
li $s0, 0
li $s1, -4
PRINT_BOARD_LOOP1:
li $s0, 0
PRINT_BOARD_LOOP2:
move $a0, $s0
move $a1, $s1
lw $a2, ($s2)
move $a3, $s4
jal PRINT_SQUARE
subi $s2, $s2, 4
addi $s0, $s0, 1
bne $s0, 10, PRINT_BOARD_LOOP2
addi $s1, $s1, 1
bne $s1, 21, PRINT_BOARD_LOOP1
lw $s4, ($sp)
addi $sp, $sp, 4
lw $s3, ($sp)
addi $sp, $sp, 4
lw $s2, ($sp)
addi $sp, $sp, 4
lw $s1, ($sp)
addi $sp, $sp, 4
lw $s0, ($sp)
addi $sp, $sp, 4
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## END PRINT BOARD ##
#########################################################################################################
#########################################################################################################
## INIT SCORE ##
#########################################################################################################
INIT_SCORE:
addi $sp, $sp, -4
sw $ra, 0($sp)
li $t0, 0
subi $t1, $s7, OFFSET_SCORES
sw $t0, ($t1)
subi $t1, $t1, -4
sw $t0, ($t1)
subi $t1, $t1, -4
sw $t0, ($t1)
subi $t1, $t1, -4
sw $t0, ($t1)
subi $t7, $s7, OFFSET_NUMBER_OF_PLAYERS
lw $t7, ($t7)
li $a0, 1
li $a1, 0
jal UPDATE_SCORE
beq $t7, 1, END_INIT
li $a0, 2
li $a1, 0
jal UPDATE_SCORE
beq $t7, 2, END_INIT
li $a0, 3
li $a1, 0
jal UPDATE_SCORE
beq $t7, 3, END_INIT
li $a0, 4
li $a1, 0
jal UPDATE_SCORE
END_INIT:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#########################################################################################################
## END INIT SCORE ##
#########################################################################################################