An assembler for JIT compilation for use on ESP32-C3 (rv32imc)
rustup toolchain install nightly
rustup target add riscv32imc-unknown-none-elf
cargo install ldproxy
cargo install cargo-espflash
cargo build --release
To flash and run:
cargo espflash --release --monitor
If you get a certificate error on macOS:
curl "https://ccadb-public.secure.force.com/mozilla/IncludedRootsPEMTxt?TrustBitsInclude=Websites" > IncludedRootsPEM.txt
SSL_CERT_FILE=$(pwd)/IncludedRootsPEM.txt cargo build
To test locally (replace aarch64-apple-darwin
with your system's target triple, use rustup target list
to find installed targets):
cargo run --example assemble --target aarch64-apple-darwin -- ~/Desktop/test2.bin
The above will write a binary that you can upload in e.g. this disassembler.
cd rv32assembler
cargo test --target aarch64-apple-darwin
fn test_jit_simple() {
let mut f = Fragment::new();
f.addi(Register::A0, Register::A0, 42);
f.ret();
let program = JitFunction::from(&f);
let s = unsafe { program.call(88) };
assert_eq!(s, 88 + 42);
}
If you want to use this crate in your own project, be sure to set the following option in sdkconfig.defaults
:
# Required to allow JIT
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n
Useful reference material:
- RISC-V reference card
- RV32I Base Integer Instruction Set, Version 2.1
- Online RISC-V assembler
- Online RISC-V disassembler
- Another RISC-V assembler in Rust
- Berkeley CS61C RISC-V instruction formats lecture
- Sign extension
To create a binary file from "[1, 2, 3]" debug output:
const fs = require("fs");
fs.writeFileSync("foo.bin", Buffer.from([1, 2, 3]));