-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimmips.cpp
149 lines (137 loc) · 3.62 KB
/
simmips.cpp
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
148
149
// implement entry point for simmips here
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <QApplication>
#include "virtualMachine.hpp"
#include "virtual_machine_gui.hpp"
#include <qstring.h>
using namespace std;
int main(int argc, char *argv[])
{
vector<std::string> args;
for (auto i = 0; i < argc; ++i) {
args.push_back(argv[i]);
}
if (args.size()<2 || args.size()>3) {
std::cerr << "Error: " << "invalid file name." << std::endl;
}
if (args.size() == 3) {
if (args[1] == "--gui") {
string a = args[2];
QApplication app(argc, argv);
VirtualMachineGUI myGui;
myGui.load(QString::fromStdString(args[2]));
myGui.show();
return app.exec();
}
}
//if (1) {
//string a = "test20.asm";
//QApplication app(argc, argv);
//VirtualMachineGUI myGui;
//myGui.load(QString::fromStdString(a));
//myGui.show();
//return app.exec();
//}
else {
ifstream inputfile(args[1]);
//ifstream inputfile("test20.asm");
//string a = "test20.asm";
string a = args[1];
if (!inputfile.good()) {
std::cerr << "Error: " << "invalid file name." << std::endl;
}
TokenList mylist = tokenize(inputfile);
parser myparser(mylist);
if (!myparser.parseToken()) {
if (myparser.isLexerError()) {
cerr << myparser.getlexer_error() << std::endl;
return EXIT_FAILURE;
}
cerr << myparser.get_error() << std::endl;
return EXIT_FAILURE;
}
myparser.getInstructionLine(a);
VirtualMachine myvirtual;
myvirtual.setValues(myparser.getLabelMap(), myparser.getMemory(),
myparser.getInstructions(), myparser.getImmidiates(), myparser.getTextLabels(), myparser.getMainLabels(),
myparser.getFirstTextLine());
cout << "simmips> ";
string input;
bool isRunning = false;
bool isThreadUp = false;
getline(cin, input);
MessageQueue myqueue;
bool whileDeter = true;
while (whileDeter) {
string print = input.substr(0, 7);
if (isRunning==true) {
if (input == "break") {
myqueue.push(input);
isRunning = false;
}
else {
cout << "Error: simulation running. Type break to halt." << endl;
}
}
else if (print == "print $") {
string registerPrint = input.substr(6, 12);
myvirtual.getRegister(registerPrint);
if (myvirtual.isError()) {
cerr << "Error: Invalid register" << endl;
}
else {
cout << "0x";
cout << std::setfill('0') << std::setw(8) << std::hex << myvirtual.printRegisters(registerPrint) << '\n';
}
}
else if (print == "print &") {
string address = input.substr(7, 18);
istringstream buffer{ address };
uint32_t memoryadd;
buffer >> std::hex >> memoryadd;
cout << "0x";
cout << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(myvirtual.getMemoryAdd(memoryadd)) << '\n';
}
else if (input == "step") {
if (myvirtual.getPc() < myvirtual.getInstructionSize()) {
myvirtual.pcSimulate();
}
cout << "0x";
cout << std::setfill('0') << std::setw(8) << std::hex << myvirtual.printRegisters("$pc") << '\n';
}
else if (input == "status") {
}
else if (input == "run") {
if (isThreadUp == false) {
myqueue.push(input);
myvirtual.threadSet(&myqueue);
isRunning = true;
isThreadUp = true;
}
else {
myqueue.push(input);
isRunning = true;
}
}
else {
cerr << "Error: unknown command." << std::endl;
}
cout << "simmips> ";
getline(cin, input);
if (input == "quit" && isRunning!=true) {
whileDeter = false;
myqueue.push(input);
}
}
if (isThreadUp) {
myvirtual.breakRun();
}
return EXIT_SUCCESS;
}
}