1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// Copyright (c) 2025 taitep
// SPDX-License-Identifier: BSD-2-Clause
//
// This file is part of TRVE (https://gitea.taitep.se/taitep/trve)
// See LICENSE file in the project root for full license text.
use crate::{core::Core, decode::Instruction, exceptions::Exception};
use std::ops::{BitAnd, BitOr, BitXor};
mod mem;
pub use mem::*;
instr_op!(add, addi, u64::wrapping_add);
instr_op!(addw, addiw, |a, b| u64::wrapping_add(a, b) as i32 as i64
as u64);
instr_op_r!(sub, u64::wrapping_sub);
instr_op_r!(subw, |a, b| u64::wrapping_sub(a, b) as i32 as i64 as u64);
instr_op!(and, andi, u64::bitand);
instr_op!(or, ori, u64::bitor);
instr_op!(xor, xori, u64::bitxor);
instr_op!(sll, slli, |x, shamt| x << (shamt & 0b111111));
instr_op!(
sllw,
slliw,
|x, shamt| (x << (shamt & 0b11111)) as i32 as i64 as u64
);
instr_op!(srl, srli, |x, shamt| x >> (shamt & 0b111111));
instr_op!(
srlw,
srliw,
|x, shamt| (x as u32 >> (shamt & 0b11111)) as i32 as i64 as u64
);
instr_op!(sra, srai, |x, shamt| (x as i64 >> (shamt & 0b111111))
as u64);
instr_op!(
sraw,
sraiw,
|x, shamt| (x as i32 >> (shamt & 0b11111)) as i64 as u64
);
instr_op!(sltu, sltiu, |a, b| (a < b) as u64);
instr_op!(slt, slti, |a, b| ((a as i64) < (b as i64)) as u64);
pub fn lui(core: &mut Core, instr: Instruction) -> Result<(), Exception> {
core.reg_write(instr.rd(), instr.imm_u());
core.advance_pc();
Ok(())
}
pub fn auipc(core: &mut Core, instr: Instruction) -> Result<(), Exception> {
core.reg_write(instr.rd(), core.pc.wrapping_add(instr.imm_u()));
core.advance_pc();
Ok(())
}
pub fn jal(core: &mut Core, instr: Instruction) -> Result<(), Exception> {
core.reg_write(instr.rd(), core.pc.wrapping_add(4));
core.pc = core.pc.wrapping_add(instr.imm_j());
Ok(())
}
pub fn jalr(core: &mut Core, instr: Instruction) -> Result<(), Exception> {
let target = core.reg_read(instr.rs1()).wrapping_add(instr.imm_i()) & !1;
core.reg_write(instr.rd(), core.pc.wrapping_add(4));
core.pc = target;
Ok(())
}
instr_branch!(beq, |a, b| a == b);
instr_branch!(bne, |a, b| a != b);
instr_branch!(bltu, |a, b| a < b);
instr_branch!(bgeu, |a, b| a >= b);
instr_branch_signed!(blt, |a, b| a < b);
instr_branch_signed!(bge, |a, b| a >= b);
|