Skip to content

Commit

Permalink
The tool is really sus not working for no reason
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Nov 7, 2024
1 parent e8cc729 commit a14dffc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ members = [
"tools/tb",
"tools/calyx-ffi-macro",
"tools/calyx-ffi",
"tools/tb/examples/calyx",
"tools/tb/examples/calyx-native",
]

[workspace.package]
Expand Down
17 changes: 16 additions & 1 deletion tools/tb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ There are two ways to use `tb`:
### Directly

For example, if you make sure to follow the instructions under [`examples/cocotb/doc_examples_quickstart/`](examples/cocotb/doc_examples_quickstart/),

```
make
./tb examples/cocotb/doc_examples_quickstart/my_design.sv -t examples/cocotb/doc_examples_quickstart/test_my_design.py --using cocotb
```

should run `cocotb` on the input file and harness.

### Via `fud2`:
### Via `fud2`

> THIS SECTION IS INVALIDATED SINCE I HAVE REMOVED FUD2 SUPPORT FOR THE TIME
> BEING.
You can follow the above steps but invoke the following command instead.

```
fud2 my_design.sv -s tb.test=test_my_design.py -s tb.using=cocotb --to tb
```
Expand All @@ -34,8 +40,17 @@ fud2 my_design.sv -s tb.test=test_my_design.py -s tb.using=cocotb --to tb

I've provided a [Makefile](Makefile) in this directory for local testing. Use `make` to build the `tb` executable locally.

## Testing Calyx Code

Your input file should be the calyx file you test and each `-t` test should be a
single-file Rust program using calyx-ffi to declare one or more components in
the file and then to write a single `#[calyx_ffi_tests]` module in which you
write your test functions.

## Writing a Plugin

> WE NO LONGER ALLOW DYNAMICALLY LOADING PLUGINS.
First, setup a simple rust library as you would any other, but **ensure that `lib.crate-type` is `cdylib`**.
Here, we're writing the plugin in `lib.rs`.
Remember to update the `path` in the `dependencies.tb` dependency!
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions tools/tb/examples/calyx-tb/adder.futil
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import "primitives/core.futil";

component main(lhs: 64, rhs: 64) -> (result: 64) {
cells {
adder = std_add(64);
temp = std_reg(64);
}
wires {
group add {
adder.left = lhs;
adder.right = rhs;
temp.in = adder.out;
temp.write_en = 1'b1;
add[done] = temp.done;
}
result = temp.out;
}
control {
add;
}
}


23 changes: 23 additions & 0 deletions tools/tb/examples/calyx-tb/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use calyx_ffi::prelude::*;

Check failure on line 1 in tools/tb/examples/calyx-tb/main.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

failed to resolve: use of undeclared crate or module `calyx_ffi`

use calyx_ffi::cider_ffi_backend;

Check failure on line 3 in tools/tb/examples/calyx-tb/main.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

unresolved import `calyx_ffi`
#[calyx_ffi(

Check failure on line 4 in tools/tb/examples/calyx-tb/main.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

cannot find attribute `calyx_ffi` in this scope
src = "adder.futil",
comp = "main",
backend = cider_ffi_backend
)]
struct Adder;

#[cfg(test)]
#[calyx_ffi_tests]
mod tests {
#[calyx_ffi_test]
fn test_add(adder: &mut Adder) {
adder.reset();
adder.lhs = 4;
adder.rhs = 5;
println!("foo");
adder.go();
assert_eq!(9, adder.result());
}
}

Check failure on line 23 in tools/tb/examples/calyx-tb/main.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

