// SPDX-License-Identifier: GPL-3.0-or-later #pragma once #include "core/MachineSession.h" #include "devices/FD502/Fd502Device.h" #include "devices/HardDisk/HardDiskDevice.h" #include "devices/SDCDevice.h" #include #include #include #include #include namespace BasicDiskTests { using namespace VCC::Mac; using namespace VCC::Devices; namespace fs = std::filesystem; using Bytes = std::vector; static Bytes readFile(const fs::path& path) { std::ifstream f(path, std::ios::binary); if (!f) throw std::runtime_error("Cannot open " + path.string()); return {std::istreambuf_iterator(f), std::istreambuf_iterator()}; } static void require(bool ok, const std::string& reason) { if (!ok) throw std::runtime_error(reason); } enum class Profile { fd, rgbHD, rgbFD, sdc }; constexpr std::size_t rgbStart = 0x5a000u * 256; struct Session { MachineSession machine; std::shared_ptr floppy; std::shared_ptr hard; std::shared_ptr sdc; fs::path diskPath; Bytes original; Bytes second; Profile profile; Session(const fs::path& roms, const fs::path& disk, Processor cpu, Profile mode, bool expectSDCStartupMount = true) : diskPath(disk), original(readFile(disk)), profile(mode) { require(original.size() == 35 * 18 * 256, "Expected a 35-track test disk"); const auto secondPath = disk.parent_path() / "SECOND.DSK"; if (fs::exists(secondPath)) second = readFile(secondPath); if (mode == Profile::sdc) { // startup.cfg lives next to the disposable disk and selects SDC drive 0. sdc = std::make_shared(readFile(roms / "sdcdos.rom"), disk.parent_path().string(), false); machine.cartridges().configure({nullptr, nullptr, nullptr, nullptr, sdc}, true, 4); } else { floppy = std::make_shared(readFile(roms / (mode == Profile::fd ? "disk11.rom" : "rgbdos.rom"))); floppy->mount(0, DiskImage::load(original)); if (!second.empty()) floppy->mount(1, DiskImage::load(second, true)); if (mode != Profile::fd) { hard = std::make_shared(); Bytes vhd(rgbStart + original.size() + second.size(), 0); std::copy(original.begin(), original.end(), vhd.begin() + rgbStart); std::copy(second.begin(), second.end(), vhd.begin() + rgbStart + original.size()); hard->mount(0, HardDiskImage(std::move(vhd))); } machine.cartridges().configure({nullptr, hard, nullptr, nullptr, floppy}, true, 4); } machine.load(readFile(roms / "coco3.rom"), cpu); if (sdc) { auto config = machine.snapshot().configuration; config.ram = 2; // 2 MiB, as in the built-in CoCo SDC profile machine.configure(config); } machine.resume(); frames(300); until(hard ? "HARD DISK BASIC" : "OK"); if (sdc) require(sdc->snapshot().drives[0].mounted == expectSDCStartupMount, "Unexpected SDC drive 0 at startup"); if (mode == Profile::rgbFD) type("DRIVE OFF\r"); type("CLS\r"); } void frames(unsigned count) { while (count--) machine.advance(); machine.takeAudio(); } void until(const std::string& marker, unsigned limit = 9000) { for (unsigned i = 0; i < limit; ++i) { auto text = machine.copyScreenText(); if (text.find("FAIL LINE") != text.npos) { frames(10); throw std::runtime_error(machine.copyScreenText()); } if (text.find(marker) != text.npos) return; frames(1); } throw std::runtime_error("Timed out waiting for " + marker + "\n" + machine.copyScreenText()); } void type(const std::string& keys) { for (char c : keys) { unsigned bit; bool shift = false; if (c >= 'A' && c <= 'Z') bit = c - '@'; else if (c >= '0' && c <= '9') bit = 32 + c - '0'; else if (c == ' ') bit = 31; else if (c == '\r') bit = 48; else if (c == ':') bit = 42; else if (c == '"') { bit = 34; shift = true; } else if (c == '*') { bit = 42; shift = true; } else throw std::logic_error("Unknown fixture key"); machine.setKeyboard((std::uint64_t(1) << bit) | (std::uint64_t(shift) << 55)); frames(5); machine.setKeyboard(0); frames(5); } } void launch(const fs::path& binary) { const auto program = readFile(binary); const auto n = program.size(); require(n >= 5 && program[n - 5] == 0xff, "Incomplete DECB binary"); machine.debuggerLoadBinary(program, std::uint16_t(program[n - 2] << 8 | program[n - 1])); machine.debuggerClearDevelopmentBreakpoints(); machine.resume(); } std::uint64_t reads() const { if (sdc) return sdc->snapshot().drives[0].reads; if (profile == Profile::rgbHD) return hard->readEvents(0); return floppy->readEvents(0); } std::uint64_t otherReads() const { if (!hard) return 0; return profile == Profile::rgbHD ? floppy->readEvents(0) : hard->readEvents(0); } void verifyMedia() const { if (floppy) require(floppy->writeEvents(0) == 0 && floppy->image(0)->bytes() == original, "Floppy changed"); if (floppy && !second.empty()) require(floppy->writeEvents(1) == 0 && floppy->image(1)->bytes() == second, "Second floppy changed"); if (hard) { const auto& bytes = hard->image(0)->bytes(); require(hard->writeEvents(0) == 0 && !hard->image(0)->dirty() && bytes.size() == rgbStart + original.size() + second.size() && std::equal(second.begin(), second.end(), bytes.begin() + rgbStart + original.size()) && std::equal(original.begin(), original.end(), bytes.begin() + rgbStart), "VHD changed"); } if (sdc && !second.empty()) require(sdc->snapshot().drives[1].writes == 0 && readFile(diskPath.parent_path() / "SECOND.DSK") == second, "Second SDC image changed"); if (sdc) require(sdc->snapshot().drives[0].writes == 0 && readFile(diskPath) == original, "SDC image changed"); } void eject() { if (sdc) sdc->eject(0); else if (profile == Profile::rgbHD) hard->eject(0); else floppy->eject(0); } void verifyReturn() { until("OK"); // OK is drawn before the ROM finishes returning to its line editor. frames(12); type("PRINT 7*9\r"); until(" 63"); } }; } // namespace BasicDiskTests