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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [common/] [crt0.S] - Blame information for rev 56

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
/* ################################################################################################# */
2 21 zero_gravi
/* # << NEORV32 - crt0.S - Start-Up Code >>                                                        # */
3 2 zero_gravi
/* # ********************************************************************************************* # */
4
/* # BSD 3-Clause License                                                                          # */
5
/* #                                                                                               # */
6 53 zero_gravi
/* # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     # */
7 2 zero_gravi
/* #                                                                                               # */
8
/* # Redistribution and use in source and binary forms, with or without modification, are          # */
9
/* # permitted provided that the following conditions are met:                                     # */
10
/* #                                                                                               # */
11
/* # 1. Redistributions of source code must retain the above copyright notice, this list of        # */
12
/* #    conditions and the following disclaimer.                                                   # */
13
/* #                                                                                               # */
14
/* # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     # */
15
/* #    conditions and the following disclaimer in the documentation and/or other materials        # */
16
/* #    provided with the distribution.                                                            # */
17
/* #                                                                                               # */
18
/* # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  # */
19
/* #    endorse or promote products derived from this software without specific prior written      # */
20
/* #    permission.                                                                                # */
21
/* #                                                                                               # */
22
/* # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   # */
23
/* # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               # */
24
/* # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    # */
25
/* # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     # */
26
/* # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE # */
27
/* # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    # */
28
/* # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     # */
29
/* # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  # */
30
/* # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            # */
31
/* # ********************************************************************************************* # */
32
/* # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting # */
33
/* ################################################################################################# */
34
 
35 21 zero_gravi
.file   "crt0.S"
36
.section .text.boot
37
.balign 4
38
.global _start
39 2 zero_gravi
 
40
 
41 21 zero_gravi
// IO region
42 47 zero_gravi
.equ IO_BEGIN, 0xFFFFFF00 // start of processor-internal IO region
43 2 zero_gravi
 
44
 
45
_start:
46 21 zero_gravi
.cfi_startproc
47
.cfi_undefined ra
48 2 zero_gravi
 
49
// *********************************************************
50 52 zero_gravi
// Clear integer register file (lower half, assume E extension)
51 2 zero_gravi
// *********************************************************
52
__crt0_reg_file_clear:
53 32 zero_gravi
//addi  x0, x0, 0 // hardwired to zero
54
  addi  x1, x0, 0
55
  addi  x2, x0, 0
56
  addi  x3, x0, 0
57
  addi  x4, x0, 0
58
  addi  x5, x0, 0
59
  addi  x6, x0, 0
60
  addi  x7, x0, 0
61
  addi  x8, x0, 0
62
  addi  x9, x0, 0
63
//addi x10, x0, 0
64
//addi x11, x0, 0
65
//addi x12, x0, 0
66
//addi x13, x0, 0
67
  addi x14, x0, 0
68
  addi x15, x0, 0
69 2 zero_gravi
 
70
 
71
// *********************************************************
72 56 zero_gravi
// Setup pointers using linker script symbols
73 32 zero_gravi
// *********************************************************
74 56 zero_gravi
__crt0_pointer_init:
75
.option push
76
.option norelax
77
  la    sp, __crt0_stack_begin
78
  andi  sp, sp, 0xfffffffc // make sure this is aligned
79
  addi  fp, sp, 0          // frame pointer = stack pointer
80
  la gp, __global_pointer$ // global pointer
81
.option pop
82 52 zero_gravi
 
83
 
84
// *********************************************************
85 56 zero_gravi
// Setup CPU core CSRs (some of them DO NOT have a dedicated reset and need to be explicitly initialized)
86
// *********************************************************
87
__crt0_cpu_csr_init:
88
 
89
  // set address of first-level exception handler
90
  la   x10, __crt0_dummy_trap_handler
91
  csrw mtvec,  x10
92
  csrw mepc,   x10
93
  csrw mtval,  zero
94
  csrw mcause, zero
95
 
96
  // no global IRQ enable (is also done by hardware)
97
  csrw mstatus, zero
98
 
99
  // absolutely no interrupts, thanks
100
  csrw mie, zero
101
 
102
  // no access from less-privileged modes to counter CSRs
103
  csrw mcounteren, zero
104
 
105
  // stop all counters except for [m]cycle[h] and [m]instret[h]
106
  li   x11, ~5
107
  csrw mcountinhibit, x11
108
 
109
  // clear cycle counters
110
  csrw mcycle,    zero
111
  csrw mcycleh,   zero
112
 
113
  // clear instruction counters
114
  csrw minstret,  zero
115
  csrw minstreth, zero
116
 
117
#if defined(__riscv_flen) && (__riscv_flen != 0)
118
  // clear floating-point CSR (rounding mode & exception flags)
119
  csrw fcsr, zero
120
#endif
121
 
122
 
123
// *********************************************************
124 52 zero_gravi
// Clear integer register file (upper half, if no E extension)
125
// *********************************************************
126 32 zero_gravi
#ifndef __riscv_32e
127 52 zero_gravi
// DO NOT DO THIS if compiling bootloader (to save some program space)
128 32 zero_gravi
#ifndef make_bootloader
129
  addi x16, x0, 0
130
  addi x17, x0, 0
131
  addi x18, x0, 0
132
  addi x19, x0, 0
133
  addi x20, x0, 0
134
  addi x21, x0, 0
135
  addi x22, x0, 0
136
  addi x23, x0, 0
137
  addi x24, x0, 0
138
  addi x25, x0, 0
139
  addi x26, x0, 0
140
  addi x27, x0, 0
141
  addi x28, x0, 0
