summaryrefslogtreecommitdiff
path: root/src/instructions/rvi.rs
blob: 5e2faa4aa6ba1144b0b6f32f0438f51b5c82d002 (plain)
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
79
80
81
// 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::{consts::RegValue, core::Core, decode::Instruction, exceptions::ExceptionType};

use std::ops::{BitAnd, BitOr, BitXor};

mod mem;

pub use mem::*;

instr_op!(add, addi, RegValue::wrapping_add);
instr_op!(
    addw,
    addiw,
    |a, b| RegValue::wrapping_add(a, b) as i32 as i64 as RegValue
);
instr_op_r!(sub, RegValue::wrapping_sub);
instr_op_r!(subw, |a, b| RegValue::wrapping_sub(a, b) as i32 as i64
    as RegValue);

instr_op!(and, andi, RegValue::bitand);
instr_op!(or, ori, RegValue::bitor);
instr_op!(xor, xori, RegValue::bitxor);

instr_op!(sll, slli, |x, shamt| x << (shamt & 0b111111));
instr_op!(
    sllw,
    slliw,
    |x, shamt| (x << (shamt & 0b11111)) as i32 as i64 as RegValue
);
instr_op!(srl, srli, |x, shamt| x >> (shamt & 0b111111));
instr_op!(
    srlw,
    srliw,
    |x, shamt| (x >> (shamt & 0b11111)) as i32 as i64 as RegValue
);
instr_op!(sra, srai, |x, shamt| (x as i64 >> (shamt & 0b111111))
    as RegValue);
instr_op!(
    sraw,
    sraiw,
    |x, shamt| (x as i32 >> (shamt & 0b11111)) as i64 as RegValue
);

instr_op!(sltu, sltiu, |a, b| (a < b) as RegValue);
instr_op!(slt, slti, |a, b| ((a as i64) < (b as i64)) as RegValue);

pub fn lui(core: &mut Core, instr: Instruction) -> Result<(), ExceptionType> {
    core.reg_write(instr.rd(), instr.imm_u());
    core.advance_pc();
    Ok(())
}

pub fn auipc(core: &mut Core, instr: Instruction) -> Result<(), ExceptionType> {
    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<(), ExceptionType> {
    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<(), ExceptionType> {
    core.reg_write(instr.rd(), core.pc.wrapping_add(4));
    core.pc = core.reg_read(instr.rs1()).wrapping_add(instr.imm_i());
    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);