uvm32 initial version

This commit is contained in:
Toby Jaffey 2025-12-06 16:44:23 +00:00
commit c9d30b6d28
34 changed files with 2088 additions and 0 deletions

5
apps/Dockerfile Normal file
View file

@ -0,0 +1,5 @@
FROM ubuntu:25.04
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
RUN apt-get -y update
RUN apt-get install -y gcc-riscv64-unknown-elf build-essential

15
apps/Makefile Normal file
View file

@ -0,0 +1,15 @@
DOCKER_IMAGE=riscv-dev
all:
docker build -t ${DOCKER_IMAGE} .
(cd sketch && make)
(cd helloworld && make)
(cd zig-mandel && make)
(cd rust-hello && make)
clean:
(cd sketch && make clean)
(cd helloworld && make clean)
(cd zig-mandel && make clean)
(cd rust-hello && make clean)

10
apps/crt0.s Normal file
View file

@ -0,0 +1,10 @@
.section .initial_jump , "ax", %progbits
.global _start
.align 4
_start:
# sp is already setup by vm
sw ra,12(sp)
jal ra, main
csrwi 0x138,0 # halt
.section .data

27
apps/helloworld/Makefile Normal file
View file

@ -0,0 +1,27 @@
PROJECT:=helloworld
DOCKER_IMAGE=riscv-dev
DOCKER_CMD:=docker run --rm -v ${PWD}../../../:/data -w /data/apps/${PROJECT} ${DOCKER_IMAGE}
PREFIX:=${DOCKER_CMD} riscv64-unknown-elf-
CFLAGS+=-I../../common
CFLAGS+=-fno-stack-protector
CFLAGS+=-static-libgcc -fdata-sections -ffunction-sections
CFLAGS+=-g -Os -march=rv32ima_zicsr -mabi=ilp32 -static
LDFLAGS:= -T ../linker.ld -nostdlib -Wl,--gc-sections
LIBS:= #-lgcc # needed for softfp
SRCS=${PROJECT}.c ../crt0.s
all:
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}
$(PREFIX)objcopy ${PROJECT}.elf -O binary ${PROJECT}.bin
disasm: all
$(PREFIX)objdump -S -d -f ${PROJECT}.elf
test: all
../../emulator/emulator ${PWD}/${PROJECT}.bin
clean:
rm -f ${PROJECT}.o ${PROJECT}.elf ${PROJECT}.bin

View file

@ -0,0 +1,7 @@
#define USE_MAIN
#include "uvm32_target.h"
void main(void) {
println("Hello world");
}

85
apps/linker.ld Normal file
View file

@ -0,0 +1,85 @@
/*__heap_size = 0x100; */
__stack_size = 0x100;
ENTRY(_start)
SECTIONS
{
. = 0x80000000;
/*
.header : ALIGN( 16 )
{
LONG( 0 )
LONG( 0 )
}
*/
.text : ALIGN(16) {
__TEXT_BEGIN__ = .;
*(.initial_jump)
*(.entry.text)
*(.init.literal)
*(.init)
*(.text)
*(.literal .text .literal.* .text.* .stub)
*(.out_jump.literal.*)
*(.out_jump.*)
__TEXT_END__ = .;
}
/* If we're on a newer compiler */
/DISCARD/ :
{
*(.interp)
*(.dynsym)
*(.dynstr)
*(.header)
} : phdr
.data : ALIGN(16) {
__DATA_BEGIN__ = .;
*(.rodata)
*(.rodata.*)
*(.gnu.linkonce.r.*)
*(.rodata1)
*(.dynsbss)
*(.gnu.linkonce.sb.*)
*(.scommon)
*(.gnu.linkonce.sb2.*)
*(.sbss)
*(.sbss.*)
*(.sbss2)
*(.sbss2.*)
*(.dynbss)
*(.data)
*(.data.*)
*(.got)
*(.got.*)
__DATA_END__ = .;
}
.bss : ALIGN( 16 ) {
__BSS_BEGIN__ = .;
*(.bss) /* Tricky: BSS needs to be allocated but not sent. GCC Will not populate these for calculating data size */
*(.bss.*)
__BSS_END__ = .;
}
/*
.heap : ALIGN( 16 ) {
_sheap = .;
. = . + __heap_size;
_eheap = .;
}
*/
/*
.stack : ALIGN( 16 ) {
_estack = .;
. = . + __stack_size;
_sstack = .;
}
*/
/* _sstack = .;*/
}

