-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.S
507 lines (372 loc) · 8.29 KB
/
utils.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
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
########################################################################
# This file is part of WRAMPmon, the WRAMP monitor programe.
#
# Copyright (C) 2019 The University of Waikato, Hamilton, New Zealand.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
.text
.global exit_user_prog
exit_user_prog:
syscall
.global init_serial
init_serial:
# Setup the serial port for 38400 8N1
addui $1, $0, 0xc7
sw $1, 0x70002($0)
sw $1, serial1_control($0)
sw $0, 0x70004($0)
sw $0, serial1_iack($0)
jr $ra
# This routine saves the user's serial port settings and reverts to our defaults
.global save_serial
save_serial:
lw $1, 0x70002($0) # Serial control register
sw $1, serial1_control($0)
lw $1, 0x70004($0) # IACK register
sw $1, serial1_iack($0)
# Setup the serial port for 38400 8N1
addui $1, $0, 0xc7
sw $1, 0x70002($0)
sw $0, 0x70004($0)
jr $ra
# This routine restores the user's serial port settings
.global restore_serial
restore_serial:
lw $1, serial1_control($0)
sw $1, 0x70002($0) # Serial control register
lw $1, serial1_iack($0)
sw $1, 0x70004($0) # IACK register
jr $ra
.bss
serial1_control: .word
serial1_iack: .word
.text
.global lock_rom
lock_rom:
subui $sp, $sp, 2
sw $2, 0($sp)
sw $3, 1($sp)
la $2, start_lock
la $3, rom_protect
copy_loop:
lw $1, 0($2)
sw $1, 0($3)
addui $2, $2, 1
addui $3, $3, 1
la $1, lock_rom_exit
seq $1, $2, $1
beqz $1, copy_loop
lhi $1, 0xaaaa
ori $1, $1, 0xaaaa
lhi $2, 0x5555
ori $2, $2, 0x5555
lhi $3, 0xa0a0
ori $3, $3, 0xa0a0
j rom_protect
start_lock:
sw $1, 0x85555($0)
sw $2, 0x82aaa($0)
sw $3, 0x85555($0)
wait_lock:
lw $1, 0x85555($0)
lw $2, 0x85555($0)
xor $1, $1, $2
bnez $1, wait_lock
j lock_rom_exit
lock_rom_exit:
# Lock in the Spartan
addui $1, $1, 0x1
sw $1, 0x7fff1($0)
lw $2, 0($sp)
lw $3, 1($sp)
addui $sp, $sp, 2
jr $ra
.global unlock_rom
unlock_rom:
subui $sp, $sp, 3
sw $2, 0($sp)
sw $3, 1($sp)
sw $4, 2($sp)
# Unlock in the Spartan
sw $0, 0x7fff1($0)
la $2, start_unlock
la $3, rom_protect
unlock_copy_loop:
lw $1, 0($2)
sw $1, 0($3)
addui $2, $2, 1
addui $3, $3, 1
la $1, unlock_rom_exit
seq $1, $2, $1
beqz $1, unlock_copy_loop
lhi $1, 0xaaaa
ori $1, $1, 0xaaaa
lhi $2, 0x5555
ori $2, $2, 0x5555
lhi $3, 0x8080
ori $3, $3, 0x8080
lhi $4, 0x2020
ori $4, $4, 0x2020
j rom_protect
start_unlock:
sw $1, 0x85555($0)
sw $2, 0x82aaa($0)
sw $3, 0x85555($0)
sw $1, 0x85555($0)
sw $2, 0x82aaa($0)
sw $4, 0x85555($0)
wait_unlock:
lw $1, 0x85555($0)
lw $2, 0x85555($0)
xor $1, $1, $2
bnez $1, wait_unlock
j unlock_rom_exit
unlock_rom_exit:
lw $2, 0($sp)
lw $3, 1($sp)
lw $4, 2($sp)
addui $sp, $sp, 3
jr $ra
.bss
rom_protect:
.space 11
.text
.global program_init
# This function sets up things for a program for when it is run for the first time
program_init:
subui $sp, $sp, 3
sw $4, 0($sp)
sw $5, 1($sp)
sw $6, 2($sp)
# Setup the stack pointer to just below the bss segment
# which is at the top of RAM
la $4, 0x04000 # top of RAM
la $5, bss_size # global symbol representing bss segment size
subu $4, $4, $5 # subtract to find the stack start
# Save this into the PCB in the stack pointer slot
la $5, regsave
sw $4, 14($5)
# Setup the return address
la $4, exit_user_prog
sw $4, 15($5)
lw $4, 0($sp)
lw $5, 1($sp)
lw $6, 2($sp)
addui $sp, $sp, 3
jr $ra
.text
.global start_program
start_program:
# Restore the program's serial port settings
jal restore_serial
# First we setup a redirection vector to allow us to start the program
# without altering any registers
lw $1, 0($sp)
# Get the twenty bit address
la $2, 0xfffff
and $1, $1, $2
# Turn it into a jump instruction
lhi $2, 0x4000
or $1, $1, $2
# Store it in the vector
sw $1, start_vector($0)
# Now load all the registers
la $1, regsave
lw $2, 2($1)
lw $3, 3($1)
lw $4, 4($1)
lw $5, 5($1)
lw $6, 6($1)
lw $7, 7($1)
lw $8, 8($1)
lw $9, 9($1)
lw $10, 10($1)
lw $11, 11($1)
lw $12, 12($1)
lw $13, 13($1)
lw $sp, 14($1)
lw $ra, 15($1)
lw $1, regsave+1($0)
# And start the program
j start_vector
.bss
start_vector:
.word
tmp: .word
.text
.global command_flash
command_flash:
jal unlock_rom
la $3, 0x0
la $4, flash_code
la $5, flash_end
command_flash_loop:
lw $1, 0($4)
sw $1, 0($3)
seq $1, $4, $5
bnez $1, begin_flash
addui $3, $3, 1
addui $4, $4, 1
j command_flash_loop
begin_flash:
jr $0
flash_code:
# Setup the serial port for 38400bps, N81
addui $1, $0, 0x00c7
sw $1, 0x70002($0)
flash_start:
# Setup the stack pointer
la $sp, 0x03fff
read_char_1:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_1
lw $1, 0x70001($zero)
andi $1, $1, 0xff
seqi $1, $1, 'P'
beqz $1, flash_start
read_char_2:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_2
lw $1, 0x70001($zero)
andi $1, $1, 0xff
slli $2, $1, 8
read_char_3:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_3
lw $1, 0x70001($zero)
andi $1, $1, 0xff
or $2, $2, $1
slli $2, $2, 8
# Now the sector address is in $2
# Now we read the data
addu $3, $0, $0 # Clear a loop counter
addu $5, $0, $0 # And a checksum counter
flash_loop:
read_char_4:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_4
lw $1, 0x70001($zero)
andi $1, $1, 0xff
addu $5, $5, $1
addu $4, $1, $0
slli $4, $4, 8
read_char_5:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_5
lw $1, 0x70001($zero)
andi $1, $1, 0xff
addu $5, $5, $1
or $4, $4, $1
slli $4, $4, 8
read_char_6:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_6
lw $1, 0x70001($zero)
andi $1, $1, 0xff
addu $5, $5, $1
or $4, $4, $1
slli $4, $4, 8
read_char_7:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_7
lw $1, 0x70001($zero)
andi $1, $1, 0xff
addu $5, $5, $1
or $4, $4, $1
# Save this word into our buffer
sw $4, 0x10000($3)
# Increment the loop counter
addui $3, $3, 1
seqi $1, $3, 256
beqz $1, flash_loop
srli $1, $2, 16
sw $1, 0($sp)
send_char_1:
lw $1, 0x70003($0) # Get LSR
andi $1, $1, 0x2 # Look at TDS bit
beqz $1, send_char_1 # Wait for previous character to be sent
lw $1, 0($sp) # Get the character
sw $1, 0x70000($0) # Send the character
srli $1, $2, 8
andi $1, $1, 0xff
sw $1, 0($sp)
send_char_2:
lw $1, 0x70003($0) # Get LSR
andi $1, $1, 0x2 # Look at TDS bit
beqz $1, send_char_2 # Wait for previous character to be sent
lw $1, 0($sp) # Get the character
sw $1, 0x70000($0) # Send the character
andi $1, $5, 0xff
sw $1, 0($sp)
send_char_3:
lw $1, 0x70003($0) # Get LSR
andi $1, $1, 0x2 # Look at TDS bit
beqz $1, send_char_3 # Wait for previous character to be sent
lw $1, 0($sp) # Get the character
sw $1, 0x70000($0) # Send the character
read_char_8:
lw $1, 0x70003($zero)
andi $1, $1, 0x1
beqz $1, read_char_8
lw $1, 0x70001($zero)
andi $1, $1, 0xff
seqi $1, $1, 'Y'
beqz $1, flash_start
# Go ahead and program a sector
la $5, 0x80000
addu $5, $5, $2
addu $3, $0, $0
flash_prog_loop:
lw $1, 0x10000($3)
addu $6, $5, $3
sw $1, 0($6)
addui $3, $3, 1
seqi $1, $3, 256
beqz $1, flash_prog_loop
# Done so...
addui $1, $0, 'D'
sw $1, 0($sp)
send_char_4:
lw $1, 0x70003($0) # Get LSR
andi $1, $1, 0x2 # Look at TDS bit
beqz $1, send_char_4 # Wait for previous character to be sent
lw $1, 0($sp) # Get the character
sw $1, 0x70000($0) # Send the character
flash_end:
beqz $0, flash_start
.global init_ptable
init_ptable:
# Setup the ptable
la $1, ptable
movgs $ptable, $1
addui $1, $0, 0
lhi $2, 0xffff
ori $2, $2, 0xffff
ptable_loop:
sw $2, ptable($1)
addi $1, $1, 1
slti $3, $1, 32
bnez $3, ptable_loop
jr $ra
.bss
ptable:
.space 32