Skip to main content

logicaffeine_compile/codegen_sva/protocols/
riscv.rs

1//! RISC-V ISA Formal Verification Templates
2//!
3//! Pre-verified instruction semantics as parameterizable SVA templates.
4//! Users describe their CPU configuration, templates generate SVA properties.
5
6use super::ProtocolProperty;
7use super::SvaAssertionKind;
8
9/// RISC-V extension identifiers.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum RiscvExtension {
12    I, // Base integer
13    M, // Multiply/Divide
14    A, // Atomic
15    F, // Single-precision float
16    D, // Double-precision float
17    C, // Compressed
18}
19
20/// RISC-V CPU configuration.
21#[derive(Debug, Clone)]
22pub struct RiscvConfig {
23    pub xlen: u32,
24    pub extensions: Vec<RiscvExtension>,
25    pub reg_prefix: String,
26    pub pc_name: String,
27    pub mem_name: String,
28}
29
30impl RiscvConfig {
31    pub fn rv32i() -> Self {
32        Self {
33            xlen: 32,
34            extensions: vec![RiscvExtension::I],
35            reg_prefix: "x".into(),
36            pc_name: "pc".into(),
37            mem_name: "mem".into(),
38        }
39    }
40
41    pub fn rv64i() -> Self {
42        Self {
43            xlen: 64,
44            extensions: vec![RiscvExtension::I],
45            reg_prefix: "x".into(),
46            pc_name: "pc".into(),
47            mem_name: "mem".into(),
48        }
49    }
50}
51
52/// Generate ALU instruction properties.
53pub fn riscv_alu_properties(config: &RiscvConfig) -> Vec<ProtocolProperty> {
54    let w = config.xlen;
55    let r = &config.reg_prefix;
56    let mut props = vec![
57        ProtocolProperty {
58            name: "RISCV_ADD".into(),
59            spec: format!("When opcode is R-type ADD, rd = rs1 + rs2 ({}bit).", w),
60            sva_body: format!(
61                "(opcode == 7'h33 && funct3 == 3'h0 && funct7 == 7'h00) |-> (rd == rs1 + rs2)"
62            ),
63            kind: SvaAssertionKind::Assert,
64        },
65        ProtocolProperty {
66            name: "RISCV_SUB".into(),
67            spec: format!("When opcode is R-type SUB, rd = rs1 - rs2 ({}bit).", w),
68            sva_body: format!(
69                "(opcode == 7'h33 && funct3 == 3'h0 && funct7 == 7'h20) |-> (rd == rs1 - rs2)"
70            ),
71            kind: SvaAssertionKind::Assert,
72        },
73        ProtocolProperty {
74            name: "RISCV_AND".into(),
75            spec: format!("When opcode is R-type AND, rd = rs1 & rs2 ({}bit).", w),
76            sva_body: "(opcode == 7'h33 && funct3 == 3'h7 && funct7 == 7'h00) |-> (rd == (rs1 & rs2))".into(),
77            kind: SvaAssertionKind::Assert,
78        },
79        ProtocolProperty {
80            name: "RISCV_OR".into(),
81            spec: format!("When opcode is R-type OR, rd = rs1 | rs2 ({}bit).", w),
82            sva_body: "(opcode == 7'h33 && funct3 == 3'h6 && funct7 == 7'h00) |-> (rd == (rs1 | rs2))".into(),
83            kind: SvaAssertionKind::Assert,
84        },
85        ProtocolProperty {
86            name: "RISCV_XOR".into(),
87            spec: format!("When opcode is R-type XOR, rd = rs1 ^ rs2 ({}bit).", w),
88            sva_body: "(opcode == 7'h33 && funct3 == 3'h4 && funct7 == 7'h00) |-> (rd == (rs1 ^ rs2))".into(),
89            kind: SvaAssertionKind::Assert,
90        },
91        ProtocolProperty {
92            name: "RISCV_SLT".into(),
93            spec: "When opcode is SLT, rd = (rs1 < rs2) signed.".into(),
94            sva_body: "(opcode == 7'h33 && funct3 == 3'h2 && funct7 == 7'h00) |-> (rd == ($signed(rs1) < $signed(rs2)))".into(),
95            kind: SvaAssertionKind::Assert,
96        },
97        ProtocolProperty {
98            name: "RISCV_SLTU".into(),
99            spec: "When opcode is SLTU, rd = (rs1 < rs2) unsigned.".into(),
100            sva_body: "(opcode == 7'h33 && funct3 == 3'h3 && funct7 == 7'h00) |-> (rd == (rs1 < rs2))".into(),
101            kind: SvaAssertionKind::Assert,
102        },
103    ];
104
105    // M extension: MUL, DIV
106    if config.extensions.contains(&RiscvExtension::M) {
107        props.push(ProtocolProperty {
108            name: "RISCV_MUL".into(),
109            spec: format!("When opcode is MUL, rd = (rs1 * rs2)[{}:0].", w - 1),
110            sva_body:
111                "(opcode == 7'h33 && funct3 == 3'h0 && funct7 == 7'h01) |-> (rd == rs1 * rs2)"
112                    .into(),
113            kind: SvaAssertionKind::Assert,
114        });
115        props.push(ProtocolProperty {
116            name: "RISCV_DIV".into(),
117            spec: "When opcode is DIV, rd = rs1 / rs2 (signed).".into(),
118            sva_body: "(opcode == 7'h33 && funct3 == 3'h4 && funct7 == 7'h01) |-> (rd == $signed(rs1) / $signed(rs2))".into(),
119            kind: SvaAssertionKind::Assert,
120        });
121    }
122
123    props
124}
125
126/// Generate decoder mutual exclusion properties.
127pub fn riscv_decoder_properties(config: &RiscvConfig) -> Vec<ProtocolProperty> {
128    vec![
129        ProtocolProperty {
130            name: "RISCV_Decoder_Mutual_Exclusion".into(),
131            spec: "At most one instruction type is active per cycle.".into(),
132            sva_body: "!(is_r_type && is_i_type) && !(is_r_type && is_s_type) && !(is_r_type && is_b_type) && !(is_i_type && is_s_type) && !(is_i_type && is_b_type) && !(is_s_type && is_b_type)".into(),
133            kind: SvaAssertionKind::Assert,
134        },
135    ]
136}
137
138/// Generate register file properties.
139pub fn riscv_register_properties(config: &RiscvConfig) -> Vec<ProtocolProperty> {
140    let r = &config.reg_prefix;
141    let w = config.xlen;
142    let mut props = vec![
143        ProtocolProperty {
144            name: "RISCV_X0_Always_Zero".into(),
145            spec: format!("Register {}0 always reads as zero.", r),
146            sva_body: format!("{}0 == {}'d0", r, w),
147            kind: SvaAssertionKind::Assert,
148        },
149        ProtocolProperty {
150            name: "RISCV_X0_Write_Ignored".into(),
151            spec: format!("Writing to {}0 has no effect; it remains zero.", r),
152            sva_body: format!("(rd_addr == 5'd0) |=> ({}0 == {}'d0)", r, w),
153            kind: SvaAssertionKind::Assert,
154        },
155    ];
156
157    // PC alignment
158    if config.extensions.contains(&RiscvExtension::C) {
159        props.push(ProtocolProperty {
160            name: "RISCV_PC_Alignment_C".into(),
161            spec: format!("PC is always 2-byte aligned (C extension present)."),
162            sva_body: format!("{}[0] == 1'b0", config.pc_name),
163            kind: SvaAssertionKind::Assert,
164        });
165    } else {
166        props.push(ProtocolProperty {
167            name: "RISCV_PC_Alignment".into(),
168            spec: format!("PC is always 4-byte aligned (no C extension)."),
169            sva_body: format!("{}[1:0] == 2'b00", config.pc_name),
170            kind: SvaAssertionKind::Assert,
171        });
172    }
173
174    props
175}
176
177/// Generate branch instruction properties.
178pub fn riscv_branch_properties(config: &RiscvConfig) -> Vec<ProtocolProperty> {
179    vec![ProtocolProperty {
180        name: "RISCV_BEQ".into(),
181        spec: "When BEQ and rs1 == rs2, PC = PC + immediate.".into(),
182        sva_body: "(opcode == 7'h63 && funct3 == 3'h0 && rs1 == rs2) |-> (pc_next == pc + imm_b)"
183            .into(),
184        kind: SvaAssertionKind::Assert,
185    }]
186}
187
188/// Generate memory access properties.
189pub fn riscv_memory_properties(config: &RiscvConfig) -> Vec<ProtocolProperty> {
190    let w = config.xlen;
191    vec![
192        ProtocolProperty {
193            name: "RISCV_LW_SW_Roundtrip".into(),
194            spec: "Store word then load word at same address yields same value.".into(),
195            sva_body: format!(
196                "(sw_valid && sw_addr == addr) |=> (lw_valid && lw_addr == addr) |-> (lw_data == sw_data)"
197            ),
198            kind: SvaAssertionKind::Assert,
199        },
200        ProtocolProperty {
201            name: "RISCV_Memory_Alignment".into(),
202            spec: "LW address is 4-byte aligned.".into(),
203            sva_body: "(opcode == 7'h03 && funct3 == 3'h2) |-> (addr[1:0] == 2'b00)".into(),
204            kind: SvaAssertionKind::Assert,
205        },
206    ]
207}