// SPDX-License-Identifier: GPL-3.0-or-later // Optional real-media qualification. All guest commands use the keyboard; the // source disk/ROMs are read-only inputs and guest writes stay in owned memory. #include "../toolchain/ssc/session.h" #include "../../src/core/mc6821.h" #include static void capture(const fs::path& path, const std::vector& pcm) { std::ofstream out(path, std::ios::binary); auto put = [&](unsigned value, unsigned count) { while (count--) { out.put(char(value)); value >>= 8; } }; out.write("RIFF", 4); put(36 + unsigned(pcm.size()) * 4, 4); out.write("WAVEfmt ", 8); put(16, 4); put(1, 2); put(2, 2); put(44100, 4); put(176400, 4); put(4, 2); put(16, 2); out.write("data", 4); put(unsigned(pcm.size()) * 4, 4); for (const auto sample : pcm) for (float value : {sample.left, sample.right}) put(static_cast(std::clamp(std::lround(value * 32768), -32768l, 32767l)), 2); require(out.good(), "Could not save private audio capture"); } static void screen(Session& guest, const fs::path& path) { const auto frame = guest.machine.snapshot().frame; require(bool(frame), "Missing framebuffer"); const auto area = frame->activeArea; std::ofstream out(path, 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) { const 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(), "Could not save private guest capture"); } static bool report(Session& guest, const std::string& label, const fs::path& output) { double sum = 0, square = 0, low = 1, high = -1; std::uint64_t edges = 0, nonzero = 0; float previous = 0; for (auto sample : guest.audio) { sum += sample.left; square += double(sample.left) * sample.left; low = std::min(low, double(sample.left)); high = std::max(high, double(sample.left)); edges += sample.left != previous; previous = sample.left; nonzero += sample.left != 0 || sample.right != 0; } const double n = double(guest.audio.size()), mean = n ? sum/n : 0; const double ac = n ? std::sqrt(std::max(0.0, square/n - mean*mean)) : 0; const auto& bus = guest.machine.cartridges(); std::cout << label << " frames=" << guest.audio.size() << " nonzero=" << nonzero << " range=" << low << ":" << high << " mean=" << mean << " ac_rms=" << ac << " edges=" << edges << " mux=" << unsigned(GetMuxState()) << " sound_enabled=" << GetPIASoundState().enabled << " pc=" << guest.machine.snapshot().cpu.PC << " cartridge=" << bus.audio() << '\n'; capture(output / (label + ".wav"), guest.audio); screen(guest, output / (label + ".ppm")); return nonzero == 0; } int main(int argc, char** argv) { try { if (argc != 4 && argc != 5) throw std::runtime_error("Usage: audio-game-validation ROM_DIRECTORY DOS_BOOT_DISK OUTPUT_DIRECTORY [--expect-quiet]"); const fs::path roms(argv[1]), output(argv[3]); const auto disk = readFile(argv[2]); require(argc == 4 || std::string(argv[4]) == "--expect-quiet", "Unknown validation option"); const bool expectQuiet = argc == 5; fs::create_directories(output); auto chips = readFile(roms / "pic-7040-510.bin"), voice = readFile(roms / "sp0256-al2.bin"); require(chips.size() == 4096 && voice.size() == 2048, "Incorrect Speech/Sound ROM sizes"); chips.insert(chips.end(), voice.begin(), voice.end()); bool quiet = true; for (auto cpu : {Processor::MC6809, Processor::HD6309}) for (unsigned mode = 0; mode < 4; ++mode) { auto ssc = mode & 1 ? std::make_shared(chips) : nullptr; Session guest(cpu, readFile(roms / "coco3.rom"), readFile(roms / "disk11.rom"), ssc, false); // Match the normal Multi-Pak layout, including the optional music // ROM. It is retained while the FD-502 remains the startup device. auto orchestra = mode & 2 ? std::make_shared(readFile(roms / "orch90.rom")) : nullptr; require(guest.machine.snapshot().configuration.ram == 1, "This check requires 512 KiB"); guest.machine.cartridges().configure({nullptr, ssc, orchestra, nullptr, guest.floppy}, true, 4); guest.machine.cassette().mount({0x55, 0x55, 0x3c}, TapeFormat::CAS); // mounted, stopped guest.machine.setJoystick(0, 8191, 8191, 0, 0); guest.machine.setJoystick(1, 8191, 8191, 0, 0); guest.floppy->mount(0, DiskImage::load(disk, false)); guest.press('D'-'@'); guest.press('O'-'@'); guest.press('S'-'@'); guest.press(48); for (unsigned i = 0; i < 1800; ++i) guest.tick(); // disk boot + title settle guest.audio.clear(); for (unsigned i = 0; i < 120; ++i) guest.tick(); const std::string label = std::string(cpu == Processor::MC6809 ? "6809-" : "6309-") + std::array{"disk", "speech", "orchestra", "both"}[mode]; quiet = report(guest, label, output) && quiet; if (mode == 3) { // Start the unchanged game and exercise its standard event // effect through ordinary keyboard input, not guest memory. const auto title = guest.machine.snapshot().frame->pixels; guest.machine.setKeyboard(std::uint64_t{1} << 48); for (unsigned i = 0; i < 60; ++i) guest.tick(); guest.machine.setKeyboard(0); for (unsigned i = 0; i < 480; ++i) guest.tick(); require(guest.machine.snapshot().frame->pixels != title, "Start did not change the title framebuffer"); guest.audio.clear(); guest.machine.setKeyboard(std::uint64_t{1} << 4); // D/right for (unsigned i = 0; i < 12; ++i) guest.tick(); guest.machine.setKeyboard(0); for (unsigned i = 0; i < 108; ++i) guest.tick(); report(guest, label + "-move", output); require(std::any_of(guest.audio.begin(), guest.audio.end(), [](auto sample) { return sample.left != 0 || sample.right != 0; }), "Movement effect was muted or did not run"); if (expectQuiet) require(guest.audio.size() > 22050 && std::all_of(guest.audio.end()-22050, guest.audio.end(), [](auto sample) { return sample.left == 0 && sample.right == 0; }), "Movement did not return to silence"); } require(guest.machine.cassette().snapshot().position == 0, "Stopped tape advanced"); require(guest.floppy->readEvents(0) != 0, "Game disk was not read"); guest.machine.pause(); guest.machine.takeAudio(); guest.machine.advance(); require(guest.machine.takeAudio().empty(), "Pause generated new PCM"); } require(readFile(argv[2]) == disk, "Source game disk changed"); if (expectQuiet) require(quiet, "Expected silent idle title output in every cartridge configuration"); } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }