Files
VCCPlusOSS/tests/core/BinaryChecks.cpp
T

650 lines
46 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
#include "CoreChecks.h"
#include "../../src/core/BinaryImage.h"
#include "../../src/core/MachineSession.h"
#include "../../src/core/MachineHost.h"
#include "../../src/devices/FD502/Fd502Device.h"
#include "../../src/core/tcc1014mmu.h"
#include "../../src/core/Event.h"
#include <algorithm>
using namespace VCC::Mac;
namespace {
void require(bool ok, const char* message) { if (!ok) throw std::runtime_error(message); }
template<class F> void rejects(F operation) {
bool rejected = false;
try { operation(); } catch (const std::exception&) { rejected = true; }
require(rejected, "Invalid binary must be rejected");
}
using Bytes = std::vector<std::uint8_t>;
void block(Bytes& file, std::uint16_t address, const Bytes& bytes) {
file.insert(file.end(), {0, std::uint8_t(bytes.size() >> 8), std::uint8_t(bytes.size()),
std::uint8_t(address >> 8), std::uint8_t(address)});
file.insert(file.end(), bytes.begin(), bytes.end());
}
void finish(Bytes& file, std::uint16_t entry = 0x3000) {
file.insert(file.end(), {255,0,0,std::uint8_t(entry >> 8),std::uint8_t(entry)});
}
Bytes program() {
Bytes file; block(file, 0x3000, {0x86,0x6b,0xb7,0x50,0,0x20,0xfe}); // LDA #$6B; STA $5000; BRA *
finish(file); return file;
}
void checkPausedGraphicsBuffers(Processor processor) {
MachineSession machine; machine.loadDiagnostic(processor); machine.pause();
machine.setVideoOutput(VideoOutput::RGB);
auto* ram = Get_mem_pointer();
constexpr unsigned address = 0x10000, length = 160 * 192;
std::fill_n(ram + address, length, 0x22);
const auto before = machine.snapshot();
const Bytes original(ram, ram + GetMMUState().RAMBytes);
const DebugGraphicsBuffer back{"back", address, DebugGraphicsMode::HSCREEN2};
const auto capture = machine.debuggerGraphicsCapture(back);
const auto after = machine.snapshot();
require(capture.declared && capture.declaredIdentifier == "back" && capture.displayed.address == 0 &&
capture.declared->address == address && capture.displayed.width == 320 && capture.displayed.height == 192 &&
capture.displayed.bytesPerRow == 160 && capture.declared->bytes == Bytes(length, 0x22),
"Paused graphics capture identifies the actual display and explicitly declared physical buffer on both CPUs");
require(capture.displayed.palette == capture.declared->palette && capture.displayed.pixels.front() != capture.declared->pixels.front() &&
capture.declared->pixels.front() == 0x00ff00,
"Declared preview renders its actual bytes using the same captured palette rather than duplicating the display image");
require(capture.displayed.generation == capture.declared->generation && capture.displayed.cycles == capture.declared->cycles &&
before.generation == after.generation && before.cycles == after.cycles && before.frames == after.frames &&
before.cpu.PC == after.cpu.PC && before.frame == after.frame &&
std::equal(original.begin(), original.end(), ram),
"Paired captures do not execute the guest, publish a new raster frame or change RAM");
const auto withoutBack = machine.debuggerGraphicsCapture();
require(!withoutBack.declared && withoutBack.displayed.pixels == capture.displayed.pixels,
"A displayed-only stopped preview needs no inferred back buffer");
std::fill_n(ram + address, length, 0x33);
const auto changed = machine.debuggerGraphicsCapture(back);
require(capture.declared->bytes.front() == 0x22 && changed.declared->bytes.front() == 0x33 &&
capture.declared->pixels.front() != changed.declared->pixels.front(),
"Graphics snapshots own immutable bytes and reflect later authored buffer updates only in a new snapshot");
// Guest video writes can select another page before a raster is published.
// The fresh preview identifies this register selection, never a guessed
// provenance for the previous published Frame.
VCC::Machine().VideoWrite(VCC::VideoRegister::Offset, address / 8);
const auto selected = machine.debuggerGraphicsCapture();
require(selected.displayed.address == address && selected.displayed.bytes.front() == 0x33 &&
machine.snapshot().frame == before.frame && machine.snapshot().cycles == before.cycles,
"Current video-register selection is distinct from published-frame history without advancing the guest");
rejects([&] { machine.debuggerGraphicsCapture(DebugGraphicsBuffer{"outside", GetMMUState().RAMBytes - 1, DebugGraphicsMode::HSCREEN2}); });
rejects([&] { machine.debuggerGraphicsCapture(DebugGraphicsBuffer{"invalid", 0, static_cast<DebugGraphicsMode>(255)}); });
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cycles == before.cycles,
"Rejected buffer ranges and formats preserve paused execution state");
machine.resume();
rejects([&] { machine.debuggerGraphicsCapture(back); });
require(machine.snapshot().state == RunState::Running && machine.snapshot().cycles == before.cycles,
"A paired capture requires an explicit pause and cannot pause or advance a running machine");
}
void checkCurrentGimeCanvases(Processor processor) {
MachineSession machine; machine.loadDiagnostic(processor); machine.pause();
machine.setVideoOutput(VideoOutput::RGB);
auto* ram = Get_mem_pointer();
const auto ramSize = GetMMUState().RAMBytes;
const auto writeVideo = [](VCC::VideoRegister reg, unsigned value) { VCC::Machine().VideoWrite(reg, value); };
writeVideo(VCC::VideoRegister::Compatibility, 0);
writeVideo(VCC::VideoRegister::Mode, 0x80);
writeVideo(VCC::VideoRegister::HorizontalOffset, 0xfe); // 256-byte pitch, pan 252, horizontal row wrap.
for (const auto height : {225u, 192u}) {
const unsigned front = height == 225 ? 0 : 0x40000, back = height == 225 ? 0x20000 : 0x50000;
std::fill_n(ram + front, 65536, 0x11); std::fill_n(ram + back, 65536, 0x22);
// Distinguish the last four bytes of a row from its wrapped beginning.
ram[front + 4096 + 252] = 0x33; ram[front + 4096] = 0x44;
ram[back + 4096 + 252] = 0x22; ram[back + 4096] = 0x11;
writeVideo(VCC::VideoRegister::Resolution, height == 225 ? 0x7e : 0x1e);
writeVideo(VCC::VideoRegister::Offset, (front + 4096) / 8);
const std::vector<DebugGraphicsBuffer> canvases{
{"front", front, DebugGraphicsMode::CurrentGime, false, 65536},
{"back", back, DebugGraphicsMode::CurrentGime, false, 65536}};
const auto before = machine.snapshot();
const Bytes memory(ram, ram + ramSize);
const auto capture = machine.debuggerGraphicsCapture(canvases[1], canvases);
const auto& a = capture.displayed; const auto& b = *capture.declared;
require(a.width == 320 && a.height == height && b.width == 320 && b.height == height &&
a.bytesPerRow == 160 && a.rowStrideBytes == 256 && b.rowStrideBytes == 256 &&
a.horizontalOffsetBytes == 252 && b.horizontalOffsetBytes == 252 &&
a.canvasIdentifier == "front" && b.canvasIdentifier == "back" &&
a.canvasAddress == front && b.canvasAddress == back && a.canvasBytes == 65536 && b.canvasBytes == 65536 &&
a.viewportOffset == 4096 && b.viewportOffset == 4096 &&
a.videoOrigin == front + 4096 && b.videoOrigin == back + 4096,
"Custom GIME previews preserve actual 225/192-row geometry, pitch, pan and viewport offset on both CPUs");
require(a.address == front + 4096 + 252 && b.address == back + 4096 + 252 &&
a.byteAddresses[3] == front + 4096 + 255 && a.byteAddresses[4] == front + 4096 &&
b.byteAddresses[4] == back + 4096 && a.byteAddresses[160] == front + 4096 + 256 + 252 &&
a.bytes.front() == 0x33 && a.bytes[4] == 0x44 && b.bytes.front() == 0x22 && b.bytes[4] == 0x11 &&
a.pixels.front() == 0x0000ff && b.pixels.front() == 0x00ff00 &&
a.pixels[8] != a.pixels.front() && a.palette == b.palette,
"Custom GIME previews render live physical samples with real 256-byte horizontal wrap and palette");
const auto after = machine.snapshot();
require(a.generation == b.generation && a.cycles == b.cycles && before.generation == after.generation &&
before.cycles == after.cycles && before.frames == after.frames && before.cpu.PC == after.cpu.PC &&
before.debuggerMappingFingerprint == after.debuggerMappingFingerprint && before.frame == after.frame &&
std::equal(memory.begin(), memory.end(), ram),
"Custom paired previews do not advance either guest CPU, change RAM/MMU or publish a frame");
writeVideo(VCC::VideoRegister::Offset, (back + 4096) / 8);
const auto flipped = machine.debuggerGraphicsCapture(canvases[0], canvases);
require(flipped.displayed.canvasIdentifier == "back" && flipped.displayed.pixels == b.pixels &&
flipped.declared->canvasIdentifier == "front" && flipped.declared->pixels == a.pixels &&
capture.displayed.bytes.front() == 0x33,
"A live canvas flip selects its real declared identity and rebases the other immutable viewport");
rejects([&] { machine.debuggerGraphicsCapture(canvases[0]); });
auto invalid = canvases; invalid[0].canvasBytes = 61440;
rejects([&] { machine.debuggerGraphicsCapture(invalid[0], invalid); });
invalid = canvases; invalid[0].physicalAddress += 4096;
rejects([&] { machine.debuggerGraphicsCapture(invalid[0], invalid); });
invalid = canvases; invalid[0].physicalAddress = ramSize;
rejects([&] { machine.debuggerGraphicsCapture(invalid[0], invalid); });
invalid = canvases; invalid[0].canvasBytes = 0;
rejects([&] { machine.debuggerGraphicsCapture(invalid[0], invalid); });
invalid = canvases; invalid[1].physicalAddress = front;
rejects([&] { machine.debuggerGraphicsCapture(invalid[0], invalid); });
invalid = canvases; invalid[1].canvasBytes = 8192;
rejects([&] { machine.debuggerGraphicsCapture(invalid[1], invalid); });
writeVideo(VCC::VideoRegister::Offset, (front + 0xf000) / 8);
rejects([&] { machine.debuggerGraphicsCapture(canvases[1], canvases); });
const std::vector<DebugGraphicsBuffer> allRam{{"whole", 0, DebugGraphicsMode::CurrentGime, false, ramSize}};
writeVideo(VCC::VideoRegister::Offset, (ramSize - 4096) / 8);
rejects([&] { machine.debuggerGraphicsCapture(allRam[0], allRam); });
require(machine.snapshot().cycles == before.cycles && std::equal(memory.begin(), memory.end(), ram),
"Out-of-canvas raster addresses are refused without inventing vertical wrapping or mutating the guest");
writeVideo(VCC::VideoRegister::Offset, front / 8);
writeVideo(VCC::VideoRegister::Mode, 0); // A future graphics canvas can currently contain BASIC text.
const auto text = machine.debuggerGraphicsCapture(std::nullopt, canvases);
require(text.displayed.text && text.displayed.canvasIdentifier.empty(),
"Text registers inside a declared canvas are not falsely identified as a live graphics canvas");
rejects([&] { machine.debuggerGraphicsCapture(canvases[1], canvases); });
writeVideo(VCC::VideoRegister::Mode, 0x80);
}
}
void checkBankedDevelopment(Processor processor) {
MachineSession machine; machine.loadDiagnostic(processor);
MmuSetMapType(1); Set_MmuEnabled(1); SetMmuRegister(0, 0); SetMmuRegister(2, 2);
auto* ram = Get_mem_pointer();
const Bytes a{0x12,0x20,0xfe}, b{0x12,0x12,0x20,0xfe};
std::copy(a.begin(), a.end(), ram + 2 * 8192);
std::copy(b.begin(), b.end(), ram + 3 * 8192);
std::copy(a.begin(), a.end(), ram + 4 * 8192); // Identical code in an unqualified bank.
const std::vector<DebugBankedModule> modules{
{"a", {{2,2}}, {{0x4000,a}}},
{"b", {{2,3}}, {{0x4000,b}}}
};
machine.debuggerSetPC(0x4000);
machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, modules);
const auto armed = machine.snapshot();
require(armed.debuggerBankedDevelopment && armed.debuggerDevelopmentAttached,
"Banked attachment verifies the current module without requiring overlapping overlays simultaneously");
machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x4000,
"Bank-qualified source stop fires before the mapped instruction on both CPUs");
SetMmuRegister(2, 3);
require(machine.snapshot().debuggerDevelopmentAttached && machine.snapshot().debuggerBankedDevelopment &&
machine.snapshot().debuggerMappingFingerprint != armed.debuggerMappingFingerprint,
"Expected overlay switch preserves banked attachment despite a changed flat fingerprint");
ram[3 * 8192 + 1] = 0x39;
require(!machine.snapshot().debuggerDevelopmentAttached,
"Banked source identity checks complete module code beyond a coincidentally matching current opcode");
ram[3 * 8192 + 1] = 0x12;
SetMmuRegister(2, 4);
require(!machine.snapshot().debuggerDevelopmentAttached && machine.snapshot().debuggerBankedDevelopment,
"Identical bytes in a different physical RAM page cannot claim source identity");
machine.debuggerSetPC(0x4000); machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Running,
"Conditional source point ignores a matching opcode in the wrong physical bank");
machine.pause(); machine.debuggerClearDevelopmentBreakpoints();
auto qualified = modules;
qualified[0].breakpointAddresses = std::vector<std::uint16_t>{0x4000};
qualified[1].breakpointAddresses = std::vector<std::uint16_t>{};
SetMmuRegister(2, 3); machine.debuggerSetPC(0x4000);
machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, qualified);
require(machine.snapshot().debuggerDevelopmentAttached,
"A module without selected source stops remains available for source inspection and stepping");
machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Running,
"A source breakpoint in overlay a does not stop overlay b at the same logical address and opcode");
machine.pause(); SetMmuRegister(2, 2); machine.debuggerSetPC(0x4000);
machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x4000,
"The selected module's source point still stops in its physical bank");
machine.debuggerSetBreakpoints({0x4567});
machine.debuggerArmBankedDevelopmentBreakpoints({}, modules);
require(machine.snapshot().debuggerBankedDevelopment && machine.snapshot().debuggerDevelopmentAttached &&
machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>{0x4567},
"Inspect-only bank attachment preserves module evidence and independent manual breakpoints");
machine.resume(); machine.advance(); machine.pause();
require(machine.snapshot().debuggerDevelopmentAttached,
"Pause after continuing without source stops can still inspect verified banked code");
machine.debuggerClearDevelopmentBreakpoints(); machine.debuggerSetBreakpoints({});
qualified[0].breakpointAddresses = std::vector<std::uint16_t>{0x4001};
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, qualified); });
qualified[0].breakpointAddresses = std::vector<std::uint16_t>{};
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, qualified); });
require(!machine.snapshot().debuggerBankedDevelopment,
"Unqualified points cannot replace a valid source-breakpoint configuration");
SetMmuRegister(2, 4);
machine.debuggerSetBreakpoints({0x4000});
machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, modules);
machine.debuggerSetPC(0x4000); machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Paused && !machine.snapshot().debuggerDevelopmentAttached,
"An independently owned manual point still stops in an unqualified bank");
machine.debuggerClearDevelopmentBreakpoints();
require(machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>{0x4000},
"Clearing banked source stops preserves independently owned manual points");
machine.debuggerSetBreakpoints({});
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, {{"bad",{{1,2}},modules[0].guards}}); });
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, {{"bad",{{2,64}},modules[0].guards}}); });
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, {modules[0],modules[0]}); });
auto ambiguous = modules[0]; ambiguous.identifier = "ambiguous";
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000}, {modules[0],ambiguous}); });
require(!machine.snapshot().debuggerBankedDevelopment && machine.debuggerSnapshot().breakpoints.empty(),
"Invalid banked configurations leave breakpoint ownership unchanged");
// A resident thunk selects overlay b while stepping. Logical instruction
// addresses overlap with overlay a, including their first identical opcode.
const Bytes thunk{0x86,0x03,0xb7,0xff,0xa2,0x7e,0x40,0x00};
std::copy(thunk.begin(), thunk.end(), ram + 0x1000);
auto steppingModules = modules;
steppingModules.push_back({"resident",{{0,0}},{{0x1000,thunk}}});
SetMmuRegister(2, 2); machine.debuggerSetPC(0x1000);
machine.debuggerArmBankedDevelopmentBreakpoints({0x1000}, steppingModules);
const std::vector<DebugSourceInstruction> instructions{
{0x1000,1,{0x86,0x03},"resident"},{0x1002,1,{0xb7,0xff,0xa2},"resident"},{0x1005,1,{0x7e,0x40,0x00},"resident"},
{0x4000,10,{0x12},"a"},{0x4001,11,{0x20,0xfe},"a"},
{0x4000,20,{0x12},"b"},{0x4001,21,{0x12},"b"},{0x4002,22,{0x20,0xfe},"b"}
};
const auto start = machine.snapshot();
rejects([&] { machine.debuggerStepSource(SourceStepMode::Into, instructions, start.generation,
start.debuggerProgramRevision, start.debuggerMappingFingerprint + 1, start.cpu.PC); });
machine.debuggerStepSource(SourceStepMode::Into, instructions, start.generation,
start.debuggerProgramRevision, start.debuggerMappingFingerprint, start.cpu.PC);
machine.advance();
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x4000 &&
GetMMUState().Effective[2].SourceStart == 3 * 8192 && machine.snapshot().debuggerDevelopmentAttached &&
machine.debuggerSnapshot().stopReason == "C source location reached",
"Banked C step follows a verified resident bank switch and selects the matching overlay source");
machine.debuggerClearDevelopmentBreakpoints();
require(!machine.snapshot().debuggerBankedDevelopment, "Explicit detach clears bank-qualified mode");
}
void checkPhysicalOverlayDevelopment(Processor processor) {
const Bytes a{0x12,0x86,0x11,0xb7,0x50,0x00,0x7e,0x11,0x00};
const Bytes b{0x12,0x86,0x22,0xb7,0x50,0x00,0x7e,0x12,0x00};
auto loader = [](unsigned source, unsigned count) -> Bytes {
// LDX #$4000; LDY #source; LDB #count;
// loop: LDA ,Y+; STA ,X+; DECB; BNE loop; JMP $4000.
return {0x8e,0x40,0x00,0x10,0x8e,std::uint8_t(source>>8),std::uint8_t(source),0xc6,std::uint8_t(count),
0xa6,0xa0,0xa7,0x80,0x5a,0x26,0xf9,0x7e,0x40,0x00};
};
const auto loadA = loader(0x2000, unsigned(a.size()));
const auto loadB = loader(0x2100, unsigned(b.size()));
auto modules = [&] {
return std::vector<DebugBankedModule>{
{"a",{{2,2}},{{0x4000,a}},std::nullopt,"game","A"},
{"b",{{2,2}},{{0x4000,b}},std::nullopt,"game","B"},
{"loader",{{0,0}},{{0x1000,loadA},{0x1100,loadB},{0x1200,loadA}}}
};
};
auto load = [&](MachineSession& machine, const Bytes& next, unsigned count) {
machine.loadDiagnostic(processor);
MmuSetMapType(1); Set_MmuEnabled(1);
for (unsigned page=0;page<8;++page) SetMmuRegister(page,page);
Bytes image;
block(image,0x1000,loadA); block(image,0x1100,loader(0x2100,count)); block(image,0x1200,loadA);
block(image,0x2000,a); block(image,0x2100,next); block(image,0x4800,{0xaa});
finish(image,0x1000); machine.debuggerLoadBinary(image,0x1000);
};
auto run = [](MachineSession& machine) {
machine.resume();
for (unsigned n=0;n<10 && machine.snapshot().state==RunState::Running;++n) machine.advance();
require(machine.snapshot().state==RunState::Paused && machine.snapshot().cpu.PC==0x4000,
"Guest overlay loader reaches its qualified or independent manual entry stop");
};
{
MachineSession machine; load(machine,b,unsigned(b.size()));
const auto evidence = modules();
machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},evidence);
run(machine); const auto first=machine.snapshot();
require(first.debuggerDevelopmentAttached && first.debuggerResidencyFingerprint!=0 && MemRead8(0x4002)==0x11,
"Actual guest stores install owner A in declared shared physical RAM on both CPUs");
run(machine); const auto second=machine.snapshot();
require(second.debuggerDevelopmentAttached && second.debuggerResidencyFingerprint!=first.debuggerResidencyFingerprint && MemRead8(0x4002)==0x22 &&
second.debuggerMappingFingerprint==first.debuggerMappingFingerprint && second.debuggerProgramRevision==first.debuggerProgramRevision,
"Replacing shared physical code changes residency without a mapping or host program epoch change");
run(machine); const auto third=machine.snapshot();
require(third.debuggerDevelopmentAttached && third.debuggerResidencyFingerprint==first.debuggerResidencyFingerprint && MemRead8(0x4002)==0x11,
"Actual guest A-to-B-to-A copies reestablish current byte ownership rather than a cached last owner");
machine.debuggerArmBankedDevelopmentBreakpoints({},evidence);
const std::vector<DebugSourceInstruction> steps{{0x4000,10,{0x12},"a"},{0x4000,20,{0x12},"b"}};
rejects([&] { machine.debuggerStepSource(SourceStepMode::Into,steps,third.generation,third.debuggerProgramRevision,
third.debuggerMappingFingerprint,third.cpu.PC,1000000,600,second.debuggerResidencyFingerprint); });
machine.debuggerStepSource(SourceStepMode::Into,steps,third.generation,third.debuggerProgramRevision,
third.debuggerMappingFingerprint,third.cpu.PC,1000000,600,third.debuggerResidencyFingerprint);
machine.advance();
require(machine.snapshot().state==RunState::Paused && machine.snapshot().cpu.PC==0x4000 && MemRead8(0x4002)==0x22 &&
machine.snapshot().debuggerDevelopmentAttached && machine.debuggerSnapshot().stopReason=="C source location reached",
"C stepping crosses an executing physical-overlay copy and identifies the newly resident owner despite identical entry opcodes");
auto qualified=evidence; qualified[0].breakpointAddresses=std::vector<std::uint16_t>{0x4000}; qualified[1].breakpointAddresses=std::vector<std::uint16_t>{};
machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},qualified);
run(machine);
require(MemRead8(0x4002)==0x11 && machine.snapshot().debuggerDevelopmentAttached,
"An owner A breakpoint ignores B at the same physical and logical entry");
auto invalid=evidence; invalid[1].overlayOwner="A";
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},invalid); });
invalid=evidence; invalid[1].overlayGroup="other";
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},invalid); });
invalid=evidence; invalid[1].overlayGroup.clear(); invalid[1].overlayOwner.clear();
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},invalid); });
for (unsigned boundary=0;boundary<4;++boundary) {
invalid=evidence;
if (boundary==0) { invalid[0].overlayGroup+='\0'; invalid[1].overlayGroup+='\0'; }
if (boundary==1) invalid[0].overlayOwner+='\0';
if (boundary==2) { invalid[0].overlayGroup=std::string(129,'g'); invalid[1].overlayGroup=invalid[0].overlayGroup; }
if (boundary==3) invalid[0].overlayOwner=std::string(129,'o');
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},invalid); });
}
std::vector<DebugBankedModule> manyGroups;
for (unsigned group=0;group<65;++group) for (unsigned owner=0;owner<2;++owner) {
const auto id=std::to_string(group);
manyGroups.push_back({id+"-"+std::to_string(owner),{{1,1}},{{std::uint16_t(0x2000+group),{std::uint8_t(0x12+owner)}}},
std::nullopt,"group-"+id,owner ? "B" : "A"});
}
rejects([&] { machine.debuggerArmBankedDevelopmentBreakpoints({},manyGroups); });
require(machine.snapshot().debuggerDevelopmentAttached && machine.snapshot().debuggerResidencyFingerprint==first.debuggerResidencyFingerprint,
"Rejected alias declarations preserve the existing verified overlay attachment");
manyGroups.pop_back(); manyGroups.pop_back();
machine.debuggerArmBankedDevelopmentBreakpoints({},manyGroups);
require(machine.snapshot().debuggerResidencyFingerprint!=0,
"Exactly 64 declared physical-code overlay groups remain within the supported contract");
}
// All modifications below are executed by the guest loader, never debugger
// writes, register edits or host copies into a running program's RAM.
for (unsigned scenario=0;scenario<4;++scenario) {
MachineSession machine;
auto payload=b; if (scenario==1) payload[4]=0x51;
const unsigned count=scenario==0 ? 3 : unsigned(payload.size());
load(machine,payload,count);
auto evidence=modules(); evidence[2].guards[1].bytes=loader(0x2100,count);
if (scenario==2) evidence[1].guards=evidence[0].guards; // Two owners fit the same complete code.
if (scenario==3) {
evidence.push_back({"a-helper",{{2,2}},{{0x4800,{0xaa}}},std::vector<std::uint16_t>{},"game","A"});
evidence.push_back({"b-helper",{{2,2}},{{0x4800,{0xbb}}},std::vector<std::uint16_t>{},"game","B"});
}
machine.debuggerSetBreakpoints({0x4000}); // Independent manual point must survive source refusal.
machine.debuggerArmBankedDevelopmentBreakpoints({0x4000},evidence);
run(machine);
if (scenario==2) {
require(!machine.snapshot().debuggerDevelopmentAttached,
"Multiple complete matching owners make physical overlay identity ambiguous, never first-match selected");
} else {
const auto before=machine.snapshot(); require(before.debuggerDevelopmentAttached,"Owner A is initially verified");
run(machine); const auto after=machine.snapshot();
require(!after.debuggerDevelopmentAttached && after.debuggerResidencyFingerprint!=before.debuggerResidencyFingerprint &&
after.debuggerMappingFingerprint==before.debuggerMappingFingerprint,
"Partial, corrupt or incompletely installed multi-module owners invalidate source attachment after actual guest stores");
const std::vector<DebugSourceInstruction> steps{{0x4000,10,{0x12},"a"},{0x4000,20,{0x12},"b"}};
rejects([&] { machine.debuggerStepSource(SourceStepMode::Into,steps,after.generation,after.debuggerProgramRevision,
after.debuggerMappingFingerprint,after.cpu.PC,1000000,600,after.debuggerResidencyFingerprint); });
}
machine.debuggerClearDevelopmentBreakpoints();
require(machine.debuggerSnapshot().breakpoints==std::vector<std::uint16_t>{0x4000},
"Detaching physical-overlay evidence preserves the independent manual breakpoint");
}
}
void checkAssemblyInstructionBoundary(Processor processor) {
MachineSession machine;
machine.loadDiagnostic(processor);
machine.debuggerConfigureBreakpoints({0x4567}, {0x3f02});
Bytes binary;
block(binary, 0x3f00, {0x86,0x8f,0xb7,0x04,0x00,0x20,0xfe});
finish(binary, 0x3f00);
const std::vector<DebugCodeGuard> guards{
{0x3f00,{0x86,0x8f}}, {0x3f02,{0xb7,0x04,0x00}}, {0x3f05,{0x20,0xfe}}
};
const auto before = machine.snapshot();
auto launch = [&](const std::vector<std::uint16_t>& points, const std::vector<DebugCodeGuard>& code,
bool pause, bool hitachi, const Snapshot& expected) {
machine.launchAssembly(binary, points, code, pause, hitachi, expected.generation, expected.debuggerProgramRevision, expected.debuggerMappingFingerprint);
};
MemWrite8(0x41, 0x0400);
if (processor == Processor::MC6809) rejects([&] { launch({}, guards, true, true, before); });
rejects([&] { launch({0x3f01}, guards, true, false, before); });
rejects([&] { launch({}, {{0x3f02,{0xb7,0x04,0x01}}}, true, false, before); });
require(machine.snapshot().cpu.PC == before.cpu.PC && machine.snapshot().debuggerProgramRevision == before.debuggerProgramRevision,
"A rejected assembly launch preserves execution and ownership");
launch({0x3f02}, guards, true, false, before);
auto entry = machine.snapshot();
require(entry.state == RunState::Paused && entry.cpu.PC == 0x3f00 && entry.cycles == before.cycles,
"Debug from Entry stops before running any instruction");
machine.controlAssembly(false, entry);
machine.advance();
auto store = machine.snapshot();
require(store.state == RunState::Paused && store.cpu.PC == 0x3f02 && MemRead8(0x0400) == 0x41,
"Assembly source breakpoint stops before the screen store");
machine.controlAssembly(true, store);
require(machine.snapshot().cpu.PC == 0x3f05 && MemRead8(0x0400) == 0x8f,
"Step executes the store once");
rejects([&] { machine.controlAssembly(true, store); });
machine.updateAssembly({}, machine.snapshot());
require(machine.debuggerSnapshot().disabledBreakpoints == std::vector<std::uint16_t>{0x3f02},
"Removing an assembly point restores a disabled manual point");
launch({}, guards, false, false, machine.snapshot());
const auto repeated = machine.snapshot();
machine.clearAssembly(entry);
machine.advance();
require(machine.snapshot().state == RunState::Running && machine.snapshot().debuggerDevelopmentAttached &&
machine.snapshot().cpu.PC == 0x3f05, "Repeat run has no implicit breakpoint or stale ownership");
const auto live = machine.snapshot();
rejects([&] { machine.pauseAssembly(entry); });
auto after = machine.snapshot();
require(after.state == RunState::Running && after.cpu.PC == live.cpu.PC && after.cycles == live.cycles &&
after.debuggerAssemblyAttached, "A queued Pause from the previous program leaves the new owner running");
machine.pauseAssembly(repeated);
after = machine.snapshot();
require(after.state == RunState::Paused && after.cpu.PC == live.cpu.PC && after.cycles == live.cycles,
"Pause honors the current owner after its guest has advanced");
MemWrite8(0x12, 0x3f00);
rejects([&] { machine.controlAssembly(true, machine.snapshot()); });
require(!machine.snapshot().debuggerDevelopmentAttached, "Changed instruction bytes invalidate source stepping");
machine.clearAssembly(machine.snapshot());
require(machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>{0x4567}, "Manual breakpoint survives source detach");
}
}
void CheckBinaryParsing() {
Bytes file; block(file,0x1234,{1,2,3}); block(file,0xabcd,{}); block(file,0x1235,{4,5}); finish(file,0x7fff);
const auto image = BinaryImage::parse(file);
require(image.entry==0x7fff && image.blocks.size()==3, "Multi-record parser preserves entry and record order");
require(image.blocks[0].address==0x1234 && image.blocks[0].bytes==Bytes({1,2,3}) &&
image.blocks[1].address==0xabcd && image.blocks[1].bytes.empty() && image.blocks[2].bytes==Bytes({4,5}),
"Headers are big-endian and zero-length/overlapping records are retained");
Bytes jump; finish(jump,1); require(BinaryImage::parse(jump).blocks.empty(), "A transfer-only file retains the Windows jump behavior");
Bytes large; block(large,0,Bytes(65535,0x5a)); finish(large);
require(BinaryImage::parse(large).blocks[0].bytes.size()==65535, "Maximum 16-bit block length is accepted");
}
void CheckBinaryValidation() {
const auto good = program();
for (std::size_t size=0; size<good.size(); ++size)
rejects([&] { BinaryImage::parse({good.begin(),good.begin()+size}); });
for (unsigned type : {1u,2u,254u}) {
auto bad=good; bad[0]=type; rejects([&] { BinaryImage::parse(bad); });
}
for (unsigned address : {0u,0x8000u,0xffffu}) {
Bytes bad; finish(bad,address); rejects([&] { BinaryImage::parse(bad); });
}
auto bad=good; bad[bad.size()-3]=1; rejects([&] { BinaryImage::parse(bad); });
bad=good; bad.push_back(0); rejects([&] { BinaryImage::parse(bad); });
rejects([&] { BinaryImage::parse(Bytes(BinaryImage::maxBytes+1)); });
// A valid first block followed by bad input must not reach memory or I/O.
for (auto cpu : {Processor::MC6809,Processor::HD6309}) {
MachineSession machine; machine.loadDiagnostic(cpu); machine.resume(); machine.advance();
Bytes bytes; block(bytes,0x5000,{0x77}); block(bytes,0xffdf,{0}); bytes.push_back(9);
const auto before=machine.snapshot(); const auto memory=Get_mem_pointer();
const Bytes saved(memory,memory+0x80000); const auto events=VCC::gEvents.GetSize();
rejects([&] { machine.quickLoad(bytes); });
require(std::equal(saved.begin(),saved.end(),memory) && !MemCheckWrite(0x8000), "Late invalid header preserves all RAM and mapped I/O state");
const auto after=machine.snapshot();
require(after.state==before.state && after.cpu.PC==before.cpu.PC && after.cycles==before.cycles &&
after.frame==before.frame && events==VCC::gEvents.GetSize(), "Rejected binary preserves execution and scheduled events");
}
}
void CheckBinarySession() {
MachineSession empty; rejects([&] { empty.quickLoad(program()); });
for (auto cpu : {Processor::MC6809,Processor::HD6309}) {
for (bool running : {false,true}) {
MachineSession machine; machine.loadDiagnostic(cpu); machine.resume(); machine.advance();
auto disk=std::make_shared<VCC::Devices::Fd502Device>(Bytes(8192));
disk->mount(0,VCC::Devices::DiskImage::blank());
machine.cartridges().configure({nullptr,nullptr,nullptr,nullptr,disk},true,4);
machine.cassette().mount({0x55,0x3c},TapeFormat::CAS);
MemWrite8(0xa5,0x6000); if (!running) machine.pause();
MemWrite8(0xff,0xff02); MemWrite8(4,0xff03); MemWrite8(4,0xff01); MemWrite8(0xfd,0xff02);
machine.setKeyboard(std::uint64_t{1}<<1);
const auto before=machine.snapshot(); const auto events=VCC::gEvents.GetSize();
machine.quickLoad(program()); const auto loaded=machine.snapshot();
require(loaded.cpu.PC==0x3000 && loaded.state==RunState::Running, "Quick-load starts selected CPU at the execution record");
require(MemRead8(0x6000)==0xa5 && loaded.frames==before.frames && loaded.cycles==before.cycles &&
loaded.generation==before.generation && events==VCC::gEvents.GetSize(), "Quick-load retains unrelated memory, timing and machine identity");
require(disk->image(0) && disk->image(0)->dirty() && machine.cassette().snapshot().mounted &&
machine.cartridges().physicalSlot()==4, "Quick-load retains dirty floppy, cassette and cartridge assignments");
require((MemRead8(0xff00)&0x7f)==0x7f, "Quick-load releases held keys");
machine.advance(); require(MemRead8(0x5000)==0x6b && machine.snapshot().cpu.PC==0x3005, "Loaded program executes on both CPU engines");
}
}
}
void CheckBinaryMappedWrites() {
checkPausedGraphicsBuffers(Processor::MC6809);
checkPausedGraphicsBuffers(Processor::HD6309);
checkCurrentGimeCanvases(Processor::MC6809);
checkCurrentGimeCanvases(Processor::HD6309);
checkBankedDevelopment(Processor::MC6809);
checkBankedDevelopment(Processor::HD6309);
checkPhysicalOverlayDevelopment(Processor::MC6809);
checkPhysicalOverlayDevelopment(Processor::HD6309);
checkAssemblyInstructionBoundary(Processor::MC6809);
checkAssemblyInstructionBoundary(Processor::HD6309);
for (auto cpu : {Processor::MC6809, Processor::HD6309}) {
MachineSession machine; machine.loadDiagnostic(cpu);
machine.debuggerConfigureBreakpoints({0x4500}, {0x3005});
machine.debuggerLoadBinary(program(), 0x3005);
machine.advance();
rejects([&] { machine.debuggerContinueFromEntry(0x3000); });
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x3005,
"Continuing a different source stop preserves the actual stop");
machine.debuggerContinueFromEntry(0x3005);
machine.advance();
require(machine.snapshot().state == RunState::Running && machine.snapshot().debuggerDevelopmentAttached &&
machine.snapshot().cpu.PC == 0x3005, "Play continues through its one-time entry and retains the C program identity");
require(machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>{0x4500} &&
machine.debuggerSnapshot().disabledBreakpoints == std::vector<std::uint16_t>{0x3005},
"The one-time C entry preserves independently configured manual breakpoints");
machine.debuggerConfigureBreakpoints({0x4500}, {0x3005});
machine.debuggerLoadBinary(program(), std::vector<std::uint16_t>{0x3000,0x3005});
machine.advance(); require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x3000,
"Multiple development points stop before the first instruction on both CPUs");
require(machine.snapshot().debuggerDevelopmentAttached, "Atomic quickload attaches its verified source epoch");
machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x3005,
"Continuing passes only the current point and can stop in another source file");
machine.debuggerClearDevelopmentBreakpoints();
auto state = machine.debuggerSnapshot();
require(state.breakpoints == std::vector<std::uint16_t>{0x4500} && state.disabledBreakpoints == std::vector<std::uint16_t>{0x3005},
"Clearing development points preserves manual points and restores temporarily enabled ones");
require(!machine.snapshot().debuggerDevelopmentAttached, "Explicit detach clears source attachment");
// Guards cover code beyond the selected instruction, so a coincidentally
// matching opcode in an earlier loader cannot be mistaken for the program.
const std::vector<DebugCodeGuard> guards{{0x3000,{0x86,0x6b}}, {0x3002,{0xb7,0x50,0}}, {0x3005,{0x20,0xfe}}, {0x6000,{0x39}}};
machine.debuggerSetPC(0x3000);
machine.debuggerArmDevelopmentBreakpoints({}, guards);
require(!machine.snapshot().debuggerDevelopmentAttached,
"Empty flat inspection guards do not invent an attachment before a verified stop");
machine.debuggerArmDevelopmentBreakpoints({0x3000}, guards);
const auto before = machine.snapshot(); machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Running && !machine.snapshot().debuggerDevelopmentAttached,
"Disk-source points ignore boot code until every expected code range is loaded");
machine.pause(); MemWrite8(0x39,0x6000); machine.debuggerSetPC(0x3000);
machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x3000 &&
machine.snapshot().debuggerDevelopmentAttached && machine.snapshot().generation == before.generation &&
machine.snapshot().debuggerProgramRevision == before.debuggerProgramRevision,
"Disk-loaded exact code attaches and stops without quickload or resetting the machine");
const std::vector<DebugCodeGuard> mergedGuards{{0x3000,{0x86,0x6b,0xb7,0x50,0,0x20,0xfe}}, {0x6000,{0x39}}};
machine.debuggerArmDevelopmentBreakpoints({}, mergedGuards);
require(machine.snapshot().debuggerDevelopmentAttached,
"Empty inspection preserves verified mapping when only guard segmentation changes");
machine.resume(); machine.advance(); machine.pause();
require(machine.snapshot().debuggerDevelopmentAttached && machine.snapshot().cpu.PC == 0x3005,
"Flat continue removes selected stops while retaining verified code identity");
MemWrite8(0x12,0x6000);
require(!machine.snapshot().debuggerDevelopmentAttached,
"Paused capture rejects guest-modified executable bytes without debugger writes");
MemWrite8(0x39,0x6000); machine.debuggerSetPC(0x3000);
machine.debuggerArmDevelopmentBreakpoints({0x3000}, guards); machine.resume(); machine.advance();
require(machine.snapshot().debuggerDevelopmentAttached,
"A new verified stop can re-establish exact flat code and runtime mapping");
machine.debuggerWriteMemory(0x6000,{0x12});
require(!machine.snapshot().debuggerDevelopmentAttached && machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>{0x4500},
"Debugger writes cancel conditional source points and preserve manual points");
machine.debuggerConfigureBreakpoints({0x3000,0x4500}, {0x3005});
machine.debuggerSetPC(0x3000); machine.debuggerArmDevelopmentBreakpoints({0x3000}, guards);
machine.resume(); machine.advance();
require(machine.snapshot().state == RunState::Paused && !machine.snapshot().debuggerDevelopmentAttached,
"An independently enabled manual breakpoint still stops before disk code is loaded");
machine.debuggerClearDevelopmentBreakpoints();
require(machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>({0x3000,0x4500}),
"A shared manual breakpoint is never claimed by the development feature");
rejects([&] { machine.debuggerArmDevelopmentBreakpoints({0x3001}, guards); });
rejects([&] { machine.debuggerArmDevelopmentBreakpoints({0x3000}, {{0x3000,{0x12}}, {0x3000,{0x12}}}); });
rejects([&] { machine.debuggerArmDevelopmentBreakpoints({0xff00}, {{0xff00,{0x12}}}); });
machine.debuggerArmDevelopmentBreakpoints({0x3002}, guards); machine.softReset();
require(!machine.snapshot().debuggerDevelopmentAttached &&
machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>({0x3000,0x4500}),
"Reset drops pending development points without erasing user breakpoints");
}
// The development loader is stricter than legacy quickload: two logical
// spans must not overwrite the same physical RAM through MMU aliases.
for (auto cpu : {Processor::MC6809,Processor::HD6309}) {
MachineSession machine; machine.loadDiagnostic(cpu);
const auto original = machine.snapshot();
Set_MmuEnabled(1); SetMmuRegister(0,3); SetMmuRegister(1,3);
const auto remapped = machine.snapshot();
require(original.debuggerMappingFingerprint != remapped.debuggerMappingFingerprint,
"Source provenance detects active MMU remapping without a reset");
SetMmuRegister(9,12); // Task 1 is inactive.
require(machine.snapshot().debuggerMappingFingerprint == remapped.debuggerMappingFingerprint,
"Inactive-task register edits do not change effective source mapping");
machine.debuggerSetBreakpoints({0x4500});
Bytes image; block(image,0x0100,{0x12}); block(image,0x2100,{0x39}); finish(image,0x0100);
auto* ram = Get_mem_pointer(); const auto previousByte = ram[3*8192+0x100];
rejects([&] { machine.debuggerLoadBinary(image,0x0100); });
require(ram[3*8192+0x100] == previousByte && machine.snapshot().debuggerProgramRevision == remapped.debuggerProgramRevision &&
machine.debuggerSnapshot().breakpoints == std::vector<std::uint16_t>{0x4500},
"Rejected aliased development image preserves RAM, breakpoints and provenance epoch");
SetMmuRegister(1,4);
machine.debuggerLoadBinary(image,0x0100);
const auto loaded = machine.snapshot();
require(loaded.debuggerProgramRevision > remapped.debuggerProgramRevision && loaded.generation == remapped.generation,
"Successful development load changes program provenance without restarting machine");
machine.pause(); machine.debuggerStep();
require(machine.snapshot().debuggerProgramRevision == loaded.debuggerProgramRevision,
"Pause and instruction stepping preserve program provenance");
machine.debuggerWriteMemory(0x2100,{0x12});
require(machine.snapshot().debuggerProgramRevision > loaded.debuggerProgramRevision,
"Explicit debugger memory edits invalidate a loaded C source map");
}
for (auto cpu : {Processor::MC6809,Processor::HD6309}) {
MachineSession machine; machine.loadDiagnostic(cpu);
Set_MmuEnabled(1); SetMmuRegister(0,3); SetMmuRegister(1,9);
Bytes file; block(file,0x1ffe,{1,2,3,4}); block(file,0x1fff,{9}); finish(file);
machine.quickLoad(file);
auto* ram=Get_mem_pointer();
require(ram[3*8192+8190]==1 && ram[3*8192+8191]==9 && ram[9*8192]==3 && ram[9*8192+1]==4,
"Ordered overlapping loads follow MMU mapping across a bank boundary");
// Ordered I/O writes can change the mapping before a later data record.
file.clear(); block(file,0xffa0,{5}); block(file,0x123,{0x7a}); block(file,0xffdf,{0});
block(file,0x8000,{0x66}); block(file,0xfffe,{0x11,0x22,0x33,0x44}); finish(file);
machine.quickLoad(file);
require(GetMMUState().Task0[0]==5 && ram[5*8192+0x123]==0x7a && MemRead8(0x8000)==0x66,
"Quick-load preserves live I/O writes and RAM under a disabled ROM overlay");
require(MemRead8(0)==0x33 && MemRead8(1)==0x44, "Records wrap at the 16-bit address boundary");
}
}