Write a program#
A Solux program is an ordinary bare-metal RISC-V program. It knows nothing about Solana: it prints to a serial port, reads keys from it, and powers off when it is done. If it runs on the local runner, it runs on-chain, instruction for instruction.
The machine#
| Processor | RV32IMA: 32-bit RISC-V with multiply/divide and atomics. Machine and user modes. No MMU, no compressed instructions, no floating-point unit. |
| RAM | At 0x8000_0000, as big as the RAM account (up to 10 MiB). It starts zeroed. |
| Entry | The ELF entry point, with a0 = 0 (hart id) and a1 = the device tree's address. |
| Serial port | 8250 UART at 0x1000_0000. Write a byte to +0 to print it. Bit 0 of +5 (line status) says a key is waiting; read it from +0. |
| Timer | CLINT at 0x1100_0000: mtime at +0xbff8, mtimecmp at +0x4000. mtime advances one tick per instruction, never with wall-clock time. |
| Power | syscon at 0x1110_0000: write 0x5555 to power off, 0x7777 to reboot. |
Floating point still works: Rust and C compile it to software routines for
rv32im targets, and the results are the same on every machine.
Start from the starter#
guest/starter is a complete program to copy: about 150 lines of Rust with a
print! macro, a line reader and a power-off. It totals the amounts you type.
cp -r guest/starter my-program && cd my-program
rustup target add riscv32im-unknown-none-elf
cargo build --release
Its three files do all the setup:
.cargo/config.tomlsets the target,riscv32im-unknown-none-elf.link.xputs the code at0x8000_0000and reserves a 64 KB stack after it.src/main.rssets the stack pointer in_start, then callsmain.
The parts you will reuse:
const UART: *mut u8 = 0x1000_0000 as _;
const UART_LSR: *const u8 = 0x1000_0005 as _;
const SYSCON: *mut u32 = 0x1110_0000 as _;
/// Waits for one typed byte (on-chain: from a Type transaction).
fn read_byte() -> u8 {
loop {
if unsafe { UART_LSR.read_volatile() } & 1 != 0 {
return unsafe { (UART as *const u8).read_volatile() };
}
}
}
fn power_off() -> ! {
unsafe { SYSCON.write_volatile(0x5555) };
loop {}
}
Run it locally first#
The runner loads your program exactly as the client uploads it and runs it on the same core. It prints what the program printed and how many Steps it takes.
cargo run --release -p runner -- my-program/target/riscv32im-unknown-none-elf/release/starter --ram 1M --type '> =>12.50\n7.25\n\n'
Total 19.75 (2 items)
exit: PowerOff at pc 0x80000488
instructions: 22204
chunks: 2 of 20000 instructions (one transaction each)
--type 'EXPECT=>KEYS' types KEYS once the program has printed EXPECT, like
someone at the keyboard. The playground does the same in the browser:
choose Your program and drop the ELF.
Other languages#
Anything that produces a 32-bit RISC-V ELF works. Link it at 0x8000_0000,
set up a stack, and talk to the devices above.
| Language | What to ask the toolchain for |
|---|---|
| Rust | riscv32im-unknown-none-elf (or riscv32imac-… without the c), no_std |
| C, C++ | GCC or Clang with -march=rv32ima -mabi=ilp32 -nostdlib, and the starter's link.x |
| Zig | a freestanding riscv32 target with the m and a features, no c |
| Linux programs | a nommu RISC-V 32-bit Linux image with your program in its initramfs, like the one the playground boots |
These are the requirements; only the Rust and Linux paths are tested in this repository today.
Rules that keep it verifiable#
- Use integers for money. The loan example keeps cents in
i64. Floats are deterministic here too, but integer cents are what an auditor expects. - Do not wait on time.
mtimecounts instructions, so a delay loop is a fixed number of Steps, not seconds. - Waiting for keys costs Steps. A program polling the serial port keeps running while nobody types. Crank it only when you have something to send.
- Power off at the end. It marks the machine
powered off, and every later Step fails withHalted, so nobody pays for a machine that is done.