aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/instructions/arithmetic.cpp
blob: f3cad86e6a2bd8d82383d10d3458b3e1e03ae6a0 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "instructions/arithmetic.h"

InstructionArithmetic::InstructionArithmetic(enum InstructionArithmeticT type, std::uint8_t rs, std::uint8_t rd, std::uint8_t rt)
    : InstructionR(rs, rd, rt, 0) {
    this->type = type;
}

void InstructionArithmetic::decode(Registers *regs) {
    Instruction::decode(regs);
    this->rs_d = regs->read_gp(this->rs);
    this->rd_d = regs->read_gp(this->rd);
}

void InstructionArithmetic::execute() {
    Instruction::execute();
    switch (this->type) {
    case IAT_ADD:
        this->rt_d = (std::uint32_t)((std::int32_t)this->rs_d + (std::int32_t)this->rd_d);
        break;
    case IAT_ADDU:
        this->rt_d = this->rs_d + this->rd_d;
        break;
    case IAT_SUB:
        this->rt_d = (std::uint32_t)((std::int32_t)this->rs_d - (std::int32_t)this->rd_d);
        break;
    case IAT_SUBU:
        this->rt_d = this->rs_d - this->rd_d;
        break;
    case IAT_AND:
        this->rt_d = this->rs_d & this->rd_d;
        break;
    case IAT_OR:
        this->rt_d = this->rs_d | this->rd_d;
        break;
    case IAT_XOR:
        this->rt_d = this->rs_d ^ this->rd_d;
        break;
    case IAT_NOR:
        // TODO
        break;
    case IAT_SLT:
        // TODO
        break;
    case IAT_SLTU:
        // TODO
        break;
    }
}

void InstructionArithmetic::memory(Memory *mem) {
    Instruction::memory(mem);
    // pass
}

void InstructionArithmetic::write_back(Registers *regs) {
    Instruction::write_back(regs);
    regs->write_gp(this->rt, this->rt_d);
}

QVector<QString> InstructionArithmetic::to_strs() {
    QVector<QString> str = this->InstructionR::to_strs();
    str.erase(str.begin() + 4); // Drop sa field
    switch (this->type) {
    case IAT_ADD:
        str[0] = "add";
        break;
    case IAT_ADDU:
        str[0] = "addu";
        break;
    case IAT_SUB:
        str[0] = "sub";
        break;
    case IAT_SUBU:
        str[0] = "subu";
        break;
    case IAT_AND:
        str[0] = "and";
        break;
    case IAT_OR:
        str[0] = "or";
        break;
    case IAT_XOR:
        str[0] = "xor";
        break;
    case IAT_NOR:
        str[0] = "nor";
        break;
    case IAT_SLT:
        str[0] = "slt";
        break;
    case IAT_SLTU:
        str[0] = "sltu";
        break;
    default:
        // TODO different exception
        throw std::exception();
    }
    return str;
}

InstructionArithmeticImmediate::InstructionArithmeticImmediate(enum InstructionArithmeticImmediateT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t value)
    : InstructionI(rs, rt, value) {
    this->type = type;
}

void InstructionArithmeticImmediate::decode(Registers *regs) {
    Instruction::decode(regs);
    this->rs_d = regs->read_gp(this->rs);
}

void InstructionArithmeticImmediate::execute() {
    Instruction::execute();
    switch (this->type) {
    case IAT_ADDI:
        this->rt_d = (std::uint32_t)((std::int32_t)this->rs_d + (std::int32_t)this->immediate);
        break;
    case IAT_ADDIU:
        this->rt_d = this->rs_d + this->immediate;
        break;
    case IAT_ANDI:
        this->rt_d = (std::uint32_t)((std::int32_t)this->rs_d - (std::int32_t)this->immediate);
        break;
    case IAT_ORI:
        this->rt_d = this->rs_d - this->immediate;
        break;
    case IAT_XORI:
        this->rt_d = this->rs_d & this->immediate;
        break;
    }
}

void InstructionArithmeticImmediate::memory(Memory *mem) {
    Instruction::memory(mem);
    // pass
}

void InstructionArithmeticImmediate::write_back(Registers *regs) {
    Instruction::write_back(regs);
    regs->write_gp(this->rt, this->rt_d);
}

QVector<QString> InstructionArithmeticImmediate::to_strs() {
    QVector<QString> str = this->InstructionI::to_strs();
    switch (this->type) {
    case IAT_ADDI:
        str[0] = "addi";
        break;
    case IAT_ADDIU:
        str[0] = "addiu";
        break;
    case IAT_ANDI:
        str[0] = "andi";
        break;
    case IAT_ORI:
        str[0] = "ori";
        break;
    case IAT_XORI:
        str[0] = "xori";
        break;
    case IAT_SLTI:
        str[0] = "slti";
        break;
    case IAT_SLTIU:
        str[0] = "sltiu";
        break;
    case IAT_LUI:
        str[0] = "lui";
        break;
    default:
        // TODO different exception
        throw std::exception();
    }
    return str;
}