75 lines
1.5 KiB
C++
75 lines
1.5 KiB
C++
#include "executor.h"
|
|
#include "machine.h"
|
|
#include<chrono>
|
|
#include<thread>
|
|
|
|
Executor::Executor() {}
|
|
|
|
Executor::Executor(Machine* m) {
|
|
machine = m;
|
|
}
|
|
|
|
void Executor::start() {
|
|
if (ended) {
|
|
emit signalEnded();
|
|
return;
|
|
}
|
|
running = true;
|
|
emit signalStarted();
|
|
while (running && !ended) {
|
|
int pc_before = machine->getPC();
|
|
machine->execute();
|
|
int pc_after = machine->getPC();
|
|
emit updateRequested(); // signal za posodobitev UI
|
|
if (pc_before == pc_after) {
|
|
ended = true;
|
|
emit signalEnded();
|
|
break;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
}
|
|
|
|
void Executor::stop() {
|
|
running = false;
|
|
emit signalStopped();
|
|
emit updateRequested();
|
|
}
|
|
|
|
bool Executor::isRunning() {
|
|
return running;
|
|
}
|
|
|
|
bool Executor::hasEnded() {
|
|
return ended;
|
|
}
|
|
|
|
void Executor::resetProgram() {
|
|
ended = false;
|
|
running = false;
|
|
emit signalStopped();
|
|
}
|
|
|
|
void Executor::step() {
|
|
if (ended) {
|
|
emit signalEnded();
|
|
return;
|
|
}
|
|
emit signalStarted();
|
|
int pc_before = machine->getPC();
|
|
|
|
// Izvedi en ukaz
|
|
machine->execute();
|
|
|
|
int pc_after = machine->getPC();
|
|
emit updateRequested();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
emit signalStopped();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
// Če PC ostane isti → neskončna zanka → HALT
|
|
if (pc_after == pc_before) {
|
|
ended = true;
|
|
emit signalEnded();
|
|
}
|
|
}
|