// SPDX-License-Identifier: GPL-3.0-or-later #import #include "../../../src/core/MachineSession.h" #include "../../../src/core/tcc1014mmu.h" #include "../../../src/core/Event.h" #include #include using namespace VCC::Mac; namespace { template bool rejectsMemory(F action) { try { action(); } catch (const std::exception&) { return true; } return false; } constexpr unsigned sizes[] = {0x20000, 0x80000, 0x200000, 0x800000}; } @interface DebuggerMemoryTests : XCTestCase @end @implementation DebuggerMemoryTests - (void)testPhysicalSnapshotsReachAllInstalledRAMSizesOnBothCPUs { for (auto cpu : {Processor::MC6809, Processor::HD6309}) for (unsigned ram = 0; ram < 4; ++ram) { MachineSession machine; MachineConfiguration config; config.processor = cpu; config.ram = ram; machine.configure(config); machine.loadDiagnostic(cpu); const auto last = sizes[ram] - 1; SetMem(0, 0x11); SetMem(0xffff, 0x22); SetMem(0x10000, 0x33); SetMem(last, 0x5a); const auto before = machine.snapshot(); const auto clocks = VCC::gEvents.GetClocks(); auto first = machine.debuggerSnapshot(0, 0, 0, 1); auto crossing = machine.debuggerSnapshot(0, 0, 0xffff, 2); auto end = machine.debuggerSnapshot(0, 0, last, 256); XCTAssertEqual(first.physicalMemory, (std::vector{0x11})); XCTAssertEqual(crossing.physicalMemory, (std::vector{0x22,0x33})); XCTAssertEqual(end.physicalMemory, (std::vector{0x5a})); XCTAssertEqual(end.physicalAddress, last); XCTAssertEqual(end.mmu.RAMBytes, sizes[ram]); XCTAssertEqual(end.cpu.PC, before.cpu.PC); XCTAssertEqual(end.state, before.state); XCTAssertEqual(end.cycles, before.cycles); XCTAssertEqual(end.generation, before.generation); XCTAssertEqual(VCC::gEvents.GetClocks(), clocks); XCTAssertEqual(GetMem(last), 0x5a); SetMem(last, 0xa5); XCTAssertEqual(end.physicalMemory[0], 0x5a, "Snapshot owns immutable bytes"); XCTAssertTrue(rejectsMemory([&] { machine.debuggerSnapshot(0,0,sizes[ram],1); })); XCTAssertTrue(rejectsMemory([&] { machine.debuggerSnapshot(0,0,std::numeric_limits::max(),256); })); XCTAssertTrue(rejectsMemory([&] { machine.debuggerSnapshot(0,0,0,257); })); machine.shutdown(); XCTAssertEqual(machine.debuggerSnapshot().mmu.RAMBytes, 0u); XCTAssertEqual(GetMMUState().RAMBytes, 0u); } } - (void)testBothTaskBanksAndDisabledFixedMappingAreReportedExactly { for (auto cpu : {Processor::MC6809, Processor::HD6309}) for (unsigned ram = 0; ram < 4; ++ram) { MachineSession machine; MachineConfiguration config; config.processor = cpu; config.ram = ram; machine.configure(config); machine.loadDiagnostic(cpu); MemWrite8(0, 0xffdf); // all RAM MemWrite8(5, 0xffa0); MemWrite8(9, 0xffa8); SetMem(5 * 8192, 0x55); SetMem(9 * 8192, 0x99); MemWrite8(0x40, 0xff90); auto task0 = machine.debuggerSnapshot(0,1,5*8192,1); XCTAssertTrue(task0.mmu.Enabled); XCTAssertTrue(task0.mmu.AllRAM); XCTAssertEqual(task0.mmu.ActiveTask,0); XCTAssertEqual(task0.mmu.Task0[0],5); XCTAssertEqual(task0.mmu.Task1[0],9); XCTAssertEqual(task0.mmu.Effective[0].Page,5); XCTAssertEqual(task0.mmu.Effective[0].RAMStart,5u*8192); XCTAssertEqual(task0.memory[0],0x55); XCTAssertEqual(task0.memory,task0.physicalMemory); MemWrite8(1, 0xff91); auto task1 = machine.debuggerSnapshot(0,1,9*8192,1); XCTAssertEqual(task1.mmu.ActiveTask,1); XCTAssertEqual(task1.mmu.Effective[0].Page,9); XCTAssertEqual(task1.memory[0],0x99); XCTAssertEqual(task1.memory,task1.physicalMemory); MemWrite8(0,0xff90); auto fixed = machine.debuggerSnapshot(0,1); XCTAssertFalse(fixed.mmu.Enabled); XCTAssertEqual(fixed.mmu.ActiveTask,1); XCTAssertEqual(fixed.mmu.Task0[0],5); XCTAssertEqual(fixed.mmu.Task1[0],9); const auto base = ram == 0 ? 8 : 56; for (unsigned bank = 0; bank < 8; ++bank) { XCTAssertEqual(fixed.mmu.Effective[bank].Page,base+bank); XCTAssertEqual(fixed.mmu.Effective[bank].Source,VCC::MMUSource::RAM); XCTAssertEqual(fixed.mmu.Effective[bank].SourceStart,(base+bank)*8192u); } } } - (void)testROMOverlaysAndVectorExceptionsDoNotMasqueradeAsPhysicalRAM { for (unsigned ram = 0; ram < 4; ++ram) { MachineSession machine; MachineConfiguration config; config.ram = ram; machine.configure(config); machine.loadDiagnostic(); const unsigned base = ram == 0 ? 8 : 56; const unsigned vectorPage = ram == 0 ? 15 : 63; for (unsigned map = 0; map < 4; ++map) { MemWrite8(map,0xff90); const auto snapshot = machine.debuggerSnapshot(); XCTAssertEqual(snapshot.mmu.RomMap,map); XCTAssertFalse(snapshot.mmu.AllRAM); for (unsigned bank = 0; bank < 4; ++bank) { const auto& entry = snapshot.mmu.Effective[bank+4]; XCTAssertEqual(entry.Page,base+4+bank); XCTAssertEqual(entry.RAMStart,(base+4+bank)*8192u); const bool internal = map == 2 || (map < 2 && bank < 2); XCTAssertEqual(entry.Source,internal ? VCC::MMUSource::InternalROM : VCC::MMUSource::CartridgeROM); XCTAssertEqual(entry.SourceStart,(internal ? bank : bank^2)*8192u); } } MemWrite8(0,0xffdf); auto ramMapping = machine.debuggerSnapshot(); for (const auto& entry : ramMapping.mmu.Effective) XCTAssertEqual(entry.Source,VCC::MMUSource::RAM); MemWrite8(0x40,0xff90); MemWrite8(1,0xffa7); SetMem(0x2000+0x1e00,0x17); SetMem(vectorPage*8192+0x1e00,0x63); auto ordinary = machine.debuggerSnapshot(0xfe00,1); XCTAssertEqual(ordinary.memory[0],0x17); XCTAssertFalse(ordinary.mmu.RamVectors); MemWrite8(0x48,0xff90); auto vectors = machine.debuggerSnapshot(0xfe00,1,vectorPage*8192+0x1e00,1); XCTAssertTrue(vectors.mmu.RamVectors); XCTAssertEqual(vectors.memory[0],0x63); XCTAssertEqual(vectors.mmu.RAMVectorAddress,vectorPage*8192+0x1e00); XCTAssertEqual(vectors.mmu.Effective[7].SourceStart,0x2000u, "FE00 override must not relabel the whole E000 bank"); XCTAssertEqual(vectors.physicalMemory,vectors.memory); auto resetVector = machine.debuggerSnapshot(0xfffe,2); XCTAssertEqual(resetVector.memory,(std::vector{0x80,0x00}), "SAM vectors remain internal ROM even in all-RAM mode"); } } - (void)testExtendedPagesLatchedPrefixAndPhysicalAliasing { MachineSession machine; MachineConfiguration config; config.ram = 3; machine.configure(config); machine.loadDiagnostic(); MemWrite8(0x40,0xff90); MemWrite8(0x30,0xff9b); MemWrite8(0xab,0xffa0); SetMem(0x3ab*8192+0x123,0x7c); auto high = machine.debuggerSnapshot(0x123,1,0x3ab*8192+0x123,1); XCTAssertEqual(high.mmu.Prefix,0x300); XCTAssertEqual(high.mmu.Task0[0],0x3ab); XCTAssertEqual(high.mmu.Effective[0].Page,0x3ab); XCTAssertEqual(high.mmu.Effective[0].SourceStart,0x3ab*8192u); XCTAssertEqual(high.memory,high.physicalMemory); XCTAssertEqual(high.memory[0],0x7c); MemWrite8(0,0xff9b); auto prefixChanged = machine.debuggerSnapshot(); XCTAssertEqual(prefixChanged.mmu.Prefix,0); XCTAssertEqual(prefixChanged.mmu.Task0[0],0x3ab, "Prefix changes apply to subsequent writes, not latched pages"); config.ram = 0; machine.configure(config); // A raw page index can alias installed RAM without receiving the ROM // overlay assigned to the lower reserved page index. SetMmuPrefix(3); SetMmuRegister(0,15); Set_MmuEnabled(1); SetMem(15*8192,0xa4); auto alias = machine.debuggerSnapshot(0,1,15*8192,1); XCTAssertEqual(alias.mmu.Effective[0].Page,0x30f); XCTAssertEqual(alias.mmu.Effective[0].RAMStart,15u*8192); XCTAssertEqual(alias.mmu.Effective[0].Source,VCC::MMUSource::RAM); XCTAssertEqual(alias.memory,alias.physicalMemory); XCTAssertEqual(alias.memory[0],0xa4); } @end