final version

This commit is contained in:
zanostro 2025-12-07 11:16:00 +01:00
parent 098766bb65
commit 042bae9089
8 changed files with 143 additions and 21 deletions

View file

@ -210,6 +210,14 @@ void divf_handler(Machine &m, int ea, AddressingMode mode)
void j_handler(Machine &m, int ea, AddressingMode mode)
{
int target = resolveJumpTarget(m, ea, mode);
int instrSize = 3;
int instrAddr = m.getPC() - instrSize;
// Check if jumping to itself (halt pattern)
if (target == instrAddr) {
m.halt();
}
m.setPC(target);
}
@ -265,6 +273,8 @@ void ldch_handler(Machine &m, int ea, AddressingMode mode)
int val;
if (mode == AddressingMode::IMMEDIATE) {
val = ea & 0xFF;
} else if (mode == AddressingMode::INDIRECT) {
val = m.getByte(m.getWord(ea));
} else {
val = m.getByte(ea);
}

View file

@ -95,15 +95,45 @@ int main()
loadInstructionSet();
std::shared_ptr<Machine> machine = std::make_shared<Machine>();
Loader loader(machine, std::string(PATH_RESOURCES) + "test.obj");
Loader loader(machine, std::string(PATH_RESOURCES) + "rec.obj");
loader.load();
machine->execute();
machine->execute();
machine->execute();
machine->execute();
machine->execute();
machine->execute();
cout << "Register A after execution: " << machine->getA() << endl;
cout << "=== Starting execution of rec.obj ===" << endl;
cout << "Initial PC: 0x" << std::hex << machine->getPC() << std::dec << endl;
int maxSteps = 10000;
for (int step = 0; step < maxSteps; step++) {
int pc = machine->getPC();
int opcode = machine->getByte(pc) & 0xFC;
const char* instName = (opcode < 256 && instructions[opcode].type != InstructionType::INVALID)
? instructions[opcode].name : "???";
int regA = machine->getA();
int regB = machine->getB();
int regX = machine->getX();
int regL = machine->getL();
int sw = machine->getSW();
printf("Step %4d: PC=0x%05X %-6s A=0x%06X B=0x%06X X=0x%06X L=0x%06X SW=0x%06X\n",
step, pc, instName, regA, regB, regX, regL, sw);
machine->execute();
if (machine->isStopped()) {
cout << "Machine halted at step " << step << endl;
break;
}
if (step > 100 && machine->getPC() == pc) {
cout << "ERROR: Infinite loop detected at PC=0x" << std::hex << pc << std::dec << endl;
break;
}
}
cout << "\n=== Final state ===" << endl;
cout << "A: " << machine->getA() << endl;
cout << "B: " << machine->getB() << endl;
cout << "X: " << machine->getX() << endl;
return 0;