Add registers in rust

This commit is contained in:
2025-11-12 23:51:16 +01:00
parent 8326c1f169
commit 3044a54a09
12 changed files with 98 additions and 32 deletions

11
.gitignore vendored
View File

@@ -6,7 +6,12 @@ mill*
bench/mem
.metals
.bloop
.rpt
.Xil
*.rpt
*.jou
*.log
*.mmi
*.Xil
clockInfo.txt
.csv
*.csv
*.bit
*.dcp

View File

@@ -61,7 +61,8 @@ TEST_MAX_PROCS := $$(($$(nproc) - 2)) ## Garde un processeur libre
.PRECIOUS: $(MEM_DIR)/%.elf
.PHONY: synthese fpga clean compile autotest simulation
.PHONY: synthese fpga clean compile autotest simulation force
force:
compile: $(MEM)
$(MEM_DIR):
@@ -69,7 +70,7 @@ $(MEM_DIR):
@mkdir -p $@/programs
@mkdir -p $@/programs_rust
$(MEM_DIR)/programs_rust/%.elf: bench/programs_rust/% |$(MEM_DIR)
$(MEM_DIR)/programs_rust/%.elf: bench/programs_rust/% force |$(MEM_DIR)
cd bench/programs_rust && cargo build --release --bin=$(basename $(notdir $@))
cp -f bench/programs_rust/target/riscv64i/release/$(basename $(notdir $@)) $@
$(MEM_DIR)/crt.o: $(BENCH_DIR)/crt.S |$(MEM_DIR)

View File

@@ -1,6 +1,6 @@
[workspace]
resolver = "3"
members = ["fpga_lib", "hello_rust"]
members = ["fpga-lib", "fpga-lib-macros", "hello-rust"]
[profile.release]
panic = "abort"

View File

@@ -0,0 +1,12 @@
[package]
name = "fpga-lib-macros"
version = "0.1.0"
edition = "2024"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", features = ["full"] }

View File

@@ -0,0 +1,34 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::{format_ident, quote};
#[proc_macro]
pub fn define_regs(_input: TokenStream) -> TokenStream {
let mut tokens = proc_macro2::TokenStream::new();
for n in 0..32usize {
let load = format!("li x{}, {{}}", n);
let store = format!("mv {{}}, x{}", n);
let ident = format_ident!("X{}", n);
let t = quote! {
impl Reg<#n> {
pub fn write_const<const X: u64>() {
unsafe {
core::arch::asm!(#load, const X)
}
}
pub fn read() -> u64 {
let out: u64;
unsafe {
core::arch::asm!(#store, out(reg) out)
}
out
}
}
pub type #ident = Reg<#n>;
};
tokens.extend(t);
}
tokens.into()
}

View File

@@ -0,0 +1,7 @@
[package]
name = "fpga-lib"
version = "0.1.0"
edition = "2024"
[dependencies]
fpga-lib-macros = { path = "../fpga-lib-macros" }

View File

@@ -1,5 +1,7 @@
#![no_std]
pub mod register;
#[macro_export]
macro_rules! panic_handler {
() => {

View File

@@ -0,0 +1,3 @@
pub struct Reg<const N: usize> {}
fpga_lib_macros::define_regs! {}

View File

@@ -1,6 +1,7 @@
[package]
name = "fpga_lib"
name = "hello-rust"
version = "0.1.0"
edition = "2024"
[dependencies]
fpga-lib = { path = "../fpga-lib" }

View File

@@ -0,0 +1,26 @@
#![no_std]
#![no_main]
use core::arch::asm;
use fpga_lib::{panic_handler, register::X31};
panic_handler! {}
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
X31::write_const::<0xdead1ffffffff>();
let test = X31::read() + 10;
unsafe {
asm!(
"add x31, x0, x0",
"add x31, x0, {}", in(reg) test
);
const VGA: *mut u8 = 0x80000000 as *mut u8;
VGA.write_volatile(255);
// VGA.add(1).write_volatile(255);
}
loop {}
}

View File

@@ -1,7 +0,0 @@
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2024"
[dependencies]
fpga_lib = { path = "../fpga_lib" }

View File

@@ -1,18 +0,0 @@
#![no_std]
#![no_main]
use core::arch::asm;
use fpga_lib::panic_handler;
panic_handler! {}
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
unsafe {
asm! {
"li x31, 0xdead0"
}
}
loop {}
}