// SPDX-License-Identifier: GPL-3.0-or-later #include "../basic-disk-session.h" using namespace BasicDiskTests; // Read the completed scanout, including the GIME palette and active-image placement. static std::uint32_t pixel(const Frame& frame, unsigned x, unsigned y) { const auto area = frame.activeArea; return frame.pixels[(area.y + y * area.height / 192) * Frame::width + area.x + x * area.width / 320]; } template static void waitFor(Session& session, Predicate ready, const char* failure) { for (unsigned n = 0; n < 900; ++n) { session.frames(1); const auto frame = session.machine.snapshot().frame; if (frame && ready(*frame)) return; } throw std::runtime_error(failure); } static void capture(const Frame& frame, const fs::path& filename) { const auto area = frame.activeArea; std::ofstream out(filename, std::ios::binary); out << "P6\n" << area.width << " " << area.height << "\n255\n"; for (int y = 0; y < area.height; ++y) for (int x = 0; x < area.width; ++x) { auto rgb = frame.pixels[(area.y + y) * Frame::width + area.x + x]; out.put(char(rgb >> 16)); out.put(char(rgb >> 8)); out.put(char(rgb)); } require(out.good(), "Failed to write scanout"); } static void fly(const fs::path& roms, const fs::path& disk, const fs::path& binary, Processor cpu, bool fromDisk, const fs::path& screenshot) { Session session(roms, disk, cpu, Profile::fd); constexpr auto center = 8191; auto joystick = [&](unsigned x, unsigned button) { session.machine.setJoystick(0, x, center, button, 0); }; if (!fromDisk) { session.launch(binary); // The workspace's ordinary development Run. } else { session.type("LOADM\"PROBE\":EXEC\r"); } joystick(center, 0); auto shipAt = [](const Frame& f, unsigned x) { return pixel(f, x, 57) == 0xffffff && pixel(f, x + 19, 57) == 0xffffff; }; waitFor(session, [&](const Frame& f) { return shipAt(f, 144); }, "Lander never appeared"); joystick(16383, 0); waitFor(session, [&](const Frame& f) { return shipAt(f, 214); }, "Steering did not approach the deposit"); joystick(center, 0); session.frames(16); const auto initial = session.machine.snapshot().frame; const auto gold = pixel(*initial, 218, 152); const auto cyan = pixel(*initial, 219, 158); // Support beneath the deposit. const auto sky = pixel(*initial, 212, 130); require(pixel(*initial, 2, 172) != sky && pixel(*initial, 192, 162) == pixel(*initial, 2, 172) && pixel(*initial, 2, 162) == sky, "Terrain profile lost its changing heights"); require(gold != cyan && cyan != sky && gold != sky, "Invalid survey palette or missing deposit"); unsigned launchX = 0; for (unsigned x = 200; x < 240; ++x) { if (pixel(*initial, x, 57) == 0xffffff) { launchX = x + 9; break; } } require(launchX >= 216 && launchX < 242, "Lander did not stop above the deposit"); joystick(center, 1); waitFor(session, [&](const Frame& f) { return pixel(f, launchX, 67) == cyan; }, "A press did not launch the short probe"); waitFor(session, [&](const Frame& f) { return pixel(f, 56, 10) == gold; }, "Holding did not fill the gauge"); // Give the first probe enough time to disappear; a repeating press would // leave a probe in the sky, hit the deposit, or increase the final count. session.frames(60); const auto charged = session.machine.snapshot().frame; require(pixel(*charged, 218, 152) == gold, "First probe reached the distant deposit"); for (unsigned y = 66; y < 150; ++y) require(pixel(*charged, launchX, y) == sky, "Holding the button relaunched a probe"); joystick(center, 0); waitFor(session, [&](const Frame& f) { return pixel(f, 56, 10) == cyan; }, "Release did not lock the charge"); joystick(center, 1); waitFor(session, [&](const Frame& f) { return pixel(f, launchX, 104) == cyan; }, "Second launch did not travel beyond the first probe's range"); if (!screenshot.empty()) capture(*session.machine.snapshot().frame, screenshot); waitFor(session, [&](const Frame& f) { return pixel(f, 218, 152) == cyan; }, "Charged probe missed the deposit"); joystick(center, 0); session.frames(12); session.type("Q"); session.until("PROBES LAUNCHED: 2\nDEPOSIT SURVEYED"); session.verifyReturn(); session.verifyMedia(); } int main(int argc, char** argv) { try { require(argc == 4 || argc == 5, "Usage: run ROM_DIR DISK BIN [CAPTURE.ppm]"); for (auto cpu : {Processor::MC6809, Processor::HD6309}) { const bool captureDirectRun = argc == 5 && cpu == Processor::MC6809; fly(argv[1], argv[2], argv[3], cpu, false, captureDirectRun ? argv[4] : ""); fly(argv[1], argv[2], argv[3], cpu, true, ""); } std::cout << "PASS MC6809 and HD6309 direct Run and disk EXEC: steering, two presses, charge, release, deposit and BASIC return\n"; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }