Skip to content

Commit

Permalink
Merge pull request #7 from chipsalliance/plusarg
Browse files Browse the repository at this point in the history
[gcdemu] implement plusarg to parse args
  • Loading branch information
sequencer authored Sep 11, 2024
2 parents 440eb14 + d30d3e5 commit 31bab96
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 223 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/checkfmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ jobs:
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: "Check Scala format"
continue-on-error: true
if: "!cancelled()"
run: |
nix develop -c bash -c 'mill -i gcd.checkFormat && mill -i elaborator.checkFormat'
- name: "Check Rust format"
continue-on-error: true
if: "!cancelled()"
run: |
cd gcdemu
nix develop -c cargo fmt --check
cd ..
- name: "Check nix format"
continue-on-error: true
if: "!cancelled()"
run: |
nix fmt -- --check nix flake.nix
193 changes: 0 additions & 193 deletions templates/chisel/gcdemu/Cargo.lock

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

1 change: 0 additions & 1 deletion templates/chisel/gcdemu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[dependencies]
svdpi = { version = "0.0.1" }
clap = { version = "4.4.18", features = ["derive"] }
rand = "0.8"
num-traits = "0.2.19"
num-bigint = { version = "0.4.6", features = ["rand"] }
Expand Down
5 changes: 3 additions & 2 deletions templates/chisel/gcdemu/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::ffi::{c_char, CString};
use std::sync::Mutex;

use crate::drive::Driver;
use crate::plusarg::PlusArgMatcher;
use crate::GcdArgs;
use clap::Parser;
use num_bigint::BigUint;
use svdpi::sys::dpi::{svBitVecVal, svLogic};
use svdpi::SvScope;
Expand Down Expand Up @@ -64,7 +64,8 @@ unsafe fn fill_test_payload(dst: *mut SvBitVecVal, data_width: u64, payload: &Te

#[no_mangle]
unsafe extern "C" fn gcd_init() {
let args = GcdArgs::parse();
let plusargs = PlusArgMatcher::from_args();
let args = GcdArgs::from_plusargs(&plusargs);
args.setup_logger().unwrap();
let scope = SvScope::get_current().expect("failed to get scope in gcd_init");
let driver = Box::new(Driver::new(scope, &args));
Expand Down
8 changes: 4 additions & 4 deletions templates/chisel/gcdemu/src/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl Driver {
dump_end: args.dump_end,
#[cfg(feature = "trace")]
dump_started: false,
data_width: args.data_width,
timeout: args.timeout,
test_size: args.test_size,
clock_flip_time: args.clock_flip_time,
data_width: env!("DESIGN_DATA_WIDTH").parse().unwrap(),
timeout: env!("DESIGN_TIMEOUT").parse().unwrap(),
test_size: env!("DESIGN_TEST_SIZE").parse().unwrap(),
clock_flip_time: env!("CLOCK_FLIP_TIME").parse().unwrap(),
test_num: 0,
last_input_cycle: 0,
}
Expand Down
32 changes: 14 additions & 18 deletions templates/chisel/gcdemu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
use clap::Parser;
use plusarg::PlusArgMatcher;
use tracing::Level;
use tracing_subscriber::{EnvFilter, FmtSubscriber};

pub mod dpi;
pub mod drive;
pub mod plusarg;

#[derive(Parser)]
pub(crate) struct GcdArgs {
#[cfg(feature = "trace")]
#[arg(long)]
dump_start: u64,

#[cfg(feature = "trace")]
#[arg(long)]
dump_end: u64,

#[cfg(feature = "trace")]
#[arg(long)]
pub wave_path: String,

#[arg(long, default_value = "info")]
pub log_level: String,

#[arg(long, hide = true,default_value = env!("DESIGN_DATA_WIDTH"))]
data_width: u64,

#[arg(long, hide = true,default_value = env!("DESIGN_TIMEOUT"))]
timeout: u64,

#[arg(long, hide = true,default_value = env!("DESIGN_TEST_SIZE"))]
test_size: u64,

#[arg(long, hide = true,default_value = env!("CLOCK_FLIP_TIME"))]
clock_flip_time: u64,
}

impl GcdArgs {
Expand All @@ -50,4 +34,16 @@ impl GcdArgs {
.expect("internal error: fail to setup log subscriber");
Ok(())
}

pub fn from_plusargs(matcher: &PlusArgMatcher) -> Self {
Self {
#[cfg(feature = "trace")]
dump_start: matcher.match_("dump-start").parse().unwrap(),
#[cfg(feature = "trace")]
dump_end: matcher.match_("dump-end").parse().unwrap(),
#[cfg(feature = "trace")]
wave_path: matcher.match_("wave-path").into(),
log_level: matcher.try_match("log-level").unwrap_or("info").into(),
}
}
}
31 changes: 31 additions & 0 deletions templates/chisel/gcdemu/src/plusarg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pub struct PlusArgMatcher {
plusargs: Vec<String>,
}

impl PlusArgMatcher {
pub fn from_args() -> Self {
let plusargs = std::env::args()
.filter(|arg| arg.starts_with('+'))
.collect();

Self { plusargs }
}

pub fn try_match(&self, arg_name: &str) -> Option<&str> {
let prefix = &format!("+{arg_name}=");

for plusarg in &self.plusargs {
if plusarg.starts_with(prefix) {
return Some(&plusarg[prefix.len()..]);
}
}
None
}

pub fn match_(&self, arg_name: &str) -> &str {
self.try_match(arg_name).unwrap_or_else(|| {
tracing::error!("required plusarg '+{arg_name}=' not found");
panic!("failed to match '+{arg_name}='");
})
}
}
Loading

0 comments on commit 31bab96

Please sign in to comment.