235
apps/rust-hello/Cargo.lock generated Normal file
View file

@ -0,0 +1,235 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "aho-corasick"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
dependencies = [
"memchr",
]
[[package]]
name = "bindgen"
version = "0.71.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f58bf3d7db68cfbac37cfc485a8d711e87e064c3d0fe0435b92f7a407f9d6b3"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"itertools",
"log",
"prettyplease",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn",
]
[[package]]
name = "bitflags"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "clang-sys"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "glob"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "libc"
version = "0.2.178"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
[[package]]
name = "libloading"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [
"cfg-if",
"windows-link",
]
[[package]]
name = "log"
version = "0.4.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "panic-halt"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11"
[[package]]
name = "prettyplease"
version = "0.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
[[package]]
name = "rust-hello"
version = "0.0.1"
dependencies = [
"bindgen",
"panic-halt",
]
[[package]]
name = "rustc-hash"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.111"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

View file

@ -0,0 +1,19 @@
[package]
name = "rust-hello"
version = "0.0.1"
edition = "2021"
[dependencies]
panic-halt = "1.0.0"
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
opt-level = "z" # Optimize for size
lto = true
strip = true
[build-dependencies]
bindgen = "0.71.0"

8
apps/rust-hello/Makefile Normal file
View file

@ -0,0 +1,8 @@
all:
cargo build -r --target riscv32im-unknown-none-elf && docker run -v `pwd`:/data -w /data --rm riscv-dev riscv64-unknown-elf-objcopy target/riscv32im-unknown-none-elf/release/rust-hello -O binary rust-hello.bin
test: all
../../emulator/emulator rust-hello.bin
clean:
rm -rf rust-hello.bin target

21
apps/rust-hello/build.rs Normal file
View file

@ -0,0 +1,21 @@
use std::env;
use std::path::PathBuf;
fn main() {
// linker
println!("cargo:rustc-link-arg-bin=rust-hello=-T../linker.ld");
let bindings = bindgen::Builder::default()
.header("../../common/uvm32_sys.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

View file

@ -0,0 +1,48 @@
#![no_std]
#![no_main]
use core::arch::global_asm;
use core::arch::asm;
use core::panic::PanicInfo;
// fetch IOREQ definitions from C header
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
// startup code
global_asm!(include_str!("../../crt0.s"));
fn println(message: &str) {
unsafe {
asm!(
"csrw {i}, {x}",
i = const IOREQ_PRINTLN,
x = in(reg) message.as_ptr(),
);
}
}
fn printd(n: u32) {
unsafe {
asm!(
"csrw {i}, {x}",
i = const IOREQ_PRINTD,
x = in(reg) n,
);
}
}
#[no_mangle]
pub extern "C" fn main() {
for i in 0..10 {
printd(i);
}
println("Hello, world!");
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
//println("Something went wrong");
loop {
continue;
}
}

27
apps/sketch/Makefile Normal file
View file

@ -0,0 +1,27 @@
PROJECT:=sketch
DOCKER_IMAGE=riscv-dev
DOCKER_CMD:=docker run --rm -v ${PWD}../../../:/data -w /data/apps/${PROJECT} ${DOCKER_IMAGE}
PREFIX:=${DOCKER_CMD} riscv64-unknown-elf-
CFLAGS+=-I../../common
CFLAGS+=-fno-stack-protector
CFLAGS+=-static-libgcc -fdata-sections -ffunction-sections
CFLAGS+=-g -Os -march=rv32ima_zicsr -mabi=ilp32 -static
LDFLAGS:= -T ../linker.ld -nostdlib -Wl,--gc-sections
LIBS:= #-lgcc # needed for softfp
SRCS=${PROJECT}.c ../crt0.s
all:
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}
$(PREFIX)objcopy ${PROJECT}.elf -O binary ${PROJECT}.bin
disasm: all
$(PREFIX)objdump -S -d -f ${PROJECT}.elf
test: all
../../emulator/emulator ${PWD}/${PROJECT}.bin
clean:
rm -f ${PROJECT}.o ${PROJECT}.elf ${PROJECT}.bin

18
apps/sketch/sketch.c Normal file
View file

@ -0,0 +1,18 @@
#include "uvm32_target.h"
uint32_t count;
bool loop(void) {
printd(count);
if (count++ >= 10) {
return false;
} else {
return true;
}
}
void setup(void) {
count = 0;
}

12
apps/zig-mandel/Makefile Normal file
View file

@ -0,0 +1,12 @@
all:
# zig's objcopy is broken, so use external tool
# https://ziggit.dev/t/addobjcopy-producing-zero-padding-at-start-of-binary/13384
zig build && docker run -v `pwd`:/data -w /data --rm riscv-dev riscv64-unknown-elf-objcopy zig-out/bin/mandel -O binary mandel.bin
clean:
rm -rf mandel.bin zig-out .zig-cache
test: all
../../emulator/emulator mandel.bin

51
apps/zig-mandel/build.zig Normal file
View file

@ -0,0 +1,51 @@
const std = @import("std");
const CrossTarget = @import("std").zig.CrossTarget;
const Target = @import("std").Target;
const Feature = @import("std").Target.Cpu.Feature;
pub fn build(b: *std.Build) void {
const features = Target.riscv.Feature;
var disabled_features = Feature.Set.empty;
var enabled_features = Feature.Set.empty;
// disable all CPU extensions
disabled_features.addFeature(@intFromEnum(features.a));
disabled_features.addFeature(@intFromEnum(features.c));
disabled_features.addFeature(@intFromEnum(features.d));
disabled_features.addFeature(@intFromEnum(features.e));
disabled_features.addFeature(@intFromEnum(features.f));
// except multiply
enabled_features.addFeature(@intFromEnum(features.m));
const target = b.resolveTargetQuery(.{
.cpu_arch = Target.Cpu.Arch.riscv32,
.os_tag = Target.Os.Tag.freestanding,
.abi = Target.Abi.none,
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32},
.cpu_features_sub = disabled_features,
.cpu_features_add = enabled_features
});
const exe = b.addExecutable(.{
.name = "mandel",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .ReleaseSmall,
}),
});
b.installArtifact(exe);
exe.addAssemblyFile(b.path("../crt0.s"));
exe.setLinkerScript(b.path("../linker.ld"));
exe.addIncludePath(b.path("../../common"));
const bin = b.addObjCopy(exe.getEmittedBin(), .{
.format = .bin,
});
bin.step.dependOn(&exe.step);
const copy_bin = b.addInstallBinFile(bin.getOutput(), "mandel.bin");
b.default_step.dependOn(&copy_bin.step);
}

View file

@ -0,0 +1,40 @@
const uvm = @import("uvm.zig");
fn mandel() void {
const xmin: i32 = -8601;
const xmax: i32 = 2867;
const ymin: i32 = -4915;
const ymax: i32 = 4915;
const maxiter: usize = 32;
const dx: i32 = @divTrunc((xmax - xmin), 79);
const dy: i32 = @divTrunc((ymax - ymin), 24);
var cy = ymin;
while (cy <= ymax) {
var cx = xmin;
while (cx <= xmax) {
var x: i32 = 0;
var y: i32 = 0;
var x2: i32 = 0;
var y2: i32 = 0;
var iter: usize = 0;
while (iter < maxiter) : (iter += 1) {
if (x2 + y2 > 16384) break;
y = ((x * y) >> 11) + cy;
x = x2 - y2 + cx;
x2 = (x * x) >> 12;
y2 = (y * y) >> 12;
uvm.yield();
}
uvm.printc(' ' + @as(u8, @intCast(iter)));
cx += dx;
}
uvm.printc('\n');
cy += dy;
}
}
export fn main() void {
mandel();
uvm.println("Hello world");
}

View file

@ -0,0 +1,38 @@
const uvm32 = @cImport({
@cDefine("USE_MAIN", "1");
@cInclude("uvm32_target.h");
});
const std = @import("std");
pub inline fn println(val: [:0]const u8) void {
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTLN}) ++ ", %[arg1]"
:
: [arg1] "r" (val.ptr),
);
}
pub inline fn printd(val: u32) void {
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTD}) ++ ", %[arg1]"
:
: [arg1] "r" (val),
);
}
pub inline fn printx(val: u32) void {
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTX}) ++ ", %[arg1]"
:
: [arg1] "r" (val),
);
}
pub inline fn printc(val: u32) void {
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTC}) ++ ", %[arg1]"
:
: [arg1] "r" (val),
);
}
pub inline fn yield() void {
asm volatile (std.fmt.comptimePrint("csrwi 0x{x}, 0", .{uvm32.IOREQ_YIELD}));
}