mirror of
https://github.com/Noratrieb/rustv32i.git
synced 2026-01-14 21:35:02 +01:00
make it work
This commit is contained in:
parent
29bb73425b
commit
ab8e4ebc13
10 changed files with 608 additions and 186 deletions
|
|
@ -1,5 +1,12 @@
|
|||
CC = clang -Wall -Wpedantic -target riscv32-unknown-linux-gnu -fuse-ld=lld -march=rv32i
|
||||
CC_STATIC = $(CC) -static -nostdlib -nodefaultlibs
|
||||
|
||||
RUSTC = rustc --target riscv32i-unknown-none-elf
|
||||
|
||||
all: init init1
|
||||
|
||||
init: init.c
|
||||
$(CC_STATIC) init.c -o init
|
||||
|
||||
init1: init1.rs
|
||||
$(RUSTC) init1.rs
|
||||
|
|
|
|||
17
test/init.c
17
test/init.c
|
|
@ -1,11 +1,20 @@
|
|||
void _start(void)
|
||||
// <https://jborza.com/post/2021-05-11-riscv-linux-syscalls/>
|
||||
|
||||
_Noreturn void __attribute__ ((noinline)) exit(int code)
|
||||
{
|
||||
__asm__ volatile (
|
||||
__asm__ volatile(
|
||||
"li a7, 93;" // exit
|
||||
"li a0, 0;" // code 0
|
||||
"mv a0, %0;" // code
|
||||
"ecall"
|
||||
:
|
||||
:
|
||||
: "r"(code)
|
||||
: "a7", "a0", "memory"
|
||||
);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
_Noreturn void _start(void)
|
||||
{
|
||||
|
||||
exit(10);
|
||||
}
|
||||
|
|
|
|||
73
test/init1.rs
Normal file
73
test/init1.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::fmt::Write;
|
||||
|
||||
fn exit(code: i32) -> ! {
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"li a7, 93",
|
||||
"ecall",
|
||||
in("a0") code,
|
||||
options(noreturn),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn write(fd: i32, data: &[u8]) -> isize {
|
||||
let mut out;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"li a7, 64",
|
||||
"ecall",
|
||||
in("a0") fd,
|
||||
in("a1") data.as_ptr(),
|
||||
in("a2") data.len(),
|
||||
lateout("a0") out,
|
||||
out("a7") _,
|
||||
)
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn read(fd: i32, buf: &mut [u8]) -> isize {
|
||||
let mut out;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"li a7, 63",
|
||||
"ecall",
|
||||
in("a0") fd,
|
||||
in("a1") buf.as_mut_ptr(),
|
||||
in("a2") buf.len(),
|
||||
lateout("a0") out,
|
||||
out("a7") _,
|
||||
)
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
struct Stderr;
|
||||
|
||||
impl core::fmt::Write for Stderr {
|
||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||
let ret = write(2, s.as_bytes());
|
||||
if ret < 0 { Err(core::fmt::Error) } else { Ok(()) }
|
||||
}
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn handle(info: &core::panic::PanicInfo<'_>) -> ! {
|
||||
let _ = writeln!(Stderr, "panicked: {}", info.message());
|
||||
unsafe { core::arch::asm!("unimp", options(noreturn)) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
fn _start() {
|
||||
write(1, b"enter a number: ");
|
||||
let mut buf = [0; 10];
|
||||
let len = read(0, &mut buf);
|
||||
let buf = &buf[..(len as usize)];
|
||||
let n = str::from_utf8(buf).unwrap().trim().parse::<i32>().unwrap();
|
||||
|
||||
exit(n);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue