Skip to main content

logicaffeine_compile/codegen_sva/
power.rs

1//! Power-Aware Formal Verification
2//!
3//! Power domains introduce isolation, retention, and level shifting requirements.
4//! Missing isolation cells cause functional bugs in power-managed designs.
5
6use super::{SvaAssertionKind, SvaProperty};
7
8/// Power domain state.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum PowerState {
11    On,
12    Off,
13    Retention,
14}
15
16/// A power domain definition.
17#[derive(Debug, Clone)]
18pub struct PowerDomain {
19    pub name: String,
20    pub state: PowerState,
21    pub signals: Vec<String>,
22    pub always_on: bool,
23}
24
25/// A signal crossing between power domains.
26#[derive(Debug, Clone)]
27pub struct PowerCrossing {
28    pub signal: String,
29    pub source_domain: String,
30    pub dest_domain: String,
31    pub has_isolation: bool,
32    pub has_level_shifter: bool,
33}
34
35/// Power violation type.
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub enum PowerViolationType {
38    MissingIsolation,
39    MissingRetention,
40    MissingLevelShifter,
41    IncorrectSequence,
42}
43
44/// A power violation.
45#[derive(Debug, Clone)]
46pub struct PowerViolation {
47    pub signal: String,
48    pub violation_type: PowerViolationType,
49    pub message: String,
50}
51
52/// Power analysis report.
53#[derive(Debug, Clone)]
54pub struct PowerReport {
55    pub domains: Vec<PowerDomain>,
56    pub crossings: Vec<PowerCrossing>,
57    pub violations: Vec<PowerViolation>,
58}
59
60/// Analyze power domains and crossings for violations.
61pub fn analyze_power(domains: &[PowerDomain], crossings: &[PowerCrossing]) -> PowerReport {
62    let mut violations = Vec::new();
63
64    for crossing in crossings {
65        // Find source domain
66        let source = domains.iter().find(|d| d.name == crossing.source_domain);
67
68        // Check isolation requirement
69        if let Some(src) = source {
70            if !src.always_on && !crossing.has_isolation {
71                violations.push(PowerViolation {
72                    signal: crossing.signal.clone(),
73                    violation_type: PowerViolationType::MissingIsolation,
74                    message: format!(
75                        "Signal '{}' crosses from '{}' to '{}' without isolation cell",
76                        crossing.signal, crossing.source_domain, crossing.dest_domain
77                    ),
78                });
79            }
80        }
81
82        // Check level shifter
83        if !crossing.has_level_shifter {
84            // Only flag if domains have different implied voltage levels
85            // Simplified: flag if explicitly missing
86            violations.push(PowerViolation {
87                signal: crossing.signal.clone(),
88                violation_type: PowerViolationType::MissingLevelShifter,
89                message: format!(
90                    "Signal '{}' crosses domains without level shifter",
91                    crossing.signal
92                ),
93            });
94        }
95    }
96
97    // Check retention
98    for domain in domains {
99        if domain.state == PowerState::Retention {
100            let has_retention_signals = !domain.signals.is_empty();
101            if !has_retention_signals {
102                violations.push(PowerViolation {
103                    signal: domain.name.clone(),
104                    violation_type: PowerViolationType::MissingRetention,
105                    message: format!("Domain '{}' in retention but has no signals", domain.name),
106                });
107            }
108        }
109    }
110
111    PowerReport {
112        domains: domains.to_vec(),
113        crossings: crossings.to_vec(),
114        violations,
115    }
116}
117
118/// Generate SVA properties for power isolation verification.
119pub fn verify_isolation(domain: &PowerDomain) -> Vec<SvaProperty> {
120    let mut props = Vec::new();
121
122    if !domain.always_on {
123        for sig in &domain.signals {
124            props.push(SvaProperty {
125                name: format!("power_isolation_{}", sig),
126                clock: "clk".into(),
127                body: format!("({}_power_off) |-> ({}_iso == 1'b0)", domain.name, sig),
128                kind: SvaAssertionKind::Assert,
129            });
130        }
131    }
132
133    props
134}
135
136/// Generate SVA properties for power sequencing.
137pub fn power_sequence_properties(domains: &[PowerDomain]) -> Vec<SvaProperty> {
138    let mut props = Vec::new();
139
140    for domain in domains {
141        if !domain.always_on {
142            props.push(SvaProperty {
143                name: format!("power_seq_{}", domain.name),
144                clock: "clk".into(),
145                body: format!(
146                    "({name}_power_on) |-> ({name}_iso_release)",
147                    name = domain.name
148                ),
149                kind: SvaAssertionKind::Assert,
150            });
151        }
152    }
153
154    props
155}