libriscv 1.0.1
libriscv: ^1.0.1 copied to clipboard
A RISC-V emulator in pure Dart supporting RV32/RV64 with M, A, F, D, C extensions, virtual memory, SBI runtime, and Linux/xv6 boot.
import 'dart:typed_data';
import 'package:libriscv/libriscv.dart';
/// Minimal example: create an RV32 emulator, load a program, and run it.
void main() {
// Build the emulator with desired extensions
final emulator = EmulatorBuilder()
.xlen(XLEN.rv32)
.memorySize(4 * 1024 * 1024)
.enableRV32M(true)
.enableRV32A(true)
.enableZicsr(true)
.build();
// Create 4 MB of RAM and map it at the standard base address
final ram = Uint8List(4 * 1024 * 1024);
emulator.cpu.memory.add(
BasicMemoryDevice.from(ByteData.view(ram.buffer), MemoryProtections.rwx),
0x80000000,
);
// Write a tiny program: ADDI x1, x0, 42 followed by EBREAK
// addi x1, x0, 42 -> 0x02a00093
// ebreak -> 0x00100073
final view = ByteData.view(ram.buffer);
view.setUint32(0, 0x02a00093, Endian.little); // addi x1, x0, 42
view.setUint32(4, 0x00100073, Endian.little); // ebreak
// Set the program counter to the start of RAM
emulator.cpu.pc = 0x80000000;
// Execute two instructions
emulator.step(2);
// x1 should now contain 42
print('x1 = ${emulator.cpu.x[1]}'); // prints: x1 = 42
}