92 lines
5.1 KiB
C++
92 lines
5.1 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Optional catalog spot check using private local ROMs/media. No host UI or
|
|
// source-media writes. Command is typed into the guest, never a host shell.
|
|
// Usage: catalog-launch-probe SYSTEM_ROM DISK_ROM MEDIA COMMAND OUTPUT_PREFIX [cassette]
|
|
// Link against the matching revision's libVCCCore.a; see catalog review notes.
|
|
#include "../../src/core/MachineSession.h"
|
|
#include "../../src/devices/FD502/Fd502Device.h"
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
using namespace VCC::Mac;
|
|
using namespace VCC::Devices;
|
|
static std::vector<std::uint8_t> read(const std::string& path) {
|
|
std::ifstream stream(path, std::ios::binary);
|
|
if (!stream) throw std::runtime_error("Cannot read input");
|
|
return {std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()};
|
|
}
|
|
static std::uint64_t key(char c) {
|
|
unsigned bit;
|
|
if (c >= 'A' && c <= 'Z') bit = c - '@';
|
|
else if (c >= '0' && c <= '9') bit = 32 + c - '0';
|
|
else if (c == ':') bit = 42;
|
|
else if (c == ',') bit = 44;
|
|
else if (c == '.') bit = 46;
|
|
else if (c == '/') bit = 47;
|
|
else if (c == '"') return (std::uint64_t{1} << 34) | (std::uint64_t{1} << 55);
|
|
else if (c == ' ') bit = 31;
|
|
else if (c == '\r') bit = 48;
|
|
else throw std::runtime_error("Unsupported guest input character");
|
|
return std::uint64_t{1} << bit;
|
|
}
|
|
static void capture(MachineSession& machine, const std::string& prefix) {
|
|
const auto frame = machine.snapshot().frame;
|
|
if (!frame) throw std::runtime_error("No frame");
|
|
std::ofstream out(prefix + ".ppm", std::ios::binary);
|
|
out << "P6\n" << Frame::width << ' ' << Frame::height << "\n255\n";
|
|
for (auto p : frame->pixels) { out.put(char(p >> 16)); out.put(char(p >> 8)); out.put(char(p)); }
|
|
if (!out) throw std::runtime_error("Cannot write capture");
|
|
try { std::cout << machine.copyScreenText() << '\n'; }
|
|
catch (const std::logic_error&) { std::cout << "Graphics screen; inspect capture.\n"; }
|
|
}
|
|
int main(int argc, char** argv) {
|
|
try {
|
|
if (argc != 6 && argc != 7) throw std::runtime_error("Expected system ROM, disk ROM, disk, guest command and output prefix");
|
|
const bool cassette = argc == 7 && std::string(argv[6]) == "cassette";
|
|
if (argc == 7 && !cassette) throw std::runtime_error("Unknown media mode");
|
|
const auto original = read(argv[3]);
|
|
MachineSession machine;
|
|
auto disk = std::make_shared<Fd502Device>(read(argv[2]));
|
|
if (!cassette) disk->mount(0, DiskImage::load(original, true));
|
|
machine.cartridges().configure({nullptr, nullptr, nullptr, nullptr, disk}, true, 4);
|
|
MachineConfiguration config; config.ram = 1; config.frameInterval = 6;
|
|
machine.configure(config);
|
|
machine.load(read(argv[1])); machine.resume();
|
|
auto advance = [&](unsigned frames) { while (frames--) { machine.advance(); machine.takeAudio(); } };
|
|
auto type = [&](const std::string& text) {
|
|
for (char c : text) { machine.setKeyboard(key(c)); advance(4); machine.setKeyboard(0); advance(4); }
|
|
};
|
|
advance(300);
|
|
if (machine.copyScreenText().find("OK") == std::string::npos) throw std::runtime_error("BASIC did not boot");
|
|
if (cassette) {
|
|
machine.cassette().mount(original, TapeFormat::CAS);
|
|
machine.cassette().setFast(true); machine.cassette().play();
|
|
}
|
|
// Separate cassette load and execution. Never type EXEC while CLOADM
|
|
// is still consuming the tape. The production recipe uses prompt waits.
|
|
std::string command = argv[4];
|
|
if (command.empty() || command.back() != '\r') command += '\r';
|
|
std::size_t start = 0;
|
|
while (start < command.size()) {
|
|
const auto end = command.find('\r', start);
|
|
type(command.substr(start, end - start + 1)); advance(1800);
|
|
if (cassette && machine.cassette().snapshot().motor)
|
|
throw std::runtime_error("Cassette is still loading at command boundary");
|
|
start = end + 1;
|
|
}
|
|
capture(machine, std::string(argv[5]) + "-launch");
|
|
// A single Enter gives text adventures an opportunity to advance past
|
|
// their introduction. It does not establish gameplay completion.
|
|
type("\r"); advance(180);
|
|
capture(machine, std::string(argv[5]) + "-enter");
|
|
const auto reads = cassette ? machine.cassette().snapshot().readEvents : disk->readEvents(0);
|
|
const auto writes = cassette ? machine.cassette().snapshot().writeEvents : disk->writeEvents(0);
|
|
const auto finalBytes = cassette ? machine.cassette().exportImage() : disk->image(0)->bytes();
|
|
std::cout << "reads=" << reads << " writes=" << writes
|
|
<< " frames=" << machine.snapshot().frames << '\n';
|
|
if (!reads || writes || finalBytes != original || read(argv[3]) != original)
|
|
throw std::runtime_error("Read-only media check failed");
|
|
std::cout << "PASS: media was read, original bytes unchanged; visually review launch captures.\n";
|
|
} catch (const std::exception& e) { std::cerr << e.what() << '\n'; return 1; }
|
|
}
|