forked from wjy-yy/cpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diy.asm
364 lines (344 loc) · 7.39 KB
/
diy.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
start:
addi x11, x0, 0 # i row
addi x12, x0, 0 # j col
addi x13, x0, 8
addi x14, x0, 6
addi x15, x0, 1400 #used
addi x16, x0, 2000 #color
sw x0, -16(x16)
addi x17, x17, 0xf
sw x17, 192(x16)
#clear 600
addi x17, x0, 0 #flag
addi x18, x0, 0 #count
addi x10, x0, 1
addi x21, x0, 0 #nowx row, need to save
addi x22, x0, 0 #nowy col, need to save
lui x20, 0xdffff
ori x20, x0, 0x0 #button info
#lui x20, 0xdfff0 #vram
# led/score: x9
#2000 is nowx, 2004 is nowy
#2008 is yellow
addi x28, x0, 6
sw x28, 0(x16)
addi x28, x0, 7
sw x28, 4(x16)
addi x28, x0, 4
sw x28, 8(x16)
addi x28, x0, 4
sw x28, 12(x16)
addi x28, x0, 6
sw x28, 16(x16)
addi x28, x0, 6
sw x28, 20(x16)
addi x28, x0, 5
sw x28, 24(x16)
addi x28, x0, 7
sw x28, 28(x16)
addi x28, x0, 4
sw x28, 32(x16)
addi x28, x0, 4
sw x28, 36(x16)
addi x28, x0, 5
sw x28, 40(x16)
addi x28, x0, 4
sw x28, 44(x16)
addi x28, x0, 7
sw x28, 48(x16)
addi x28, x0, 5
sw x28, 52(x16)
addi x28, x0, 7
sw x28, 56(x16)
addi x28, x0, 6
sw x28, 60(x16)
addi x28, x0, 6
sw x28, 64(x16)
addi x28, x0, 5
sw x28, 68(x16)
addi x28, x0, 4
sw x28, 72(x16)
addi x28, x0, 4
sw x28, 76(x16)
addi x28, x0, 5
sw x28, 80(x16)
addi x28, x0, 5
sw x28, 84(x16)
addi x28, x0, 7
sw x28, 88(x16)
addi x28, x0, 7
sw x28, 92(x16)
addi x28, x0, 6
sw x28, 96(x16)
addi x28, x0, 7
sw x28, 100(x16)
addi x28, x0, 4
sw x28, 104(x16)
addi x28, x0, 4
sw x28, 108(x16)
addi x28, x0, 6
sw x28, 112(x16)
addi x28, x0, 6
sw x28, 116(x16)
addi x28, x0, 5
sw x28, 120(x16)
addi x28, x0, 7
sw x28, 124(x16)
addi x28, x0, 4
sw x28, 128(x16)
addi x28, x0, 4
sw x28, 132(x16)
addi x28, x0, 5
sw x28, 136(x16)
addi x28, x0, 4
sw x28, 140(x16)
addi x28, x0, 7
sw x28, 144(x16)
addi x28, x0, 5
sw x28, 148(x16)
addi x28, x0, 7
sw x28, 152(x16)
addi x28, x0, 6
sw x28, 156(x16)
addi x28, x0, 6
sw x28, 160(x16)
addi x28, x0, 5
sw x28, 164(x16)
addi x28, x0, 4
sw x28, 168(x16)
addi x28, x0, 4
sw x28, 172(x16)
addi x28, x0, 5
sw x28, 176(x16)
addi x28, x0, 5
sw x28, 180(x16)
addi x28, x0, 7
sw x28, 184(x16)
addi x28, x0, 7
sw x28, 188(x16)
addi x9, x0, 0
work:
# flush the used array
loop1:
#addi x15, x0, 1400
addi x12, x0, 0
loop2:
sw x0, 0(x15)
addi x15, x15, 4
addi x12, x12, 1
bne x12, x14, loop2
addi x11, x11, 1
bne x11, x13, loop1
addi x8, x0, 0
ctrl:
addi x28, x0, 2000
sw x21, -8(x28)
sw x22, -12(x28)
lui x20, 0xdffff
lw x19, 0(x20) #button info
beq x8, x19, ctrl
addi x8, x19, 0
sw x0, 0(x20)
lui x20, 0xcffff
sw x9, 0(x20)
addi x23, x0, 2 #up
addi x24, x0, 5 #left
addi x25, x0, 6 #ok
addi x26, x0, 7 #right
addi x27, x0, 10#down
addi x28, x0, 2000
beq x19, x24, left
beq x19, x23, up
beq x19, x25, solve
beq x19, x26, right
beq x19, x27, down
beq x0, x0, ctrl
left:
beq x22, x0, ctrl
addi x22, x22, -1
sw x22, -12(x28)
jal x1, ctrl
up:
beq x21, x0, ctrl
addi x21, x21, -1
sw x21, -8(x28)
jal x1, ctrl
right:
addi x28, x22, 1
beq x14, x28, ctrl
addi x22, x22, 1
sw x22, -12(x28)
jal x1, ctrl
down:
addi x28, x21, 1
beq x13, x28, ctrl
addi x21, x21, 1
sw x21, -8(x28)
jal x1, ctrl
mut: #x28*x29
beq x29, x0, end4
addi x30, x0, 0
addi x31, x0, 0
loop:
add x31, x31, x28
addi x30, x30, 1
blt x30, x29, loop
end4:
jalr x0, x1, 0
solve:
addi x16, x0, 2000
addi x28, x21, 0 # row num
addi x29, x12, 0 # col count
jal x1, mut # x31 is x28*x29
add x28, x31, x22 # x28 = the order of the block, x28 needs to multiply
add x28, x28, x22
add x28, x28, x22
add x28, x28, x22
addi x15, x0, 1400
add x15, x15, x28 # x15 is the used (used=0: it is still there)
lw x30, 0(x15)
bne x30, x0, ctrl# x28 is offset
#addi x29, x0, 4
#jal x1, mut # x31 is 4*offset
#add x16, x16, x31
add x16, x16, x28
lw x29, 0(x16) # x29 is the color
addi x17, x0, 1 # flag=1
addi x15, x0, 1600 # clear
add x15, x15, x28
#add x15, x15, x31
sw x10 0(x15)
addi x8, x15, 0
addi x26, x0, 1 # count of joint blocks
jal x1, search
bne x26, x10, clear
#addi x15, x15, 400
sw x0, 0(x8) # clear[now] is 0
clear:
addi x28, x26, 0
addi x29, x26, 0
jal x1, mut
add x9, x9, x31 # the score add to x^2
# after search, if x26 is 1, recover, else, clear.
# while(flag) for(i=0 to 6)for(j=0 to 5) if(down is empty) down
# after that clear are set to 0
while:
addi x17, x0, 0 #flag
addi x11, x0, 0 #i
addi x13, x13, -1 ######
addi x16, x0, 1600
addi x15, x0, 2000 #color
addi x26, x0, 2000
addi x12, x0, 0 #j
loop7:
addi x12, x12, 1 #j
addi x26, x26, 4 #color of nextline
bne x12, x14, loop7
loop5:
addi x12, x0, 0 #j
loop6:
lw x23, 0(x16)
beq x23, x10, end2 # this block is clear
addi x28, x14, 0
addi x29, x0, 4
jal x1, mut
add x25, x16, x31 # clear of nextline
lw x24, 0(x25)
beq x24, x0, end2
lw x24, 0(x15)
sw x24, 0(x26)#mv color
sw x0, 0(x15)
sw x10, 0(x16)#clear=1
sw x0, 0(x25) #clear=0
addi x17, x0, 1
end2:
addi x12, x12, 1
addi x16, x16, 4
addi x15, x15, 4
addi x26, x26, 4
bne x12, x14, loop6
addi x11, x11, 1
bne x11, x13, loop5
bne x17, x0, while
addi x13, x13, 1 ######
# update used with clear
addi x15, x0, 1600 #clear, -200=used
addi x16, x0, 2000
addi x11, x0, 0
loop8:
addi x12, x0, 0
loop9:
lw x26, 0(x15)
beq x26, x0, end3
sw x10, -200(x15)
sw x0, 0(x15)
sw x0, 0(x16)
end3:
addi x12, x12, 1
addi x15, x15, 4
addi x16, x16, 4
bne x12, x14, loop9
addi x11, x11, 1
bne x11, x13, loop8
beq x0, x0, ctrl
search:
addi x17, x0, 0 # flag
addi x11, x0, 0 # i
addi x15, x0, 2000 # color
addi x16, x0, 1600 # clear
loop3:
addi x12, x0, 0
loop4:
lw x23, 0(x15)
bne x29, x23, end1
lw x23, 0(x16) #x16 is the address of clear
bne x23, x0, end1 # if clear also skip
beq x12, x0, tt2 # if j=0 skip
tt1:#left
beq x12, x0, tt2
lw x24, -4(x16)
beq x24, x0, tt2
sw x24, 0(x16)
addi x17, x0, 1
addi x26, x26, 1
beq x11, x0, tt3
tt2:#up
sub x25, x16, x14 # -row number
sub x25, x25, x14 # -row number
sub x25, x25, x14 # -row number
sub x25, x25, x14 # -row number
lw x24, 0(x25) #-6(x16)
beq x24, x0, tt3
sw x24, 0(x16)
addi x17, x0, 1
addi x26, x26, 1
addi x25, x11, 1
beq x25, x13, tt4
tt3:#down
add x25, x16, x14
add x25, x25, x14
add x25, x25, x14
add x25, x25, x14
lw x24, 0(x25)
beq x24, x0, tt4
sw x24, 0(x16)
addi x17, x0, 1
addi x26, x26, 1
addi x25, x12, 1
beq x25, x14, end1
tt4:
lw x24, 4(x16)
beq x24, x0, end1
sw x24, 0(x16)
addi x17, x0, 1
addi x26, x26, 1
end1:
addi x15, x15, 4
addi x16, x16, 4
addi x12, x12, 1
bne x12, x14, loop4
addi x11, x11, 1
bne x11, x13, loop3
bne x17, x0, search
jalr x0, x1, 0
# after searching, all the blocks that should be cleared is set to 1