Skip to main content

logicaffeine_compile/codegen_sva/
mod.rs

1//! SVA/PSL Code Generation for Hardware Verification
2//!
3//! Generates SystemVerilog Assertions (SVA), Property Specification Language (PSL),
4//! and Rust runtime monitors from temporal property specifications.
5
6pub mod arbiter_rtl;
7pub mod autopipe;
8pub mod backend;
9pub mod bignum_rtl;
10pub mod bitblast;
11pub mod block_engine_rtl;
12pub mod caravel_project;
13pub mod caravel_rtl;
14pub mod cdc;
15pub mod chan_rtl;
16pub mod chip_protocol;
17pub mod choreography;
18pub mod ci;
19pub mod computedram_rtl;
20pub mod controller_gen;
21pub mod coproc_core;
22pub mod coproc_soc;
23pub mod coverage;
24pub mod crdt_rtl;
25pub mod dae;
26pub mod ddr2_dfi;
27pub mod ddr2_jedec;
28pub mod ddr2_phy;
29pub mod ddr2_traffic;
30pub mod ddr4_jedec;
31pub mod decision_workload;
32pub mod dram;
33pub mod engine_build;
34pub mod fabric_config;
35pub mod fabric_grid;
36pub mod fabric_pipe;
37pub mod fabric_rtl;
38pub mod fabric_soc;
39pub mod fabric_target;
40pub mod fabric_tile;
41pub mod fanout;
42pub mod flow_config;
43pub mod fol_to_sva;
44pub mod fol_to_verify;
45pub mod floorplan;
46pub mod fp_hard;
47pub mod fp_hypotheses;
48pub mod fp_oracle;
49pub mod fp_receipt;
50pub mod full_asm;
51pub mod fusion;
52pub mod gpu_simt_rtl;
53pub mod handshake_rtl;
54pub mod hart;
55pub mod hw_pipeline;
56pub mod infinichip_rtl;
57pub mod keccak_rtl;
58pub mod lutmap;
59pub mod memsched;
60pub mod metapipeline;
61pub mod onboard;
62pub mod mul_tree;
63pub mod netlist;
64pub mod pdk_target;
65pub mod plasticine;
66pub mod plasticine_place;
67pub mod plasticine_report;
68pub mod pmu;
69pub mod pnr_certificate;
70pub mod power;
71pub mod preflight;
72pub mod protocols;
73pub mod psram;
74pub mod qor_harness;
75pub mod quartus;
76pub mod reconfig_fabric_rtl;
77pub mod reverse_module;
78pub mod rtl;
79pub mod rtl_extract;
80pub mod rtl_kg;
81pub mod scalar_asm;
82pub mod scratchpad_rtl;
83pub mod select_hoist;
84pub mod sequencer_rtl;
85pub mod shuttle_wrapper;
86pub mod signal_design;
87pub mod signoff_blocks;
88pub mod simt_ports;
89pub mod simd;
90pub mod soft_core_model;
91pub mod spatial_emit;
92pub mod storage_controller_rtl;
93pub mod sufficiency;
94pub mod sva_model;
95pub mod sva_to_proof;
96pub mod sva_to_verify;
97pub mod sva_vacuity;
98pub mod synthesis_refine;
99pub mod synthesize;
100pub mod systolic;
101pub mod terminal_protocol;
102pub mod terminal_rtl;
103pub mod timing_graph;
104pub mod traffic_flow;
105pub mod unified_placement;
106pub mod verified_compiler;
107pub mod verify_to_kernel;
108pub mod wpe_caravel;
109pub mod wpe_floorplan;
110pub mod wpe_offchip;
111pub mod wprog_engine_rtl;
112pub mod xpu_soc_rtl;
113pub mod z3_synth;
114
115#[cfg(feature = "verification")]
116pub mod decompose;
117#[cfg(feature = "verification")]
118pub mod invariants;
119#[cfg(feature = "verification")]
120pub mod spec_health;
121#[cfg(feature = "verification")]
122pub mod testgen;
123#[cfg(feature = "verification")]
124pub mod waveform;
125
126/// Assertion type — determines the SVA wrapper.
127#[derive(Debug, Clone, PartialEq, Eq)]
128pub enum SvaAssertionKind {
129    /// `assert property` — hard safety requirement.
130    Assert,
131    /// `cover property` — liveness / expected behavior.
132    Cover,
133    /// `assume property` — environment constraint.
134    Assume,
135}
136
137/// A single SVA property ready for emission.
138#[derive(Debug, Clone)]
139pub struct SvaProperty {
140    pub name: String,
141    pub clock: String,
142    pub body: String,
143    pub kind: SvaAssertionKind,
144}
145
146/// Sanitize a human-readable property name into a valid SVA identifier.
147/// "Data Integrity" → "p_data_integrity"
148pub fn sanitize_property_name(name: &str) -> String {
149    let sanitized: String = name
150        .chars()
151        .map(|c| {
152            if c.is_alphanumeric() {
153                c.to_ascii_lowercase()
154            } else {
155                '_'
156            }
157        })
158        .collect();
159    format!("p_{}", sanitized.trim_matches('_'))
160}
161
162/// Emit a single SVA property with its assertion wrapper.
163pub fn emit_sva_property(prop: &SvaProperty) -> String {
164    let wrapper = match prop.kind {
165        SvaAssertionKind::Assert => "assert property",
166        SvaAssertionKind::Cover => "cover property",
167        SvaAssertionKind::Assume => "assume property",
168    };
169
170    let error_action = match prop.kind {
171        SvaAssertionKind::Assert => {
172            format!(" else $error(\"{} violation\")", prop.name)
173        }
174        _ => String::new(),
175    };
176
177    format!(
178        "property {};\n    @(posedge {}) {};\nendproperty\n{} ({}){};",
179        prop.name, prop.clock, prop.body, wrapper, prop.name, error_action
180    )
181}
182
183/// Emit multiple SVA properties as a complete module.
184pub fn emit_sva_module(props: &[SvaProperty]) -> String {
185    props
186        .iter()
187        .map(|p| emit_sva_property(p))
188        .collect::<Vec<_>>()
189        .join("\n\n")
190}
191
192/// Emit a single PSL property.
193pub fn emit_psl_property(prop: &SvaProperty) -> String {
194    let kind = match prop.kind {
195        SvaAssertionKind::Assert => "assert always",
196        SvaAssertionKind::Cover => "cover",
197        SvaAssertionKind::Assume => "assume always",
198    };
199    format!("-- {}\n{} ({});", prop.name, kind, prop.body)
200}
201
202/// Emit a Rust runtime monitor scaffold for a property.
203///
204/// Generates a struct with a `check()` method stub. The generated `check()`
205/// always returns `true` — callers must fill in the signal-dependent logic
206/// for their specific use case. This is a code generation template, not a
207/// complete monitor implementation.
208pub fn emit_rust_monitor(prop: &SvaProperty) -> String {
209    let struct_name = prop
210        .name
211        .replace("p_", "")
212        .split('_')
213        .map(|w| {
214            let mut c = w.chars();
215            match c.next() {
216                None => String::new(),
217                Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
218            }
219        })
220        .collect::<String>()
221        + "Monitor";
222
223    format!(
224        r#"pub struct {struct_name} {{
225    cycle: u64,
226}}
227
228impl {struct_name} {{
229    pub fn new() -> Self {{
230        Self {{ cycle: 0 }}
231    }}
232
233    /// Check the property for one clock cycle.
234    /// Returns true if the property holds.
235    pub fn check(&mut self) -> bool {{
236        self.cycle += 1;
237        // Property: {body}
238        // Scaffold: fill in signal-dependent check logic for your design
239        true
240    }}
241}}"#,
242        struct_name = struct_name,
243        body = prop.body,
244    )
245}