From 51ce7b079ec1b0ea53c31348be86f5c2fb52c124 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Fri, 20 Sep 2024 19:11:30 -0700 Subject: [PATCH] add ReadyValid --- crates/fayalite/src/util.rs | 2 ++ crates/fayalite/src/util/ready_valid.rs | 34 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 crates/fayalite/src/util/ready_valid.rs diff --git a/crates/fayalite/src/util.rs b/crates/fayalite/src/util.rs index fc5daf4..5b97e3b 100644 --- a/crates/fayalite/src/util.rs +++ b/crates/fayalite/src/util.rs @@ -25,3 +25,5 @@ pub use scoped_ref::ScopedRef; pub use misc::{ interned_bit, iter_eq_by, BitSliceWriteWithBase, DebugAsDisplay, DebugAsRawString, MakeMutSlice, }; + +pub mod ready_valid; diff --git a/crates/fayalite/src/util/ready_valid.rs b/crates/fayalite/src/util/ready_valid.rs new file mode 100644 index 0000000..a5893cf --- /dev/null +++ b/crates/fayalite/src/util/ready_valid.rs @@ -0,0 +1,34 @@ +use crate::prelude::*; + +#[hdl] +pub struct ReadyValid { + pub data: HdlOption, + #[hdl(flip)] + pub ready: Bool, +} + +impl ReadyValid { + #[hdl] + pub fn fire(expr: Expr) -> Expr { + #[hdl] + let fire: Bool = wire(); + #[hdl] + match expr.data { + HdlNone => connect(fire, false), + HdlSome(_) => connect(fire, expr.ready), + } + fire + } + #[hdl] + pub fn map( + expr: Expr, + f: impl FnOnce(Expr) -> Expr, + ) -> Expr> { + let data = HdlOption::map(expr.data, f); + #[hdl] + let mapped = wire(ReadyValid[Expr::ty(data).HdlSome]); + connect(mapped.data, data); + connect(expr.ready, mapped.ready); + mapped + } +}