Skip to content

SimRV Baremetal Programming & MMIO Guide

This guide details the physical memory map, MMIO device registers, and compilation workflow for developing baremetal programs to run on SimRV.


Physical Memory Map

SimRV uses a physical memory map where memory-mapped I/O (MMIO) and PCIe devices are allocated in lower address ranges, and RAM is mapped starting at 0x80000000.

Device / Region Base Address Size Description
Power Controller 0x00100000 0x00001000 (4 KB) SiFive test-finisher poweroff / reboot port
UART 16550A 0x10000000 0x00000100 (256 B) Serial console input/output port
VirtIO MMIO Slots 0x10001000 0x00008000 (32 KB) VirtIO MMIO transport slots (Disk, Net, Console, GPU, Input, Sound, RNG)
DMA Controller 0x10009000 0x00001000 (4 KB) Standalone interrupt-driven DMA controller (IRQ 12)
PCIe ECAM Space 0x30000000 0x10000000 (256 MB) PCI Express enhanced configuration space
PCIe 32-bit MMIO 0x40000000 0x10000000 (256 MB) PCI Express non-prefetchable 32-bit BAR window
PLIC / AIA APLIC 0x0C000000 / 0x50000000 0x04000000 (64 MB) Platform-Level Interrupt Controller / AIA APLIC
CLINT / ACLINT 0x02000000 / 0x60000000 0x000C0000 (768 KB) Core Local Interruptor (mtimecmp / software interrupts)
RTC 0x70000000 0x00001000 (4 KB) Real-Time Clock (mtime mirror)
System RAM 0x80000000 Configurable (e.g. 128 MB – 2 GB) Main physical memory space

The conventional base addresses and DMA region constants are shared with the firmware header PlatformMap.h. Platform configuration files may override individual SoC device descriptors.


Writing Baremetal Programs

To develop a baremetal program, you need:

  1. An assembly startup file (startup.S) to initialize the stack pointer and invoke your main entry point.
  2. A linker script (link.ld) to map the sections starting at the physical RAM base (0x80000000).
  3. An exit routine using the tohost termination register or Power MMIO device to signal simulation shutdown.

1. Linker Script (link.ld)

OUTPUT_ARCH( "riscv" )
ENTRY(_start)

SECTIONS
{
  . = 0x80000000;
  .text : {
    *(.text.init)
    *(.text .text.*)
  }
  .rodata : { *(.rodata .rodata.*) }
  .data : { *(.data .data.*) }
  .bss : { *(.bss .bss.*) }
}

2. Startup Assembly (startup.S)

Initialize the stack pointer sp to a safe region in RAM (e.g., 0x8E000000) and call main:

.section .text.init
.global _start
_start:
    # Set up stack pointer to 0x8E000000
    li sp, 0x8E000000

    # Call main C/C++ function
    jal main

    # Fallback exit: write 1 to tohost (0x80001000) or poweroff
_startup_exit:
    li t0, 0x00100000
    li t1, 0x5555
    sw t1, 0(t0)
loop:
    wfi
    j loop

3. Compiling and Packaging

Compile and link your source files as an ELF executable. SimRV loads the ELF segments, BSS, entry point, and symbols directly:

# Compile and Link
riscv64-unknown-elf-gcc -march=rv32imac -mabi=ilp32 -T link.ld startup.S main.c -o program.elf -ffreestanding -O2 -nostdlib

# Optional: create a flat image for environments that specifically require one
riscv64-unknown-elf-objcopy -O binary program.elf program.bin

4. Running the Program

Run the ELF in baremetal mode (-b / --baremetal). SimRV starts in interactive TUI mode by default:

# Interactive TUI mode (Default)
./build/rv32-release/simrv -b -m program.elf

# Headless / CLI-only mode (fast execution)
./build/rv32-release/simrv -b -m program.elf --mode fast --cli

# Cycle-accurate five-stage pipeline simulation
./build/rv32-release/simrv -b -m program.elf --mode cycle-accurate --cli

