forked from libre-chip/fayalite
		
	add ReadyValid<T>
This commit is contained in:
		
							parent
							
								
									ff269e5def
								
							
						
					
					
						commit
						51ce7b079e
					
				
					 2 changed files with 36 additions and 0 deletions
				
			
		|  | @ -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; | ||||
|  |  | |||
							
								
								
									
										34
									
								
								crates/fayalite/src/util/ready_valid.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								crates/fayalite/src/util/ready_valid.rs
									
										
									
									
									
										Normal 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 | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue