-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.asm
More file actions
478 lines (391 loc) · 9 KB
/
Copy pathDisplay.asm
File metadata and controls
478 lines (391 loc) · 9 KB
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
# Display.asm
# This file implements the Display functions.
.globl Display
.data
tempBox: .space 20 # This is a temp box for drawing
.text
# Box Struct
####################################
# Object Structure
# 0-4 The x position
# 4-8 The y position
# 8-12 The width of the box
# 12-16 The height of the box
# 16-20 The color of the box
####################################
# Constructor for the Display Class
#
# Args:
# $a0 The width of the display buffer
# $a1 The height of the display buffer
# $a2 The address of the display buffer
#
# Return:
# $v0 The address of the new instance
#
# Usage:
# li $a0, 8 # the width
# li $a1, 4 # the height
# code to put the address of the display buffer into $a2
# jal Display
# move $t1, $v0
#
####################################
# Object Structure
# 0-4 The drawBox function
# 4-8 The moveBoxFast function
# 8-12 The clearBox function
# 12-16 display width
# 16-20 display height
# 20-24 display buffer address
# 24-28 player box address
# 28-32 computer box address
####################################
Display:
move $t0, $a0 # start of new Display()
move $t1, $a1
move $t2, $a2
# alloc Display
li $v0, 9 # sbrk code
li $a0, 32 # number of bytes needed
syscall
# load methods
la $t3, drawbox
# la $t4, movebox
la $t4, moveboxfast
la $t5, clearBox
# Setup object
sw $t3, 0($v0)
sw $t4, 4($v0)
sw $t5, 8($v0)
sw $t0, 12($v0)
sw $t1, 16($v0)
sw $t2, 20($v0)
jr $ra
# Moves a box on the display fast
#
# Args:
# $a0 The address of the display
# $a1 The address of the box
# $a2 The movement in the x
# $a3 The movement in the y
#
# Return:
# none
#
# Usage:
# code to put the address of display into $a0
# code to put the address of box into $a1
# li $a2, 8 # the dx
# li $a3, 4 # the dy
# lw $t9, 4($a0)
# jalr $t9
#
moveboxfast:
sub $sp, $sp, 4 # allocate stack frame - Start of Display.moveboxfast
sw $ra, 0($sp) # with return address at 0($sp)
# draw background where old box was and is no longer needed
sub $sp, $sp, 20 # allocate stack frame
sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $a3, 12($sp)
# save color
lw $t0, 16($a1)
sw $t0, 16($sp)
moveboxBGY:
# make temp box for y bg
# $t0 tempbox
la $t0, tempBox
lw $t1, 0($a1)
sw $t1, 0($t0)
lw $t1, 4($a1)
sw $t1, 4($t0)
lw $t1, 8($a1)
sw $t1, 8($t0)
lw $t1, 12($a1)
sw $t1, 12($t0)
bltz $a3, moveboxBGAtBottom
# draw background at top (dy>0)
moveboxBGAtTop:
sw $a3, 12($t0)
b moveboxBGYDraw
# draw background at bottom (dy<0)
moveboxBGAtBottom:
lw $t1, 4($t0)
lw $t2, 12($t0)
add $t1, $t1, $t2
add $t1, $t1, $a3
sw $t1, 4($t0)
move $t1, $a3
mul $t1, $t1, -1
sw $t1, 12($t0)
moveboxBGYDraw:
# change color
lw $t1, bgColor
sw $t1, 16($t0)
move $a1, $t0
jal drawbox
lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
moveboxBGYEnd:
moveboxBGX:
# make temp box for x bg
# $t0 tempbox
la $t0, tempBox
lw $t1, 0($a1)
sw $t1, 0($t0)
lw $t1, 4($a1)
add $t1, $t1, $a3
sw $t1, 4($t0)
lw $t1, 8($a1)
sw $t1, 8($t0)
lw $t1, 12($a1)
# add $t1, $t1, $a3
sw $t1, 12($t0)
bltz $a2, moveboxBGAtRight
# draw background at top (dx>0)
moveboxBGAtLeft:
sw $a2, 8($t0)
b moveboxBGXDraw
# draw background at bottom (dx<0)
moveboxBGAtRight:
lw $t1, 0($t0)
lw $t2, 8($t0)
add $t1, $t1, $t2
add $t1, $t1, $a2
sw $t1, 0($t0)
move $t1, $a2
mul $t1, $t1, -1
sw $t1, 8($t0)
moveboxBGXDraw:
# change color
lw $t1, bgColor
sw $t1, 16($t0)
move $a1, $t0
jal drawbox
lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
moveboxBGXEnd:
# reset color
lw $t0, 16($sp)
sw $t0, 16($a1)
# move box
lw $t0, 0($a1) # move x
add $t0, $t0, $a2
sw $t0, 0($a1)
lw $t0, 4($a1) # move y
add $t0, $t0, $a3
sw $t0, 4($a1)
moveboxFGY:
# make temp box
# $t0 tempbox
la $t0, tempBox
lw $t1, 0($a1)
sw $t1, 0($t0)
lw $t1, 4($a1)
sw $t1, 4($t0)
lw $t1, 8($a1)
sw $t1, 8($t0)
lw $t1, 12($a1)
sw $t1, 12($t0)
lw $t1, 16($a1)
sw $t1, 16($t0)
bgtz $a3, moveboxFGAtBottom
# draw background at top (dy<0)
moveboxFGAtTop:
move $t1, $a3
mul $t1, $t1, -1
sw $t1, 12($t0)
b moveboxFGYDraw
# draw background at bottom (dy>0)
moveboxFGAtBottom:
lw $t1, 4($t0)
lw $t2, 12($t0)
add $t1, $t1, $t2
sub $t1, $t1, $a3
sw $t1, 4($t0)
sw $a3, 12($t0)
moveboxFGYDraw:
move $a1, $t0
jal drawbox
lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
moveboxFGYEnd:
moveboxFGX:
# make temp box
# $t0 tempbox
la $t0, tempBox
lw $t1, 0($a1)
sw $t1, 0($t0)
lw $t1, 4($a1)
sw $t1, 4($t0)
lw $t1, 8($a1)
sw $t1, 8($t0)
lw $t1, 12($a1)
sw $t1, 12($t0)
lw $t1, 16($a1)
sw $t1, 16($t0)
bgtz $a2, moveboxFGAtRight
# draw background at left (dy<0)
moveboxFGAtLeft:
move $t1, $a2
mul $t1, $t1, -1
sw $t1, 8($t0)
b moveboxFGXDraw
# draw background at right (dy>0)
moveboxFGAtRight:
lw $t1, 0($t0)
lw $t2, 8($t0)
add $t1, $t1, $t2
sub $t1, $t1, $a2
sw $t1, 0($t0)
sw $a2, 8($t0)
moveboxFGXDraw:
move $a1, $t0
jal drawbox
moveboxFGXEnd:
add $sp, $sp, 20 # and deallocate it
lw $ra, 0($sp) # restore return address
add $sp, $sp, 4 # and deallocate it
jr $ra
# Moves a box on the display
#
# Args:
# $a0 The address of the display
# $a1 The address of the box
# $a2 The movement in the x
# $a3 The movement in the y
#
# Return:
# none
#
# Usage:
# !!!Do not use this method use moveboxfast instead!!!
# code to put the address of display into $a0
# code to put the address of box into $a1
# li $a2, 8 # the x
# li $a3, 4 # the y
# lw $t9, 4($a0)
# jalr $t9
#
movebox:
sub $sp, $sp, 4 # allocate stack frame - Start of Display.movebox
sw $ra, 0($sp) # with return address at 0($sp)
# draw background where old box was
sub $sp, $sp, 20 # allocate stack frame
sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $a3, 12($sp)
# change color
lw $t0, 16($a1)
sw $t0, 16($sp)
li $t1, 0 # !!!HACK!!! this should be the background
sw $t1, 16($a1)
jal drawbox
lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
# reset color
lw $t0, 16($sp)
sw $t0, 16($a1)
add $sp, $sp, 20 # and deallocate it
# move box
lw $t0, 0($a1) # move x
add $t0, $t0, $a2
sw $t0, 0($a1)
lw $t0, 4($a1) # move y
add $t0, $t0, $a3
sw $t0, 4($a1)
jal drawbox
lw $ra, 0($sp) # restore return address
add $sp, $sp, 4 # and deallocate it
jr $ra
# Draws a box on the display
#
# Args:
# $a0 The address of the Display
# $a1 The address of the Box
#
# Return:
# none
#
# Usage:
# code to put the address of Display into $a0
# code to put the address of Box into $a1
# lw $t9, 0($a0)
# jalr $t9
#
drawbox:
lw $t0, 0($a1) # x - start of Display.drawbow(box, display)
lw $t1, 4($a1) # y
lw $t2, 8($a1) # box width
lw $t3, 12($a1) # box height
lw $t4, 12($a0) # display width
lw $t5, 20($a0) # display buffer
lw $t6, 16($a1) # the box color
li $t7, 0 # current x
li $t8, 0 # current y
blt $t2, 1, drawBoxLoopEnd
blt $t3, 1, drawBoxLoopEnd
drawBoxLoop:
# display[y+cury][x+curx] = color
move $t9, $t1 # put posy into index
add $t9, $t9, $t8 # add cury
mul $t9, $t9, $t4 # mul by the width
add $t9, $t9, $t0 # add posx
add $t9, $t9, $t7 # add curx
mul $t9, $t9, 4 # convert from index to bytes
add $t9, $t9, $t5 # add display
sw $t6, ($t9)
add $t7, $t7, 1
blt $t7, $t2, drawBoxLoop
li $t7, 0
add $t8, $t8, 1
blt $t8, $t3, drawBoxLoop
drawBoxLoopEnd:
jr $ra
# Removes a box on the display
#
# Args:
# $a0 The address of the Display
# $a1 The address of the Box
#
# Return:
# none
#
# Usage:
# code to put the address of Display into $a0
# code to put the address of Box into $a1
# lw $t9, 8($a0)
# jalr $t9
#
clearBox:
sub $sp, $sp, 4 # allocate stack frame - Start of Display.movebox
sw $ra, 0($sp) # with return address at 0($sp)
# draw background where box was
sub $sp, $sp, 8 # allocate stack frame
sw $a1, 0($sp)
# save color
lw $t0, 16($a1)
sw $t0, 4($sp)
# change color to bgColor
lw $t1, bgColor
sw $t1, 16($a1)
jal drawbox
# replace old color
lw $t1, 0($sp)
lw $t0, 4($sp)
sw $t0, 16($t1)
add $sp, $sp, 8
lw $ra, 0($sp) # restore return address
add $sp, $sp, 4 # and deallocate it
jr $ra