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

Subversion Repositories neorv32

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

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
_start:
42 21 zero_gravi
.cfi_startproc
43
.cfi_undefined ra
44 2 zero_gravi
 
45 59 zero_gravi
 
46 61 zero_gravi
// ************************************************************************************************
47 62 zero_gravi
// This is the very first instruction that is executed after hardware reset. It ensures that x0 is
48
// written at least once - the CPU HW will ensure it is always set to zero on any write access.
49
// ************************************************************************************************
50
  lui zero, 0 // "dummy" instruction that uses no reg-file input operands at all
51
 
52
 
53
// ************************************************************************************************
54 56 zero_gravi
// Setup pointers using linker script symbols
55 61 zero_gravi
// ************************************************************************************************
56 56 zero_gravi
__crt0_pointer_init:
57 61 zero_gravi
  .option push
58
  .option norelax
59 52 zero_gravi
 
60 61 zero_gravi
  la sp, __crt0_stack_begin  // stack pointer
61
  la gp, __global_pointer$   // global pointer
62 52 zero_gravi
 
63 61 zero_gravi
  .option pop
64
 
65
 
66
// ************************************************************************************************
67
// Setup CPU core CSRs (some of them DO NOT have a dedicated
68
// reset and need to be explicitly initialized)
69
// ************************************************************************************************
70 56 zero_gravi
__crt0_cpu_csr_init:
71
 
72 61 zero_gravi
  la   x10,   __crt0_dummy_trap_handler // configure early trap handler
73
  csrw mtvec, x10
74
  csrw mepc,  x10                       // just to init mepc
75 56 zero_gravi
 
76 61 zero_gravi
  csrw mstatus, zero                    // disable global IRQ
77 56 zero_gravi
 
78 61 zero_gravi
  csrw mie, zero                        // absolutely no interrupts sources, thanks
79 56 zero_gravi
 
80 61 zero_gravi
  csrw mcounteren, zero                 // no access from less-privileged modes to counter CSRs
81 56 zero_gravi
 
82 61 zero_gravi
  li   x11,   ~5                        // stop all counters except for [m]cycle[h] and [m]instret[h]
83
  csrw 0x320, x11                       // = mcountinhibit (literal address for lagacy toolchain compatibility)
84 56 zero_gravi
 
85 61 zero_gravi
  csrw mcycle,    zero                  // reset cycle counters
86
  csrw mcycleh,   zero
87
  csrw minstret,  zero                  // reset instruction counters
88 56 zero_gravi
  csrw minstreth, zero
89
 
90 61 zero_gravi
#if defined(__riscv_flen)
91 62 zero_gravi
  li   x11, 0x00005000                  // enable FPU (state = initial)
92
  csrs mstatus, x11
93 61 zero_gravi
  csrw fcsr, zero                       // reset floating-point CSR
94 56 zero_gravi
#endif
95
 
96
 
97 61 zero_gravi
// ************************************************************************************************
98
// Initialize integer register file (lower half)
99
// ************************************************************************************************
100
__crt0_reg_file_clear:
101
//addi  x0, x0, 0 // hardwired to zero
102
  addi  x1, x0, 0
103
//addi  x2, x0, 0 // stack pointer sp
104
//addi  x3, x0, 0 // gloabl pointer gp
105
  addi  x4, x0, 0
106
  addi  x5, x0, 0
107
  addi  x6, x0, 0
108
  addi  x7, x0, 0
109
//addi  x8, x0, 0 // initialized within crt0
110
//addi  x9, x0, 0 // initialized within crt0
111
//addi x10, x0, 0 // initialized within crt0
112
//addi x11, x0, 0 // initialized within crt0
113
//addi x12, x0, 0 // initialized within crt0
114
//addi x13, x0, 0 // initialized within crt0
115
  addi x14, x0, 0
116
  addi x15, x0, 0
117
 
118
 
119
// ************************************************************************************************
120
// Initialize integer register file (upper half, if no E extension)
121
// ************************************************************************************************
122 32 zero_gravi
#ifndef __riscv_32e
123 61 zero_gravi
// do not do this if compiling bootloader (to save some program space)
124 32 zero_gravi
#ifndef make_bootloader
125
  addi x16, x0, 0
126
  addi x17, x0, 0
127
  addi x18, x0, 0
128
  addi x19, x0, 0
129
  addi x20, x0, 0
130
  addi x21, x0, 0
131
  addi x22, x0, 0
132
  addi x23, x0, 0
133
  addi x24, x0, 0
134
  addi x25, x0, 0
135
  addi x26, x0, 0
136
  addi x27, x0, 0
137
  addi x28, x0, 0
138
  addi x29, x0, 0
139
  addi x30, x0, 0
140
  addi x31, x0, 0
141
#endif
142
#endif
143
 
144
 
145 61 zero_gravi
// ************************************************************************************************
146 2 zero_gravi
// Reset/deactivate IO/peripheral devices
147 61 zero_gravi
// Devices, that are not implemented, will cause a store bus access fault
148
// which is captured (but actually ignored) by the dummy trap handler.
149
// ************************************************************************************************
150 2 zero_gravi
__crt0_reset_io:
151 61 zero_gravi
  la   x8,   __ctr0_io_space_begin         // start of processor-internal IO region
152
  la   x9,   __ctr0_io_space_end           // end of processor-internal IO region
153 2 zero_gravi
 
154
__crt0_reset_io_loop:
155 58 zero_gravi
  sw   zero, 0(x8)
156
  addi x8,   x8, 4
157
  bne  x8,   x9, __crt0_reset_io_loop
158 2 zero_gravi
 
159
 
