OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [examples/] [boot/] [src/] [crt.S] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
##!  Register   ABI Name        Description                     Saver
2
##!  x0         zero            Hard-wired zero                 —
3
##!  x1         ra              Return address                  Caller
4
##!  x2         s0/fp           Saved register/frame pointer    Callee
5
##!  x3–13      s1–11           Saved registers                 Callee
6
##!  x14        sp              Stack pointer                   Callee
7
##!  x15        tp              Thread pointer                  Callee
8
##!  x16–17     v0–1            Return values                   Caller
9
##!  x18–25     a0–7            Function arguments              Caller
10
##!  x26–30     t0–4            Temporaries                     Caller
11
##!  x31        gp              Global  pointer                 —
12
##!  f0–15      fs0–15          FP saved registers              Callee
13
##!  f16–17     fv0–1           FP return values                Caller
14
##!  f18–25     fa0–7           FP arguments                    Caller
15
##!  f26–31     ft0–5           FP temporaries                  Caller
16
 
17
#include "encoding.h"
18
 
19
  .text
20
  .align 4
21
  .globl reset_vector
22
  .globl _start
23
 
24
trap_table:
25
#define BAD_TRAP_VECTOR 0
26
  .word bad_trap
27
  .word bad_trap # pmp_trap
28
  .word bad_trap # illegal_insn_trap
29
  .word bad_trap
30
  .word bad_trap # misaligned_load_trap
31
  .word bad_trap # pmp_trap
32
  .word bad_trap # misaligned_store_trap
33
  .word bad_trap # pmp_trap
34
  .word bad_trap
35
  .word bad_trap # mcall_trap
36
  .word bad_trap
37
  .word bad_trap
38
  .word bad_trap
39
#define TRAP_FROM_MACHINE_MODE_VECTOR 13
40
  .word bad_trap # __trap_from_machine_mode
41
  .word bad_trap
42
  .word bad_trap
43
 
44
##! Disabling the compressed code
45
#  .option norvc
46
reset_vector:
47
  j _start
48
 
49
bad_trap:
50
  j bad_trap
51
 
52
_start:
53
  li  x1, 0
54
  li  x2, 0
55
  li  x3, 0
56
  li  x4, 0
57
  li  x5, 0
58
  li  x6, 0
59
  li  x7, 0
60
  li  x8, 0
61
  li  x9, 0
62
  li  x10,0
63
  li  x11,0
64
  li  x12,0
65
  li  x13,0
66
  li  x14,0
67
  li  x15,0
68
  li  x16,0
69
  li  x17,0
70
  li  x18,0
71
  li  x19,0
72
  li  x20,0
73
  li  x21,0
74
  li  x22,0
75
  li  x23,0
76
  li  x24,0
77
  li  x25,0
78
  li  x26,0
79
  li  x27,0
80
  li  x28,0
81
  li  x29,0
82
  li  x30,0
83
  li  x31,0
84
 
85
  ##! csrs (pseudo asm instruction) - set bit
86
  ##! csrrs - atomic read and set bit
87
  ##! csrc (pseudo asm instruction) - clear bit
88
  ##! csrrc - atomic read and clear bit
89
 
90
  li t0, 0x00001800   # MPP[12:11] = 0x3 (Previous to machine mode)
91
  csrc mstatus, t0    # run tests in user mode = 0, by clearing bits
92
  li t0, 0x00000008   # Enable irq in machine and user modes after execution of xRET
93
  csrs mstatus, t0    # enable interrupts in user mode
94
  #li t0, MSTATUS_FS;
95
  #csrs mstatus, t0    # enable FPU
96
  #li t0, MSTATUS_XS;
97
  #csrs mstatus, t0    # enable accelerator
98
 
99
  ##! init mtvec register (see https://github.com/riscv/riscv-test-env/blob/master/p/riscv_test.h)
100
  la t0, trap_entry
101
  csrw mtvec, t0
102
  li t0, 0x00000800
103
  csrs mie, t0       # Enable External irq (ftom PLIC) for M mode
104
 
105
#if 0
106
  ##! see https://github.com/riscv/riscv-tests/benchmarks/common
107
  csrr t0, mstatus
108
  li t1, MSTATUS_XS
109
  and t1, t0, t1
110
  sw t1, have_vec, t2
111
 
112
  ## if that didn't stick, we don't have a FPU, so don't initialize it
113
  li t1, MSTATUS_FS
114
  and t1, t0, t1
115
  beqz t1, 1f
116
#endif
117
 
118
# intialization when FPU enabled
119
#ifdef REMOVETHIS__riscv_hard_float
120
  fssr    x0
121
  fmv.s.x f0, x0
