-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMachine.cpp
More file actions
106 lines (81 loc) · 2.78 KB
/
Copy pathMachine.cpp
File metadata and controls
106 lines (81 loc) · 2.78 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
#include "Machine.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
Machine::Machine(){}
void Machine::loadProgramFile(const string& filename,int address = 16) {
if(address != 16){
cpu.set_pc(address);
}
ifstream file(filename);
string instruct;
while (getline(file, instruct) && address <=255)) {
if(instruct.size()<2){
continue ;
}
mem.set_value(address++, instruct.substr(0,2));
if(address > 255){
break ;
}
mem.set_value(address++, instruct.substr(2));
}
if(address <= 254 && mem.get_value(address - 1) != "C0" && mem.get_value(address) != "00"){
mem.set_value(address++, "C0");
mem.set_value(address++, "00");
}
file.close();
}
void Machine::loadInstructions(const string& Instructions,int address = 16) {
if(address != 16){
cpu.set_pc(address);
}
stringstream ss(Instructions);
string instruct;
while ((ss >> instruct) && (address <=255)) {
if(instruct.size()<2){
continue ;
}
mem.set_value(address++, instruct.substr(0,2));
if(address > 255){
break ;
}
mem.set_value(address++, instruct.substr(2));
}
if(address <= 254 && mem.get_value(address - 1) != "C0" && mem.get_value(address) != "00"){
mem.set_value(address++, "C0");
mem.set_value(address++, "00");
}
}
void Machine::runProgram() {
while (!cpu.cu.IsHalted()) {
cpu.runNextInstruction(mem);
}
}
void Machine::reset(){
cpu.reset();
mem.reset() ;
}
void Machine::outputState(){
string x = "0123456789ABCDEF";
cout << "=============================== =================================================================\n";
cout << " Register State Memory State\n";
cout << "=============================== =================================================================\n";
cout << setw(16) << "Register no" << setw(15) << "Value" << " ";
for (char c : x) {
cout << setw(4) << c;
}
cout << '\n';
for (int i = 0; i < 16; ++i) {
cout << setw(15) << x[i] << setw(15) << cpu.reg.get_value(i);
cout << " ";
cout << setw(2) << x[i];
for (int j = 0; j < 16; ++j) {
cout << setw(4) << mem.get_value(i * 16 + j);
)
}
cout << '\n';
}
cout << "============================== =================================================================\n";
}