142
  addi x29, x0, 0
143
  addi x30, x0, 0
144
  addi x31, x0, 0
145
#endif
146
#endif
147
 
148
 
149
// *********************************************************
150 2 zero_gravi
// Reset/deactivate IO/peripheral devices
151
// Devices, that are not implemented, will cause a store access fault
152
// which is captured but actually ignored due to the dummy handler.
153
// *********************************************************
154
__crt0_reset_io:
155
  li x11, IO_BEGIN // start of processor-internal IO region
156
 
157
__crt0_reset_io_loop:
158
  sw   zero, 0(x11)
159
  addi x11, x11, 4
160
  bne  zero, x11, __crt0_reset_io_loop
161
 
162
 
163
// *********************************************************
164 23 zero_gravi
// Clear .bss section (byte-wise) using linker script symbols
165 2 zero_gravi
// *********************************************************
166
__crt0_clear_bss:
167
  la x11, __crt0_bss_start
168
  la x12, __crt0_bss_end
169
 
170
__crt0_clear_bss_loop:
171
  bge  x11, x12, __crt0_clear_bss_loop_end
172
  sb   zero, 0(x11)
173
  addi x11, x11, 1
174
  j    __crt0_clear_bss_loop
175
 
176
__crt0_clear_bss_loop_end:
177
 
178
 
179
// *********************************************************
180 23 zero_gravi
// Copy initialized .data section from ROM to RAM (byte-wise) using linker script symbols
181 2 zero_gravi
// *********************************************************
182
__crt0_copy_data:
183
  la x11, __crt0_copy_data_src_begin  // start of data area (copy source)
184
  la x12, __crt0_copy_data_dst_begin  // start of data area (copy destination)
185
  la x13, __crt0_copy_data_dst_end    // last address of destination data area
186
 
187
__crt0_copy_data_loop:
188
  bge  x12, x13,  __crt0_copy_data_loop_end
189
  lb   x14, 0(x11)
190
  sb   x14, 0(x12)
191
  addi x11, x11, 1
192
  addi x12, x12, 1
193
  j    __crt0_copy_data_loop
194
 
195
__crt0_copy_data_loop_end:
196
 
197
 
198
// *********************************************************
199 39 zero_gravi
// Call main function
200 2 zero_gravi
// *********************************************************
201
__crt0_main_entry:
202
 
203 39 zero_gravi
  // setup arguments for calling main
204 2 zero_gravi
  addi x10, zero, 0 // argc = 0
205
  addi x11, zero, 0 // argv = 0
206
 
207 40 zero_gravi
  // call actual app's main function
208 2 zero_gravi
  jal ra, main
209
 
210
 
211
// *********************************************************
212
// Go to endless sleep mode if main returns
213
// *********************************************************
214
__crt0_this_is_the_end:
215 11 zero_gravi
  csrrci zero, mstatus, 8 // mstatus: disable global IRQs (MIE)
216 39 zero_gravi
  nop
217 2 zero_gravi
  wfi
218 39 zero_gravi
__crt0_this_is_the_end_my_friend:
219
  j __crt0_this_is_the_end_my_friend // in case WFI is not available
220 2 zero_gravi
 
221
 
222
// *********************************************************
223 14 zero_gravi
// dummy trap handler (for exceptions & IRQs)
224
// tries to move on to next instruction
225 2 zero_gravi
// *********************************************************
226 21 zero_gravi
.global __crt0_dummy_trap_handler
227
.balign 4
228 14 zero_gravi
__crt0_dummy_trap_handler:
229 2 zero_gravi
 
230 14 zero_gravi
  addi  sp, sp, -8
231
  sw      x8, 0(sp)
232
  sw      x9, 4(sp)
233 2 zero_gravi
 
234 14 zero_gravi
  csrr  x8, mcause
235
  blt   x8, zero, __crt0_dummy_trap_handler_irq  // skip mepc modification if interrupt
236 2 zero_gravi
 
237 14 zero_gravi
  csrr  x8, mepc
238 2 zero_gravi
 
239 14 zero_gravi
// is compressed instruction?
240 23 zero_gravi
__crt0_dummy_trap_handler_exc_c_check:
241 14 zero_gravi
  lh    x9, 0(x8)   // get compressed instruction or lower 16 bits of uncompressed instruction that caused exception
242
  andi  x9, x9, 3   // mask: isolate lowest 2 opcode bits (= 11 for uncompressed instructions)
243 2 zero_gravi
 
244 14 zero_gravi
  addi  x8, x8, +2  // only this for compressed instructions
245
  csrw  mepc, x8    // set return address when compressed instruction
246 2 zero_gravi
 
247 14 zero_gravi
  addi  x8, zero, 3
248
  bne   x8, x9, __crt0_dummy_trap_handler_irq // jump if compressed instruction
249 7 zero_gravi
 
250 14 zero_gravi
// is uncompressed instruction
251 23 zero_gravi
__crt0_dummy_trap_handler_exc_uncrompressed:
252 14 zero_gravi
  csrr  x8, mepc
253
  addi  x8, x8, +2  // add another 2 (making +4) for uncompressed instructions
254
  csrw  mepc, x8
255 2 zero_gravi
 
256 14 zero_gravi
__crt0_dummy_trap_handler_irq:
257 2 zero_gravi
 
258 53 zero_gravi
  lw    x8, 0(sp)
259
  lw    x9, 4(sp)
260 23 zero_gravi
  addi  sp, sp, +8
261 2 zero_gravi
 
262
  mret
263
 
264 21 zero_gravi
.cfi_endproc
265
.end

powered by: WebSVN 2.1.0

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