`main` function not found in crate `calyx_tb`
104 changes: 64 additions & 40 deletions tools/tb/src/builtin_plugins/calyx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,63 +34,87 @@ impl Plugin for CalyxTB {
work_dir: tempdir::TempDir,
_config: &Config,
) -> LocalResult<()> {
println!(
eprintln!(
"recommendation: Run the #[calyx_ffi_tests] as Rust tests directly"
);

eprintln!("tb: --using {}: setting up dummy crate", self.name());

let mut dut_path = PathBuf::from(work_dir.path());
dut_path.push(&input);
if let Some(parent) = dut_path.parent() {
fs::create_dir_all(parent)?;
}
fs::copy(&input, dut_path)?;

let mut calyx_ffi_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
calyx_ffi_path.push("../../../calyx-ffi");
calyx_ffi_path.push("../calyx-ffi");

let mut main_file = PathBuf::from(work_dir.path());
main_file.push("main.rs");
fs::write(&main_file, DRIVER_CODE)?;
fs::write(main_file, DRIVER_CODE)?;

let mut manifest_path = PathBuf::from(work_dir.path());
manifest_path.push("Cargo.toml");

let mut lib_path = PathBuf::from(work_dir.path());
lib_path.push(input);

for test in tests {
let mut test_path = PathBuf::from(work_dir.path());
test_path.push(test);

let mut manifest = toml::Table::new();
manifest.insert(
"package".into(),
toml::Value::Table(toml::map::Map::from_iter([
("name".to_string(), "test_crate".into()),
("edition".to_string(), "2021".into()),
])),
);
manifest.insert(
"lib".into(),
lib_path.push("lib.rs");

let mut manifest = toml::Table::new();
manifest.insert(
"package".into(),
toml::Value::Table(toml::map::Map::from_iter([
("name".to_string(), "test_crate".into()),
("edition".to_string(), "2021".into()),
])),
);
manifest.insert(
"lib".into(),
toml::Value::Table(toml::map::Map::from_iter([(
"path".to_string(),
"lib.rs".into(),
)])),
);
manifest.insert(
"bin".into(),
vec![toml::Value::Table(toml::map::Map::from_iter([
("name".to_string(), "test".into()),
("path".to_string(), "main.rs".into()),
]))]
.into(),
);
manifest.insert(
"dependencies".into(),
toml::Value::Table(toml::map::Map::from_iter([(
"calyx-ffi".to_string(),
toml::Value::Table(toml::map::Map::from_iter([(
"path".to_string(),
"lib.rs".into(),
calyx_ffi_path.to_string_lossy().to_string().into(),
)])),
);
manifest.insert(
"bin".into(),
vec![toml::Value::Table(toml::map::Map::from_iter([
("name".to_string(), "test".into()),
("path".to_string(), "main.rs".into()),
]))]
.into(),
);
manifest.insert(
"dependencies".into(),
toml::Value::Table(toml::map::Map::from_iter([(
"calyx-ffi".to_string(),
toml::Value::Table(toml::map::Map::from_iter([(
"path".to_string(),
calyx_ffi_path.to_string_lossy().to_string().into(),
)])),
)])),
);
)])),
);

for test in tests {
fs::write(&manifest_path, manifest.to_string())?;
fs::rename(&test_path, &lib_path)?;
fs::copy(test, &lib_path)?;

eprintln!(
"tb: --using {}: building and testing `{}` with `{}`",
self.name(),
input,
test
);
eprintln!(" (may take a while because `rustc` is slow)");

let output = Command::new("cargo")
.arg("expand")
.arg("--lib")
.current_dir(work_dir.path())
.output()?;

println!("{}", unsafe {
String::from_utf8_unchecked(output.stdout)
});
let output = Command::new("cargo")
.arg("run")
.arg("--quiet")
Expand Down
4 changes: 2 additions & 2 deletions tools/tb/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl FromStr for ConfigSet {
/// Test verilog files under various harnesses.
pub struct CLI {
#[argh(positional)]
/// verilog file
/// verilog or calyx file
pub input: String,

#[argh(option, short = 't', long = "test")]
Expand All @@ -42,7 +42,7 @@ pub struct CLI {
pub set: Vec<ConfigSet>,

#[argh(option, short = 'u')]
/// the testbench to invoke
/// the testbench to invoke, e.g., verilator, cocotb, calyx
pub using: String,

/// path to the config file
Expand Down

0 comments on commit a14dffc

Please sign in to comment.