122
  fmv.s.x f1, x0
123
  fmv.s.x f2, x0
124
  fmv.s.x f3, x0
125
  fmv.s.x f4, x0
126
  fmv.s.x f5, x0
127
  fmv.s.x f6, x0
128
  fmv.s.x f7, x0
129
  fmv.s.x f8, x0
130
  fmv.s.x f9, x0
131
  fmv.s.x f10,x0
132
  fmv.s.x f11,x0
133
  fmv.s.x f12,x0
134
  fmv.s.x f13,x0
135
  fmv.s.x f14,x0
136
  fmv.s.x f15,x0
137
  fmv.s.x f16,x0
138
  fmv.s.x f17,x0
139
  fmv.s.x f18,x0
140
  fmv.s.x f19,x0
141
  fmv.s.x f20,x0
142
  fmv.s.x f21,x0
143
  fmv.s.x f22,x0
144
  fmv.s.x f23,x0
145
  fmv.s.x f24,x0
146
  fmv.s.x f25,x0
147
  fmv.s.x f26,x0
148
  fmv.s.x f27,x0
149
  fmv.s.x f28,x0
150
  fmv.s.x f29,x0
151
  fmv.s.x f30,x0
152
  fmv.s.x f31,x0
153
#endif
154
 
155
  ##! initialize global pointer (no need in it)
156
  lui gp, 0x10000
157
 
158
  ##! get core id
159
  csrr a0, mhartid            # for now, assume only 1 core
160
  li a1, 1
161
1:bgeu a0, a1, 1b
162
 
163
  # Task stack pointer (tp) uses the same value as sp.
164
#define STACK_512KB 19
165
  lui t0, 0x10000             # t0 = SRAM base address = 0x10000000 . lui = load upper immediate 20-bits
166
  add sp, zero, 1             # sp = 1
167
  sll sp, sp, STACK_512KB     # sp = sp << 19 = 0x80000
168
  add sp, t0, sp              # sp = sp + 0x10000000 = 0x10080000
169
  add tp, zero, sp            # tp = sp + 0
170
  ## Use tp register to save/restore registers context on task switching
171
  addi tp,tp,-256              # tp = tp - 256 = 0x1007ff00
172
  # 1 KB for the tap stack + TLS. Add signed extended 12-bits to register
173
  addi sp, sp, -1024          # sp = 0x1007fc00
174
 
175
  # copy image 64 KB
176
  jal _init
177
 
178
  ##! jump to entry point in SRAM = 0x10000000
179
  ##!     'meps' - Machine Exception Program Coutner
180
  lui t0, 0x10000             # t0 = SRAM base address = 0x10000000 . lui = load upper immediate 20-bits
181
  csrw mepc, t0
182
  ##! @see riscv-priv-spec-1.7.pdf. 3.2.1
183
  ##! After handling a trap, the ERET instruction is used to return to the privilege level at which the
184
  ##! trap occurred. In addition to manipulating the privilege stack as described in Section 3.1.5, ERET
185
  ##! sets the pc to the value stored in the Xepc register, where X is the privilege mode (S, H, or M) in
186
  ##! which the ERET instruction was executed.
187
  mret
188
 
189
trap_entry:
190
  ##! module CSRFile rises io_fatc signal that is cause of the 'ptw.invalidate'.
191
  fence
192
  csrw mscratch, a0;
193
 
194
  _save_context(tp)
195
 
196
  ## @brief Call function :
197
  ##       long handle_trap(long cause, long epc, long long regs[32])
198
  ##             a0 = argument 1: cause
199
  ##             a1 = argument 2: mepc
200
  ##             a2 = argument 3: pointer on stack
201
  ## @return     a0 New instruction pointer offset
202
  csrr a0, mcause
203
  csrr a1, mepc
204
  sd a1,COOP_REG_TP(tp)
205
  mv a2, sp
206
  # !!! Cannot reset external pending bits only via IrqController (page 28)
207
  li t0, 0x00000800
208
  csrc mip, t0      #csrc pseudo asm instruction clear CSR bit.
209
                    #[11] MEIP: machine pending external interrupt
210
  jal handle_trap
211
 
212
  # tp-offset in the context array is used to save mepc value. An it may be
213
  # modified by isr handler during preemtive task switching.
214
  ld a1,COOP_REG_TP(tp)
215
  csrw mepc,a1
216
  _restore_context(tp)
217
  mret
218
 
219
.section ".tdata.begin"
220
.globl _tdata_begin
221
_tdata_begin:
222
 
223
.section ".tdata.end"
224
.globl _tdata_end
225
_tdata_end:
226
 
227
.section ".tbss.end"
228
.globl _tbss_end
229
_tbss_end:

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.