// SPDX-License-Identifier: GPL-3.0-or-later #include "../basic-disk-session.h" using namespace BasicDiskTests; namespace { constexpr unsigned bankBase = 0x50000; constexpr unsigned bankSize = 8192; constexpr unsigned capacity = 4 * bankSize; bool sameMapping(const VCC::MMUState& before, const VCC::MMUState& after) { return before.Enabled == after.Enabled && before.ActiveTask == after.ActiveTask && before.RamVectors == after.RamVectors && before.RomMap == after.RomMap && before.AllRAM == after.AllRAM && before.Prefix == after.Prefix && before.Task0 == after.Task0 && before.Task1 == after.Task1; } void until(MachineSession& machine, const std::string& message) { for (unsigned n = 0; n < 1000; ++n) { const auto screen = machine.copyScreenText(); if (screen.find(message) != screen.npos) return; if (screen.find("FAIL LINE") != screen.npos) { for (unsigned i = 0; i < 6; ++i) machine.advance(); throw std::runtime_error(machine.copyScreenText()); } machine.advance(); machine.takeAudio(); } throw std::runtime_error("Expected " + message + " on screen: " + machine.copyScreenText()); } enum class Mapping { basic, taskOne, disabled, splitLayout }; void stopAt(MachineSession& machine, std::uint16_t address) { machine.debuggerSetBreakpoints({address}); machine.resume(); for (unsigned i = 0; i < 300 && machine.snapshot().state == RunState::Running; ++i) machine.advance(); require(machine.debuggerSnapshot().cpu.PC == address, "Did not reach bank API breakpoint"); machine.debuggerSetBreakpoints({}); } // Inject unusual mappings only around bank_open(). BASIC's own screen/IRQ // handling can program the GIME again after it regains control. void checkRejectedMapping(MachineSession& machine, Mapping mapping, std::uint16_t open) { stopAt(machine, open); const auto before = machine.debuggerSnapshot(); const auto stack = machine.debuggerMemoryRange(before.cpu.S, 2, false).bytes; const auto caller = std::uint16_t(stack[0] << 8 | stack[1]); const auto init0 = SafeMemRead8(0xff90); const auto init1 = SafeMemRead8(0xff91); if (mapping == Mapping::taskOne) { // Mirror task 0 to keep the running instructions and stack in place. for (unsigned i = 0; i < 8; ++i) MmuWrite8(std::uint8_t(before.mmu.Task0[i]), std::uint16_t(0xffa8 + i)); MmuWrite8(init1 | 1, 0xff91); } else { MmuWrite8(init0 & ~0x40, 0xff90); } const auto challenged = machine.debuggerSnapshot().mmu; require(challenged.ActiveTask == (mapping == Mapping::taskOne) && challenged.Enabled == (mapping != Mapping::disabled), "Failed to establish test mapping"); stopAt(machine, caller); const auto after = machine.debuggerSnapshot(); require(sameMapping(challenged, after.mmu), "Rejected open changed custom mapping"); require(after.cpu.B == 0, "Unexpected open with custom mapping"); MmuWrite8(init0, 0xff90); MmuWrite8(init1, 0xff91); if (mapping == Mapping::taskOne) for (unsigned i = 0; i < 8; ++i) MmuWrite8(std::uint8_t(before.mmu.Task1[i]), std::uint16_t(0xffa8 + i)); } void qualify(const Bytes& rom, const Bytes& program, Processor cpu, unsigned ram, Mapping mapping, std::uint16_t open) { MachineConfiguration configuration; configuration.processor = cpu; configuration.ram = ram; MachineSession machine; machine.configure(configuration); machine.load(rom, cpu); machine.resume(); until(machine, "OK"); auto physical = [&](unsigned address, unsigned length) { return machine.debuggerMemoryRange(address, length, true).bytes; }; const auto size = program.size(); require(size >= 5 && program[size - 5] == 0xff, "Invalid DECB fixture"); machine.debuggerLoadBinary(program, std::uint16_t(program[size - 2] << 8 | program.back())); machine.debuggerClearDevelopmentBreakpoints(); const auto initial = machine.debuggerSnapshot().mmu; const bool supported = ram != 0 && mapping == Mapping::basic; Bytes expected; Bytes freeRam, belowProgram; if (ram != 0) { // One real physical byte on either side catches off-by-one accesses. expected = physical(bankBase - 1, capacity + 2); } else { // On 128 KiB the asset banks mirror BASIC's physical banks $08..$0B. // Exclude live BASIC state and the loaded program, starting at $2800. freeRam = physical(0, 65536); belowProgram = physical(0x11000, 0x1800); } if (supported) { for (unsigned boundary = bankSize; boundary < capacity; boundary += bankSize) { for (unsigned i = 1; i <= 513; ++i) expected[boundary - 3 + i] = std::uint8_t(boundary / bankSize + i); } expected[capacity] = 0xbd; // index zero is the lower guard byte } if (mapping == Mapping::taskOne || mapping == Mapping::disabled) checkRejectedMapping(machine, mapping, open); machine.resume(); until(machine, supported ? "BANK PASSED\nOK" : "BANK UNAVAILABLE\nOK"); require(sameMapping(initial, machine.debuggerSnapshot().mmu), "Guest changed the BASIC MMU map"); if (ram != 0) { require(physical(bankBase - 1, capacity + 2) == expected, "A reserved byte or a neighboring physical byte differs from the expected image"); } else { require(physical(0, 65536) == freeRam && physical(0x11000, 0x1800) == belowProgram, "RAM changed during a rejected 128 KiB reservation"); } // Enter PRINT 7*9 through the keyboard matrix after returning from main. constexpr auto key = [](unsigned bit) { return std::uint64_t(1) << bit; }; machine.paste({key(16), key(18), key(9), key(14), key(20), key(31), key(39), key(55) | key(42), key(41), key(48)}); until(machine, " 63"); std::cout << "PASS " << (cpu == Processor::MC6809 ? "6809" : "6309") << ", " << initial.RAMBytes / 1024 << " KiB: "; if (mapping == Mapping::taskOne) std::cout << "task 1, "; if (mapping == Mapping::disabled) std::cout << "MMU disabled, "; if (mapping == Mapping::splitLayout) std::cout << "writable data below code, "; std::cout << (supported ? "physical bank contents" : "safe refusal") << ", restored MMU and BASIC input\n"; } } // namespace int main(int argc, char** argv) { try { require(argc == 5, "Usage: bank-check COCO3_ROM QUAL.BIN BANK_OPEN SPLIT.BIN"); const auto rom = readFile(argv[1]); const auto program = readFile(argv[2]); const auto split = readFile(argv[4]); const auto open = std::uint16_t(std::stoul(argv[3], nullptr, 16)); for (auto cpu : {Processor::MC6809, Processor::HD6309}) { for (unsigned ram = 0; ram < 4; ++ram) qualify(rom, program, cpu, ram, Mapping::basic, open); qualify(rom, program, cpu, 1, Mapping::taskOne, open); qualify(rom, program, cpu, 1, Mapping::disabled, open); qualify(rom, split, cpu, 1, Mapping::splitLayout, 0); } } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }