// SPDX-License-Identifier: GPL-3.0-or-later #include "../ssc/session.h" #include "../guest-captures.h" // A passive second MPI slot records commands in bus order. The real SSC // supplies every status read, runs both ROMs and produces the captured audio. struct CommandLog final : CartridgeDevice { Bytes bytes; void reset() override { bytes.clear(); } void horizontalSync(DeviceHost&) override {} std::uint8_t readMemory(std::uint16_t) const override { return 0xff; } std::uint8_t readPort(std::uint8_t, DeviceHost&) override { return 0; } void writePort(std::uint8_t port, std::uint8_t value, DeviceHost&) override { if (port == 0x7e) bytes.push_back(value); } }; template static void waitUntil(Session& run, Predicate done, const char* failure) { for (unsigned i = 0; i < 360; ++i) { if (done()) return; run.tick(); } throw std::runtime_error(std::string(failure) + "\n" + run.machine.copyScreenText()); } static void frames(Session& run, unsigned count) { while (count--) run.tick(); } static void fire(Session& run, bool pressed) { run.machine.setJoystick(0, 8191, 8191, pressed ? 1 : 0, 0); } static void awaitSpeech(Session& run, unsigned baseline) { waitUntil(run, [&] { return run.ssc->diagnostics().AldSpeechCount() > baseline && !(run.machine.cartridges().readPort(0x7e) & 0x40); }, "The speech chip never began a new phoneme"); } static float amplitude(const std::vector& pcm, std::size_t first, std::size_t last) { require(first < last && last <= pcm.size(), "Empty PCM measurement"); auto range = std::minmax_element(pcm.begin() + first, pcm.begin() + last, [](const auto& a, const auto& b) { return a.left < b.left; }); return range.second->left - range.first->left; } static Bytes expectedBanks() { Bytes writes{0xb7}; for (unsigned i = 2; i <= 63; ++i) writes.push_back(i); writes.insert(writes.end(), { 0, 0xff, 0xb0, 43, 58, 15, 0, 0xff, // SP0256 ZZ, OR, AX, PA1 0x97, 'M', 'A', 'R', 'S', ' ', 13, 0x9f, 0x6f, 255, 255, 0, 0x50, 15, 255, 255, 0x6f, 255, 255, 0, 0x40, 15, 255, 0, 255, 0x98, 0x60, 16, 0, 0, 0x10, 0, 250, 60, 0x60, 16, 0, 0, 0, 0, 250, 0, 255 }); return writes; } static void qualify(Session& run, const Bytes& guest, CommandLog& log) { log.bytes.clear(); run.launch(guest); run.until("BANKS LOADED"); frames(run, 20); auto expected = expectedBanks(); require(log.bytes == expected, "Invalid input wrote a byte or a stored packet was malformed"); const auto& chip = run.ssc->diagnostics(); require(chip.AldSpeechCount() == 0, "Loading a slot played it"); run.press(1); awaitSpeech(run, 0); fire(run, true); run.until("FIRE DURING NAME"); run.until("ALERT READY"); fire(run, false); frames(run, 120); // isolate the tone from the earlier phrase require(chip.AldSpeechCount() == 3, "Unexpected stored pronunciation length"); run.machine.setKeyboard(std::uint64_t{1} << 2); waitUntil(run, [&] { return !(run.machine.cartridges().readPort(0x7e) & 0x20); }, "Stored envelope never raised the sound activity signal"); run.machine.setKeyboard(0); const auto start = run.audio.size(); fire(run, true); run.until("FIRE DURING ALERT", 30); fire(run, false); run.until("REPLAY READY"); frames(run, 7); const auto middle = run.audio.size(); frames(run, 14); const auto late = run.audio.size(); frames(run, 40); const auto tail = run.audio.size(); frames(run, 10); const auto earlyAmplitude = amplitude(run.audio, start, middle); const auto laterAmplitude = amplitude(run.audio, late, tail); const auto tailAmplitude = amplitude(run.audio, tail, run.audio.size()); require(earlyAmplitude > 0.02f && laterAmplitude < earlyAmplitude * 0.7f && tailAmplitude < 0.01f, "Expected a loud attack, a softer envelope and silence: " + std::to_string(earlyAmplitude) + ", " + std::to_string(laterAmplitude) + ", " + std::to_string(tailAmplitude)); require((run.machine.cartridges().readPort(0x7e) & 0x20) != 0, "SAC stayed active after the effect"); run.press(3); run.until("TEXT READY"); frames(run, 150); require(chip.AldSpeechCount() == 6, "One-byte replay did not reproduce the first pronunciation"); run.press(4); run.until("BANKS PASSED"); frames(run, 150); require(chip.AldSpeechCount() > 6 && chip.CpuIllegalCount() == 0, "English phrase failed"); expected.insert(expected.end(), {0xf0, 0xd8, 0xf0, 0xd7}); require(log.bytes == expected, "Replay rewrote a stored buffer"); std::cout << " " << log.bytes.size() << " commands, " << chip.AldSpeechCount() << " phonemes, envelope peak-to-peak " << earlyAmplitude << " -> " << laterAmplitude << " -> " << tailAmplitude << '\n'; } static void bookExercise(Session& run, bool fromDisk, bool capture) { if (fromDisk) run.diskExec(readFile("VOL.DSK"), "ANNOUNCE"); else run.launch(readFile("ANNOUNCE.BIN")); run.until("Q RETURN TO BASIC", 1200); run.audio.clear(); const auto& chip = run.ssc->diagnostics(); run.press(34); // 2: English text in its own slot awaitSpeech(run, 0); frames(run, 180); auto baseline = chip.AldSpeechCount(); require(baseline > 3, "WELCOME ABOARD never spoke"); run.press(33); // 1: invented planet name run.until("APPROACHING ZORA"); awaitSpeech(run, baseline); fire(run, true); run.until("LANDING ALERT 1", 30); frames(run, 50); require(!run.contains("LANDING ALERT 2"), "Holding fire repeated the alert"); fire(run, false); frames(run, 5); fire(run, true); run.until("LANDING ALERT 2", 30); fire(run, false); frames(run, 70); require(chip.AldSpeechCount() == baseline + 3, "Joystick input interrupted the announcement"); run.checkSignal(0); if (capture) { BasicDiskTests::saveActiveFrame(*run.machine.snapshot().frame, "announcer.ppm"); saveWav("announcer.wav", run.audio); } run.press(17); // Q run.until("OK"); frames(run, 12); run.press(47, true); run.press(39); run.press(43, true); run.press(35); run.press(48); // ?7+3 run.until(" 10"); if (fromDisk) require(run.floppy->readEvents(0) > 0 && run.floppy->writeEvents(0) == 0, "Unexpected demo disk I/O"); } int main(int argc, char** argv) { try { require(argc == 2 || argc == 3, "Usage: run ROM_DIRECTORY [capture]"); const fs::path rom(argv[1]); const auto system = readFile(rom / "coco3.rom"), disk = readFile(rom / "disk11.rom"); auto chips = readFile(rom / "pic-7040-510.bin"); auto speech = readFile(rom / "sp0256-al2.bin"); require(chips.size() == 4096 && speech.size() == 2048, "Missing Speech/Sound firmware"); chips.insert(chips.end(), speech.begin(), speech.end()); const auto guest = readFile("QUAL.BIN"); for (auto cpu : {Processor::MC6809, Processor::HD6309}) { std::cout << (cpu == Processor::MC6809 ? "6809" : "6309") << std::endl; { auto log = std::make_shared(); Session fixture(cpu, system, disk, std::make_shared(chips), false, log); qualify(fixture, guest, *log); } { Session absent(cpu, system, disk, nullptr); absent.launch(guest); absent.until("NO SPEECH CART"); } for (bool fromDisk : {false, true}) { Session demo(cpu, system, disk, std::make_shared(chips)); bookExercise(demo, fromDisk, argc == 3 && !fromDisk && cpu == Processor::MC6809); } std::cout << " PASS stored banks, joystick during speech and alert, direct and disk launch, BASIC return\n"; } } catch (const std::exception& e) { std::cerr << e.what() << '\n'; return 1; } }