# 5-stage classic pipeline simulation
./build/rv32-release/simrv -b -m program.elf --mode cycle-accurate --pipeline 5stage --cli

Device Driver API Examples

UART 16550A Serial I/O

The UART registers are aligned to 4-byte boundaries (word-addressed) or 1-byte boundaries.

#define UART_BASE 0x10000000
#define UART_REG(offset) ((volatile uint32_t*)(UART_BASE + (offset)))

#define UART_THR UART_REG(0x00) // Transmitter Holding Register (write)
#define UART_LSR UART_REG(0x14) // Line Status Register (read)

void uart_putc(char c) {
    // Wait until Transmit Holding Register Empty (THRE) bit 5 is set
    while ((*UART_LSR & 0x20) == 0);
    *UART_THR = c;
}

void uart_puts(const char* s) {
    while (*s) {
        uart_putc(*s++);
    }
}

Real-Time Clock / Timer Delay

Use the RTC mtime register at 0x70000000 (or CLINT mtime at 0x0200bff8 / ACLINT mtime at 0x02007ff8) to measure platform delays. By default, mtime runs at 10 MHz (10,000 ticks = 1 ms), configurable via --timebase-hz <FREQ>. In cycle-accurate mode, timer progression is derived from the nominal CPU clock (--cpu-clock <FREQ>, default: 160 MHz) via a deterministic divider. In instruction-accurate mode it advances deterministically per retired instruction, not as a hardware timing prediction.

#define RTC_MTIME ((volatile uint64_t*)0x70000000)

uint32_t get_time_ms() {
    return (uint32_t)(*RTC_MTIME / 10000);
}

void delay_ms(uint32_t ms) {
    uint32_t start = get_time_ms();
    while (get_time_ms() - start < ms);
}

Power Controller (Shutdown & Reboot)

Write SiFive test-finisher commands to 0x00100000 to cleanly terminate or reboot the machine:

#define POWER_BASE ((volatile uint32_t*)0x00100000)

void power_shutdown() {
    *POWER_BASE = 0x5555; // Pass / Clean Poweroff
}

void power_reboot() {
    *POWER_BASE = 0x7777; // Reset / Warm Reboot
}

Generic DMA Controller (0x10009000, IRQ 12)

SimRV provides a hardware DMA controller accessible via standard 32-bit MMIO registers. Any baremetal firmware, RTOS, or operating system driver can perform zero-copy bulk transfers.

Include <simrv/device/simrv_dma.h> or define the registers directly:

#include <simrv/device/simrv_dma.h>

void test_dma_copy() {
    static char src[1024] = "Hello, high-speed DMA from baremetal!";
    static char dst[1024] = {0};

    // Synchronous memory-to-memory DMA copy
    int ret = simrv_dma_memcpy(SIMRV_DMA_BASE_ADDR, dst, src, sizeof(src));
    if (ret == 0) {
        uart_puts("DMA transfer completed successfully!\n");
    }
}

// Or manually using registers (polling or PLIC interrupt on IRQ 12):
#define DMA_REG(off) (*(volatile uint32_t*)(0x10009000 + (off)))

void dma_manual_transfer(uintptr_t src, uintptr_t dst, uint32_t bytes) {
    DMA_REG(0x08) = (uint32_t)(src & 0xFFFFFFFF); // SRC_ADDR_LO
    DMA_REG(0x0C) = (uint32_t)(src >> 32);         // SRC_ADDR_HI
    DMA_REG(0x10) = (uint32_t)(dst & 0xFFFFFFFF); // DST_ADDR_LO
    DMA_REG(0x14) = (uint32_t)(dst >> 32);         // DST_ADDR_HI
    DMA_REG(0x18) = bytes;                        // BYTE_COUNT
    DMA_REG(0x00) = 0x1;                          // START (Bit 0)

    // Wait for completion (Bit 2: BUSY)
    while (DMA_REG(0x00) & 0x4);

    // Acknowledge status and clear completion interrupt
    DMA_REG(0x1C) = 1;
}