-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (47 loc) · 1.3 KB
/
Copy pathmain.cpp
File metadata and controls
56 lines (47 loc) · 1.3 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
#include "Processor.h"
#include <iostream>
#include <string>
// argc = argument count of argument passed through terminal
// argv = argument vector = array of strings = argument passed through terminal
using namespace std;
int main(int argc, char *argv[]) {
if (argc < 2) {
cerr << "Usage: ./main <filename.s> [-cycles N]\n";
return 1; // return error code
}
int max_cycles = -1;
if (argc == 4 && string(argv[2]) == "-cycles") {
max_cycles = stoi(argv[3]);
}
ProcessorConfig config;
Processor cpu = Processor(config);
try {
cpu.loadProgram(argv[1]);
} catch (...) {
cerr << "Failed to parse instruction file.\n";
return 1;
}
int cycle_count = 0;
while (cpu.step()) {
cycle_count++;
if (max_cycles != -1 && cycle_count == max_cycles) {
cout << "[!] Execution halted at cycle limit: " << max_cycles << "\n";
break;
}
}
if (max_cycles == -1) {
if (cpu.exception) {
cout << "[+] Execution halted due to exception after "
<< cpu.clock_cycle << " cycles.\n";
} else {
cout << "[+] Execution complete naturally in " << cpu.clock_cycle
<< " cycles.\n";
}
}
cpu.dumpArchitecturalState();
for (size_t i = 0; i < cpu.Memory.size(); i++) {
cout << cpu.Memory[i] << " ";
}
cout << endl;
return 0;
}