// SPDX-License-Identifier: GPL-3.0-or-later #include "core/MachineSession.h" #include "devices/Orchestra90Device.h" #include "devices/SpeechSoundDevice.h" #include #include #include #include #include #include #include using namespace VCC::Mac; using namespace VCC::Devices; namespace fs = std::filesystem; using Bytes = std::vector; static void require(bool ok, const std::string& reason) { if (!ok) throw std::runtime_error(reason); } static Bytes read(const fs::path& path) { std::ifstream file(path, std::ios::binary); require(file.good(), "Cannot open " + path.string()); return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; } struct Session { MachineSession machine; std::shared_ptr speech; std::shared_ptr stereo = std::make_shared(); unsigned stage, ack, report; std::vector pcm; Session(const fs::path& roms, const Bytes& guest, Processor cpu, unsigned s, unsigned a, unsigned r) : stage(s), ack(a), report(r) { auto chips = read(roms / "pic-7040-510.bin"); auto phonemes = read(roms / "sp0256-al2.bin"); chips.insert(chips.end(), phonemes.begin(), phonemes.end()); speech = std::make_shared(chips); machine.cartridges().configure({nullptr, speech, stereo, nullptr, nullptr}, true, 4); machine.load(read(roms / "coco3.rom"), cpu); machine.resume(); for (unsigned i = 0; i < 180; ++i) machine.advance(); require(machine.copyScreenText().find("OK") != std::string::npos, "BASIC did not boot"); require(guest.size() >= 5 && guest[guest.size()-5] == 0xff, "Missing guest entry point"); auto entry = std::uint16_t((guest[guest.size()-2] << 8) | guest.back()); machine.debuggerLoadBinary(guest, entry); machine.debuggerClearDevelopmentBreakpoints(); machine.resume(); machine.takeAudio(); } void tick() { machine.advance(); auto block = machine.takeAudio(); pcm.insert(pcm.end(), block.begin(), block.end()); } void wait(unsigned target) { for (unsigned i = 0; i < 400; ++i) { if (machine.debuggerMemoryRange(stage, 1, false).bytes[0] == target) return; tick(); } throw std::runtime_error("Guest missed checkpoint " + std::to_string(target) + ": " + machine.copyScreenText()); } void release(unsigned target) { machine.pause(); machine.debuggerWriteMemory(ack, {static_cast(target)}); machine.resume(); } }; // Each guest row is x, y, dx, dy, held, pressed, released. Opposite ports // deliberately have different axes/buttons to detect accidental shared state. using Row = std::array; using Pair = std::array; static const Pair steps[] = { {{{31,32,0,0,1,0,0}, {63,0,1,-1,2,0,0}}}, // Baseline with held buttons. {{{26,37,0,0,1,0,0}, {37,26,0,0,2,0,0}}}, // Inclusive dead zone limits. {{{25,38,-1,1,3,2,0}, {38,25,1,-1,1,1,2}}}, {{{0,63,-1,1,2,0,1}, {63,0,1,-1,0,0,1}}}, {{{32,31,0,0,0,0,2}, {31,32,0,0,2,2,0}}}, {{{32,31,0,0,1,1,0}, {31,32,0,0,2,0,0}}}, {{{32,31,0,0,3,0,0}, {31,32,0,0,3,0,0}}}, // Explicit re-baseline. {{{32,31,0,0,0,0,3}, {31,32,0,0,0,0,3}}} }; static void checkInputs(Session& run) { run.wait(1); for (unsigned i = 0; i < std::size(steps); ++i) { run.release(i + 1); // Pausing releases the host controls, so apply them after resuming. for (unsigned port = 0; port < 2; ++port) { const auto& row = steps[i][port]; // Sample exactly at a six-bit DAC threshold with the serial pin set. run.machine.setJoystick(port, 256 * row[0], 256 * row[1], row[4], 0); } run.machine.setKeyboard(std::uint64_t(1) << 1); // A held during sampling must not become fire. run.wait(i + 2); auto bytes = run.machine.debuggerMemoryRange(run.report, 14, false).bytes; for (unsigned port = 0; port < 2; ++port) { for (unsigned j = 0; j < steps[i][port].size(); ++j) { auto expected = static_cast(steps[i][port][j]); require(bytes[7 * port + j] == expected, "Input mismatch at update " + std::to_string(i+1) + ", port " + std::to_string(port) + ", field " + std::to_string(j) + ": expected " + std::to_string(expected) + ", got " + std::to_string(bytes[7 * port + j])); } } } run.release(9); } // Measure from the first through the last plateau onset *within* each block. // The last sample stays latched while the CoCo polls input between blocks. static void analyzeBlock(const std::vector& pcm, bool right) { unsigned plateaus = 0, first = 0, last = 0; float previous = 0; for (unsigned i = 0; i < pcm.size(); ++i) { const float delta = right ? pcm[i].right - pcm[i].left : pcm[i].left - pcm[i].right; if (delta != 0.25f && delta != 0.50f) continue; if (delta != previous) { require(delta == (plateaus % 2 ? 0.25f : 0.50f), "Wrong stereo level or channel order"); if (!plateaus) first = i; last = i; previous = delta; ++plateaus; } } require(plateaus == 32, "Expected 32 alternating stereo plateaus, found " + std::to_string(plateaus)); const double rate = 44100.0 * (31 * 8) / (last - first); require(std::fabs(rate - 4000.0) < 200, "Stereo playback rate drifted: " + std::to_string(rate)); std::cout << (right ? "right " : "left ") << int(rate) << " Hz "; } static void checkStereo(Session& run, unsigned clock) { unsigned stage = 10 + clock; run.wait(stage); run.pcm.clear(); run.release(stage); // Change the physical button mid-block. Guest software first sees the // change when orch90_play returns and input_sample reads the port. for (bool right : {false, true}) { unsigned n = 0; while (n++ < 90) { run.tick(); auto latch = run.stereo->audio(); if (right ? (latch == 64 || latch == 128) : (latch == 16384 || latch == 32768)) break; } require(n <= 90, "Missing stereo block: " + run.machine.copyScreenText()); run.machine.setJoystick(0,8191,8191,right ? 0 : 1,0); } run.wait(stage + 1); analyzeBlock(run.pcm, false); analyzeBlock(run.pcm, true); std::cout << (clock ? "(fast)\n" : "(normal)\n"); } static void checkSpeech(Session& run) { run.wait(12); run.release(12); run.machine.setJoystick(0,8191,8191,1,0); run.pcm.clear(); for (unsigned i = 0; i < 240; ++i) { run.tick(); if (run.machine.copyScreenText().find("INPUT PASSED\nOK") != std::string::npos) break; } require(run.machine.copyScreenText().find("INPUT PASSED\nOK") != std::string::npos, "Guest did not return to BASIC: " + run.machine.copyScreenText()); auto& diagnostics = run.speech->diagnostics(); require(diagnostics.AldSpeechCount() > 4 && diagnostics.CpuIllegalCount() == 0 && diagnostics.CommandCount() == std::string("SURVEY PROBE READY").size()+2, "Incomplete spoken phrase"); require(!run.pcm.empty(), "No speech samples captured"); auto range = std::minmax_element(run.pcm.begin(), run.pcm.end(), [](auto a, auto b){ return a.left < b.left; }); require(range.second->left - range.first->left > 0.02f, "Speech PCM stayed silent"); std::cout << diagnostics.AldSpeechCount() << " speech phonemes while polling, input and return passed\n"; } int main(int argc, char** argv) { try { require(argc == 6, "Usage: run ROM_DIR QUAL.BIN STAGE ACK REPORT"); const auto guest = read(argv[2]); for (auto cpu : {Processor::MC6809, Processor::HD6309}) { Session run(argv[1], guest, cpu, std::stoul(argv[3],nullptr,16), std::stoul(argv[4],nullptr,16), std::stoul(argv[5],nullptr,16)); std::cout << (cpu == Processor::MC6809 ? "6809\n" : "6309\n"); checkInputs(run); for (unsigned clock = 0; clock <= 1; ++clock) checkStereo(run, clock); checkSpeech(run); } } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }