Skip to main content

sp1_core_executor/events/
instr.rs

1use deepsize2::DeepSizeOf;
2use serde::{Deserialize, Serialize};
3
4use crate::{Instruction, Opcode};
5
6use super::MemoryRecordEnum;
7
8/// Alu Instruction Event.
9///
10/// This object encapsulated the information needed to prove a RISC-V ALU operation.
11#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, DeepSizeOf)]
12#[repr(C)]
13pub struct AluEvent {
14    /// The clock cycle.
15    pub clk: u64,
16    /// The program counter.
17    pub pc: u64,
18    /// The opcode.
19    pub opcode: Opcode,
20    /// The first operand value.
21    pub a: u64,
22    /// The second operand value.
23    pub b: u64,
24    /// The third operand value.
25    pub c: u64,
26    /// Whether the first operand is register 0.
27    pub op_a_0: bool,
28}
29
30impl AluEvent {
31    /// Create a new [`AluEvent`].
32    #[must_use]
33    #[allow(clippy::too_many_arguments)]
34    pub fn new(clk: u64, pc: u64, opcode: Opcode, a: u64, b: u64, c: u64, op_a_0: bool) -> Self {
35        Self { clk, pc, opcode, a, b, c, op_a_0 }
36    }
37}
38
39/// Memory Instruction Event.
40///
41/// This object encapsulated the information needed to prove a RISC-V memory operation.
42#[derive(Debug, Clone, Copy, Serialize, Deserialize, DeepSizeOf)]
43#[repr(C)]
44pub struct MemInstrEvent {
45    /// The clk.
46    pub clk: u64,
47    /// The program counter.
48    pub pc: u64,
49    /// The opcode.
50    pub opcode: Opcode,
51    /// The first operand value.
52    pub a: u64,
53    /// The second operand value.
54    pub b: u64,
55    /// The third operand value.
56    pub c: u64,
57    /// Whether the first operand is register 0.
58    pub op_a_0: bool,
59    /// The memory access record for memory operations.
60    pub mem_access: MemoryRecordEnum,
61}
62
63impl MemInstrEvent {
64    /// Create a new [`MemInstrEvent`].
65    #[must_use]
66    #[allow(clippy::too_many_arguments)]
67    pub fn new(
68        clk: u64,
69        pc: u64,
70        opcode: Opcode,
71        a: u64,
72        b: u64,
73        c: u64,
74        op_a_0: bool,
75        mem_access: MemoryRecordEnum,
76    ) -> Self {
77        Self { clk, pc, opcode, a, b, c, op_a_0, mem_access }
78    }
79}
80
81/// Branch Instruction Event.
82///
83/// This object encapsulated the information needed to prove a RISC-V branch operation.
84#[derive(Debug, Clone, Copy, Serialize, Deserialize, DeepSizeOf)]
85#[repr(C)]
86pub struct BranchEvent {
87    /// The clock cycle.
88    pub clk: u64,
89    /// The program counter.
90    pub pc: u64,
91    /// The next program counter.
92    pub next_pc: u64,
93    /// The opcode.
94    pub opcode: Opcode,
95    /// The first operand value.
96    pub a: u64,
97    /// The second operand value.
98    pub b: u64,
99    /// The third operand value.
100    pub c: u64,
101    /// Whether the first operand is register 0.
102    pub op_a_0: bool,
103}
104
105impl BranchEvent {
106    /// Create a new [`BranchEvent`].
107    #[must_use]
108    #[allow(clippy::too_many_arguments)]
109    pub fn new(
110        clk: u64,
111        pc: u64,
112        next_pc: u64,
113        opcode: Opcode,
114        a: u64,
115        b: u64,
116        c: u64,
117        op_a_0: bool,
118    ) -> Self {
119        Self { clk, pc, next_pc, opcode, a, b, c, op_a_0 }
120    }
121}
122
123/// Jump Instruction Event.
124///
125/// This object encapsulated the information needed to prove a RISC-V jump operation.
126#[derive(Debug, Clone, Copy, Serialize, Deserialize, DeepSizeOf)]
127#[repr(C)]
128pub struct JumpEvent {
129    /// The clock cycle.
130    pub clk: u64,
131    /// The program counter.
132    pub pc: u64,
133    /// The next program counter.
134    pub next_pc: u64,
135    /// The opcode.
136    pub opcode: Opcode,
137    /// The first operand value.
138    pub a: u64,
139    /// The second operand value.
140    pub b: u64,
141    /// The third operand value.
142    pub c: u64,
143    /// Whether the first operand is register 0.
144    pub op_a_0: bool,
145}
146
147impl JumpEvent {
148    /// Create a new [`JumpEvent`].
149    #[must_use]
150    #[allow(clippy::too_many_arguments)]
151    pub fn new(
152        clk: u64,
153        pc: u64,
154        next_pc: u64,
155        opcode: Opcode,
156        a: u64,
157        b: u64,
158        c: u64,
159        op_a_0: bool,
160    ) -> Self {
161        Self { clk, pc, next_pc, opcode, a, b, c, op_a_0 }
162    }
163}
164/// `UType` Instruction Event.
165///
166/// This object encapsulated the information needed to prove a RISC-V AUIPC and LUI operation.
167#[derive(Debug, Clone, Copy, Serialize, Deserialize, DeepSizeOf)]
168#[repr(C)]
169pub struct UTypeEvent {
170    /// The clock cycle.
171    pub clk: u64,
172    /// The program counter.
173    pub pc: u64,
174    /// The opcode.
175    pub opcode: Opcode,
176    /// The first operand value.
177    pub a: u64,
178    /// The second operand value.
179    pub b: u64,
180    /// The third operand value.
181    pub c: u64,
182    /// Whether the first operand is register 0.
183    pub op_a_0: bool,
184}
185
186impl UTypeEvent {
187    /// Create a new [`UTypeEvent`].
188    #[must_use]
189    #[allow(clippy::too_many_arguments)]
190    pub fn new(clk: u64, pc: u64, opcode: Opcode, a: u64, b: u64, c: u64, op_a_0: bool) -> Self {
191        Self { clk, pc, opcode, a, b, c, op_a_0 }
192    }
193}
194
195/// Instruction Fetch Event.
196///
197/// This object encapsulated the information needed to prove an instruction fetch from memory.
198#[derive(Debug, Clone, Copy, Serialize, Deserialize, DeepSizeOf)]
199#[repr(C)]
200pub struct InstructionFetchEvent {
201    /// The clock cycle.
202    pub clk: u64,
203    /// The program counter.
204    pub pc: u64,
205    /// Decoded instruction.
206    pub instruction: Instruction,
207    /// Encoded instruction
208    pub encoded_instruction: u32,
209}
210
211impl InstructionFetchEvent {
212    /// Create a new [`InstructionFetchEvent`].
213    #[must_use]
214    #[allow(clippy::too_many_arguments)]
215    pub fn new(clk: u64, pc: u64, instruction: Instruction, encoded_instruction: u32) -> Self {
216        Self { clk, pc, instruction, encoded_instruction }
217    }
218}
219
220/// Instruction Decode Event.
221///
222/// This object encapsulated the information needed to prove an instruction decode.
223#[derive(Debug, Clone, Copy, Serialize, Deserialize, DeepSizeOf)]
224#[repr(C)]
225pub struct InstructionDecodeEvent {
226    /// Decoded instruction.
227    pub instruction: Instruction,
228    /// Encoded instruction
229    pub encoded_instruction: u32,
230    /// The multiplicity of the instruction.
231    pub multiplicity: usize,
232}
233
234impl InstructionDecodeEvent {
235    /// Create a new [`InstructionDecodeEvent`].
236    #[must_use]
237    #[allow(clippy::too_many_arguments)]
238    pub fn new(instruction: Instruction, encoded_instruction: u32, multiplicity: usize) -> Self {
239        Self { instruction, encoded_instruction, multiplicity }
240    }
241}