How to Call Rust Code from SystemVerilog

You’re not limited to just C. Not even just C and C++.

Image by Christopher Burns on Unsplash

How the DPI Normally Works

// my_function.c
extern "C" void my_function() {
printf("Hello, world!");
}
// my_import.sv
import "DPI" void my_function();

task run_phase();
my_function(); // When run, will print "Hello, world!"
endtask
$ gcc -c my_function.c
$ gcc -g -O -c my_function.c -fPIC -DPIC -o my_function.o
vcs -cc /usr/bin/gcc -ld /usr/bin/g++ -cpp /usr/bin/g++ -LDFLAGS "-L<folder_of_object_file> -l<object_name> -Wl,-E -Wl,-rpath-link,<folder_of_object_file> -Wl,-rpath=<folder_of_object_file> <systemverilog_file_to_compile>

Beyond C

cargo new dpi_fns --lib
[lib]
name = "dpi_fns"
crate-type = ["dylib"]
#[no_mangle]
fn import_me() {
println!("Hello from Rust!");
}
import "DPI-C" function void import_me();

task main();
import_me();
endtask
vcs -cc /usr/bin/gcc -ld /usr/bin/g++ -cop /usr/bin/g++ -LDFLAGS "-L~/dpi_fns/target/release -ldpi_fns -Wl,-E -Wl,-rpath-link,~/dpi_fns/target/release -Wl,-rpath=~/dpi_fns/target/release ~/dpi_fns_sv/dpi_fns_sv.sv

Performance

Conclusion

Resources

System-on-Chip Validation Engineer and Software Engineer at Intel working on technologies to improve everyone’s lives.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store