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
|
/* $NetBSD: start.S,v 1.4 2021/12/03 10:49:25 andvar Exp $ */
/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code was written by Alessandro Forin and Neil Pittman
* at Microsoft Research and contributed to The NetBSD Foundation
* by Microsoft Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* Trivial support for printing stuff on the serial line from C pgms.
*/
#include <mips/asm.h>
#include <mips/cpuregs.h>
#define __ASSEMBLER__ 1
#include <machine/emipsreg.h>
/* Offsets in the CXTINFO structure
*/
#define TS_AT (1 * 4)
#define TS_V0 (2 * 4)
#define TS_V1 (3 * 4)
#define TS_A0 (4 * 4)
#define TS_A1 (5 * 4)
#define TS_A2 (6 * 4)
#define TS_A3 (7 * 4)
#define TS_T0 (8 * 4)
#define TS_T1 (9 * 4)
#define TS_T2 (10 * 4)
#define TS_T3 (11 * 4)
#define TS_T4 (12 * 4)
#define TS_T5 (13 * 4)
#define TS_T6 (14 * 4)
#define TS_T7 (15 * 4)
#define TS_S0 (16 * 4)
#define TS_S1 (17 * 4)
#define TS_S2 (18 * 4)
#define TS_S3 (19 * 4)
#define TS_S4 (20 * 4)
#define TS_S5 (21 * 4)
#define TS_S6 (22 * 4)
#define TS_S7 (23 * 4)
#define TS_T8 (24 * 4)
#define TS_T9 (25 * 4)
#define TS_K0 (26 * 4)
#define TS_K1 (27 * 4)
#define TS_GP (28 * 4)
#define TS_SP (29 * 4)
#define TS_FP (30 * 4)
#define fp s8
#define TS_RA (31 * 4)
#define TS_PC (32 * 4)
#define TS_SR (33 * 4)
#define TS_HI (34 * 4)
#define TS_LO (35 * 4)
#define TS_EC (36 * 4)
#define SIZEOF_CXTINFO (37*4)
/* PROM_MODE means the user plans to keep this code around while running an OS.
* So we act kind of like PROM code (BIOS?), but we live in RAM.
* So we need to safeguard ourselves against corruptions, some unavoidable.
* Like the overriding of the exception vectors, right where our "start" code is.
*/
IMPORT(main,4)
IMPORT(_end,4)
.set noreorder
EXPORT(start)
bgezal zero,_C_LABEL(real_start)
nop
/* Does not handle the exception, really.
* But to test interrupts should be enough
*/
.org 0x00000080
NESTED_NOPROFILE(ExceptionHandler,SIZEOF_CXTINFO,$31)
la k1, UserInterruptHandler
lw k1,0(k1)
bne k1,zero,Dispatch
mfc0 k0, MIPS_COP_0_EXC_PC
j k0
nop /* do not! pop status */
EXPORT(UserInterruptHandler)
.word 0
EXPORT(Dispatch)
/* Save state on stack */
addiu sp, sp, -SIZEOF_CXTINFO
/* save registers */
.set noat
sw AT, TS_AT(sp)
.set at
sw v0, TS_V0(sp)
sw v1, TS_V1(sp)
sw a0, TS_A0(sp)
sw a1, TS_A1(sp)
sw a2, TS_A2(sp)
sw a3, TS_A3(sp)
sw t0, TS_T0(sp)
sw t1, TS_T1(sp)
sw t2, TS_T2(sp)
sw t3, TS_T3(sp)
sw t4, TS_T4(sp)
sw t5, TS_T5(sp)
sw t6, TS_T6(sp)
sw t7, TS_T7(sp)
sw s0, TS_S0(sp)
sw s1, TS_S1(sp)
sw s2, TS_S2(sp)
sw s3, TS_S3(sp)
sw s4, TS_S4(sp)
sw s5, TS_S5(sp)
sw s6, TS_S6(sp)
sw s7, TS_S7(sp)
sw t8, TS_T8(sp)
sw t9, TS_T9(sp)
sw k0, TS_K0(sp)
sw k1, TS_K1(sp)
sw gp, TS_GP(sp)
/* sp: later */
sw fp, TS_FP(sp)
sw ra, TS_RA(sp)
mfc0 a0, MIPS_COP_0_STATUS
mflo t0
mfhi t1
sw a0, TS_SR(sp)
sw t0, TS_LO(sp)
sw t1, TS_HI(sp)
sw k0, TS_PC(sp)
/* Save original stack */
move a0,sp
addiu t0, sp, SIZEOF_CXTINFO
jalr k1
sw t0, TS_SP(sp)
/* Returned value is new PCXINFO */
move a0,v0
/* First load most registers */
.set noat
lw AT, TS_AT(a0)
lw v0, TS_V0(a0)
lw v1, TS_V1(a0)
/* a0 later */
lw a1, TS_A1(a0)
lw a2, TS_A2(a0)
lw a3, TS_A3(a0)
lw t0, TS_T0(a0)
lw t1, TS_T1(a0)
lw t2, TS_T2(a0)
lw t3, TS_T3(a0)
lw t4, TS_T4(a0)
lw t5, TS_T5(a0)
lw t6, TS_T6(a0)
lw t7, TS_T7(a0)
lw s0, TS_S0(a0)
lw s1, TS_S1(a0)
lw s2, TS_S2(a0)
lw s3, TS_S3(a0)
lw s4, TS_S4(a0)
lw s5, TS_S5(a0)
lw s6, TS_S6(a0)
lw s7, TS_S7(a0)
lw t8, TS_T8(a0)
lw t9, TS_T9(a0)
/* k0,k1 not restored */
lw gp, TS_GP(a0)
/* sp later */
lw fp, TS_FP(a0)
lw ra, TS_RA(a0)
lw k1, TS_HI(a0)
lw k0, TS_LO(a0)
mthi k1
mtlo k0
lw k1, TS_SR(a0)
mtc0 k1, MIPS_COP_0_STATUS
/* NB: After this instruction we cannot take any interrupts or traps
*/
lw sp, TS_SP(a0)
/* Put pc into k0 */
lw k0, TS_PC(a0)
lw a0, TS_A0(a0)
j k0
rfe
.set at
END(ExceptionHandler)
.org 0x00000200
EXPORT(real_start)
.ent _C_LABEL(real_start)
#ifdef SECONDARY_BOOTBLOCK
/*
* If this is the program that goes into FLASH we must copy ourselves down to RAM.
* FLASH default on the MLx is at 0xf0000000, DRAM at 0.
*/
addi a0,ra,-8 /* Compensate for the first two instructions */
/* Get the address(relative) of TextStart
*/
bgezal zero, _C_LABEL(MipsStart2) /* Always jumps */
nop
/* All of the static data, since we are at it.
*/
TextStart: /* + 0 */
/* Text start at final link address */
.int start
DataEnd: /* + 4 */
/* Data end == bss start */
.int _edata
BssEnd: /* + 8 */
/* Bss end */
.int _end
RelocToRAM: /* *+12 */
.int InRAM
MipsStart2:
/* Source = a0, Dst = t2 */
lw t2, 0(ra) /* _C_LABEL(TextStart) */
/* EndPtr = t3 */
/* in bdelay slot */
/* If a0 != t2 then we are running in Flash but should run in RAM
* In that case copy .text. Otherwise skip to .bss.
*/
beq a0,t2,ZroLoop-4
lw t3, 4(ra) /* _C_LABEL(DataEnd) */
CpyLoop:
/* loop copying 2 words at a time */
lw t4,0(a0)
lw t5,4(a0)
addiu a0,a0,8
sw t4,0(t2)
addiu t2,t2,8
sltu t1,t2,t3
bne t1,zero,CpyLoop
sw t5,-4(t2)
/* zero the bss
*/
lw t4, 8(ra) /* _C_LABEL(BssEnd) */
ZroLoop:
sltu t1,t3,t4
sw zero,0(t3)
bne t1,zero,ZroLoop
addiu t3,t3,4
/* Jump to RAM copy (below)
*/
lw t1, 12(ra) /* _C_LABEL(RelocToRAM) */
jr t1
nop
/*
* Execute from here after copying out of FLASH into RAM
*/
InRAM:
#endif /* SECONDARY_BOOTBLOCK */
/* Get a stack
*/
#ifdef __GP_SUPPORT__
la gp, _C_LABEL (_gp)
#endif
la sp,_end
addiu sp,sp,(8*1024) /* BUGBUG arbitrary */
/* Jump to main
*/
jal main
add a0,sp,zero
/* Load failed, reset the processor and jump back to the origins.
*/
EXPORT(_rtt) /* ahem */
li t0,0x1260ff80 /* NB: On new builds this is a SYS-RESET as well */
mtc0 t0,MIPS_COP_0_STATUS
lui t0,(BRAM_DEFAULT_ADDRESS>>16) /* nb: knows about 16bit chop */
jr t0
nop
EXPORT(Stop)
b Stop
nop
END(real_start)
.set noreorder
.set noat
.set nomacro
/* void Delay(UINT32 count)
*/
LEAF(Delay)
bne a0,zero,_C_LABEL(Delay)
subu a0,1
j ra
nop
END(Delay)
/* UINT32 GetPsr(void)
* Returns the PSR (coprocessor 0 status)
*/
LEAF(GetPsr)
mfc0 v0, MIPS_COP_0_STATUS
j ra
nop
END(GetPsr)
/* void SetPsr(UINT32 Psr)
* Sets the PSR (coprocessor 0 status)
*/
LEAF(SetPsr)
mtc0 a0,MIPS_COP_0_STATUS
j ra
nop
END(SetPsr)
/* UINT32 GetCause(void)
* Returns the Cause register (coprocessor 0)
*/
LEAF(GetCause)
mfc0 v0,MIPS_COP_0_CAUSE
j ra
nop
END(GetCause)
/* UINT32 GetEpc(void)
* Returns the Epc register (coprocessor 0)
*/
LEAF(GetEpc)
mfc0 v0,MIPS_COP_0_EXC_PC
j ra
nop
END(GetEpc)
/* int PutWord(UINT32 Word);
* Returns: 0 if ok, -1 otherwise
*/
NESTED(PutWord,12,$31)
subu sp,sp,12
sw s0,8(sp)
sw s1,4(sp)
sw ra,0(sp)
or s1,a0,zero
/* Spit all nibbles
*/
li s0,8
PutWordLoop:
srl a0,s1,32-4
li t0,10
sltu t1,a0,t0
bnez t1,$Digit
li a1,'0'
subu a0,a0,t0
li a1,'a'
$Digit:
sll s1,s1,4
jal PutChar
add a0,a0,a1
subu s0,s0,1
bne v0,zero,PutWordDone /* printed ok? */
li v0,-1
/* done yet? */
bne s0,zero,PutWordLoop
nop
/* done
*/
li v0,0
PutWordDone:
lw ra,0(sp)
lw s1,4(sp)
lw s0,8(sp)
jr ra
addiu sp,sp,12
END(PutWord)
/* int Puts(char *String);
* Returns: 0 if ok, -1 otherwise
*/
NESTED(Puts,8,$31)
subu sp,sp,8
sw s0,4(sp)
sw ra,0(sp)
or s0,a0,zero
/* Spit all chars until zero
*/
PutsLoop:
lbu a0,0(s0)
addiu s0,s0,1
beq a0,zero,PutsDoneOk
nop
jal PutChar
nop
beq v0,zero,PutsLoop
nop
/* Timed out
*/
b PutsDone
li v0,-1
/* done
*/
PutsDoneOk:
li v0,0
PutsDone:
lw ra,0(sp)
lw s0,4(sp)
jr ra
addiu sp,sp,8
END(Puts)
/* int GetChar(void);
* Returns: a non-negative value if ok, -1 otherwise
*/
LEAF(GetChar)
lui t0,(USART_DEFAULT_ADDRESS>>16) /* nb: knows about 16bit chop */
lui t1,1000 /* n*65k spins max */
RxNotReady:
lw t4,USARTST(t0) /* ChannelStatus */
andi t4,t4,USI_RXRDY
bgtz t4,$GotByte
subu t1,t1,1
/* still ok to spin? */
bgtz t1,RxNotReady
nop
/* Timed out
*/
jr ra
li v0,-1
/* Gottabyte
*/
$GotByte:
lw v0,USARTRX(t0) /* RxData */
jr ra
andi v0,0xff
END(GetChar)
/* int PutChar(UINT8 v);
* Returns: 0 if ok, -1 otherwise
*/
LEAF(PutChar)
lui t0,(USART_DEFAULT_ADDRESS>>16) /* nb: knows about 16bit chop */
lui t1,1000 /* n*65k spins max */
li v0,0
TxNotReady:
lw t4,USARTST(t0) /* ChannelStatus */
andi t4,t4,USI_TXRDY
bgtz t4,TxReady
subu t1,t1,1
/* still ok to spin? */
bgtz t1,TxNotReady
nop
/* Timed out
*/
jr ra
li v0,-1
/* Send it
*/
TxReady:
jr ra
sw a0,USARTTX(t0)
END(PutChar)
/* Second arg is a function to call with the first arg:
* void switch_stack_and_call(void *arg, void (*function)(void *));
*/
LEAF(switch_stack_and_call)
/* Get a stack and jump. It would be a very bad idea to return but..
*/
lui sp,%hi(_end)
addiu sp,%lo(_end)
jr a1
addiu sp,sp,(2*1024) /* BUGBUG arbitrary */
END(switch_stack_and_call)
|