-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstr.cpp
More file actions
98 lines (89 loc) · 2.46 KB
/
Copy pathinstr.cpp
File metadata and controls
98 lines (89 loc) · 2.46 KB
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
#include "instr.h"
#include "cpu.h"
#include <QRandomGenerator>
#include <sstream>
std::map<std::string, uint32_t> Instr::m_instrsDict;
void Instr::clear() {
m_opcode = 0;
m_r1 = 0;
m_r2 = 0;
m_r3_imm = 0;
}
void Instr::executeCode(CPU *cpu, uint32_t code) {
decode(code);
execute(cpu);
}
void Instr::execute(CPU *cpu) {
switch (m_opcode) {
default:
cpu->stop();
cpu->showMsg("[Error] Wrong opcode on execution");
break;
#define _ISA(_opcode, _name, _execute, _asmargs, _disasmargs, _dumpregs) \
case _opcode: { \
_execute \
} break;
#include "ISA.h"
#undef _ISA
}
}
std::string Instr::assembler(std::stringstream &input) {
clear();
std::string name;
input >> name;
m_opcode = m_instrsDict[name];
std::string arg;
switch (m_opcode) {
default:
// Return label
return name;
break;
#define _ISA(_opcode, _name, _execute, _asmargs, _disasmargs, _dumpregs) \
case _opcode: { \
_asmargs \
} break;
#include "ISA.h"
#undef _ISA
}
return std::string();
}
std::string Instr::disasm() {
std::stringstream args;
switch (m_opcode) {
default:
args << "[Opcode Error]";
break;
#define _ISA(_opcode, _name, _execute, _asmargs, _disasmargs, _dumpregs) \
case _opcode: { \
args << #_name; \
_disasmargs \
} break;
#include "ISA.h"
#undef _ISA
}
return args.str();
}
std::string Instr::dumpRegs(CPU *cpu) {
std::stringstream args;
switch (m_opcode) {
default:
args << "[Opcode Error]";
break;
#define _ISA(_opcode, _name, _execute, _asmargs, _disasmargs, _dumpregs) \
case _opcode: { \
_dumpregs \
} break;
#include "ISA.h"
#undef _ISA
}
return args.str();
}
uint32_t Instr::code() {
return (m_opcode << 24) | (m_r1 << 20) | (m_r2 << 16) | (m_r3_imm & 0xFFFF);
}
void Instr::decode(uint32_t code) {
m_opcode = code >> 24;
m_r1 = (code >> 20) & 0xF;
m_r2 = (code >> 16) & 0xF;
m_r3_imm = code & 0xFFFF;
}