merged
This commit is contained in:
parent
18a14d204c
commit
717568b6d0
30 changed files with 4093 additions and 209 deletions
76
simulator_SIC_XE/gui/qt/MachineController.cpp
Normal file
76
simulator_SIC_XE/gui/qt/MachineController.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include "MachineController.h"
|
||||
#include "../../include/machine.h"
|
||||
#include <chrono>
|
||||
#include <QDebug>
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
MachineController::MachineController(std::shared_ptr<Machine> machine, QObject *parent)
|
||||
: QObject(parent), m_machine(std::move(machine))
|
||||
{
|
||||
if (!m_machine) {
|
||||
m_machine = std::make_shared<Machine>();
|
||||
}
|
||||
m_lastUpdateTime = steady_clock::now();
|
||||
}
|
||||
|
||||
MachineController::~MachineController() {
|
||||
stop();
|
||||
}
|
||||
|
||||
void MachineController::start() {
|
||||
if (m_running.exchange(true)) return;
|
||||
m_thread = std::thread([this]{ runLoop(); });
|
||||
}
|
||||
|
||||
void MachineController::stop() {
|
||||
if (!m_running.exchange(false)) return;
|
||||
if (m_thread.joinable()) m_thread.join();
|
||||
}
|
||||
|
||||
void MachineController::step() {
|
||||
try {
|
||||
if (m_machine) {
|
||||
m_machine->execute();
|
||||
m_machine->tick();
|
||||
emit tick();
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
emit error(QString::fromStdString(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
void MachineController::runLoop() {
|
||||
const auto minUpdateInterval = milliseconds(16);
|
||||
|
||||
while (m_running.load()) {
|
||||
try {
|
||||
if (m_machine) {
|
||||
m_machine->execute();
|
||||
m_machine->tick();
|
||||
m_ticksSinceLastUpdate++;
|
||||
|
||||
// Throttle GUI updates to 60 Hz
|
||||
auto now = steady_clock::now();
|
||||
auto elapsed = duration_cast<milliseconds>(now - m_lastUpdateTime);
|
||||
|
||||
if (elapsed >= minUpdateInterval) {
|
||||
emit tick();
|
||||
m_lastUpdateTime = now;
|
||||
m_ticksSinceLastUpdate = 0;
|
||||
}
|
||||
|
||||
if (m_machine->isStopped()) {
|
||||
emit tick();
|
||||
m_running.store(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
emit error(QString::fromStdString(e.what()));
|
||||
// Stop on fatal error
|
||||
m_running.store(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue