add ReadyValid<T>
All checks were successful
/ test (push) Successful in 4m26s

This commit is contained in:
Jacob Lifshay 2024-09-20 19:11:30 -07:00
parent ff269e5def
commit 51ce7b079e
Signed by: programmerjake
SSH key fingerprint: SHA256:B1iRVvUJkvd7upMIiMqn6OyxvD2SgJkAH3ZnUOj6z+c
2 changed files with 36 additions and 0 deletions

View file

@ -25,3 +25,5 @@ pub use scoped_ref::ScopedRef;
pub use misc::{ pub use misc::{
interned_bit, iter_eq_by, BitSliceWriteWithBase, DebugAsDisplay, DebugAsRawString, MakeMutSlice, interned_bit, iter_eq_by, BitSliceWriteWithBase, DebugAsDisplay, DebugAsRawString, MakeMutSlice,
}; };
pub mod ready_valid;

View file

@ -0,0 +1,34 @@
use crate::prelude::*;
#[hdl]
pub struct ReadyValid<T> {
pub data: HdlOption<T>,
#[hdl(flip)]
pub ready: Bool,
}
impl<T: Type> ReadyValid<T> {
#[hdl]
pub fn fire(expr: Expr<Self>) -> Expr<Bool> {
#[hdl]
let fire: Bool = wire();
#[hdl]
match expr.data {
HdlNone => connect(fire, false),
HdlSome(_) => connect(fire, expr.ready),
}
fire
}
#[hdl]
pub fn map<R: Type>(
expr: Expr<Self>,
f: impl FnOnce(Expr<T>) -> Expr<R>,
) -> Expr<ReadyValid<R>> {
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
}
}