-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
147 lines (126 loc) · 3.2 KB
/
main.cpp
File metadata and controls
147 lines (126 loc) · 3.2 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
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
#include <iostream>
#include <thread>
#include <array>
#include <algorithm>
#include <chrono>
#include <fstream>
#define LFN() std::cout << "`" << __PRETTY_FUNCTION__ << "` called" << std::endl
#define LOG(x) std::cout << __FUNCTION__ << ":" << std::dec << __LINE__ << ": " << x << std::endl
#define LOG_V(x) LOG(#x << " = " << x)
#define LOG_VX(x) LOG(#x << " = " << std::hex << x)
using uint = unsigned int;
struct instruction_t {
uint S : 12;
uint opcode : 4;
};
class MU0
{
public:
MU0(const std::string& filepath)
: memory(new std::uint16_t[s_mem_size])
, pc(0)
, acc(0) {
std::ifstream file(filepath, std::ios::binary | std::ios::in);
file.read(reinterpret_cast<char*>(memory), s_mem_size);
file.close();
}
~MU0() {
delete[] memory;
}
void step() {
LOG_VX(pc);
instruction_t* i = reinterpret_cast<instruction_t*>(&memory[pc]);
LOG_VX(i->S);
LOG_VX(i->opcode);
execute(i);
LOG_VX(acc);
std::this_thread::sleep_for(std::chrono::milliseconds(400));
}
void execute(instruction_t* i) {
switch (i->opcode) {
case 0b0000:
instr_lda(i);
break;
case 0b0001:
instr_sto(i);
break;
case 0b0010:
instr_add(i);
break;
case 0b0011:
instr_sub(i);
break;
case 0b0100:
instr_jmp(i);
break;
case 0b0101:
instr_jge(i);
break;
case 0b0110:
instr_jne(i);
break;
case 0b0111:
instr_stp(i);
break;
default:
LOG("not implemented: " << std::hex << i->opcode);
break;
}
}
void instr_lda(instruction_t* i) {
LOG("Decoded => LDA 0x" << std::hex << i->S);
acc = memory[i->S];
++pc;
}
void instr_sto(instruction_t* i) {
LOG("Decoded => STO 0x" << std::hex << i->S);
memory[i->S] = acc;
++pc;
}
void instr_add(instruction_t* i) {
LOG("Decoded => ADD 0x" << std::hex << i->S);
acc += memory[i->S];
++pc;
}
void instr_sub(instruction_t* i) {
LOG("Decoded => SUB 0x" << std::hex << i->S);
acc -= memory[i->S];
++pc;
}
void instr_jmp(instruction_t* i) {
LOG("Decoded => JMP 0x" << std::hex << i->S);
pc = i->S;
}
void instr_jge(instruction_t* i) {
LOG("Decoded => JGE 0x" << std::hex << i->S);
if (acc >= 0)
pc = i->S;
else
++pc;
}
void instr_jne(instruction_t* i) {
LOG("Decoded => JNE 0x" << std::hex << i->S);
if (acc != 0)
pc = i->S;
else
++pc;
}
void instr_stp(instruction_t* i) {
LOG("Decoded => STP");
}
constexpr static std::size_t s_mem_size = 4096;
private:
std::uint16_t* memory;
std::uint16_t pc;
std::uint16_t acc;
};
int main(int argc, char** argv) {
std::string filename = "memory.bin";
if (argc == 2) {
filename = argv[1];
}
MU0 proc(filename);
while (true) {
proc.step();
}
}