// SPDX-License-Identifier: GPL-3.0-or-later #include "../basic-disk-session.h" using namespace BasicDiskTests; static void snapshot(Session& session, const char *name) { const auto& original = session.original; Bytes volume; if (session.profile == Profile::rgbHD) { const auto& vhd = session.hard->image(0)->bytes(); volume.assign(vhd.begin() + rgbStart, vhd.begin() + rgbStart + original.size()); require(session.floppy->image(0)->bytes() == original, "Hard-disk run changed FD0"); } else if (session.sdc) { volume = readFile(session.diskPath); } else { volume = session.floppy->image(0)->bytes(); if (session.hard) require(!session.hard->image(0)->dirty(), "DRIVE OFF wrote to the VHD"); } std::ofstream out(name, std::ios::binary); out.write(reinterpret_cast(volume.data()), volume.size()); require(bool(out), "Could not save volume at a checkpoint"); } static void protect(Session& session, bool enabled) { if (session.sdc) session.sdc->setWriteProtected(enabled); else if (session.profile == Profile::rgbHD) session.hard->image(0)->protect(enabled); else session.floppy->setWriteProtected(0, enabled); } // Additional matrix chords for ordinary Disk BASIC file commands. static void basic(Session& session, const std::string& command) { std::vector chords; for (const auto c : command) { unsigned bit; bool shift = false; if (c >= 'A' && c <= 'Z') bit = c - '@'; else if (c >= '0' && c <= '9') bit = c - '0' + 32; else if (c == ' ') bit = 31; else if (c == ':') bit = 42; else if (c == ',') bit = 44; else if (c == '.') bit = 46; else if (c == '\r') bit = 48; else { shift = true; const std::string shifted = "!\"#$%&'()"; const auto n = shifted.find(c); require(n != std::string::npos, "Unhandled guest key"); bit = 33 + n; } chords.push_back((std::uint64_t(1) << bit) | (std::uint64_t(shift) << 55)); } session.machine.paste(chords); for (unsigned n = 0; session.machine.pasteRemaining() && n < 9000; ++n) session.frames(1); require(!session.machine.pasteRemaining(), "Guest paste did not complete"); if (!command.empty() && command.front() >= '0' && command.front() <= '9') { session.frames(10); return; } for (unsigned n = 0; n < 1200; ++n) { session.frames(1); const auto screen = session.machine.copyScreenText(); require(screen.find("ERROR") == std::string::npos, "Disk BASIC command failed: " + screen); const auto end = screen.find_last_not_of(" \r\n"); if (end != std::string::npos && end > 0 && screen.compare(end - 1, 2, "OK") == 0) return; } throw std::runtime_error("Disk BASIC did not return: " + session.machine.copyScreenText()); } static void run(const fs::path& roms, const fs::path& disk, const fs::path& binary, Processor cpu, Profile profile, const std::string& scenario) { Session session(roms, disk, cpu, profile); // Prime Disk BASIC's own DOS allocation table before the C program writes. basic(session, "PRINT FREE(0)\r"); if (scenario == "B") basic(session, "OPEN\"I\",#1,\"SAFE.DAT\"\r"); session.launch(binary); session.until("SELECT CHECK"); if (scenario == "N" && profile == Profile::fd) { session.floppy->setWriteProtected(1, false); session.type("D"); } else session.type(scenario); if (scenario == "W") { char key = 'X'; // Distinct keys at adjacent prompts avoid ROM keyboard repeat debounce. for (const auto& stage : {std::pair{"FIRST FLUSH", "sector.dsk"}, {"FULL GRANULE", "granule.dsk"}, {"APPENDED", "append.dsk"}}) { session.until(stage.first); snapshot(session, stage.second); session.frames(10); session.type(std::string(1, key++)); } } else if (scenario == "N") { session.until("RECLAIM REFUSED"); snapshot(session, "rollback.dsk"); session.type("X"); } else if (scenario == "F") { session.until("LAST FOUR BYTES"); snapshot(session, "full.dsk"); session.type("X"); } else if (scenario == "P" || scenario == "T" || scenario == "U") { session.until("PROTECT NOW"); snapshot(session, "protected.dsk"); protect(session, true); session.type("X"); if (scenario == "T") { session.until("ALLOW RETRY"); snapshot(session, "refused.dsk"); protect(session, false); session.type("Y"); } } if (scenario == "N") session.until("DIRECTORY PASSED"); else if (scenario == "U") session.until("UPDATES PASSED"); else if (scenario == "S" || scenario == "T" || scenario == "M") session.until("POSITIONS PASSED"); else session.until("WRITES PASSED"); session.verifyReturn(); snapshot(session, "closed.dsk"); if (scenario == "N" && profile == Profile::fd) { const auto& drive1 = session.floppy->image(1)->bytes(); std::ofstream out("drive1.dsk", std::ios::binary); out.write(reinterpret_cast(drive1.data()), drive1.size()); require(bool(out), "Could not capture FD1"); } if (scenario == "W") { basic(session, "10 OPEN\"I\",#1,\"REPORT.TXT\"\r"); basic(session, "20 LINE INPUT #1,A$\r"); basic(session, "30 CLOSE#1:PRINT A$\r"); basic(session, "40 OPEN\"O\",#1,\"BASIC.TXT\"\r"); basic(session, "50 PRINT#1,\"FROM BASIC\":CLOSE#1\r"); basic(session, "RUN\r"); session.until("MARS RIDGE"); snapshot(session, "basic.dsk"); } } int main(int argc, char** argv) { try { require(argc == 7, "Usage: run ROMS DSK BIN PROFILE CPU TEST"); const std::string profile = argv[4]; const auto mode = profile == "disk" ? Profile::fd : profile == "rgb-fd" ? Profile::rgbFD : profile == "rgb-hd" ? Profile::rgbHD : Profile::sdc; const auto cpu = std::string(argv[5]) == "6809" ? Processor::MC6809 : Processor::HD6309; run(argv[1], argv[2], argv[3], cpu, mode, argv[6]); } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }