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

Subversion Repositories neorv32

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

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
/* # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     # */
7
/* #                                                                                               # */
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
.equ IO_BEGIN, 0xFFFFFF80 // start of processor-internal IO region
43 2 zero_gravi
 
44 21 zero_gravi
// SYSINFO
45
.equ SYSINFO_DSPACE_BASE, 0xFFFFFFF4
46
.equ SYSINFO_DSPACE_SIZE, 0xFFFFFFFC
47 2 zero_gravi
 
48 12 zero_gravi
 
49 2 zero_gravi
_start:
50 21 zero_gravi
.cfi_startproc
51
.cfi_undefined ra
52 2 zero_gravi
 
53
// *********************************************************
54
// Clear register file
55 21 zero_gravi
// Assume 'worst case': rv32e
56 2 zero_gravi
// *********************************************************
57
__crt0_reg_file_clear:
58 14 zero_gravi
//addi  x0,  x0, 0 // hardwired to zero
59 2 zero_gravi
  addi  x1,  x0, 0
60 20 zero_gravi
  addi  x2,  x0, 0
61
  addi  x3,  x0, 0
62
  addi  x4,  x0, 0
63
  addi  x5,  x0, 0
64
  addi  x6,  x0, 0
65
  addi  x7,  x0, 0
66
  addi  x8,  x0, 0
67
  addi  x9,  x0, 0
68
//addi x10,  x0, 0
69
//addi x11,  x0, 0
70
//addi x12,  x0, 0
71 21 zero_gravi
  addi x13,  x0, 0
72
  addi x14,  x0, 0
73 20 zero_gravi
  addi x15,  x0, 0
74 2 zero_gravi
 
75
 
76
// *********************************************************
77
// Setup stack pointer
78
// *********************************************************
79
__crt0_stack_pointer_init:
80 12 zero_gravi
  lw    x11, SYSINFO_DSPACE_BASE(zero) // data memory space base address
81
  lw    x12, SYSINFO_DSPACE_SIZE(zero) // data memory space size
82 2 zero_gravi
  add   sp, x11, x12
83
  addi  sp, sp, -4 // stack pointer = last entry
84
  addi  fp, sp, 0  // frame pointer = stack pointer
85
 
86
 
87
// *********************************************************
88
// Setup global pointer
89
// *********************************************************
90
__crt0_global_pointer_init:
91 21 zero_gravi
.option push
92
.option norelax
93 2 zero_gravi
  la gp, __global_pointer$
94 21 zero_gravi
.option pop
95 2 zero_gravi
 
96
 
97
// *********************************************************
98 14 zero_gravi
// Init trap handler base address
99 2 zero_gravi
// *********************************************************
100 14 zero_gravi
__crt0_neorv32_trap_init:
101
  la    x11, __crt0_dummy_trap_handler
102 6 zero_gravi
  csrw  mtvec, x11 // set address of first-level exception handler
103 2 zero_gravi
 
104
 
105
// *********************************************************
106
// Reset/deactivate IO/peripheral devices
107
// Devices, that are not implemented, will cause a store access fault
108
// which is captured but actually ignored due to the dummy handler.
109
// *********************************************************
110
__crt0_reset_io:
111
  li x11, IO_BEGIN // start of processor-internal IO region
112
 
113
__crt0_reset_io_loop:
114
  sw   zero, 0(x11)
115
  addi x11, x11, 4
116
  bne  zero, x11, __crt0_reset_io_loop
117
 
118
 
119
// *********************************************************
120
// Clear .bss section (byte-wise)
121
// *********************************************************
122
__crt0_clear_bss:
123
  la x11, __crt0_bss_start
124
  la x12, __crt0_bss_end
125
 
126
__crt0_clear_bss_loop:
127
  bge  x11, x12, __crt0_clear_bss_loop_end
128
  sb   zero, 0(x11)
129
  addi x11, x11, 1
130
  j    __crt0_clear_bss_loop
131
 
132
__crt0_clear_bss_loop_end:
133
 
134
 
135
// *********************************************************
136
// Copy initialized .data section from ROM to RAM (byte-wise)
137
// *********************************************************
138
__crt0_copy_data:
139
  la x11, __crt0_copy_data_src_begin  // start of data area (copy source)
140
  la x12, __crt0_copy_data_dst_begin  // start of data area (copy destination)
141
  la x13, __crt0_copy_data_dst_end    // last address of destination data area
142
 
143
__crt0_copy_data_loop:
144
  bge  x12, x13,  __crt0_copy_data_loop_end
145
  lb   x14, 0(x11)
146
  sb   x14, 0(x12)
147
  addi x11, x11, 1
148
  addi x12, x12, 1
149
  j    __crt0_copy_data_loop
150
 
151
__crt0_copy_data_loop_end:
152
 
153
 
154
// *********************************************************
155
// Call main function (with argc = argv = 0)
156
// *********************************************************
157
__crt0_main_entry:
158
 
159
  addi x10, zero, 0 // argc = 0
160
  addi x11, zero, 0 // argv = 0
161
 
162
  jal ra, main
163
 
164
 
165
// *********************************************************
166
// Go to endless sleep mode if main returns
167
// *********************************************************
168
__crt0_this_is_the_end:
169 11 zero_gravi
  csrrci zero, mstatus, 8 // mstatus: disable global IRQs (MIE)
170 2 zero_gravi
  wfi
171 21 zero_gravi
  j . // in case WFI is not available
172 2 zero_gravi
 
173
 
174
// *********************************************************
175 14 zero_gravi
// dummy trap handler (for exceptions & IRQs)
176
// tries to move on to next instruction
177 2 zero_gravi
// *********************************************************
178 21 zero_gravi
.global __crt0_dummy_trap_handler
179
.balign 4
180 14 zero_gravi
__crt0_dummy_trap_handler:
181 2 zero_gravi
 
182 14 zero_gravi
  addi  sp, sp, -8
183
  sw      x8, 0(sp)
184
  sw      x9, 4(sp)
185 2 zero_gravi
 
186 14 zero_gravi
  csrr  x8, mcause
187
  blt   x8, zero, __crt0_dummy_trap_handler_irq  // skip mepc modification if interrupt
188 2 zero_gravi
 
189 14 zero_gravi
  csrr  x8, mepc
190 2 zero_gravi
 
191 14 zero_gravi
// is compressed instruction?
192
  lh    x9, 0(x8)   // get compressed instruction or lower 16 bits of uncompressed instruction that caused exception
193
  andi  x9, x9, 3   // mask: isolate lowest 2 opcode bits (= 11 for uncompressed instructions)
194 2 zero_gravi
 
195 14 zero_gravi
  addi  x8, x8, +2  // only this for compressed instructions
196
  csrw  mepc, x8    // set return address when compressed instruction
197 2 zero_gravi
 
198 14 zero_gravi
  addi  x8, zero, 3
199
  bne   x8, x9, __crt0_dummy_trap_handler_irq // jump if compressed instruction
200 7 zero_gravi
 
201 14 zero_gravi
// is uncompressed instruction
202
  csrr  x8, mepc
203
  addi  x8, x8, +2  // add another 2 (making +4) for uncompressed instructions
204
  csrw  mepc, x8
205 2 zero_gravi
 
206 14 zero_gravi
__crt0_dummy_trap_handler_irq:
207 2 zero_gravi
 
208 14 zero_gravi
  lw      x9, 0(sp)
209
  lw      x8, 4(sp)
210
  addi  sp, sp, +8
211 2 zero_gravi
 
212
  mret
213
 
214 21 zero_gravi
.cfi_endproc
215
.end

powered by: WebSVN 2.1.0

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