// SPDX-License-Identifier: GPL-3.0-or-later #include "TestMachine.h" using namespace VCCTest; void CheckMmuMapping() { const unsigned sizes[] = {0x20000, 0x80000, 0x200000, 0x800000}; const unsigned masks[] = {15, 63, 255, 1023}; for (unsigned char config = 0; config < 4; ++config) { MachineFixture f(config); const auto base = config == 0 ? 8 : 56; auto state = GetMMUState(); require(!state.Enabled && state.ActiveTask == 0 && state.Task0[0] == base && state.Task1[7] == base + 7, "MMU reset maps upper 64 KiB for both tasks"); for (unsigned p = 0; p < sizes[config] / 8192; ++p) { SetMem(p * 8192 + 0x123, p & 255); SetMem(p * 8192 + 0x124, p >> 8); } f.write(0xffdf, 0); // SAM all-RAM mode require(MemRead8(0x123) == base, "MMU disabled mapping"); f.write(0xffa0, 5); f.write(0xffa8, 9); f.write(0xff90, 0x40); require(MemRead8(0x123) == 5, "MMU task zero mapping via GIME register"); f.write(0xff91, 1); require(MemRead8(0x123) == 9, "MMU task one mapping via GIME register"); f.write(0x123, 0x55); require(GetMem(9 * 8192 + 0x123) == 0x55 && GetMem(5 * 8192 + 0x123) == 5, "MMU tasks isolate physical writes"); f.write(0xff91, 0); f.write(0xffa0, 0xff); require(GetMMUState().Task0[0] == (255 & masks[config]), "MMU installed RAM bank mask"); f.write(0xff9b, 0x30); f.write(0xffa0, 0xab); const auto page = config == 3 ? 0x3ab : (0xab & masks[config]); require(GetMMUState().Task0[0] == page && MemRead8(0x123) == (page & 255) && MemRead8(0x124) == (page >> 8), "MMU extended bank prefix"); std::array snapshot; GetMMUPage(page, snapshot); require(snapshot[0x123] == (page & 255), "MMU debugger physical page snapshot"); f.write(0xff90, 0); // disabled mapping independent of selected task require(MemRead8(0x123) == base, "MMU disable restores fixed map"); } } void CheckMmuRomAndVectors() { for (unsigned char config = 0; config < 4; ++config) { MachineFixture f(config); const auto vectorPage = config == 0 ? 15 : 63; const unsigned char expected[4][4] = { {0xa0,0xa1,0xc0,0xc1}, {0xa0,0xa1,0xc0,0xc1}, {0xa0,0xa1,0xa2,0xa3}, {0xc2,0xc3,0xc0,0xc1} }; for (unsigned char map = 0; map < 4; ++map) { f.write(0xff90, map); for (unsigned bank = 0; bank < 4; ++bank) { const auto address = 0x8000 + bank * 8192; require(MemRead8(address) == expected[map][bank], "MMU internal/cartridge ROM selection"); f.write(address, 0x17); require(MemRead8(address) == expected[map][bank] && !MemCheckWrite(address), "MMU ROM writes are blocked"); } require(MemRead16(0xfffe) == 0x4000, "SAM vectors always read internal ROM"); } f.write(0xffdf, 0); f.write(0x8000, 0x17); require(MemRead8(0x8000) == 0x17 && MemCheckWrite(0x8000), "SAM all-RAM enables writes behind ROM"); f.write(0xffde, 0); require(MemRead8(0x8000) == 0xc2, "SAM restores selected ROM overlay"); f.write(0xffdf, 0); f.write(0xff90, 0x40); f.write(0xffa7, 1); SetMem(8192 + 0x1e00, 0x11); SetMem(vectorPage * 8192 + 0x1e00, 0x22); require(MemRead8(0xfe00) == 0x11, "FE00 follows mapped page with RAM vectors disabled"); f.write(0xff90, 0x48); require(MemRead8(0xfe00) == 0x22, "RAM vector window bypasses MMU mapping"); f.write(0xfe00, 0x33); require(GetMem(vectorPage * 8192 + 0x1e00) == 0x33 && GetMem(8192 + 0x1e00) == 0x11, "RAM vector write reaches fixed physical page"); } } void CheckMmuLifecycle() { MachineFixture f; auto* original = Get_mem_pointer(); require(original[0] == 255 && original[3] == 255 && original[4] == 0 && original[7] == 0, "MMU retains upstream power-on RAM pattern"); require(!MmuInit(4) && Get_mem_pointer() == original, "MMU rejects invalid RAM size without losing machine"); f.host.romFailure = true; f.host.rom.fill(0x17); require(!MmuInit(0) && Get_mem_pointer() == original && GetRamConfiguration() == 1, "MMU failed ROM load preserves prior allocation/configuration"); LoadRom(); require(Getint_rom_pointer()[0] == 0xa0, "Failed ROM reload leaves current ROM intact"); std::array page; GetMMUPage(std::numeric_limits::max(), page); require(std::all_of(page.begin(), page.end(), [](auto v) { return v == 255; }), "Invalid debugger page is bounded"); f.host.romFailure = false; require(MmuInit(0) && GetRamConfiguration() == 0, "MMU can reinitialize to another RAM size"); require(MemRead8(0xfffe) == 0x17, "SAM reads replacement ROM without stale pointer"); MmuShutdown(); MmuShutdown(); require(!Get_mem_pointer() && !Getint_rom_pointer() && MemRead8(0) == 255 && SafeMemRead8(0xff00) == 0 && GetMem(0) == 0 && !MemCheckWrite(0), "MMU teardown and inspection are safe"); } void CheckGimeRegisters() { MachineFixture f(1, true); f.write(0x4000, 0xc0); f.write(0x4001, 0x40); MC6809ForcePC(0x4001); HD6309ForcePC(0x4000); f.write(0xffa0, 5); require(MemRead8(0xffa0) == 0xc5, "GIME floating bus uses selected 6309, not inactive 6809"); f.write(0xffb3, 0xff); require(MemRead8(0xffb3) == 0xff && std::get<0>(f.host.video.back()) == VCC::VideoRegister::Palette && std::get<1>(f.host.video.back()) == 63 && std::get<2>(f.host.video.back()) == 3, "GIME palette masks data and forwards index"); f.write(0xff95, 0x34); f.write(0xff94, 0xf2); f.write(0xff91, 0x20); require(f.host.timer == std::vector>{ {VCC::TimerRegister::Reload, 0x34}, {VCC::TimerRegister::Restart, 0x234}, {VCC::TimerRegister::ClockSource, 0x20}}, "GIME timer reload/restart/source forwarding"); f.write(0xff9d, 0x12); f.write(0xff9e, 0x34); require(std::get<0>(f.host.video.back()) == VCC::VideoRegister::Offset && std::get<1>(f.host.video.back()) == 0x1234, "GIME video offset combines bytes"); f.write(0xff96, 0xff); f.write(0xffd9, 0); require(f.host.turbo == 1 && f.host.speed == 1, "GIME/SAM speed outputs"); f.write(0xffd8, 0); require(f.host.speed == 0, "SAM normal speed output"); f.write(0xffc1, 0); f.write(0xffc5, 0); f.write(0xffc7, 0); require(VDG_Modes() == 5 && VDG_Offset() == 1, "SAM bit-addressed video controls"); f.write(0xff40, 0x12); require(MemRead8(0xff40) == 0x5a && f.host.cartWrites == 1 && f.host.cartValue == 0x12, "Unowned I/O ports route to cartridge host"); f.write(0xff90, 0x30); f.write(0xff92, 8); f.write(0xff93, 16); GimeAssertVertInterupt(); GimeAssertHorzInterupt(); require(f.host.lines[IS_GIME] == (Bit(INT_IRQ) | Bit(INT_FIRQ)), "GIME independently asserts IRQ and FIRQ"); require(SafeMemRead8(0xff92) == 8 && (f.host.lines[IS_GIME] & Bit(INT_IRQ)), "Debugger GIME read does not acknowledge IRQ"); require(MemRead8(0xff92) == 8 && f.host.lines[IS_GIME] == Bit(INT_FIRQ), "GIME IRQ read acknowledges only IRQ"); require(MemRead8(0xff93) == 16 && !f.host.lines[IS_GIME], "GIME FIRQ read acknowledges FIRQ"); GimeAssertVertInterupt(); GimeAssertHorzInterupt(); GimeRegistersReset(); require(!f.host.lines[IS_GIME], "GIME reset clears asserted CPU lines"); } void CheckPiaRegisters() { MachineFixture f; f.write(0xff02, 0x0f); // PIA0 B direction require(MemRead8(0xff02) == 0x0f, "PIA direction register access"); f.write(0xff03, 4); f.write(0xff02, 5); f.write(0xff01, 4); require(MemRead8(0xff00) == 0x7e && f.host.columns == 0xf5, "PIA keyboard scan respects output direction"); f.write(0xff00, 0); require(f.host.joystickStarts == 1, "PIA starts CCMax joystick measurement"); f.write(0xff20, 0xff); f.write(0xff21, 4); f.write(0xff23, 8); f.write(0xff20, 0xfe); require(f.host.printerBit == 1 && f.host.joystickStarts == 2, "PIA serial and Tandy outputs"); require(DACState() == (63u << 8), "PIA six-bit DAC is at full scale with serial output high"); require(GetDACSample() == ((126u * 128u) << 16 | (126u * 128u)), "PIA DAC stereo sample"); f.write(0xff23, 0); f.write(0xff20, 0xfc); require(GetCasSample() == 0xfc, "PIA cassette output selection"); f.write(0xff21, 12); require(f.host.motor == 1, "PIA cassette motor output"); f.host.fastTape = true; f.host.tapeBit = 1; require((MemRead8(0xff20) & 1) == 1, "PIA fast cassette input"); f.host.fastTape = false; SetCassetteSample(0); require((MemRead8(0xff20) & 1) == 0, "PIA sampled cassette input"); f.write(0xff01, 12); f.write(0xff03, 12); require(GetMuxState() == 3, "PIA analog mux selection"); } void CheckPiaInterrupts() { MachineFixture f; f.write(0xff01, 5); f.write(0xff03, 7); // HS falling, VS rising irq_hs(RISING); require(!f.host.lines[IS_PIA0_HSYNC], "PIA ignores opposite HS edge"); irq_hs(FALLING); irq_fs(RISING); require((MemRead8(0xff01) & 128) && (MemRead8(0xff03) & 128), "PIA control reads retain interrupt flags"); require(f.host.lines[IS_PIA0_HSYNC] && f.host.lines[IS_PIA0_VSYNC], "PIA HS/VS IRQ sources coexist"); (void)SafeMemRead8(0xff00); require(f.host.lines[IS_PIA0_HSYNC], "Debugger PIA read has no acknowledge side effect"); (void)MemRead8(0xff00); require(!f.host.lines[IS_PIA0_HSYNC] && f.host.lines[IS_PIA0_VSYNC], "PIA data read acknowledges only its source"); f.write(0xff23, 5); AssertCart(); require(f.host.lines[IS_PIA1_CART] == Bit(INT_FIRQ), "PIA cartridge FIRQ"); (void)MemRead8(0xff22); require(!f.host.lines[IS_PIA1_CART], "PIA cartridge data read acknowledges FIRQ"); irq_hs(FALLING); AssertCart(); PiaReset(); require(!f.host.lines[IS_PIA0_HSYNC] && !f.host.lines[IS_PIA0_VSYNC] && !f.host.lines[IS_PIA1_CART], "PIA reset clears its CPU interrupt sources"); } void CheckMappedCpuExecution() { for (bool use6309 : {false, true}) { MachineFixture f(1, use6309); const std::vector program = { 0x10,0xce,0x70,0x00, // LDS #$7000 0x86,0x42,0xb7,0xff,0x90, // enable MMU, internal ROM 0x86,0x05,0xb7,0xff,0xa0, // task 0 page 0 = physical page 5 0x86,0xa5,0xb7,0x01,0x23, // write through task 0 0x86,0x06,0xb7,0xff,0xa8, // task 1 page 0 = physical page 6 0x86,0x01,0xb7,0xff,0x91, // select task 1 0x86,0x5a,0xb7,0x01,0x23, // write through task 1 0x86,0x00,0xb7,0xff,0x91, // select task 0 0xb6,0x01,0x23, // read original task 0 value 0x1c,0xaf,0x12 // unmask interrupts; NOP }; for (unsigned i = 0; i < program.size(); ++i) f.write(0x4000+i, program[i]); const unsigned short stop = 0x4000 + program.size() - 3; auto exec = use6309 ? HD6309Exec : MC6809Exec; auto breakpoints = use6309 ? HD6309SetBreakpoints : MC6809SetBreakpoints; breakpoints({stop}); const auto cycles = 1000 - exec(1000); require(f.debugger.halted && f.host.CpuState().PC == stop && f.host.CpuState().A == 0xa5 && GetMem(5*8192+0x123) == 0xa5 && GetMem(6*8192+0x123) == 0x5a, "CPU executes guest bank switching through actual MMU/GIME/I/O"); require(cycles > 0 && VCC::gEvents.GetClocks() == cycles*16u, "Mapped CPU advances scheduler"); breakpoints({}); f.debugger.halted = false; exec(1); // ANDCC f.write(0x5000, 0x3b); // IRQ handler: RTI f.write(0xff95, 1); f.write(0xff90, 0x62); f.write(0xff92, 0x20); GimeAssertTimerInterupt(); breakpoints({0x5000}); exec(100); require(f.debugger.halted && f.host.CpuState().PC == 0x5000 && f.host.CpuState().S == 0x6ff4, "GIME IRQ reaches selected CPU through real SAM vector and mapped stack"); require(MemRead8(0xff92) == 0x20, "Guest acknowledges GIME timer IRQ"); breakpoints({}); f.debugger.halted = false; exec(1); require(f.host.CpuState().PC == stop+2 && f.host.CpuState().S == 0x7000, "CPU RTI restores guest after mapped IRQ"); } }