160 61 zero_gravi
// ************************************************************************************************
161 23 zero_gravi
// Clear .bss section (byte-wise) using linker script symbols
162 61 zero_gravi
// ************************************************************************************************
163 2 zero_gravi
__crt0_clear_bss:
164 61 zero_gravi
  la   x11,  __crt0_bss_start
165
  la   x12,  __crt0_bss_end
166 2 zero_gravi
 
167
__crt0_clear_bss_loop:
168 61 zero_gravi
  bge  x11,  x12, __crt0_clear_bss_loop_end
169 2 zero_gravi
  sb   zero, 0(x11)
170 61 zero_gravi
  addi x11,  x11, 1
171 2 zero_gravi
  j    __crt0_clear_bss_loop
172
 
173
__crt0_clear_bss_loop_end:
174
 
175
 
176 61 zero_gravi
// ************************************************************************************************
177 23 zero_gravi
// Copy initialized .data section from ROM to RAM (byte-wise) using linker script symbols
178 61 zero_gravi
// ************************************************************************************************
179 2 zero_gravi
__crt0_copy_data:
180 61 zero_gravi
  la   x11, __crt0_copy_data_src_begin        // start of data area (copy source)
181
  la   x12, __crt0_copy_data_dst_begin        // start of data area (copy destination)
182
  la   x13, __crt0_copy_data_dst_end          // last address of destination data area
183 2 zero_gravi
 
184
__crt0_copy_data_loop:
185
  bge  x12, x13,  __crt0_copy_data_loop_end
186
  lb   x14, 0(x11)
187
  sb   x14, 0(x12)
188
  addi x11, x11, 1
189
  addi x12, x12, 1
190
  j    __crt0_copy_data_loop
191
 
192
__crt0_copy_data_loop_end:
193
 
194
 
195 61 zero_gravi
// ************************************************************************************************
196
// Setup arguments and call main function
197
// ************************************************************************************************
198 2 zero_gravi
__crt0_main_entry:
199 61 zero_gravi
  addi x10, zero, 0 // a0 = argc = 0
200
  addi x11, zero, 0 // a1 = argv = 0
201
  jal  ra,  main    // call actual app's main function, this "should" not return
202 2 zero_gravi
 
203
 
204 61 zero_gravi
// ************************************************************************************************
205
// call "after main" handler (if there is any) if main really returns
206
// ************************************************************************************************
207
__crt0_main_aftermath:
208
  csrw  mscratch, a0                 // copy main's return code in mscratch for debugger
209 2 zero_gravi
 
210 61 zero_gravi
#ifndef make_bootloader              // after_main handler not supported for bootloader
211
  .weak __neorv32_crt0_after_main
212
  la   ra, __neorv32_crt0_after_main
213
  beqz ra, __crt0_main_aftermath_end // check if an aftermath handler has been specified
214
  jalr ra                            // execute handler, main's return code in a0
215
#endif
216 2 zero_gravi
 
217
 
218 61 zero_gravi
// ************************************************************************************************
219
// go to endless sleep mode
220
// ************************************************************************************************
221
__crt0_main_aftermath_end:
222
  csrci mstatus,  8                  // mstatus: disable global IRQs (mstatus.mie)
223
__crt0_main_aftermath_end_loop:
224
  wfi                                // try to go to sleep mode
225
  j __crt0_main_aftermath_end_loop   // endless loop
226 2 zero_gravi
 
227 61 zero_gravi
 
228
// ************************************************************************************************
229
// dummy trap handler (for exceptions & IRQs during very early boot stage)
230
// does nothing but tries to move on to next instruction
231
// ************************************************************************************************
232 21 zero_gravi
.balign 4
233 14 zero_gravi
__crt0_dummy_trap_handler:
234 2 zero_gravi
 
235 61 zero_gravi
  addi  sp,   sp, -8
236
  sw      x8,   0(sp)
237
  sw      x9,   4(sp)
238 2 zero_gravi
 
239 61 zero_gravi
  csrr  x8,   mcause
240
  blt   x8,   zero, __crt0_dummy_trap_handler_irq  // skip mepc modification if interrupt
241 2 zero_gravi
 
242 61 zero_gravi
  csrr  x8,   mepc
243 2 zero_gravi
 
244 61 zero_gravi
__crt0_dummy_trap_handler_exc_c_check:             // is compressed instruction?
245
  lh    x9,   0(x8)                                // get compressed instruction or lower 16 bits of uncompressed instruction that caused exception
246
  andi  x9,   x9, 3                                // mask: isolate lowest 2 opcode bits (= 11 for uncompressed instructions)
247 2 zero_gravi
 
248 61 zero_gravi
  addi  x8,   x8, +2                               // only this for compressed instructions
249
  csrw  mepc, x8                                   // set return address when compressed instruction
250 2 zero_gravi
 
251 61 zero_gravi
  addi  x8,   zero, 3
252
  bne   x8,   x9, __crt0_dummy_trap_handler_irq    // jump if compressed instruction
253
 
254
__crt0_dummy_trap_handler_exc_uncrompressed:       // is uncompressed instruction!
255
  csrr  x8,   mepc
256
  addi  x8,   x8, +2                               // add another 2 (making +4) for uncompressed instructions
257 14 zero_gravi
  csrw  mepc, x8
258 2 zero_gravi
 
259 14 zero_gravi
__crt0_dummy_trap_handler_irq:
260 61 zero_gravi
  lw    x8,   0(sp)
261
  lw    x9,   4(sp)
262
  addi  sp,   sp, +8
263 2 zero_gravi
 
264
  mret
265
 
266 21 zero_gravi
.cfi_endproc
267
.end

powered by: WebSVN 2.1.0

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