diff --git a/crates/celox-macros/src/generator.rs b/crates/celox-macros/src/generator.rs index d424937412f..72ead189a51 100644 --- a/crates/celox-macros/src/generator.rs +++ b/crates/celox-macros/src/generator.rs @@ -25,7 +25,21 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { let mut top_level_ports = Vec::new(); for (var_path, var_id) in &module.ports { - if let Some((r#type, _clock)) = module.port_types.get(var_path) { + if let Some(r#type) = module + .port_types + .get(var_path) + .map(|(ty, _)| ty) + .or_else(|| { + // Member types live on the flattened variable; port_types + // retains the authored modport at the root path. + (var_path.0.len() == 2 + && module + .port_types + .contains_key(&VarPath(vec![var_path.0[0]]))) + .then(|| module.variables.get(var_id).map(|var| &var.r#type)) + .flatten() + }) + { if var_path.0.len() > 1 { let interface_name = var_path.0[0].to_string(); interface_map @@ -96,17 +110,16 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { let getter_name = format_ident!("get_{}", name); if array_dims > 0 { + let update = + array_element_update(quote! { self.#port_ident }, element_width, total_array); + let io_update = array_element_update( + quote! { self.ids.#port_ident }, + element_width, + total_array, + ); methods.extend(quote! { pub fn #setter_name(&self, sim: &mut celox::Simulator, index: usize, val: #rust_type) { - let mut val_full = sim.get(self.#port_ident); - let element_width = #element_width; - let bit_offset = index * element_width; - - let mask = ((celox::BigUint::from(1u32) << element_width) - 1u32) << bit_offset; - - val_full = (val_full.clone() ^ (val_full & &mask)) | (celox::BigUint::from(val) << bit_offset); - - sim.modify(|io| io.set_wide(self.#port_ident, val_full)).unwrap(); + sim.modify(|io| { #update }).unwrap(); } pub fn #getter_name(&self, sim: &mut celox::Simulator, index: usize) -> #rust_type { let val_full = sim.get(self.#port_ident); @@ -122,8 +135,9 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { pub fn #getter_name(&mut self, index: usize) -> #rust_type { self.ids.#getter_name(self.sim, index) } }); io_methods.extend(quote! { - pub fn #setter_name(&mut self, _index: usize, _val: #rust_type) { - // TODO: IO setter for arrays + pub fn #setter_name(&mut self, index: usize, val: #rust_type) { + let io = &mut *self.io; + #io_update } }); } else { @@ -195,7 +209,8 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { for (if_name, mut if_ports) in interfaces { if_ports.sort_by_key(|(_, var_id, _)| *var_id); - let if_struct_name = format_ident!("{}", if_name); + // Several modules in one project can use the same modport name. + let if_struct_name = format_ident!("{}Interface{}", struct_name, if_name); let if_bound_name = format_ident!("{}Bound", if_struct_name); let if_io_name = format_ident!("{}IO", if_struct_name); @@ -214,7 +229,13 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { } if_fields.extend(quote! { pub #member_ident: celox::SignalRef, }); - if_init.extend(quote! { #member_ident: sim.signal(#member_name), }); + let signal_name = format!("{if_name}.{member_name}"); + if_init.extend(quote! { + #member_ident: sim.named_signals().into_iter() + .find(|signal| signal.name == #signal_name) + .unwrap_or_else(|| panic!("cannot resolve interface signal: {}", #signal_name)) + .signal, + }); let element_width = r#type.total_width().unwrap_or(1); let array_dims = r#type.array.dims(); @@ -240,14 +261,19 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { let getter_name = format_ident!("get_{}", member_name); if array_dims > 0 { + let update = array_element_update( + quote! { self.#member_ident }, + element_width, + total_array, + ); + let io_update = array_element_update( + quote! { self.ids.#member_ident }, + element_width, + total_array, + ); if_methods.extend(quote! { pub fn #setter_name(&self, sim: &mut celox::Simulator, index: usize, val: #rust_type) { - let mut val_full = sim.get(self.#member_ident); - let element_width = #element_width; - let bit_offset = index * element_width; - let mask = ((celox::BigUint::from(1u32) << element_width) - 1u32) << bit_offset; - val_full = (val_full & !mask) | (celox::BigUint::from(val) << bit_offset); - sim.modify(|io| io.set_wide(self.#member_ident, val_full)).unwrap(); + sim.modify(|io| { #update }).unwrap(); } pub fn #getter_name(&self, sim: &mut celox::Simulator, index: usize) -> #rust_type { let val_full = sim.get(self.#member_ident); @@ -263,7 +289,10 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { pub fn #getter_name(&mut self, index: usize) -> #rust_type { self.ids.#getter_name(self.sim, index) } }); if_io_methods.extend(quote! { - pub fn #setter_name(&mut self, _index: usize, _val: #rust_type) { } + pub fn #setter_name(&mut self, index: usize, val: #rust_type) { + let io = &mut *self.io; + #io_update + } }); } else { let setter_body = if width <= 128 { @@ -329,6 +358,7 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { } extra_structs.extend(quote! { + #[derive(Clone)] pub struct #if_struct_name { #if_fields } impl #if_struct_name { #if_methods } pub struct #if_bound_name<'a> { sim: &'a mut celox::Simulator, ids: &'a #if_struct_name } @@ -344,7 +374,7 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { quote! { pub fn #if_ident(&self) -> #if_struct_name { self.#if_ident.clone() } }, ); bound_methods.extend(quote! { pub fn #if_ident<'b>(&'b mut self) -> #if_bound_name<'b> { #if_bound_name { sim: self.sim, ids: &self.ids.#if_ident } } }); - io_methods.extend(quote! { pub fn #if_ident<'c>(&'c mut self) -> #if_io_name<'c, '_> { #if_io_name { io: self.io, ids: &self.ids.#if_ident } } }); + io_methods.extend(quote! { pub fn #if_ident<'c>(&'c mut self) -> #if_io_name<'c, 'b> { #if_io_name { io: self.io, ids: &self.ids.#if_ident } } }); } let tick_body_main = if let Some(clk) = &clk_ident { @@ -416,5 +446,26 @@ pub fn generate_project(ir: &Ir) -> proc_macro2::TokenStream { all_expanded } +/// Update just the selected element in the current, possibly already modified, +/// backing image. Bits and unknown masks of neighboring elements stay intact. +fn array_element_update( + signal: proc_macro2::TokenStream, + element_width: usize, + total_array: usize, +) -> proc_macro2::TokenStream { + quote! { + assert!(index < #total_array, "array index {} out of bounds for {} elements", index, #total_array); + let signal = #signal; + let (payload, xmask) = io.get_four_state(signal); + let bit_offset = index * #element_width; + let element_mask = (celox::BigUint::from(1u32) << #element_width) - 1u32; + let mask = &element_mask << bit_offset; + let payload = (&payload ^ (&payload & &mask)) + | ((celox::BigUint::from(val) & element_mask) << bit_offset); + let xmask = &xmask ^ (&xmask & &mask); + io.set_four_state(signal, payload, xmask); + } +} + #[cfg(test)] mod test_generator; diff --git a/crates/celox-macros/src/generator/snapshots/celox_macros__generator__test_generator__tests__generate_full_project.snap b/crates/celox-macros/src/generator/snapshots/celox_macros__generator__test_generator__tests__generate_full_project.snap index 818e8644b2d..3c2ddce8680 100644 --- a/crates/celox-macros/src/generator/snapshots/celox_macros__generator__test_generator__tests__generate_full_project.snap +++ b/crates/celox-macros/src/generator/snapshots/celox_macros__generator__test_generator__tests__generate_full_project.snap @@ -536,13 +536,22 @@ impl LinearSorter { sim.get_four_state(self.d_in) } pub fn set_d_out(&self, sim: &mut celox::Simulator, index: usize, val: u16) { - let mut val_full = sim.get(self.d_out); - let element_width = 16usize; - let bit_offset = index * element_width; - let mask = ((celox::BigUint::from(1u32) << element_width) - 1u32) << bit_offset; - val_full = (val_full.clone() ^ (val_full & &mask)) - | (celox::BigUint::from(val) << bit_offset); - sim.modify(|io| io.set_wide(self.d_out, val_full)).unwrap(); + sim.modify(|io| { + assert!( + index < 8usize, "array index {} out of bounds for {} elements", + index, 8usize + ); + let signal = self.d_out; + let (payload, xmask) = io.get_four_state(signal); + let bit_offset = index * 16usize; + let element_mask = (celox::BigUint::from(1u32) << 16usize) - 1u32; + let mask = &element_mask << bit_offset; + let payload = (&payload ^ (&payload & &mask)) + | ((celox::BigUint::from(val) & element_mask) << bit_offset); + let xmask = &xmask ^ (&xmask & &mask); + io.set_four_state(signal, payload, xmask); + }) + .unwrap(); } pub fn get_d_out(&self, sim: &mut celox::Simulator, index: usize) -> u16 { let val_full = sim.get(self.d_out); @@ -672,7 +681,21 @@ impl<'a, 'b> LinearSorterIO<'a, 'b> { pub fn set_d_in_4state(&mut self, val: celox::BigUint, mask: celox::BigUint) { self.io.set_four_state(self.ids.d_in, val, mask); } - pub fn set_d_out(&mut self, _index: usize, _val: u16) {} + pub fn set_d_out(&mut self, index: usize, val: u16) { + let io = &mut *self.io; + assert!( + index < 8usize, "array index {} out of bounds for {} elements", index, 8usize + ); + let signal = self.ids.d_out; + let (payload, xmask) = io.get_four_state(signal); + let bit_offset = index * 16usize; + let element_mask = (celox::BigUint::from(1u32) << 16usize) - 1u32; + let mask = &element_mask << bit_offset; + let payload = (&payload ^ (&payload & &mask)) + | ((celox::BigUint::from(val) & element_mask) << bit_offset); + let xmask = &xmask ^ (&xmask & &mask); + io.set_four_state(signal, payload, xmask); + } pub fn set_d_out_4state(&mut self, val: celox::BigUint, mask: celox::BigUint) { self.io.set_four_state(self.ids.d_out, val, mask); } @@ -758,13 +781,22 @@ impl LinearSorterWrapper { sim.get_four_state(self.d_in) } pub fn set_d_out(&self, sim: &mut celox::Simulator, index: usize, val: u16) { - let mut val_full = sim.get(self.d_out); - let element_width = 16usize; - let bit_offset = index * element_width; - let mask = ((celox::BigUint::from(1u32) << element_width) - 1u32) << bit_offset; - val_full = (val_full.clone() ^ (val_full & &mask)) - | (celox::BigUint::from(val) << bit_offset); - sim.modify(|io| io.set_wide(self.d_out, val_full)).unwrap(); + sim.modify(|io| { + assert!( + index < 8usize, "array index {} out of bounds for {} elements", + index, 8usize + ); + let signal = self.d_out; + let (payload, xmask) = io.get_four_state(signal); + let bit_offset = index * 16usize; + let element_mask = (celox::BigUint::from(1u32) << 16usize) - 1u32; + let mask = &element_mask << bit_offset; + let payload = (&payload ^ (&payload & &mask)) + | ((celox::BigUint::from(val) & element_mask) << bit_offset); + let xmask = &xmask ^ (&xmask & &mask); + io.set_four_state(signal, payload, xmask); + }) + .unwrap(); } pub fn get_d_out(&self, sim: &mut celox::Simulator, index: usize) -> u16 { let val_full = sim.get(self.d_out); @@ -897,7 +929,21 @@ impl<'a, 'b> LinearSorterWrapperIO<'a, 'b> { pub fn set_d_in_4state(&mut self, val: celox::BigUint, mask: celox::BigUint) { self.io.set_four_state(self.ids.d_in, val, mask); } - pub fn set_d_out(&mut self, _index: usize, _val: u16) {} + pub fn set_d_out(&mut self, index: usize, val: u16) { + let io = &mut *self.io; + assert!( + index < 8usize, "array index {} out of bounds for {} elements", index, 8usize + ); + let signal = self.ids.d_out; + let (payload, xmask) = io.get_four_state(signal); + let bit_offset = index * 16usize; + let element_mask = (celox::BigUint::from(1u32) << 16usize) - 1u32; + let mask = &element_mask << bit_offset; + let payload = (&payload ^ (&payload & &mask)) + | ((celox::BigUint::from(val) & element_mask) << bit_offset); + let xmask = &xmask ^ (&xmask & &mask); + io.set_four_state(signal, payload, xmask); + } pub fn set_d_out_4state(&mut self, val: celox::BigUint, mask: celox::BigUint) { self.io.set_four_state(self.ids.d_out, val, mask); } diff --git a/crates/celox/src/lib.rs b/crates/celox/src/lib.rs index 892bc41a031..f26383447d6 100644 --- a/crates/celox/src/lib.rs +++ b/crates/celox/src/lib.rs @@ -144,6 +144,12 @@ mod host_api { self.write(signal, |backend| backend.set_wide(signal, val)); } + /// Read the current stored payload and X/Z mask without evaluating + /// combinational logic. Includes earlier writes in this `modify` call. + pub fn get_four_state(&self, signal: crate::SignalRef) -> (crate::BigUint, crate::BigUint) { + self.backend.get_four_state(signal) + } + pub fn set_four_state( &mut self, signal: crate::SignalRef, diff --git a/crates/celox/tests/macro_array_project/Veryl.lock b/crates/celox/tests/macro_array_project/Veryl.lock new file mode 100644 index 00000000000..6d4416b47f7 --- /dev/null +++ b/crates/celox/tests/macro_array_project/Veryl.lock @@ -0,0 +1,4 @@ +# This file is automatically @generated by Veryl. +# It is not intended for manual editing. +version = 1 +projects = [] diff --git a/crates/celox/tests/macro_array_project/Veryl.toml b/crates/celox/tests/macro_array_project/Veryl.toml new file mode 100644 index 00000000000..d883fa9e977 --- /dev/null +++ b/crates/celox/tests/macro_array_project/Veryl.toml @@ -0,0 +1,3 @@ +[project] +name = "macro_array_project" +version = "0.1.0" diff --git a/crates/celox/tests/macro_array_project/src/Arrays.veryl b/crates/celox/tests/macro_array_project/src/Arrays.veryl new file mode 100644 index 00000000000..1d5b062f420 --- /dev/null +++ b/crates/celox/tests/macro_array_project/src/Arrays.veryl @@ -0,0 +1,27 @@ +module Arrays ( + a: input logic<5> [3], + wide: input logic<129> [2], + y: output logic<5> [3], + wide_y: output logic<129> [2], +) { + assign y = a; + assign wide_y = wide; +} + +interface ArrayBus { + var data: logic<5> [3]; + modport sink { data: input, } +} +module InterfaceArrays ( + bus: modport ArrayBus::sink, + y: output logic<5> [3], +) { + assign y = bus.data; +} + +module AnotherInterfaceArrays ( + bus: modport ArrayBus::sink, + y: output logic<5> [3], +) { + assign y = bus.data; +} diff --git a/crates/celox/tests/macro_test.rs b/crates/celox/tests/macro_test.rs index 41ba4811fb8..74a43287460 100644 --- a/crates/celox/tests/macro_test.rs +++ b/crates/celox/tests/macro_test.rs @@ -43,3 +43,123 @@ fn test_macro_io_modify() { dut.tick(); assert_eq!(dut.get_b(), 100); } + +mod arrays { + use super::*; + veryl_test!("tests/macro_array_project"); + + #[test] + fn modify_array_elements_preserves_other_elements() { + let code = include_str!("macro_array_project/src/Arrays.veryl"); + let mut sim = SimulatorBuilder::new(code, "Arrays").build().unwrap(); + let ids = Arrays::new(&sim); + let mut dut = ids.bind(&mut sim); + dut.modify(|io| { + io.set_a(0, 3); + io.set_a(1, 5); + io.set_a(2, 7); + io.set_a(1, 9); + }); + assert_eq!(dut.get_y(0), 3); + assert_eq!(dut.get_y(1), 9); + assert_eq!(dut.get_y(2), 7); + } + #[test] + fn modify_array_elements_masks_values_and_supports_wide_elements() { + let code = include_str!("macro_array_project/src/Arrays.veryl"); + let mut sim = SimulatorBuilder::new(code, "Arrays").build().unwrap(); + let ids = Arrays::new(&sim); + let mut dut = ids.bind(&mut sim); + let high = celox::BigUint::from(1u32) << 128usize; + dut.modify(|io| { + io.set_a(0, u8::MAX); + io.set_a(1, 2); + io.set_wide(0, &high | celox::BigUint::from(3u32)); + io.set_wide(1, (&high << 1usize) | celox::BigUint::from(7u32)); + }); + assert_eq!(dut.get_y(0), 31); + assert_eq!(dut.get_y(1), 2); + assert_eq!(dut.get_y(2), 0); + assert_eq!(dut.get_wide_y(0), &high | celox::BigUint::from(3u32)); + assert_eq!(dut.get_wide_y(1), celox::BigUint::from(7u32)); + // The unbatched setter uses the same element update. + dut.set_a(0, u8::MAX); + dut.set_wide(1, high.clone()); + assert_eq!(dut.get_y(1), 2); + assert_eq!(dut.get_wide_y(1), high); + } + + #[test] + fn modify_array_elements_preserves_neighbor_unknown_masks() { + let code = include_str!("macro_array_project/src/Arrays.veryl"); + let mut sim = SimulatorBuilder::new(code, "Arrays") + .four_state(true) + .build() + .unwrap(); + let ids = Arrays::new(&sim); + // A mixture of X and Z bits in every element. + let initial = celox::BigUint::from(0x5555u32); + let unknown = celox::BigUint::from(0x7fffu32); + sim.modify(|io| io.set_four_state(ids.a, initial.clone(), unknown)) + .unwrap(); + let mut dut = ids.bind(&mut sim); + dut.modify(|io| io.set_a(1, 3)); + let cleared = celox::BigUint::from(31u32 << 5); + let expected_payload = (&initial ^ (&initial & &cleared)) | celox::BigUint::from(3u32 << 5); + let expected_mask = celox::BigUint::from(0x7fffu32 ^ (31u32 << 5)); + assert_eq!( + dut.get_a_4state(), + (expected_payload.clone(), expected_mask.clone()) + ); + assert_eq!(dut.get_y_4state(), (expected_payload, expected_mask)); + // Clearing another element must retain the remaining neighbor's X/Z. + dut.set_a(0, 4); + let (payload, mask) = dut.get_a_4state(); + assert_eq!( + payload & celox::BigUint::from(31u32), + celox::BigUint::from(4u32) + ); + assert_eq!(mask, celox::BigUint::from(31u32 << 10)); + } + + #[test] + fn modify_interface_array_elements() { + let code = include_str!("macro_array_project/src/Arrays.veryl"); + let mut sim = SimulatorBuilder::new(code, "InterfaceArrays") + .build() + .unwrap(); + let ids = InterfaceArrays::new(&sim); + let mut dut = ids.bind(&mut sim); + dut.modify(|io| { + io.bus().set_data(0, 3); + io.bus().set_data(1, 5); + io.bus().set_data(2, 7); + io.bus().set_data(1, u8::MAX); + }); + dut.bus().set_data(0, 9); + assert_eq!(dut.get_y(0), 9); + assert_eq!(dut.get_y(1), 31); + assert_eq!(dut.get_y(2), 7); + assert_eq!(dut.bus().get_data(2), 7); + } + + #[test] + #[should_panic(expected = "array index 3 out of bounds for 3 elements")] + fn modify_array_elements_rejects_out_of_bounds() { + let code = include_str!("macro_array_project/src/Arrays.veryl"); + let mut sim = SimulatorBuilder::new(code, "Arrays").build().unwrap(); + let ids = Arrays::new(&sim); + ids.bind(&mut sim).modify(|io| io.set_a(3, 1)); + } + #[test] + fn interface_array_types_are_scoped_to_the_module() { + let code = include_str!("macro_array_project/src/Arrays.veryl"); + let mut sim = SimulatorBuilder::new(code, "AnotherInterfaceArrays") + .build() + .unwrap(); + let ids = AnotherInterfaceArrays::new(&sim); + let mut dut = ids.bind(&mut sim); + dut.modify(|io| io.bus().set_data(2, 11)); + assert_eq!(dut.get_y(2), 11); + } +}