Skip to content

Commit

Permalink
feat: Start work on #[spade] macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Jan 16, 2025
1 parent beb28f3 commit 45765e9
Show file tree
Hide file tree
Showing 23 changed files with 827 additions and 477 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
test_crate/artifacts/
test_crates/test_crate/artifacts/
132 changes: 132 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
[workspace]
members = [
"test_crate",
"spade/spade",
"spade/spade-macro",
"test_crates/test_crate",
"test_crates/test_swim/test_swim_crate",
"verilator",
"verilog/verilog",
"verilog/verilog-macro",
"verilog/verilog-macro-builder",
]
resolver = "2"

Expand All @@ -21,3 +25,4 @@ snafu = "0.8.5"
camino = "1.1.9"
libc = "0.2.169"
libloading = "0.8.6"
syn = { version = "2.0.96", features = ["full"] }
20 changes: 20 additions & 0 deletions spade/spade-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "spade-macro"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
description.workspace = true
readme.workspace = true
license.workspace = true

[lib]
proc-macro = true

[dependencies]
verilog-macro-builder = { path = "../../verilog/verilog-macro-builder/" }
proc-macro2 = "1.0.93"
syn.workspace = true
quote = "1.0.38"
toml = "0.8.19"
camino.workspace = true
57 changes: 57 additions & 0 deletions spade/spade-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::env;

use camino::Utf8PathBuf;
use proc_macro::TokenStream;
use verilog_macro_builder::{build_verilated_struct, MacroArgs};

fn search_for_swim_toml(mut start: Utf8PathBuf) -> Option<Utf8PathBuf> {
while !start.as_str().is_empty() {
if start.join("swim.toml").is_file() {
return Some(start.join("swim.toml"));
}
start.pop();
}
None
}

#[proc_macro_attribute]
pub fn spade(args: TokenStream, item: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as MacroArgs);

let manifest_directory = Utf8PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("Please use CARGO"),
);
let Some(swim_toml) = search_for_swim_toml(manifest_directory) else {
return syn::Error::new_spanned(
args.source_path,
"Could not find swim.toml",
)
.into_compile_error()
.into();
};

let verilog_args = MacroArgs {
source_path: {
let mut source_path = swim_toml.clone();
source_path.pop();
source_path.push("build/spade.sv");

// TODO: parse spade file directory and remove this
if !source_path.is_file() {
return syn::Error::new_spanned(
args.source_path,
"Please run swim build or similar",
)
.into_compile_error()
.into();
}

syn::LitStr::new(source_path.as_str(), args.source_path.span())
},
name: args.name,
clock_port: args.clock_port,
reset_port: args.reset_port,
};

build_verilated_struct(verilog_args, item.into(), "spade").into()
}
15 changes: 15 additions & 0 deletions spade/spade/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "spade"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
description.workspace = true
readme.workspace = true
license.workspace = true

[dependencies]
verilog = { path = "../../verilog/verilog/" }
spade-macro = { path = "../spade-macro/" }
camino.workspace = true
snafu.workspace = true
52 changes: 52 additions & 0 deletions spade/spade/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pub use spade_macro::spade;
pub use verilog::__reexports;

use std::env::current_dir;

use camino::{Utf8Path, Utf8PathBuf};
use snafu::{whatever, ResultExt, Whatever};
use verilog::{VerilatorRuntime, __reexports::verilator::VerilatedModel};

fn search_for_swim_toml(mut start: Utf8PathBuf) -> Option<Utf8PathBuf> {
while !start.as_str().is_empty() {
if start.join("swim.toml").is_file() {
return Some(start.join("swim.toml"));
}
start.pop();
}
None
}

pub struct SpadeRuntime {
verilator_runtime: VerilatorRuntime,
}

impl SpadeRuntime {
pub fn new(artifact_directory: &Utf8Path) -> Result<Self, Whatever> {
let Some(swim_toml_path) = search_for_swim_toml(
current_dir()
.whatever_context("Failed to get current directory")?
.try_into()
.whatever_context(
"Failed to convert current directory to UTF-8",
)?,
) else {
whatever!("Failed to find swim.toml");
};

let mut spade_sv_path = swim_toml_path;
spade_sv_path.pop();
spade_sv_path.push("build/spade.sv");

Ok(Self {
verilator_runtime: VerilatorRuntime::new(
artifact_directory,
&[&spade_sv_path],
)?,
})
}

pub fn create_model<M: VerilatedModel>(&mut self) -> Result<M, Whatever> {
self.verilator_runtime.create_model()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ license.workspace = true

[dependencies]
snafu.workspace = true
verilog = { path = "../verilog/verilog" }
verilog = { path = "../../verilog/verilog" }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Main;
#[snafu::report]
fn main() -> Result<(), Whatever> {
let mut runtime =
VerilatorRuntime::new("artifacts".into(), &["sv/main.sv".as_ref()]);
VerilatorRuntime::new("artifacts".into(), &["sv/main.sv".as_ref()])?;

let mut main = runtime.create_model::<Main>()?;

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test_crates/test_swim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
4 changes: 4 additions & 0 deletions test_crates/test_swim/src/main.spade
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
entity main(#[no_mangle] out: inv &int<8>) {
set out = 42;
}
2 changes: 2 additions & 0 deletions test_crates/test_swim/swim.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[spade]
commit = "86ebf2f41fd79aaab13c486c697136777d812803"
1 change: 1 addition & 0 deletions test_crates/test_swim/swim.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "test_swim"
1 change: 1 addition & 0 deletions test_crates/test_swim/test_swim_crate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
artifacts/
13 changes: 13 additions & 0 deletions test_crates/test_swim/test_swim_crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "test_swim_crate"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
description.workspace = true
readme.workspace = true
license.workspace = true

[dependencies]
snafu.workspace = true
spade = { path = "../../../spade/spade/" }
Loading

0 comments on commit 45765e9

Please # to comment.