-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinner.s
executable file
·394 lines (273 loc) · 6.51 KB
/
spinner.s
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
.equ JP1, 0xFF200060
.equ TIMER2, 0xFF202020 #the big timer for the whole game
.equ off, 0xFFFFFFFF
.equ FULLOFF, 0XFFF5555
.equ onR, 0xFFFFFFFC #FORAWRD, ON
.equ Timer1, 0xFF202000
.equ INIT_STATE, 0xACE10000 #inital num to generate random number
.equ MIN_TIME, 1000000000
.equ MAX_TIME, 32766
.equ WHOLE_SECONDS, 20
#.equ GAME_TURNS, 5
#moderate speed
.equ MOD_REV_TIME,4500000 #300000000 #400000000 #4000000000 #try one extra 0, 2000
.equ MOD_STOP_TIME, 20000000 #179000000 #1790000000#160000000 #600
# SLOW
.equ SLOW_REV_TIME, 4300000 #300000000 #400000000 #4000000000 #1000
.equ SLOW_STOP_TIME, 20000000 #250000000 #2500000000 #160000000 #600
#VERY SLOW
.equ VERY_SLOW_REV_TIME,4050000 #400000000 #4000000000 #1000
.equ VERY_SLOW_STOP_TIME,20000000 #350000000 #3000000000 #6000
#almost stop
.equ ALMOST_STOP_REV_TIME,4000000 #400000000 #4000000000 #20
.equ ALMOST_STOP_STOP_TIME,20000000 #450000000 #3500000000 #need to call delay_time 9 times!
#phase 400000000
.equ PHASE4_SPIN_TIME, 2000000
.equ PHASE4_STOP_TIME, 20000000
.section .text
.global spinner
spinner:
subi sp, sp, 4
stw ra, 0(sp)
# stw r2, 4(sp)
# stw r4, 8(sp)
# stw r5, 12(sp)
# #stw r6, 20(sp)
# stw r7, 16(sp)
# stw r8, 20(sp)
# stw r9, 24(sp)
# stw r10, 28(sp)
# stw r11, 32(sp)
# stw r12, 36(sp)
# #stw r14, 52(sp)
# #stw r15, 56(sp)
# stw r16, 40(sp)
# stw r17, 44(sp)
# stw r18, 48(sp)
mov r4, r0
mov r2, r0
mov r5, r0
mov r7, r0
mov r8, r0
mov r9, r0
mov r10, r0
mov r11, r0
mov r12, r0
mov r16, r0
mov r17, r0
mov r18, r0
movia r4, INIT_STATE #very first num, set
#movia r12, 10000
#movi r13, 1000 #slow down speed factor ########motor has a buzz, prob need to adjust the stop time
INIT:
movia r4, INIT_STATE #very first num, set
movia r8, JP1
movia r9, 0x07F557FF #initialize directions
stwio r9, 4(r8)
movia r9, off
stwio r9, 0(r8)
DECIDE:
beq r7, r0, PHASE0
movi r8, 1
beq r7, r8, PHASE1
movi r8, 2
beq r7, r8, PHASE2
movi r8, 3
beq r7, r8, PHASE3
movi r8, 4
beq r7, r8, PHASE4
movi r8, 5
beq r7, r8, DONE
PHASE0:
movia r11, MOD_REV_TIME
movia r10, MOD_STOP_TIME
movia r4, INIT_STATE #GIVES inital value to generate rand
#addi sp, sp, -4
# stw ra, 0(sp)
call lsfr
# ldw ra, 0(sp)
# addi sp, sp, 4
mov r5, r2 #stroe the rand in
mov r12, r2 #also in r12
#addi r7, r7, 1
# START THE BIG TIMER
movia r16, TIMER2
mov r12, r5
#slli r5, r5, 16
mov r17, r5 #TIME_CNT 4,200,000,000
stwio r17, 8(r16) #lower bit
srli r12, r12, 16
mov r17, r12
stwio r17, 12(r16) #upper bit
stwio r0, 0(r16)
movi r18, 0b0100
stwio r18, 4(r16)
br SPIN
PHASE1:
movia r11, SLOW_REV_TIME
movia r10, SLOW_STOP_TIME
mov r4, r2 #continue using the result as an argument
# addi sp, sp, -4
# stw ra, 0(sp)
call lsfr
# ldw ra, 0(sp)
# addi sp, sp, 4
mov r5, r2 #stroe the rand in r4
mov r12, r2 #also in r4
addi r7, r7, 1
# # #START THE BIG TIMER
movia r16, TIMER2
# # #hard code test movia r5, 1000000000
mov r12, r5
slli r5, r5, 16
mov r17, r5 #TIME_CNT 4,200,000,000
stwio r17, 8(r16) #lower bit
srli r12, r12, 16
mov r17, r12
stwio r17, 12(r16) #upper bit
stwio r0, 0(r16)
movi r18, 0b0100
stwio r18, 4(r16)
br SPIN
PHASE2:
movia r11, VERY_SLOW_REV_TIME
movia r10, VERY_SLOW_STOP_TIME
mov r4, r2
# # #addi sp, sp, -4
# # #stw ra, 0(sp)
call lsfr
# # #ldw ra, 0(sp)
# # #addi sp, sp, 4
mov r5, r2 #stroe the rand in r4
mov r12, r2 #also in r4
addi r7, r7, 1
# # # #START THE BIG TIMER
movia r16, TIMER2
# # #movia r5, 1000000000
mov r12, r5
# slli r5, r5, 16
mov r17, r5 #TIME_CNT 4,200,000,000
stwio r17, 8(r16) #lower bit
srli r12, r12, 16
mov r17, r12
stwio r17, 12(r16) #upper bit
stwio r0, 0(r16)
movi r18, 0b0100
stwio r18, 4(r16)
br SPIN
PHASE3:
movia r11, ALMOST_STOP_REV_TIME
movia r10, ALMOST_STOP_STOP_TIME
mov r4, r2
# # #addi sp, sp, -4
# # #stw ra, 0(sp)
call lsfr
# #ldw ra, 0(sp)
# #addi sp, sp, 4
mov r5, r2 #stroe the rand in r4
mov r12, r2 #also in r4
addi r7, r7, 1
# # # #START THE BIG TIMER
movia r16, TIMER2
# # #hard ocde movia r5, 1000000000
mov r12, r5
slli r5, r5, 16
mov r17, r5 #TIME_CNT 4,200,000,000
stwio r17, 8(r16) #lower bit
srli r12, r12, 16
mov r17, r12
stwio r17, 12(r16) #upper bit
stwio r0, 0(r16)
movi r18, 0b0100
stwio r18, 4(r16)
br SPIN
PHASE4:
movia r11, PHASE4_SPIN_TIME
movia r10, PHASE4_STOP_TIME
movia r4, INIT_STATE #mov r4, r2
#addi sp, sp, -4
#stw ra, 0(sp)
call lsfr
#ldw ra, 0(sp)
#addi sp, sp, 4
#if commented, ret from big timer goes here
mov r5, r2 #stroe the rand in r4
mov r12, r2 #also in r4
addi r7, r7, 1
# #START THE BIG TIMER
movia r16, TIMER2
#hard ocde movia r5, 1000000000
mov r12, r5
slli r5, r5, 16
mov r17, r5 #TIME_CNT 4,200,000,000
stwio r17, 8(r16) #lower bit
srli r12, r12, 16
mov r17, r12
stwio r17, 12(r16) #upper bit
stwio r0, 0(r16)
movi r18, 0b0100
stwio r18, 4(r16)
br SPIN
SPIN:
#subi r6, r6, 1
movia r8, JP1
movia r9, onR
stwio r9, 0(r8)
#movi r10, 1800
SPIN_MULTI:
mov r4, r11
#movia r4,PHASE4_SPIN_TIME # #220000000 # #try spin for 2 #3500
# movi r10, 20 #couter
subi sp, sp, 4
stw ra, 0(sp)
#r5 should be random now
call delay_time
ldw ra, 0(sp)
addi sp, sp, 4
SLOW_DOWN:
movia r8, JP1
movia r9, off
stwio r9, 0(r8)
mov r4, r10
# #movia r4, PHASE4_STOP_TIME#mov r4, r10 #120000000 #r10 #has pre ef const according to phase
SLOW_CYCLE:
subi sp, sp, 4
stw ra, 0(sp)
call delay_time
ldw ra, 0(sp)
addi sp, sp, 4
#bug report:
#hits the delay cylcye, timer interrupted. always, no spin again
TIMER2_OUT:
movia r16, TIMER2
ldwio r18, 0(r16)
andi r18,r18,0x1 #timed out
beq r18,r0,SPIN
br DONE
#br DECIDE
DONE:
movia r8, JP1
movia r9, off
stwio r9, 0(r8)
#lol
#addi ra, ra, 320
ldw ra, 0(sp)
addi sp, sp, 4
ret
delay_time: #take different inputs from r4, longer for spin, shorter for off
#r17 is time counter , r16 is timer address
movia r16, Timer1
mov r17, r4 #TIME_CNT 4,200,000,000
#srli r17, r17, 16 #slli
stwio r17, 8(r16) #lower bit
mov r17, r4
srli r17, r17, 16 #srli
stwio r17, 12(r16) #upper bit
stwio r0, 0(r16) #clear bit
movi r18, 0b0100 #110 #to start the timer
stwio r18, 4(r16)
poll:
ldwio r18, 0(r16)
andi r18,r18,0b1 #timed out
beq r18,r0,poll
ret