This commit is contained in:
zanostro 2025-11-13 20:22:50 +01:00
parent 43c8fb2cce
commit 42737c0a66
5 changed files with 107 additions and 18 deletions

View file

@ -1,9 +1,14 @@
#include "MachineController.h"
#include "../../include/machine.h"
#include <chrono>
#include <QDebug>
MachineController::MachineController(QObject *parent)
: QObject(parent)
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>();
}
}
MachineController::~MachineController() {
@ -21,12 +26,30 @@ void MachineController::stop() {
}
void MachineController::step() {
emit tick();
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() {
while (m_running.load()) {
emit tick();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
try {
if (m_machine) {
m_machine->execute();
m_machine->tick();
emit tick();
}
} catch (const std::exception &e) {
emit error(QString::fromStdString(e.what()));
// Stop on fatal error
m_running.store(false);
break;
}
}
}

View file

@ -4,11 +4,14 @@
#include <QObject>
#include <atomic>
#include <thread>
#include <memory>
class Machine;
class MachineController : public QObject {
Q_OBJECT
public:
explicit MachineController(QObject *parent = nullptr);
explicit MachineController(std::shared_ptr<Machine> machine = nullptr, QObject *parent = nullptr);
~MachineController() override;
void start();
@ -17,11 +20,13 @@ public:
signals:
void tick();
void error(const QString &msg);
private:
void runLoop();
std::atomic<bool> m_running{false};
std::thread m_thread;
std::shared_ptr<Machine> m_machine;
};
#endif // MACHINECONTROLLER_H

View file

@ -1,7 +1,9 @@
#include <QApplication>
#include "mainwindow.h"
#include "../../include/opcode.h"
int main(int argc, char **argv) {
loadInstructionSet();
QApplication app(argc, argv);
MainWindow w;
w.show();

View file

@ -1,25 +1,84 @@
<?xml version='1.0'?>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>MainWindow</class>
<widget name="MainWindow" class="QMainWindow">
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
<width>1137</width>
<height>649</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget name="menubar" class="QMenuBar"/>
<widget name="centralwidget" class="QWidget"/>
<widget name="statusbar" class="QStatusBar"/>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>231</width>
<height>601</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="widget_2" native="true">
<property name="geometry">
<rect>
<x>230</x>
<y>0</y>
<width>891</width>
<height>601</height>
</rect>
</property>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>881</width>
<height>181</height>
</rect>
</property>
<property name="title">
<string>Register values</string>
</property>
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>20</y>
<width>881</width>
<height>161</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1137</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<pixmapfunction/>
<resources/>
<connections/>
</ui>

View file

@ -91,6 +91,7 @@ public:
void setSpeed(int kHz);
void start();
void stop();
void tick();
// error handling methods
void notImplemented(string mnemonic);
@ -115,7 +116,6 @@ private:
// Execution control
std::atomic<bool> running{false};
std::atomic<int> speedkHz{1}; // Default 1 kHz
void tick(); // simulate a clock tick
};