Close

Boot sequence

A project log for Sparkfun RED-V bare metal

I wanted to learn RISC-V, not on an emulator, but on a real bare metal RISC-V processor. Even though Sparkfun RED-V is no longer produced.

olaf-baeyensOlaf Baeyens 10/21/2024 at 17:030 Comments

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:

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