// SPDX-License-Identifier: GPL-3.0-or-later #include "core/MachineSession.h" #include #include #include #include #include using namespace VCC::Mac; using Bytes = std::vector; constexpr unsigned pageA = 0x40000, pageB = 0x48000, pageBytes = 160 * 192; static void require(bool ok, const std::string& message) { if (!ok) throw std::runtime_error(message); } static Bytes fileBytes(const char* path) { std::ifstream file(path, std::ios::binary); require(file.good(), std::string("Cannot read ") + path); return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; } static void tick(MachineSession& machine, unsigned frames = 1) { for (unsigned i = 0; i < frames; ++i) { machine.advance(); machine.takeAudio(); } } static std::string screen(MachineSession& machine) { try { return machine.copyScreenText(); } catch (const std::exception&) { return {}; } // Bitmap mode has no hardware text. } static void waitText(MachineSession& machine, const std::string& expected) { for (unsigned n = 0; n < 600; ++n) { if (screen(machine).find(expected) != std::string::npos) return; tick(machine); } throw std::runtime_error("Expected " + expected + " on screen: " + screen(machine)); } static void type(MachineSession& machine, const std::string& text) { std::vector keys; auto key = [](int bit) { return std::uint64_t{1} << bit; }; for (char c : text) { if (c >= 'A' && c <= 'Z') keys.push_back(key(c - '@')); else if (c >= '0' && c <= '9') keys.push_back(key(c - '0' + 32)); else if (c == ' ') keys.push_back(key(31)); else if (c == '\r') keys.push_back(key(48)); else if (c == ',') keys.push_back(key(44)); else if (c == '*') keys.push_back(key(55) | key(42)); else throw std::logic_error("Unhandled keyboard character"); } machine.paste(keys); while (machine.pasteRemaining()) tick(machine); tick(machine, 6); } static void boot(MachineSession& machine, const Bytes& rom, Processor cpu, unsigned ram) { MachineConfiguration config; config.ram = ram; config.processor = cpu; machine.configure(config); machine.load(rom, cpu); machine.resume(); tick(machine, 180); waitText(machine, "OK"); } static void launch(MachineSession& machine, const Bytes& binary) { require(binary.size() >= 5 && binary[binary.size() - 5] == 0xff, "Missing DECB entry point"); machine.debuggerLoadBinary(binary, (binary[binary.size() - 2] << 8) | binary.back()); machine.debuggerClearDevelopmentBreakpoints(); machine.resume(); } static void checkBasic(MachineSession& machine) { type(machine, "PRINT 7*9\r"); waitText(machine, " 63"); } static Bytes memory(MachineSession& machine, unsigned address, unsigned length, bool physical = false) { return machine.debuggerMemoryRange(address, length, physical).bytes; } static DebugGraphicsPreview video(MachineSession& machine) { return machine.debuggerGraphicsPreview(true, true, 0, DebugGraphicsMode::HSCREEN1); } static void waitStage(MachineSession& machine, unsigned mailbox, unsigned stage) { for (unsigned n = 0; n < 600; ++n) { if (memory(machine, mailbox, 1).front() == stage) { tick(machine, 2); // Observe two completed rasters at the stable checkpoint. return; } tick(machine); } throw std::runtime_error("Guest did not reach stage " + std::to_string(stage) + ": " + screen(machine)); } static void releaseStage(MachineSession& machine, unsigned mailbox, unsigned stage) { machine.pause(); machine.debuggerWriteMemory(mailbox, {static_cast(stage)}); machine.resume(); } static void checkMmu(const VCC::MMUState& initial, const VCC::MMUState& current, const char* when) { for (unsigned i = 0; i < 8; ++i) if (initial.Task0[i] != current.Task0[i] || initial.Task1[i] != current.Task1[i]) std::cerr << when << " MMU bank " << i << " tasks " << initial.Task0[i] << "/" << initial.Task1[i] << " -> " << current.Task0[i] << "/" << current.Task1[i] << '\n'; require(initial.Enabled == current.Enabled && initial.ActiveTask == current.ActiveTask && initial.AllRAM == current.AllRAM && initial.Prefix == current.Prefix && initial.RamVectors == current.RamVectors && initial.RomMap == current.RomMap && initial.Task0 == current.Task0 && initial.Task1 == current.Task1, "MMU mapping changed: tasks " + std::to_string(initial.ActiveTask) + "/" + std::to_string(current.ActiveTask) + ", control " + std::to_string(initial.RomMap) + "/" + std::to_string(current.RomMap)); } // Check actual RGB pixels presented for a complete frame, including untouched neighbors. static void checkRaster(MachineSession& machine, unsigned address, const Bytes& expected) { const auto image = video(machine); require(image.width == 320 && image.height == 192 && image.bytesPerRow == 160 && image.address == address && !image.text, "Unexpected active graphics page or format"); require(image.bytes == expected, "Wrong displayed page contents"); const auto frame = machine.snapshot().frame; const auto area = frame->activeArea; require(area.width == 640 && area.height == 384, "Wrong active scanout dimensions"); const std::uint32_t colors[] = {0x000000, 0x0000ff, 0xff0000, 0xffffff}; for (unsigned y = 0; y < image.height; ++y) { for (unsigned x = 0; x < image.width; ++x) { const auto byte = expected[y * 160 + x / 2]; const auto index = x % 2 ? byte & 0xf : byte >> 4; const auto offset = (area.y + 2 * y) * Frame::width + area.x + 2 * x; require(index < 4 && frame->pixels[offset] == colors[index], "Mixed or miscolored active raster at " + std::to_string(x) + "," + std::to_string(y) + " actual " + std::to_string(frame->pixels[offset])); } } } // Four consecutive calls must write alternating page origins outside active scanout, // each separated by an actual active field. The trace timestamps are guest video events. static void checkTrace(MachineSession& machine) { const auto summary = machine.debuggerTrace(); require(summary.status.find("Stop address") != std::string::npos, "Missing swap trace end"); unsigned line = 0, field = 0, lastField = 0, count = 0, address = pageA; for (const auto& event : machine.debuggerTraceForExport(0, summary.total).records) { if (event.event == VCC::TraceEvent::ScreenHSYNCLow) { line = event.line; if (line == 128) ++field; } else if (event.event == VCC::TraceEvent::ScreenReg && event.detail.rfind("VOFF: ", 0) == 0) { const unsigned next = std::stoul(event.detail.substr(6), nullptr, 16); require(line < 43 || line >= 235, "Video origin changed during active scanout"); if (next != address) { require(next == (address == pageA ? pageB : pageA), "Unexpected video origin"); require(!count || field > lastField, "Two swaps in a single blanking interval"); address = next; lastField = field; ++count; } } } require(count == 4, "Expected four independently synchronized flips, saw " + std::to_string(count)); } static void qualify(const Bytes& rom, const Bytes& binary, Processor cpu, unsigned ram, unsigned stage, unsigned ack, unsigned finished) { MachineSession machine; boot(machine, rom, cpu, ram); const unsigned width = ram == 2 ? 40 : ram == 3 ? 80 : 32; if (width != 32) type(machine, "WIDTH " + std::to_string(width) + "\r"); type(machine, "PALETTE 1,38\r"); const auto text = video(machine); const auto mmu = machine.debuggerSnapshot().mmu; const auto palette = memory(machine, 0xffb0, 16); const auto display = memory(machine, 0xff98, 8); require(palette[1] == 38, "Could not install BASIC's custom palette"); require(text.text && text.bytesPerRow / text.bytesPerCell == width, "Unexpected BASIC width before Run"); const Bytes mirrored = ram == 0 ? memory(machine, 0, 65536, true) : Bytes{}; launch(machine, binary); if (ram == 0) { waitText(machine, "GRAPHICS UNAVAILABLE\nOK"); require(memory(machine, 0, 65536, true) == mirrored, "Graphics probe corrupted mirrored RAM"); } else { waitStage(machine, stage, 1); checkMmu(mmu, machine.debuggerSnapshot().mmu, "open"); Bytes drawn(pageBytes, 0x11); for (unsigned y : {51, 52}) { drawn[y * 160 + 31] = 0x12; drawn[y * 160 + 32] = 0x22; drawn[y * 160 + 33] = 0x21; } drawn.back() = 0x23; require(memory(machine, pageB, pageBytes, true) == drawn, "Incorrect hidden page after drawing"); checkRaster(machine, pageA, Bytes(pageBytes, 0)); releaseStage(machine, ack, 1); waitStage(machine, stage, 2); checkRaster(machine, pageB, drawn); require(memory(machine, pageA, pageBytes, true) == Bytes(pageBytes, 0x22), "Old front could not be reused"); releaseStage(machine, ack, 2); waitStage(machine, stage, 3); checkRaster(machine, pageA, Bytes(pageBytes, 0x22)); releaseStage(machine, ack, 3); machine.debuggerStartTrace(100000, {}, {static_cast(finished)}, true, false); waitStage(machine, stage, 4); checkTrace(machine); checkRaster(machine, pageA, Bytes(pageBytes, 0x22)); releaseStage(machine, ack, 4); waitStage(machine, stage, 6); checkRaster(machine, pageA, Bytes(pageBytes, 0x22)); drawn.back() = 0x21; // Still wrote to the hidden page after a failed present. require(memory(machine, pageB, pageBytes, true) == drawn, "Failed swap changed the draw destination"); releaseStage(machine, ack, 6); waitStage(machine, stage, 5); checkMmu(mmu, machine.debuggerSnapshot().mmu, "closed"); releaseStage(machine, ack, 5); waitText(machine, "GRAPHICS PASSED\nOK"); } require(memory(machine, 0xffb0, 16) == palette, "Custom BASIC palette was not restored"); require(memory(machine, 0xff98, 8) == display, "BASIC video registers were not restored"); const auto restored = video(machine); require(restored.text && restored.address == text.address && restored.bytesPerRow == text.bytesPerRow && restored.width == text.width, "BASIC text layout changed"); checkBasic(machine); std::cout << " " << mmu.RAMBytes / 1024 << " KiB, WIDTH " << width << ": " << (ram == 0 ? "safe refusal" : "page contents, raster, timing") << ", restored BASIC keyboard, palette and mapping\n"; } // OS/2 24-bit BMP of the emulator's published active area, for the optional manual capture. static void saveFrame(const Frame& frame) { const auto area = frame.activeArea; const unsigned stride = (3 * area.width + 3u) & ~3u; std::ofstream out("lander.bmp", std::ios::binary); auto le = [&](unsigned value, unsigned bytes) { for (unsigned n = 0; n < bytes; ++n) out.put(static_cast(value >> (8 * n))); }; out.write("BM", 2); le(26 + stride * area.height, 4); le(0, 4); le(26, 4); le(12, 4); le(area.width, 2); le(area.height, 2); le(1, 2); le(24, 2); for (int y = area.height - 1; y >= 0; --y) { for (int x = 0; x < area.width; ++x) le(frame.pixels[(area.y + y) * Frame::width + area.x + x], 3); for (unsigned n = 3 * area.width; n < stride; ++n) out.put(0); } require(out.good(), "Unable to capture guest frame"); } static int craftLeft(MachineSession& machine) { if (!(memory(machine, 0xff98, 1)[0] & 0x80)) return -1; const auto image = video(machine); if (image.width != 320 || image.height != 192) return -1; const auto start = image.pixels.begin() + 89 * image.width; const auto x = std::find(start, start + image.width, 0xffffff); return x == start + image.width ? -1 : int(x - start) - 2; } static void fly(const Bytes& rom, const Bytes& binary, Processor cpu) { MachineSession machine; boot(machine, rom, cpu, 1); machine.setJoystick(0, 8191, 8191, 0, 0); launch(machine, binary); for (unsigned n = 0; n < 600 && craftLeft(machine) != 144; ++n) tick(machine); require(craftLeft(machine) == 144, "Lander did not appear at its documented position"); machine.setJoystick(0, 16383, 8191, 0, 0); int x = 144; unsigned firstMeasuredField = 0, measuredSteps = 0, elapsedFields = 0; for (unsigned n = 0; n < 360 && x < 226; ++n) { tick(machine); const int next = craftLeft(machine); // Skip the two one-time landscape draws when measuring steady flight. if (next > x && x >= 152) { if (firstMeasuredField) { ++measuredSteps; elapsedFields = n - firstMeasuredField; } else { firstMeasuredField = n; } } x = next; } require(x >= 226 && x <= 246, "Right joystick did not steer across the display"); machine.setJoystick(0, 8191, 8191, 0, 0); tick(machine, 12); const auto frame = machine.snapshot().frame; const auto area = frame->activeArea; const auto row = frame->pixels.begin() + (area.y + 2 * 89) * Frame::width + area.x; require(row[2 * (144 + 8)] == 0 && row[2 * (craftLeft(machine) + 8)] == 0xffffff, "Craft was absent or left a trail on the visible raster"); const auto landscape = video(machine); require((landscape.bytes[168 * 160 + 315 / 2] & 0xf) == 6, "Right edge of the ridge is missing"); if (cpu == Processor::MC6809) { saveFrame(*frame); machine.setJoystick(0, 8191, 16383, 0, 0); waitText(machine, "ON THE PAD!\nOK"); } else { machine.setKeyboard(std::uint64_t{1} << 17); // Q on the real CoCo keyboard matrix tick(machine, 3); machine.setKeyboard(0); waitText(machine, "FLIGHT ENDED\nOK"); } checkBasic(machine); std::cout << " Lander: " << measuredSteps << " moves in " << elapsedFields << " steady-flight fields; old location erased; returned to BASIC\n"; } int main(int argc, char** argv) { try { require(argc == 7, "Usage: run ROM QUAL.BIN LANDER.BIN STAGE ACK TRACE_STOP"); const auto rom = fileBytes(argv[1]), probe = fileBytes(argv[2]), lander = fileBytes(argv[3]); const auto stage = std::stoul(argv[4], nullptr, 16), ack = std::stoul(argv[5], nullptr, 16); const auto end = std::stoul(argv[6], nullptr, 16); for (auto cpu : {Processor::MC6809, Processor::HD6309}) { std::cout << (cpu == Processor::MC6809 ? "6809" : "6309") << std::endl; for (unsigned ram = 0; ram < 4; ++ram) qualify(rom, probe, cpu, ram, stage, ack, end); fly(rom, lander, cpu); } } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }