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

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}));
}