added new instructions and new test

This commit is contained in:
zanostro 2025-11-12 12:13:26 +01:00
parent d4754a048d
commit 6b3f4989ae
6 changed files with 142 additions and 38 deletions

View file

@ -5,6 +5,7 @@
#include "opcode.h"
#include "instructions.h"
#include <cmath>
#include <thread>
using std::make_shared;
@ -29,6 +30,17 @@ Machine::~Machine()
}
}
int Machine::getSpeed() const
{
return speedkHz.load();
}
void Machine::setSpeed(int kHz)
{
speedkHz.store(kHz);
}
// TODO: implement errors
void Machine::notImplemented(string mnemonic)
{
cout << prefix << "Not implemented: " << mnemonic << endl;
@ -39,6 +51,7 @@ void Machine::invalidOpcode(int opcode)
cout << prefix << "Invalid opcode: " << opcode << endl;
}
void Machine::invalidAddressing()
{
cout << prefix << "Invalid addressing mode" << endl;
@ -54,6 +67,15 @@ void Machine::undefinedHandler(int opcode)
cout << prefix << "Undefined handler for opcode: " << opcode << endl;
}
void Machine::tick()
{
const int speed = speedkHz.load();
if (speed <= 0) throw std::runtime_error("Invalid speed setting in Machine::tick");
const auto delay = std::chrono::microseconds(1000 / speed);
std::this_thread::sleep_for(delay);
}
int Machine::getReg(int regNum) const
{
switch (regNum) {
@ -307,6 +329,11 @@ void Machine::execute() {
bool Machine::execF1(int opcode)
{
if (instructions[opcode].handler) {
auto handler = reinterpret_cast<void(*)(Machine&)>(instructions[opcode].handler);
handler(*this);
return true;
}
undefinedHandler(opcode);
return false;
}
@ -369,3 +396,19 @@ bool Machine::execSICF3F4(int opcode, int ni, int x, int b, int p, int e, int op
return false;
}
void Machine::start()
{
running.store(true);
// Main execution loop
// TODO: consider running in separate thread
while (running.load()) {
execute();
tick();
}
}
void Machine::stop()
{
running.store(false);
}