#pragma once // SPDX-License-Identifier: GPL-3.0-or-later #include "CoreChecks.h" #include "../../src/core/MachineHost.h" #include "../../src/core/CpuHost.h" #include "../../src/core/CpuMemory.h" #include "../../src/core/tcc1014mmu.h" #include "../../src/core/tcc1014registers.h" #include "../../src/core/mc6821.h" #include "../../src/core/mc6809.h" #include "../../src/core/hd6309.h" #include "../../src/core/Event.h" #include #include #include #include #include #include namespace VCCTest { inline void require(bool ok, const char* message) { if (!ok) throw std::runtime_error(message); } // Explicit test replacements for components not extracted yet. The real MMU, // port decoder, GIME/SAM and PIA implementations are linked from VCCCore. struct Host : VCC::MachineHost { bool use6309 = false, romFailure = false, fastTape = false; unsigned char keyboard = 0x7e, columns = 0, tapeBit = 1, motor = 0; unsigned char turbo = 0, speed = 0, printerBit = 0, cartPort = 0, cartValue = 0; unsigned short cartAudio = 0; unsigned joystickStarts = 0, cartReads = 0, cartWrites = 0; std::array lines{}; std::array rom{}; std::vector> video; std::vector> timer; Host() { for (unsigned page = 0; page < 4; ++page) std::fill_n(rom.begin() + page * 8192, 8192, 0xa0 + page); rom[0x7ffe] = 0x40; rom[0x7fff] = 0x00; rom[0x7ff8] = 0x50; rom[0x7ff9] = 0x00; } bool LoadInternalRom(unsigned char* dest, std::size_t size) override { require(size == rom.size(), "ROM provider capacity"); std::copy(rom.begin(), rom.end(), dest); return !romFailure; } unsigned char CartridgeRead(unsigned short address) override { return 0xc0 + address / 8192; } unsigned char CartridgePortRead(unsigned char port) override { ++cartReads; cartPort = port; return 0x5a; } void CartridgePortWrite(unsigned char port, unsigned char value) override { ++cartWrites; cartPort = port; cartValue = value; } unsigned short CartridgeAudio() override { return cartAudio; } void VideoWrite(VCC::VideoRegister reg, unsigned int value, unsigned char index) override { video.emplace_back(reg, value, index); } void TimerWrite(VCC::TimerRegister reg, unsigned int value) override { timer.emplace_back(reg, value); } void SetTurbo(unsigned char value) override { turbo = value; } void SetCpuSpeed(unsigned char value) override { speed = value; } VCC::CPUState CpuState() override { return use6309 ? HD6309GetState() : MC6809GetState(); } void AssertInterrupt(InterruptSource source, Interrupt interrupt) override { lines[source] |= Bit(interrupt); if (use6309) HD6309AssertInterupt(source, interrupt); else MC6809AssertInterupt(source, interrupt); } void ClearInterrupt(InterruptSource source, Interrupt interrupt) override { lines[source] &= BitMask(interrupt); if (use6309) HD6309DeAssertInterupt(source, interrupt); else MC6809DeAssertInterupt(source, interrupt); } unsigned char KeyboardScan(unsigned char value) override { columns = value; return keyboard; } void JoystickStart(bool, unsigned char) override { ++joystickStarts; } bool TapeFastLoad() override { return fastTape; } unsigned char TapeInputBit() override { return tapeBit; } void TapeMotor(unsigned char value) override { motor = value; } void PrinterBit(unsigned char value) override { printerBit = value; } }; struct Debugger final : VCC::CpuDebugHooks { bool halted = false; bool IsHalted() const override { return halted; } void Halt() override { halted = true; } }; struct MachineFixture { Host host; Debugger debugger; explicit MachineFixture(unsigned char config = 1, bool use6309 = false) { host.use6309 = use6309; VCC::SetCpuMemoryBus(nullptr); VCC::SetMachineHost(&host); VCC::SetCpuDebugHooks(&debugger); MC6809Init(); HD6309Init(); if (!MmuInit(config)) { VCC::SetMachineHost(nullptr); VCC::SetCpuDebugHooks(nullptr); throw std::runtime_error("Initialize synthetic machine ROM/RAM"); } mc6883_reset(); GimeRegistersReset(); PiaReset(); MC6809Reset(); HD6309Reset(); MC6809SetBreakpoints({}); HD6309SetBreakpoints({}); MC6809SetTraceTriggers({}); HD6309SetTraceTriggers({}); VCC::gEvents.Reset(); } ~MachineFixture() { MC6809SetBreakpoints({}); HD6309SetBreakpoints({}); MmuShutdown(); VCC::SetCpuDebugHooks(nullptr); VCC::SetMachineHost(nullptr); VCC::gEvents.Reset(); } void write(unsigned short address, unsigned char value) { MemWrite8(value, address); } }; }