diff --git a/crates/fayalite/src/memory.rs b/crates/fayalite/src/memory.rs index e6bbea5..1762f57 100644 --- a/crates/fayalite/src/memory.rs +++ b/crates/fayalite/src/memory.rs @@ -7,7 +7,7 @@ use crate::{ array::{Array, ArrayType}, bundle::{Bundle, BundleType}, clock::Clock, - expr::{Expr, Flow, ToExpr, ToLiteralBits}, + expr::{ops::BundleLiteral, repeat, Expr, Flow, ToExpr, ToLiteralBits}, hdl, int::{Bool, DynSize, Size, UInt, UIntType}, intern::{Intern, Interned}, @@ -1050,3 +1050,32 @@ impl MemBuilder { .extend(annotations.into_annotations()); } } + +pub fn splat_mask(ty: T, value: Expr) -> Expr> { + let canonical_ty = ty.canonical(); + match canonical_ty { + CanonicalType::UInt(_) + | CanonicalType::SInt(_) + | CanonicalType::Bool(_) + | CanonicalType::AsyncReset(_) + | CanonicalType::SyncReset(_) + | CanonicalType::Reset(_) + | CanonicalType::Clock(_) + | CanonicalType::Enum(_) => Expr::from_canonical(Expr::canonical(value)), + CanonicalType::Array(array) => Expr::from_canonical(Expr::canonical(repeat( + splat_mask(array.element(), value), + array.len(), + ))), + CanonicalType::Bundle(bundle) => Expr::from_canonical(Expr::canonical( + BundleLiteral::new( + bundle.mask_type(), + bundle + .fields() + .iter() + .map(|field| splat_mask(field.ty, value)) + .collect(), + ) + .to_expr(), + )), + } +}