Rework host/target interface to use ecall and proper syscalls instead of CSRs

This commit is contained in:
Toby Jaffey 2025-12-08 02:44:38 +00:00
parent a305fce466
commit 751f068486
26 changed files with 360 additions and 364 deletions

View file

@ -1,10 +0,0 @@
.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

View file

@ -10,7 +10,7 @@ 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
SRCS=${PROJECT}.c ../crt0.S
all:
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}

View file

@ -10,7 +10,7 @@ CFLAGS+=-g -Os -march=rv32ima_zicsr -mabi=ilp32 -static
LDFLAGS:= -T ../linker.ld -nostdlib -Wl,--gc-sections
LIBS:= #-lgcc # needed for softfp
SRCS=hello-asm.s
SRCS=hello-asm.S
all:
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}

View file

@ -1,9 +0,0 @@
.section .initial_jump , "ax", %progbits
.global _start
_start:
la a5, str
csrrw zero,0x13b,a5 # println
csrwi 0x138,0 # halt
str:
.ascii "Hi"

View file

@ -10,7 +10,7 @@ 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
SRCS=${PROJECT}.c ../crt0.S
all:
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}

View file

@ -2,6 +2,18 @@
#include "uvm32_target.h"
void main(void) {
println("Hello world");
for (int i=0;i<10;i++) {
printd(i);
}
#if 0
uint32_t c;
while(c = getc()) {
if (c != 0xFFFFFFFF) {
print("Got: ");
printx(c);
println("");
}
}
#endif
}

View file

@ -9,26 +9,27 @@ use core::panic::PanicInfo;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
// startup code
global_asm!(include_str!("../../crt0.s"));
global_asm!(include_str!("../../crt0.S"));
fn println(message: &str) {
fn syscall(id: u32, n: u32) -> u32 {
let mut value;
unsafe {
asm!(
"csrw {i}, {x}",
i = const IOREQ_PRINTLN,
x = in(reg) message.as_ptr(),
asm!("ecall",
in("a0") n,
in("a7") id,
lateout("a1") value,
);
}
return value;
}
fn println(message: &str) {
let addr_value = message.as_ptr() as u32;
syscall(IOREQ_PRINTLN, addr_value);
}
fn printd(n: u32) {
unsafe {
asm!(
"csrw {i}, {x}",
i = const IOREQ_PRINTD,
x = in(reg) n,
);
}
syscall(IOREQ_PRINTD, n);
}
#[no_mangle]
@ -36,7 +37,7 @@ pub extern "C" fn main() {
for i in 0..10 {
printd(i);
}
println("Hello, world!");
println("Hello, world!\0");
}
#[panic_handler]

View file

@ -10,7 +10,7 @@ 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
SRCS=${PROJECT}.c ../crt0.S
all:
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}

View file

@ -4,35 +4,25 @@ const uvm32 = @cImport({
});
const std = @import("std");
pub inline fn syscall(id: u32, param: u32) u32 {
var val: u32 = undefined;
asm volatile ("ecall"
: [val] "={a1}" (val),
: [param] "{a0}" (param),
[id] "{a7}" (id),
: .{ .memory = true });
return val;
}
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),
);
_ = syscall(uvm32.IOREQ_PRINTLN, @intFromPtr(val.ptr));
}
pub inline fn yield() void {
asm volatile (std.fmt.comptimePrint("csrwi 0x{x}, 0", .{uvm32.IOREQ_YIELD}));
_ = syscall(uvm32.IOREQ_YIELD, 0);
}
pub inline fn printc(c:u8) void {
_ = syscall(uvm32.IOREQ_PRINTC, c);
}

View file

@ -4,70 +4,52 @@ const uvm32 = @cImport({
});
const std = @import("std");
// dupeZ would be better, but want to avoid using an allocator
var new_buf:[128]u8 = undefined;
pub inline fn print(m: []const u8) void {
@memcpy(new_buf[0..m.len], m);
new_buf[m.len] = 0;
const s = new_buf[0..m.len :0];
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINT}) ++ ", %[arg1]"
:
: [arg1] "r" (s.ptr),
);
pub inline fn syscall(id: u32, param: u32) u32 {
var val: u32 = undefined;
asm volatile ("ecall"
: [val] "={a1}" (val),
: [param] "{a0}" (param),
[id] "{a7}" (id),
: .{ .memory = true });
return val;
}
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}));
}
var millis_storage:u32 = 0;
pub inline fn millis() u32 {
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_MILLIS}) ++ ", %[arg1]"
:
: [arg1] "r" (&millis_storage),
);
return millis_storage;
}
var getch_storage:u32 = 0;
pub inline fn getch() ?u8 {
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_GETC}) ++ ", %[arg1]"
:
: [arg1] "r" (&getch_storage),
);
if (getch_storage <= 0xFF) {
return @truncate(getch_storage);
} else {
const key = syscall(uvm32.IOREQ_GETC, 0);
if (key == 0xFFFFFFFF) {
return null;
} else {
return @truncate(key);
}
}
pub inline fn millis() u32 {
return syscall(uvm32.IOREQ_MILLIS, 0);
}
// dupeZ would be better, but want to avoid using an allocator
// this is of course, unsafe...
var termination_buf:[128]u8 = undefined;
pub inline fn print(m: []const u8) void {
@memcpy(termination_buf[0..m.len], m);
termination_buf[m.len] = 0;
const s = termination_buf[0..m.len :0];
_ = syscall(uvm32.IOREQ_PRINT, @intFromPtr(s.ptr));
}
pub inline fn println(m: []const u8) void {
@memcpy(termination_buf[0..m.len], m);
termination_buf[m.len] = 0;
const s = termination_buf[0..m.len :0];
_ = syscall(uvm32.IOREQ_PRINTLN, @intFromPtr(s.ptr));
}
pub inline fn yield() void {
_ = syscall(uvm32.IOREQ_YIELD, 0);
}
pub inline fn printc(c:u8) void {
_ = syscall(uvm32.IOREQ_PRINTC, c);
}