-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.cpp
More file actions
59 lines (47 loc) · 1.42 KB
/
runner.cpp
File metadata and controls
59 lines (47 loc) · 1.42 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
#include <cstdint>
#include <iostream>
#include <fstream>
#include "instructionset.h"
#include "cpu.h"
using namespace std;
int main() {
CPU cpu;
ifstream f("program.bin", ios::binary);
if (!f) {
cout << "Cannot open program.bin\n";
return 1;
}
f.read(reinterpret_cast<char*>(cpu.inst), 256);
streamsize bytesRead = f.gcount();
f.close();
cout << "Program loaded successfully! (" << bytesRead << " bytes)\n";
while (!cpu.halt) {
uint8_t prev_pc = cpu.pc;
step(cpu);
cout << "PC=" << (int)cpu.pc
<< " R0=" << (int)cpu.r0
<< " R1=" << (int)cpu.r1 << "\n";
if (!cpu.halt && cpu.pc < prev_pc &&
cpu.inst[prev_pc] != ISA::JMP &&
cpu.inst[prev_pc] != ISA::JZ &&
cpu.inst[prev_pc] != ISA::JNZ) {
cout << "PC overflow detected at " << (int)prev_pc << ", halting.\n";
cpu.halt = true;
break;
}
cout << "Press ENTER to execute next instruction...";
cin.get();
}
cout << "Program halted!\n";
cout << "Memory snapshot:\n";
for (int i = 0; i < 256; i++) {
cout << (int)cpu.mem[i] << "\t";
if ((i + 1) % 16 == 0) cout << "\n";
}
cout << "Instruction snapshot:\n";
for (int i = 0; i < 256; i++) {
cout << (int)cpu.inst[i] << "\t";
if ((i + 1) % 16 == 0) cout << "\n";
}
return 0;
}