// Copyright (c) 2025 taitep // SPDX-License-Identifier: MIT // // This file is part of TRVE (https://gitea.taitep.se/taitep/trve) // See LICENSE file in the project root for full license text. #[macro_export] macro_rules! instr_branch { ($name:ident, $cond:expr) => { pub fn $name(core: &mut Core, instr: Instruction) -> Result<(), ExceptionType> { let a = core.reg_read(instr.rs1()); let b = core.reg_read(instr.rs2()); if $cond(a, b) { core.pc = core.pc.wrapping_add(instr.imm_b()); } else { core.advance_pc(); } Ok(()) } }; } #[macro_export] macro_rules! instr_branch_signed { ($name:ident, $cond:expr) => { instr_branch!($name, |a, b| $cond((a as i64), (b as i64))); }; } #[macro_export] macro_rules! instr_op_r { ($name:ident, $op:expr) => { pub fn $name(core: &mut Core, instr: Instruction) -> Result<(), ExceptionType> { let a = core.reg_read(instr.rs1()); let b = core.reg_read(instr.rs2()); let res = $op(a, b); core.reg_write(instr.rd(), res); core.advance_pc(); Ok(()) } }; } #[macro_export] macro_rules! instr_op_i { ($name:ident, $op:expr) => { pub fn $name(core: &mut Core, instr: Instruction) -> Result<(), ExceptionType> { let a = core.reg_read(instr.rs1()); let b = instr.imm_i(); let res = $op(a, b); core.reg_write(instr.rd(), res); core.advance_pc(); Ok(()) } }; } #[macro_export] macro_rules! instr_op { ($name:ident, $name_imm:ident, $op:expr) => { instr_op_r!($name, $op); instr_op_i!($name_imm, $op); }; }