7LLGB3UR4YOFGM7RM3TD2CVLOWTBLHRPICFNQOAL2MNE23V4NNPAC
use cxxrtl::{CxxrtlHandle, CxxrtlObject};
use std::env;
struct Blink {
handle: CxxrtlHandle,
pub clk: CxxrtlObject<1>,
pub led: CxxrtlObject<1>,
pub counter: CxxrtlObject<12>,
}
impl Blink {
fn new(lib: &str) -> Self {
let handle = unsafe { CxxrtlHandle::new(lib) };
let clk = handle.get("clk").unwrap();
let led = handle.get("led").unwrap();
let counter = handle.get("counter").unwrap();
Self {
handle,
clk,
led,
counter,
}
}
fn step(&mut self) {
self.handle.step()
}
}
fn main() {
let lib = concat!(env!("OUT_DIR"), "/design.so");
let mut blink = Blink::new(lib);
blink.step();
let mut prev_led = false;
for cycle in 0..1000 {
blink.clk.set(false);
blink.step();
blink.clk.set(true);
blink.step();
let _counter: u16 = blink.counter.get();
// println!("counter {counter}");
let curr_led = blink.led.get();
if prev_led != curr_led {
println!("cycle {cycle}, led {curr_led}");
prev_led = curr_led;
// blink.counter.set(100u16);
}
}
}
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
let sources = Path::new("blink.v");
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!(
"{}",
format!("cargo:rerun-if-changed={}", sources.to_string_lossy())
);
let output = Command::new("yosys-config")
.args(&["--datdir/include"])
.output()
.expect("failed to get yosys include dir");
let stdout = String::from_utf8_lossy(&output.stdout);
let include = stdout.trim();
let capi = Path::new(include).join("backends/cxxrtl/cxxrtl_capi.cc");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let design_cc = out_path.join("design.cc");
Command::new("yosys")
.args(&[
"-p",
&format!("write_cxxrtl -header {}", design_cc.to_string_lossy()),
])
.args(sources)
.status()
.expect("failed generate cxxrtl code");
let dest = out_path.join("design.so");
Command::new("clang++")
.args(&["-g", "-O3", "-fPIC", "-shared", "-std=c++14"])
.arg(format!("-I{}", include))
.arg(capi)
.arg(design_cc)
.arg("-o")
.arg(dest)
.status()
.expect("failed generate cxxrtl code");
}
module blink(input clk, output led);
reg [11:0] counter = 12'h0;
always @(posedge clk)
counter <= counter + 1'b1;
assign led = counter[7];
endmodule
[package]
name = "example"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cxxrtl = {path = "../rust-cxxrtl/cxxrtl"}
.git
.DS_Store
/target
Cargo.lock