Skip to main content

ab_riscv_interpreter/rv32/
zacas.rs

1//! RV32 Zacas extension
2
3#[cfg(test)]
4mod tests;
5
6use crate::{
7    ExecutableInstruction, ExecutableInstructionCsr, ExecutableInstructionOperands,
8    ExecutableInstructionResult, RegisterFile, Rs1Rs2OperandValues, Rs1Rs2Operands, VirtualMemory,
9};
10use ab_riscv_macros::instruction_execution;
11use ab_riscv_primitives::prelude::*;
12use core::ops::ControlFlow;
13
14#[instruction_execution]
15const impl<Reg> ExecutableInstructionOperands for Rv32ZacasInstruction<Reg> where
16    Reg: Register<Type = u32>
17{
18}
19
20#[instruction_execution]
21const impl<Reg, ExtState, CustomError> ExecutableInstructionCsr<ExtState, CustomError>
22    for Rv32ZacasInstruction<Reg>
23where
24    Reg: Register<Type = u32>,
25{
26}
27
28#[instruction_execution]
29const impl<Reg, Regs, ExtState, Memory, PC, InstructionHandler, CustomError>
30    ExecutableInstruction<Regs, ExtState, Memory, PC, InstructionHandler, CustomError>
31    for Rv32ZacasInstruction<Reg>
32where
33    Reg: [const] Register<Type = u32>,
34    Regs: [const] RegisterFile<Reg>,
35    Memory: [const] VirtualMemory,
36{
37    #[inline(always)]
38    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
39    fn execute(
40        self,
41        Rs1Rs2OperandValues {
42            rs1_value,
43            rs2_value,
44        }: Rs1Rs2OperandValues<<Self::Reg as Register>::Type>,
45        regs: &mut Regs,
46        _ext_state: &mut ExtState,
47        memory: &mut Memory,
48        _program_counter: &mut PC,
49        _system_instruction_handler: &mut InstructionHandler,
50    ) -> ExecutableInstructionResult<(), Self, CustomError> {
51        match self {
52            Self::AmocasW {
53                rd,
54                rs1: _,
55                rs2: _,
56                aq: _,
57                rl: _,
58            } => {
59                let addr = u64::from(rs1_value);
60                let compare = regs.read(rd);
61                let old = memory.read::<u32>(addr)?;
62                if old == compare {
63                    memory.write(addr, rs2_value)?;
64                }
65                Ok(ControlFlow::Continue((rd, old)))
66            }
67            Self::AmocasD {
68                rd,
69                rs1: _,
70                rs2,
71                rd_hi,
72                rs2_hi,
73                aq: _,
74                rl: _,
75            } => {
76                let addr = u64::from(rs1_value);
77                // Per spec, when the first register of a pair is `x0`, BOTH halves of that pair
78                // read as zero - not just the literal `x0` half. `compare_lo`/`rs2_value` are
79                // already 0 in that case since `x0` is hardwired, but `compare_hi`/`swap_hi`
80                // need an explicit override since `rd_hi`/`rs2_hi` are real registers.
81                let compare_lo = regs.read(rd);
82                let compare_hi = if rd == Reg::ZERO { 0 } else { regs.read(rd_hi) };
83                let swap_hi = if rs2 == Reg::ZERO {
84                    0
85                } else {
86                    regs.read(rs2_hi)
87                };
88                let old_lo = memory.read::<u32>(addr)?;
89                let old_hi = memory.read::<u32>(addr + 4)?;
90                if old_lo == compare_lo && old_hi == compare_hi {
91                    memory.write(addr, rs2_value)?;
92                    memory.write(addr + 4, swap_hi)?;
93                }
94                // Per spec, when `rd == x0` the whole register-pair write (both halves) is
95                // skipped, not just the low half (which is a no-op anyway since x0 is
96                // hardwired). Only `rd_hi` needs an explicit guard since it's a real register.
97                if rd != Reg::ZERO {
98                    regs.write(rd_hi, old_hi);
99                }
100                Ok(ControlFlow::Continue((rd, old_lo)))
101            }
102        }
103    }
104}