I created a separate boot sequence file called boot.cs
# boot.s
.align 2
# Stack section (adjust the size as per your requirements)
.section .bss
_stack_start:
.space 4096 # 4KB stack size (adjust as needed)
.section .text
# -----------------------------------------------------------------------
# Initialization boot sequence for Sparkfun Red-V board
# -----------------------------------------------------------------------
.globl _boot_init
_boot_init:
csrr t0, mhartid # Get machine hardware thread 0 (multi threads will run in parallel the moment the CPU gets powered on)
bnez t0, halt # If no machine hardware thread 0 0 then halt
# Set stack pointer
la sp, _estack
bss_clear_init:
# Clear .bss
la a0, _sbss
la a1, _ebss
bgeu a0, a1, skip_bss_clear
bss_clear_loop:
sw zero, 0(a0)
addi a0, a0, 4
bltu a0, a1, bss_clear_loop
skip_bss_clear:
data_init:
# Initialize .data
la a0, _sdata
la a1, _edata
la a2, _data_flash
bgeu a0, a1, skip_data_init
data_init_loop:
lw t0, 0(a2)
sw t0, 0(a0)
addi a0, a0, 4
addi a2, a2, 4
bltu a0, a1, data_init_loop
skip_data_init:
ret
# Infinite loop after main finishes
halt:
j halt
It basically does this:
- Get thread 0
- Set up stack pointer.
- Set memory of the .bss section to zero. because at startup it probably has some random bits.
- .bss section is the uninitialized data
- Copy from flash memory the content of the .data section
- .data section contains data that has has initialization.
This is called from main.cs
# main.s
.align 2
.include "./src/data/data.s"
.include "./src/data/rodata.s"
.include "./src/data/bss.s"
.section .text
.globl _start
_start:
jal _boot_init # Initialize boot sequence for Sparkfun Red-V board
# Your code goes here
halt:
j halt
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.