// SPDX-License-Identifier: GPL-3.0-or-later #include "CoreChecks.h" #include "../../src/core/CpuHost.h" #include "../../src/core/CpuMemory.h" #include "../../src/core/Event.h" #include "../../src/core/mc6809.h" #include "../../src/core/hd6309.h" #include #include #include #include #include #include // Test-only bus and joystick clock. These must never be linked into the app: // there is no ROM protection, MMU, GIME, I/O decoding, or peripheral emulation. namespace { std::array memory; unsigned char mapType; struct FlatBus final : VCC::CpuMemoryBus { std::function beforeRead; unsigned char Read(unsigned short address) override { if (beforeRead) beforeRead(address); return memory[address]; } void Write(unsigned char value, unsigned short address) override { memory[address] = value; } void SelectMap(unsigned char type) override { mapType = type; } }; void require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } struct Hooks final : VCC::CpuDebugHooks { bool halted = false, stepping = false, enabled = false, tracing = false; std::vector before, after; std::vector requested, serviced, executing; bool IsHalted() const override { return halted; } bool IsStepping() const override { return stepping; } void Halt() override { halted = true; stepping = false; } bool IsTracingEnabled() const override { return enabled; } bool IsTracing() const override { return tracing; } void TraceStart() override { tracing = true; } void TraceCaptureBefore(long, const VCC::CPUState& s) override { before.push_back(s); } void TraceCaptureAfter(long, const VCC::CPUState& s) override { after.push_back(s); } void TraceCaptureInterruptRequest(unsigned char irq, long, const VCC::CPUState&) override { requested.push_back(irq); } void TraceCaptureInterruptServicing(unsigned char irq, long, const VCC::CPUState&) override { serviced.push_back(irq); } void TraceCaptureInterruptExecuting(unsigned char irq, long, const VCC::CPUState&) override { executing.push_back(irq); } }; struct Cpu { void (*init)(); void (*reset)(); int (*exec)(int); VCC::CPUState (*state)(); void (*assertIRQ)(InterruptSource, Interrupt); void (*clearIRQ)(InterruptSource, Interrupt); void (*breakpoints)(const std::vector&); void (*triggers)(const std::vector&); }; const Cpu m6809 {MC6809Init, MC6809Reset, MC6809Exec, MC6809GetState, MC6809AssertInterupt, MC6809DeAssertInterupt, MC6809SetBreakpoints, MC6809SetTraceTriggers}; const Cpu h6309 {HD6309Init, HD6309Reset, HD6309Exec, HD6309GetState, HD6309AssertInterupt, HD6309DeAssertInterupt, HD6309SetBreakpoints, HD6309SetTraceTriggers}; struct Fixture { const Cpu& cpu; Hooks hooks; FlatBus bus; explicit Fixture(const Cpu& c) : cpu(c) { VCC::SetCpuMemoryBus(&bus); memory.fill(0); mapType = 255; VCC::gEvents.Reset(); VCC::SetCpuDebugHooks(&hooks); cpu.init(); cpu.breakpoints({}); cpu.triggers({}); MemWrite16(0x4000, 0xfffe); cpu.reset(); } ~Fixture() { cpu.breakpoints({}); cpu.triggers({}); VCC::SetCpuDebugHooks(nullptr); VCC::SetCpuMemoryBus(nullptr); VCC::gEvents.Reset(); } void load(std::initializer_list bytes, unsigned short address = 0x4000) { std::copy(bytes.begin(), bytes.end(), memory.begin() + address); } int instruction() { return 1 - cpu.exec(1); } }; void execution(const Cpu& cpu) { Fixture f(cpu); auto s = cpu.state(); require(s.PC == 0x4000 && s.CC == 0x50 && s.A == 0 && s.B == 0 && s.X == 0 && s.Y == 0 && s.U == 0 && s.S == 0 && s.DP == 0 && !s.IsNative6309, "CPU reset registers/vector/masks"); require(mapType == 0, "CPU reset selects map zero"); // Authored guest fixture: docs/macos/reference/cpu6809.asm, no ROM needed. f.load({0x1a,0x50,0x10,0xce,0x70,0x00,0x8e,0x41,0x00,0x4f, 0xc6,0x10,0x4c,0xa7,0x80,0x5a,0x26,0xfa,0x20,0xfe}); cpu.breakpoints({0x4012}); f.hooks.enabled = true; const int cycles = 1000 - cpu.exec(1000); s = cpu.state(); require(f.hooks.halted && s.PC == 0x4012 && s.A == 16 && s.B == 0 && s.X == 0x4110 && s.S == 0x7000, "CPU guest loop final registers/breakpoint"); for (unsigned i = 0; i < 16; ++i) require(memory[0x4100+i] == i+1, "CPU guest loop indexed stores"); require(f.hooks.before.size() == 69 && f.hooks.after.size() == 69, "CPU guest loop executes 69 instructions"); require(cycles == 222 && VCC::gEvents.GetClocks() == 222*16, "CPU guest loop cycle budget and scheduler clocks"); f.hooks.halted = false; cpu.breakpoints({}); cpu.reset(); f.load({0x86,0x7f,0x8b,0x01,0x8b,0x80,0x80,0x01}); require(f.instruction() == 2, "LDA immediate cycles"); f.instruction(); s = cpu.state(); require(s.A == 0x80 && (s.CC & 0x2f) == 0x2a, "ADDA signed overflow/half-carry flags"); f.instruction(); s = cpu.state(); require(s.A == 0 && (s.CC & 0x2f) == 0x07, "ADDA unsigned carry/zero/overflow flags"); f.instruction(); s = cpu.state(); require(s.A == 0xff && (s.CC & 0x0f) == 0x09, "SUBA borrow/negative flags"); cpu.reset(); f.load({0xcc,0x12,0x34,0xfd,0xff,0xff}); // LDD #$1234; STD $FFFF f.instruction(); f.instruction(); require(memory[0xffff] == 0x12 && memory[0] == 0x34, "CPU big-endian word store wraps bus address"); } // A changing peripheral flag must be sampled during TST, before instruction // completion/IRQ entry. An all-at-the-end tick starves polling loops whenever // the IRQ handler acknowledges the same flag. No proprietary guest code needed. void testOperandTiming(const Cpu& cpu) { for (unsigned mode = 0; mode < 4; ++mode) { // Direct, ,X, extended, and 8-bit indexed offset (extra EA cycle). const unsigned cycles = mode == 2 || mode == 3 ? 7 : 6; const unsigned readCycle = cycles - 2; for (unsigned edge = 1; edge <= cycles; ++edge) { Fixture f(cpu); f.load({0x86,0x42,0x1f,0x8b,0x8e,0x42,0x00,0x1a,0x03}); // DP=$42, X=$4200; set V/C for (unsigned n = 0; n < 4; ++n) f.instruction(); if (mode == 0) f.load({0x0d,0x00}, 0x4009); if (mode == 1) f.load({0x6d,0x84}, 0x4009); if (mode == 2) f.load({0x7d,0x42,0x00}, 0x4009); if (mode == 3) f.load({0x6d,0x88,0x01}, 0x4009); const unsigned address = mode == 3 ? 0x4201 : 0x4200; const auto start = VCC::gEvents.GetClocks(); unsigned reads = 0; f.bus.beforeRead = [&](unsigned short a) { if (a == address) { ++reads; require(VCC::gEvents.GetClocks() - start == readCycle * 16, "TST samples operand after addressing/data cycles, before trailing cycles"); } }; // Place the edge just before the cycle boundary (scheduler intervals are half-open). VCC::gEvents.Queue(edge * 16 - 1, [&] { memory[address] = 0x80; }, 1); require(f.instruction() == cycles, "TST retains total instruction timing"); require(reads == 1 && memory[address] == 0x80, "TST reads exactly once and advances pending events"); require((cpu.state().CC & 0x0f) == (edge <= readCycle ? 0x09 : 0x05), "TST sees only edges before operand sampling, clears V and preserves C"); } Fixture f(cpu); f.load({0x10,0xce,0x70,0x00,0x86,0x42,0x1f,0x8b,0x8e,0x42,0x00,0x1c,0xaf}); for (unsigned n = 0; n < 5; ++n) f.instruction(); // LDS, LDA, TFR, LDX, ANDCC unsigned short passed = 0; if (mode == 0) { f.load({0x0d,0x00,0x2a,0xfc,0x12}, 0x400d); passed = 0x4011; } if (mode == 1) { f.load({0x6d,0x84,0x2a,0xfc,0x12}, 0x400d); passed = 0x4011; } if (mode == 2) { f.load({0x7d,0x42,0x00,0x2a,0xfb,0x12}, 0x400d); passed = 0x4012; } if (mode == 3) { f.load({0x6d,0x88,0x01,0x2a,0xfb,0x12}, 0x400d); passed = 0x4012; } const unsigned address = mode == 3 ? 0x4201 : 0x4200; f.load({0xb6,0x43,0x00,0x3b}, 0x5000); // IRQ: read peripheral acknowledge; RTI MemWrite16(0x5000, 0xfff8); unsigned acknowledgements = 0; f.bus.beforeRead = [&](unsigned short a) { if (a == 0x4300) { ++acknowledgements; memory[address] = 0; cpu.clearIRQ(IS_PIA0_VSYNC, INT_IRQ); } }; VCC::gEvents.Queue(2 * 16, [&] { memory[address] = 0x80; cpu.assertIRQ(IS_PIA0_VSYNC, INT_IRQ); }, 1); cpu.breakpoints({passed}); cpu.exec(200); require(f.hooks.halted && cpu.state().PC == passed && acknowledgements == 1 && memory[address] == 0 && cpu.state().S == 0x7000, "TST polling exits after IRQ acknowledges flag and RTI restores sampled condition codes"); } } void interrupts(const Cpu& cpu) { for (auto irq : {INT_IRQ, INT_FIRQ, INT_NMI}) { Fixture f(cpu); f.load({0x10,0xce,0x70,0x00,0x1c,0xaf,0x12}); // LDS; ANDCC clears I/F; NOP f.load({0x3b}, 0x5000); // RTI const unsigned short vector = irq == INT_IRQ ? 0xfff8 : irq == INT_FIRQ ? 0xfff6 : 0xfffc; MemWrite16(0x5000, vector); f.instruction(); f.instruction(); const auto before = cpu.state(); f.hooks.tracing = true; cpu.breakpoints({0x5000}); const auto source = irq == INT_NMI ? IS_NMI : IS_GIME; cpu.assertIRQ(source, irq); if (irq != INT_NMI) { // Independent sources share a line; clearing one must not clear the other. cpu.assertIRQ(IS_PIA0_HSYNC, irq); cpu.clearIRQ(source, irq); } cpu.exec(100); const auto handler = cpu.state(); const unsigned frameBytes = irq == INT_FIRQ ? 3 : 12; require(f.hooks.halted && handler.PC == 0x5000 && handler.S == 0x7000-frameBytes, "CPU interrupt vector and stack frame"); require(MemRead16(0x6ffe) == before.PC, "CPU interrupt stacked return address"); require((memory[handler.S] & 0x80) == (irq == INT_FIRQ ? 0 : 0x80), "CPU interrupt entire-state flag"); require(f.hooks.serviced == std::vector{static_cast(irq)} && f.hooks.executing == f.hooks.serviced && !f.hooks.requested.empty(), "CPU interrupt debug callbacks"); cpu.clearIRQ(source, irq); if (irq != INT_NMI) cpu.clearIRQ(IS_PIA0_HSYNC, irq); cpu.breakpoints({}); f.hooks.halted = false; f.instruction(); const auto restored = cpu.state(); require(restored.PC == before.PC && restored.S == before.S && restored.A == before.A && restored.B == before.B && (restored.CC & 0x7f) == (before.CC & 0x7f), "CPU RTI restores interrupted state"); } // Reset masks IRQ/FIRQ; reset also clears previously asserted sources. for (auto irq : {INT_IRQ, INT_FIRQ}) { Fixture f(cpu); f.load({0x12,0x12}); cpu.assertIRQ(IS_GIME, irq); f.instruction(); require(cpu.state().PC == 0x4001, "CPU masks IRQ/FIRQ after reset"); cpu.reset(); f.load({0x1c,0xaf,0x12}); f.instruction(); f.instruction(); require(cpu.state().PC == 0x4003, "CPU reset clears interrupt sources"); } } } void Check6809Execution() { execution(m6809); } void Check6309Execution() { execution(h6309); } void Check6809Interrupts() { interrupts(m6809); testOperandTiming(m6809); } void Check6309Interrupts() { interrupts(h6309); testOperandTiming(h6309); } void Check6309Native() { Fixture f(h6309); // LDMD #1; LDQ #$12345678; STQ $4200; NOP. f.load({0x11,0x3d,0x01,0xcd,0x12,0x34,0x56,0x78,0x10,0xfd,0x42,0x00,0x12}); f.instruction(); require(h6309.state().IsNative6309, "6309 enters native mode"); f.instruction(); auto s = h6309.state(); require(s.A == 0x12 && s.B == 0x34 && s.E == 0x56 && s.F == 0x78, "6309 LDQ register byte order"); f.instruction(); require(MemRead16(0x4200) == 0x1234 && MemRead16(0x4202) == 0x5678, "6309 STQ memory byte order"); require(f.instruction() == 1, "6309 native NOP timing"); h6309.reset(); f.load({0x12}); require(!h6309.state().IsNative6309 && f.instruction() == 2, "6309 reset restores emulation mode timing"); } void CheckCpuDebugHooks() { for (const auto& cpu : {m6809, h6309}) { Fixture f(cpu); f.load({0x12,0x12,0x12,0x12}); cpu.triggers({0x4001}); cpu.breakpoints({0x4003}); f.hooks.enabled = true; cpu.exec(100); require(f.hooks.halted && f.hooks.before.size() == 2 && f.hooks.after.size() == 2 && f.hooks.before.front().PC == 0x4001 && f.hooks.after.back().PC == 0x4003, "CPU trace trigger starts at requested instruction and stops before breakpoint"); require(cpu.exec(10) == 10 && cpu.state().PC == 0x4003, "CPU halt consumes no cycles/instructions"); f.hooks.halted = false; f.hooks.stepping = true; require(f.instruction() == 2 && cpu.state().PC == 0x4004 && f.hooks.halted, "CPU debugger single-step advances exactly one instruction"); } } void CheckEventScheduler() { VCC::Event event; event.Tick(1); require(event.GetClocks() == 16, "Event constructor initializes default clock rate"); event.Reset(); std::vector order; event.Queue(32, [&] { order.push_back(2); event.Queue(1, [&] { order.push_back(3); }); }); event.Queue(16, [&] { order.push_back(1); }); event.Tick(1); require(order.empty(), "Events at exact tick boundary remain queued"); event.Tick(2); require(order == std::vector({1,2,3}) && event.GetClocks() == 48, "Events run in clock order including reentrant queue"); event.Queue(32, [&] { order.push_back(4); }, 1); event.ReQueue(16, [&] { order.push_back(5); }, 1); event.Queue(16, [&] { order.push_back(6); }, 2); event.Erase(2); event.Rebase(); event.Tick(2); require(order.back() == 5 && order.size() == 4 && event.GetSize() == 0, "Event reschedule/erase/rebase preserves remaining delays"); event.SetClocksPerCycle(8); event.SetOverclock(2); event.Rebase(); event.Tick(2); require(event.GetClocks() == 8, "Event overclock and cycle rate apply at rebase"); event.Reset(); event.Rebase(); event.Tick(1); require(event.GetClocks() == 16, "Event reset clears requested rate/overclock"); }