Make a host in Rust, step 1: run a program to its end
This step reads a program file, sets up memory and runs the processor until the program calls exit. It is the library of the crate in examples/host/rust (modules image, decode and cpu) and the program step1. You need Rust 1.88 or later. The crate is a workspace of its own, apart from the Tersenet sources' workspace.
The program file
A program file (.terse) is a 32-byte header, two bytes for each device the program uses, then the ROM: the program's code and data. load checks the header and makes the processor at reset: the ROM at 0001_0000h; the RAM, as many 8 KiB pages as the header asks for, all zero, at 0010_0000h; pc on the first byte of ROM and the stack pointer x2 at the end of RAM. The header:
| Offset | Size | Field |
|---|---|---|
| 0 | 6 | TERS, format version 0, machine profile 1 |
| 6 | 3 | the ROM's length, at most 262,144 bytes |
| 9 | 1 | RAM pages of 8 KiB, 1 to 96 |
| 10 | 1 | the number of devices, at most 16 |
| 32 | 2 each | each device: its slot and version (80h: optional) |
Bytes 11 to 31 hold a title, which a host does not need. The rest is in The Terse32 machine, "The image format".
use crate::cpu::{Cpu, RAM_BASE, ROM_BASE, SP}; const HEADER_SIZE: usize = 32; const MAGIC: &[u8] = b"TERS\0\x01"; const ROM_LEN_AT: usize = 6; const RAM_PAGES_AT: usize = 9; const DEVICES_AT: usize = 10; const MAX_ROM: usize = 262144; const MAX_RAM_PAGES: usize = 96; const MAX_DEVICES: usize = 16; pub const DEVICE_SIZE: usize = 2; pub const PAGE_SIZE: usize = 8192; const OPTIONAL: u8 = 0x80; pub struct Device { pub slot: u8, pub version: u8, pub optional: bool, } pub fn load(file: &[u8]) -> Result<(Cpu, Vec<Device>), String> { if file.len() < HEADER_SIZE || !file.starts_with(MAGIC) { return Err("not a Terse32 program".into()); } let r = &file[ROM_LEN_AT..]; let rom_len = u32::from_le_bytes([r[0], r[1], r[2], 0]) as usize; let pages = file[RAM_PAGES_AT] as usize; let count = file[DEVICES_AT] as usize; let rom_at = HEADER_SIZE + DEVICE_SIZE * count; if !(1..=MAX_ROM).contains(&rom_len) || !(1..=MAX_RAM_PAGES).contains(&pages) || count > MAX_DEVICES || file.len() != rom_at + rom_len { return Err("a malformed program file".into()); } let devices = file[HEADER_SIZE..rom_at] .chunks(DEVICE_SIZE) .map(|d| Device { slot: d[0], version: d[1] & !OPTIONAL, optional: d[1] & OPTIONAL != 0, }) .collect(); let mut cpu = Cpu { x: [0; 32], pc: ROM_BASE, rom: file[rom_at..].to_vec(), ram: vec![0; pages * PAGE_SIZE], }; cpu.x[SP] = RAM_BASE + cpu.ram.len() as u32; Ok((cpu, devices)) }
The list of devices matters from step 3 on.
Decoding
The processor is RV32IMC: RISC-V's 32-bit integer instructions, multiply and divide (M) and 16-bit compressed forms of the common ones (C). decode takes a 32-bit instruction apart into its fields:
#[derive(Clone, Copy, Default)] pub struct Instruction { pub op: u32, pub f3: u32, pub f7: u32, pub rd: u32, pub rs1: u32, pub rs2: u32, pub imm: u32, } pub fn sext(v: u32, bits: u32) -> u32 { ((v << (32 - bits)) as i32 >> (32 - bits)) as u32 } pub fn decode(i: u32) -> Instruction { let op = i & 0x7f; let imm = match op { 0x37 | 0x17 => i & !0xfff, 0x6f => sext( i >> 11 & 0x100000 | i & 0xff000 | i >> 9 & 0x800 | i >> 20 & 0x7fe, 21, ), 0x63 => sext( i >> 19 & 0x1000 | i << 4 & 0x800 | i >> 20 & 0x7e0 | i >> 7 & 0x1e, 13, ), 0x23 => sext(i >> 20 & 0xfe0 | i >> 7 & 31, 12), _ => sext(i >> 20, 12), }; Instruction { op, f3: i >> 12 & 7, f7: i >> 25, rd: i >> 7 & 31, rs1: i >> 15 & 31, rs2: i >> 20 & 31, imm, } }
expand gives a 16-bit instruction the same fields, as the 32-bit instruction it stands for, so that one execute runs both. After the immediates, whose bits the 16-bit forms scatter, its first cases are:
let none = Instruction::default(); let i = Instruction { rd, rs1: rd, ..none }; let compact = Instruction { rd: rdc, rs1: rdc, ..none }; match (c & 3) << 3 | c >> 13 { 0o00 if four != 0 => Instruction { op: 0x13, rd: rs2c, rs1: 2, imm: four, ..none }, 0o02 => Instruction { op: 0x03, f3: 2, rd: rs2c, rs1: rdc, imm: word, ..none }, 0o06 => Instruction { op: 0x23, f3: 2, rs1: rdc, rs2: rs2c, imm: word, ..none }, 0o10 => Instruction { op: 0x13, imm, ..i }, 0o11 => { Instruction { op: 0x6f, rd: 1, imm: jump, ..none } } 0o12 => Instruction { op: 0x13, rs1: 0, imm, ..i },
and so on for the other 16-bit forms, 140 lines in all. A form the machine does not have, or one the specification reserves, such as an immediate of zero where one is needed, comes out as none, whose opcode 0 is no instruction at all.
Executing
step fetches one instruction from ROM or RAM, 16 or 32 bits, and runs it. It returns what the instruction costs, which step 2 uses, and whether it was an ecall, or a fault:
pub fn step(&mut self) -> Result<(i64, bool), Fault> { let mut raw = self.parcel(self.pc)?; let (instruction, next) = if raw & 3 == 3 { raw |= self.parcel(self.pc + 2)? << 16; (decode(raw), self.pc + 4) } else { (expand(raw), self.pc + 2) }; let ram = self.pc >= RAM_BASE; let cost = if ram { RAM_COST } else { ROM_COST }; match self.execute(instruction, next) { Err(f) if f.kind == Kind::Illegal => Err(Fault { kind: Kind::Illegal, address: self.pc, encoding: raw, }), result => result.map(|ecall| (cost, ecall)), } }
execute is one match on the opcode, with the encoding's checks as guards. Each arm gives the value for rd (never written to x0), if any, and moves pc on; anything no arm takes is an illegal instruction:
fn execute(&mut self, i: Instruction, mut next: u32) -> Result<bool, Fault> { let (pc, link) = (self.pc, next); let a = self.x[i.rs1 as usize]; let b = self.x[i.rs2 as usize]; let shifts = i.f3 & 3 == 1; let arithmetic = i.f3 == 5 && i.f7 == 32; let alternate = arithmetic || i.f3 == 0 && i.f7 == 32; let system = i.f3 | i.rd | i.rs1 == 0; let value = match i.op { 0x37 => Some(i.imm), 0x17 => Some(pc.wrapping_add(i.imm)), 0x6f => { next = pc.wrapping_add(i.imm); Some(link) } 0x67 if i.f3 == 0 => { next = a.wrapping_add(i.imm) & !1; Some(link) } 0x63 if i.f3 >> 1 != 1 => { if branches(i.f3, a, b) { next = pc.wrapping_add(i.imm); } None } 0x03 if matches!(i.f3, 0 | 1 | 2 | 4 | 5) => { Some(self.load(a.wrapping_add(i.imm), i.f3)?) } 0x23 if i.f3 <= 2 => { self.store(a.wrapping_add(i.imm), i.f3, b)?; None } 0x13 if !shifts || i.f7 == 0 || arithmetic => { Some(alu(i.f3, arithmetic, a, i.imm)) } 0x33 if i.f7 == 1 => Some(muldiv(i.f3, a, b)), 0x33 if i.f7 == 0 || alternate => { Some(alu(i.f3, alternate, a, b)) } 0x0f if i.f3 == 0 => None, 0x73 if system && i.imm == 0 => None, 0x73 if system && i.imm == 1 => { return Err(Fault::at(Kind::Breakpoint, pc)); } _ => return Err(Fault::at(Kind::Illegal, pc)), }; if let Some(v) = value && i.rd != 0 { self.x[i.rd as usize] = v; } self.pc = next; Ok(i.op == 0x73) }
All arithmetic wraps, as the processor's does. The rest of cpu.rs is alu and muldiv (division by zero gives RISC-V's answers, not a trap), the branch conditions, and memory: access faults on a load or store that is misaligned or outside RAM (and ROM, for a load), checking alignment first.
A fault stops the machine with pc on the faulting instruction, which is not carried out: an encoding the machine does not have (ebreak and the CSR instructions among them), a fetch, load or store outside memory, or a misaligned load or store.
Calls
ecall calls a device: a7 (x17) holds the device's slot × 256 + the function, a0 to a5 (x10 to x15) the arguments, and the result goes in a0. cpu.rs names these registers, the stack pointer and the result of a call the machine does not have, FFFF_FFFFh:
pub const SP: usize = 2; pub const A0: usize = 10; pub const A1: usize = 11; pub const A5: usize = 15; pub const A7: usize = 17; pub const UNKNOWN_CALL: u32 = 0xffff_ffff;
This step has one call, exit, 106h; every other call returns that result, as a call the machine does not have must:
use host::cpu::{A0, A7, UNKNOWN_CALL}; use host::image::load; use std::error::Error; use std::process::exit; const EXIT: u32 = 0x106; fn main() -> Result<(), Box<dyn Error>> { let args: Vec<String> = std::env::args().collect(); let file = std::fs::read(&args[1])?; let (mut cpu, _) = load(&file)?; loop { match cpu.step() { Err(fault) => { eprintln!("{fault}"); exit(1) } Ok((_, true)) if cpu.x[A7] == EXIT => { exit(cpu.x[A0] as u8 as i32) } Ok((_, true)) => cpu.x[A0] = UNKNOWN_CALL, Ok(_) => {} } } }
Run it
Build all four steps at once:
cd examples/host/rust cargo build --release
A program that exits with code 7, c.li a0, 7, addi a7, x0, 106h and ecall, as a file:
xxd -r -p > exit.terse <<EOF 5445525300010a000001000000000000657869740000 000000000000000000001d459308601073000000 EOF target/release/step1 exit.terse echo $?
prints 7. This one adds 100 + 99 + ... + 1 and exits with the sum's low byte:
xxd -r -p > sum.terse <<EOF 544552530001160000010000000000007375 6d0000000000000000000000000001459302 40061695fd12e39e02fe9308601073000000 EOF target/release/step1 sum.terse echo $?
prints 186. The reference gives the same: terse run exit.terse --frames 1, then echo $?.
What you have now
A Terse32 processor that runs a program to its end: cpu.rs (219 lines), decode.rs (183), image.rs (54) and bin/step1.rs (25). Next, make it keep time: Make a host in Rust, step 2: frames and the budget.
- The same step in Go: "Make a host in Go, step 1: run a program to its end", the page before this one.
- Full implementations:
native/core-c/cpu.c(C) andcrates/terse-core/src/cpu.rs(the reference). - The rules: The Terse32 machine, sections "CPU", "Memory map", "Reset state", "Stopping and faults", "The image format" and "Calls".