-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlalwe_appmanager.cpp
176 lines (156 loc) · 6.47 KB
/
lalwe_appmanager.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "lalwe_appmanager.h"
#include "logger.h"
#include "constants.h"
LALWE_AppManager::LALWE_AppManager(QQmlContext* ctxt, QObject* obj) {
this->ctxt = ctxt;
this->guiObject = obj;
assembler = new Assembler();
processor.append(new Processor(*this));
ram = processor.at(0)->getRam();
programRunning = false;
assemblyRunning = false;
doAnimations = true;
QObject::connect(&animationTicker,SIGNAL(timeout()), this, SIGNAL(stepAnimation()));
QObject::connect(assembler, SIGNAL(assemblyDone()), this, SLOT(assemblyDone()));
QObject::connect(obj,SIGNAL(toggleFollow(bool)),ram,SLOT(toggleFollowActive(bool)));
logger = new Logger(*obj);
setGuiProperty("ramViewCellAmmount",Constants::RAM_VIEW_CELL_AMOUNT);
setGuiProperty("addressRam1",0);
setGuiProperty("addressRam2",Constants::ARRAYSIZE - Constants::RAM_VIEW_CELL_AMOUNT);
}
bool LALWE_AppManager::getSimulationRunning() {
return programRunning && programHandle.isRunning() && animationTicker.isActive();
}
void LALWE_AppManager::assembleSlot(const QString &code) {
if((!assemblyRunning || assembleHandle.isFinished()) && (!programRunning || programHandle.isFinished())) {
setGuiProperty("status","Assembling...");
assemblyRunning = true;
assembleHandle = QtConcurrent::run(assembler, &Assembler::assemble, code.toStdString(), ram);
animationTicker.start(Constants::ANIM_DELAY_MILIS);
}
}
void LALWE_AppManager::saveProgramSlot(const QString &code, const QString &path, const int &mode) {
std::ofstream saveFile(path.toStdString());
if(saveFile.is_open()) {
saveFile << code.toStdString() << std::endl;
saveFile.close();
guiObject->setProperty("fileName",path);
QVariant arg = mode;
QMetaObject::invokeMethod(guiObject, "continueWork",Q_ARG(QVariant,arg));
} else {
Logger::loggerInst->error("File save failed! Path: " + path.toStdString());
}
}
void LALWE_AppManager::openProgramSlot(const QString &path) {
std::ifstream openFile(path.toStdString());
std::string code = "";
std::string line = "";
if(openFile.is_open()) {
while(std::getline(openFile,line)) {
code += line + "\n";
}
guiObject->setProperty("fileName",path);
QVariant arg = QString::fromStdString(code);
QMetaObject::invokeMethod(guiObject, "getCodeFromFile", Q_ARG(QVariant,arg));
} else {
Logger::loggerInst->error("File open failed! Path: " + path.toStdString());
}
}
void LALWE_AppManager::playProgramSlot() {
if((!programRunning || programHandle.isFinished())&&(!assemblyRunning || assembleHandle.isFinished())) {
processor.at(0)->cancelTermination();
setGuiProperty("status","Simulation running...");
Logger::loggerInst->info("Simulation started...");
programRunning = true;
programHandle = QtConcurrent::map(processor.begin(),processor.end(),[](Processor* p){ p->runProgram(); });
animationTicker.start(Constants::ANIM_DELAY_MILIS);
} else if(programRunning && programHandle.isRunning()){
if(animationTicker.isActive()) {
animationTicker.stop();
setGuiProperty("status","Simulation paused");
emit toggleAnimPlaying(false);
} else {
animationTicker.start(Constants::ANIM_DELAY_MILIS);
setGuiProperty("status","Simulation running...");
emit toggleAnimPlaying(true);
}
}
}
void LALWE_AppManager::abortProgramSlot() {
if((programRunning && programHandle.isRunning()) || (assemblyRunning && assembleHandle.isRunning())) {
processor.at(0)->toggleAnimations(false);
emit stepAnimation();
processor.at(0)->requestTermination();
if(animationTicker.isActive()) {
processor.at(0)->toggleAnimations(true);
animationTicker.stop();
}
setGuiProperty("status","Ready");
}
}
void LALWE_AppManager::toggleAnimationsSlot(const bool &newState) {
processor.at(0)->toggleAnimations(newState);
if(!newState) {
animationTicker.stop();
emit stepAnimation();
} else if((programRunning && programHandle.isRunning()) || (assemblyRunning && assembleHandle.isRunning())) {
animationTicker.start(Constants::ANIM_DELAY_MILIS);
}
}
void LALWE_AppManager::toggleRamHex(const bool &newState) {
ram->toggleHexDisplay(newState);
}
void LALWE_AppManager::verifySlot(const QString &code) {
assembler->verify(code.toStdString());
}
void LALWE_AppManager::setCtxProperty(const QString &name, const QVariant &data) {
ctxt->setContextProperty(name,data);
}
void LALWE_AppManager::setGuiProperty(const QString &name, const QVariant &data) {
guiObject->setProperty(name.toLatin1().data(), data);
}
void LALWE_AppManager::assemblyDone() {
animationTicker.stop();
setGuiProperty("status","Ready");
}
void LALWE_AppManager::animStepForward() {
emit stepAnimation();
}
void LALWE_AppManager::windowClosing() {
if((programRunning && programHandle.isRunning()) || (assemblyRunning && assembleHandle.isRunning())) {
processor.at(0)->toggleAnimations(false);
}
emit stepAnimation();
processor.at(0)->requestTermination();
}
void LALWE_AppManager::ramViewAddressChanged(const int &index, const QString &action) {
ram->setRamViewAddress(index,action);
}
void LALWE_AppManager::printLine(const QString &line) {
QMetaObject::invokeMethod(guiObject,"printLine",Q_ARG(QVariant,line));
}
void LALWE_AppManager::changeAnimSpeed(const double &percentage) {
Constants::ANIM_DELAY_MILIS = Constants::ANIM_MAX_DELAY - (Constants::ANIM_MAX_DELAY - Constants::ANIM_MIN_DELAY) * percentage / 100;
animationTicker.setInterval(Constants::ANIM_DELAY_MILIS);
}
void LALWE_AppManager::resetProcessor() {
processor.at(0)->toggleAnimations(false);
setGuiProperty("activeRamSlot1",-1);
setGuiProperty("activeRamSlot2",-1);
setGuiProperty("activeRegister",-1);
setGuiProperty("aluActive",false);
setGuiProperty("busToAluActive",false);
setGuiProperty("busFromAluActive",false);
setGuiProperty("cycleState",-1);
setGuiProperty("operand",0);
setGuiProperty("result",0);
setGuiProperty("operation","+");
setGuiProperty("decodedOpcode","N/A");
setGuiProperty("effectiveAddress","N/A");
setGuiProperty("addressMode","N/A");
processor.at(0)->reset();
processor.at(0)->toggleAnimations(doAnimations);
}
void LALWE_AppManager::getInput(const QString &input) {
processor.at(0)->sendInput(input.toInt());
}