// SPDX-License-Identifier: GPL-3.0-or-later // Optional real-ROM and disk check. Reads all host inputs without writing. // Supply a 256-byte sector whose absolute file offset was independently determined. // For example, use 17 1 109019 --dir SHAMUS with the original protected Shamus // DMK, or 17 3 157440 with S37H.VDK. The optional --fm-doubled flag describes // a single-density sector stored with two copies of each byte in a DMK track. // Link with the matching revision of libVCCCore.a, -Isrc -Isrc/core and // -Iplatforms/windows/libcommon, as with the other standalone guest probes. #include "MachineSession.h" #include "devices/FD502/Fd502Device.h" #include #include #include #include #include #include #include namespace { using namespace VCC::Devices; using namespace VCC::Mac; using Bytes = std::vector; Bytes load(const char* path) { std::ifstream file(path, std::ios::binary); if (!file) throw std::runtime_error(std::string("Could not open ") + path); return Bytes(std::istreambuf_iterator(file), {}); } void advance(MachineSession& machine, unsigned count) { while (count--) { machine.advance(); machine.takeAudio(); } } void run(MachineSession& machine, const std::string& program, const std::string& expected) { machine.basicLoadSource("5 CLEAR 2048:CLS\n" + program, true); for (unsigned i = 0; i < 120; ++i) { advance(machine, 10); const auto text = machine.copyScreenText(); if (text.find(expected) != std::string::npos && text.find("OK") != std::string::npos) { advance(machine, 10); return; } if (expected != "?WP ERROR" && text.find(" ERROR") != std::string::npos) throw std::runtime_error(text); } throw std::runtime_error("Timed out waiting for " + expected + ":\n" + machine.copyScreenText()); } void check(Processor cpu, const Bytes& systemROM, const Bytes& diskROM, const Bytes& original, const std::string& address, std::size_t offset, unsigned stride, const std::string& directoryEntry) { MachineSession machine; auto drive = std::make_shared(diskROM); drive->mount(0, DiskImage::load(original)); machine.cartridges().configure({nullptr, nullptr, nullptr, nullptr, drive}, true, 4); MachineConfiguration config; config.frameInterval = 6; machine.configure(config); machine.load(systemROM, cpu); machine.resume(); advance(machine, 300); if (machine.copyScreenText().find("OK") == std::string::npos) throw std::runtime_error("Disk BASIC did not boot"); const auto sectorBytes = 256 * stride; const auto storedBytes = sectorBytes + (drive->image(0)->format() == DiskFormat::DMK ? 2 * stride : 0); if (offset > original.size() || storedBytes > original.size() - offset) throw std::invalid_argument("Expected sector or CRC extends beyond the source"); Bytes reference; for (unsigned i = 0; i < 256; ++i) reference.push_back(original[offset + i * stride]); const std::string read = "10 DSKI$ " + address + ",A$,B$\n" "20 FOR I=1 TO 128\n" "30 POKE 20479+I,ASC(MID$(A$,I,1))\n" "40 POKE 20607+I,ASC(MID$(B$,I,1))\n" "50 NEXT I\n60 PRINT \"READ COMPLETE\"\n"; if (!directoryEntry.empty()) run(machine, "10 DIR\n", directoryEntry); run(machine, read, "READ COMPLETE"); const auto fromGuest = machine.debuggerMemoryRange(20480, 256, false).bytes; if (fromGuest != reference) throw std::runtime_error("Disk BASIC read disagrees with the independently located file bytes"); if (drive->image(0)->bytes() != original || !drive->readEvents(0)) throw std::runtime_error("Guest read altered the source or bypassed the FD-502"); const std::string write = "10 A$=STRING$(128,65):B$=STRING$(128,66)\n" "20 DSKO$ " + address + ",A$,B$\n30 PRINT \"WRITE COMPLETE\"\n"; if (drive->image(0)->writeProtected()) { run(machine, write, "?WP ERROR"); if (drive->image(0)->bytes() != original || drive->writeEvents(0)) throw std::runtime_error("Protected disk accepted a guest write"); return; } run(machine, write, "WRITE COMPLETE"); Bytes saved = drive->image(0)->bytes(); if (saved.size() != original.size() || !drive->writeEvents(0)) throw std::runtime_error("Guest write changed image length or bypassed the FD-502"); Bytes expected(256, 'A'); std::fill(expected.begin() + 128, expected.end(), 'B'); for (unsigned i = 0; i < sectorBytes; ++i) { if (saved[offset + i] != expected[i / stride]) throw std::runtime_error("Guest write missed the expected on-disk offset"); } const auto after = offset + storedBytes; if (!std::equal(original.begin(), original.begin() + offset, saved.begin()) || !std::equal(original.begin() + after, original.end(), saved.begin() + after)) throw std::runtime_error("Guest write modified bytes outside the sector and its CRC"); drive->markSaved(0); drive->eject(0); drive->mount(0, DiskImage::load(saved)); run(machine, read, "READ COMPLETE"); if (machine.debuggerMemoryRange(20480, 256, false).bytes != expected) throw std::runtime_error("Reopened disk did not contain the saved bytes"); drive->setWriteProtected(0, true); const auto protectedBytes = drive->image(0)->bytes(); run(machine, write, "?WP ERROR"); if (drive->image(0)->bytes() != protectedBytes) throw std::runtime_error("Protected disk changed"); } } int main(int argc, char** argv) try { if (argc < 7) throw std::invalid_argument( "Usage: fd502-media-probe SYSTEM_ROM DISK_ROM IMAGE TRACK SECTOR FILE_OFFSET [--fm-doubled] [--dir ENTRY]"); const auto systemROM = load(argv[1]); const auto diskROM = load(argv[2]); const auto original = load(argv[3]); const auto track = std::stoul(argv[4]), sector = std::stoul(argv[5]), offset = std::stoul(argv[6]); if (track > 255 || !sector || sector > 18) throw std::invalid_argument("Invalid Disk BASIC track or sector"); std::string directoryEntry; unsigned stride = 1; for (int i = 7; i < argc; ++i) { const std::string option = argv[i]; if (option == "--fm-doubled") stride = 2; else if (option == "--dir" && i + 1 < argc) directoryEntry = argv[++i]; else throw std::invalid_argument("Invalid option: " + option); } const auto address = "0," + std::to_string(track) + "," + std::to_string(sector); for (auto cpu : {Processor::MC6809, Processor::HD6309}) { check(cpu, systemROM, diskROM, original, address, offset, stride, directoryEntry); std::cout << (cpu == Processor::MC6809 ? "6809" : "6309") << " passed Disk BASIC sector checks\n"; } return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }