197 lines
9.0 KiB
C++
197 lines
9.0 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include "../ssc/session.h"
|
|
#include "../guest-captures.h"
|
|
|
|
// Observe the actual guest writes in a spare MPI slot. The Orchestra-90 in a
|
|
// different slot supplies every host audio sample independently of this probe.
|
|
struct DacLog final : CartridgeDevice {
|
|
struct Write { unsigned audioTick; unsigned char port, value; };
|
|
std::vector<Write> writes;
|
|
unsigned audioTick = 0;
|
|
void reset() override { writes.clear(); audioTick = 0; }
|
|
void horizontalSync(DeviceHost&) override {}
|
|
void advanceAudio() override { ++audioTick; }
|
|
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 == 0x7a || port == 0x7b) writes.push_back({audioTick, port, value});
|
|
}
|
|
};
|
|
|
|
static void frames(Session& session, unsigned count) {
|
|
for (unsigned i = 0; i < count; ++i) session.tick();
|
|
}
|
|
|
|
static void verifyBasic(Session& session) {
|
|
session.until("OK", 180);
|
|
frames(session, 12);
|
|
session.press(47, true); session.press(39); session.press(43, true);
|
|
session.press(35); session.press(48); // ?7+3
|
|
session.until(" 10");
|
|
}
|
|
|
|
struct Pair { unsigned tick; unsigned char left, right; };
|
|
|
|
static std::vector<Pair> pairs(const DacLog& log) {
|
|
std::vector<Pair> result;
|
|
require(log.writes.size() % 2 == 0, "An incomplete stereo DAC pair");
|
|
for (std::size_t i = 0; i < log.writes.size(); i += 2) {
|
|
const auto& l = log.writes[i];
|
|
const auto& r = log.writes[i + 1];
|
|
require(l.port == 0x7a && r.port == 0x7b, "DAC writes out of stereo order");
|
|
result.push_back({l.audioTick, l.value, r.value});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static double measuredRate(std::size_t intervals, unsigned audioTicks) {
|
|
require(audioTicks != 0, "Elapsed audio ticks must be positive");
|
|
return 44100.0 * intervals / audioTicks;
|
|
}
|
|
|
|
static void qualify(Session& run, DacLog& log, bool fast) {
|
|
run.launch(readFile("QUAL.BIN"));
|
|
run.until("SELECT CLOCK");
|
|
log.writes.clear();
|
|
run.press(fast ? 34 : 33);
|
|
run.until("RESTORED", 240);
|
|
const auto writes = pairs(log);
|
|
require(writes.size() > 518, "One-shot or loop failed to write frames");
|
|
require(writes[0].left == 19 && writes[0].right == 235, "Missing fixture DAC marker");
|
|
require(writes[1].left == 128 && writes[1].right == 128, "Cancelled startup emitted a sample");
|
|
for (unsigned i = 0; i < 512; ++i) {
|
|
auto level = (i & 8) ? 64 : 192;
|
|
require(writes[i + 2].left == (i < 256 ? level : 128) &&
|
|
writes[i + 2].right == (i < 256 ? 128 : level), "One-shot frames dropped, reordered or distorted");
|
|
}
|
|
require(writes[514].left == 128 && writes[514].right == 128, "Natural end left a DAC on");
|
|
require(writes[515].left == 192 && writes[515].right == 128 &&
|
|
writes[516].left == 128 && writes[516].right == 128, "Single-frame replay failed");
|
|
const auto count = writes.size();
|
|
require(writes[count - 1].left == 21 && writes[count - 1].right == 233, "Missing final DAC marker");
|
|
require(writes[count - 2].left == 128 && writes[count - 2].right == 128, "Manual stop left a DAC on");
|
|
for (unsigned i = 517; i + 2 < count; ++i) {
|
|
auto level = ((i - 517) & 8) ? 64 : 192;
|
|
require(writes[i].left == level && writes[i].right == 128, "Loop boundary or frame mismatch");
|
|
}
|
|
const auto rate = measuredRate(511, writes[513].tick - writes[2].tick);
|
|
require(rate > 3600 && rate < 4050, "Quiet foreground sample rate: " + std::to_string(rate));
|
|
std::cout << " " << (fast ? "fast" : "normal") << " entry, " << rate
|
|
<< " frames/s, CPU speed and interrupts restored\n";
|
|
verifyBasic(run);
|
|
}
|
|
|
|
// Look at the real text scanout so a frozen picture can't pass as animation.
|
|
static int marker(Session& run, unsigned row, char symbol) {
|
|
const auto screen = run.machine.copyScreenText();
|
|
std::size_t start = 0;
|
|
for (unsigned i = 0; i < row; ++i) {
|
|
auto next = screen.find('\n', start);
|
|
if (next == std::string::npos) return -1;
|
|
start = next + 1;
|
|
}
|
|
const auto end = screen.find('\n', start);
|
|
auto column = screen.substr(start, end - start).find(symbol);
|
|
return column == std::string::npos ? -1 : int(column);
|
|
}
|
|
|
|
static void bookExercise(Session& run, DacLog& log, bool disk, bool capture, bool installed = true) {
|
|
if (disk) run.diskExec(readFile("VOL.DSK"), "FLYBY");
|
|
else run.launch(readFile("FLYBY.BIN"));
|
|
run.until("STEREO FLYBY", 1200);
|
|
for (unsigned i = 0; i < 180 && marker(run, 10, '^') != 15; ++i) run.tick();
|
|
require(marker(run, 10, '^') == 15, "Ship did not appear at its starting position");
|
|
run.press(33);
|
|
run.until("BEACON ON AIR", 240);
|
|
run.machine.setJoystick(0, 16383, 8191, 0, 0);
|
|
const auto logsBefore = log.writes.size();
|
|
const auto originalTick = log.audioTick;
|
|
run.audio.clear();
|
|
int firstStar = marker(run, 8, '*');
|
|
bool moved = false;
|
|
for (unsigned f = 0; f < 120; ++f) {
|
|
run.tick();
|
|
int star = marker(run, 8, '*');
|
|
if (star >= 2 && firstStar >= 2 && star != firstStar) moved = true;
|
|
}
|
|
require(moved && marker(run, 10, '^') >= 28, "Star or joystick-controlled ship stalled during sound");
|
|
run.machine.setJoystick(0, 8191, 8191, 0, 0);
|
|
const double rate = measuredRate((log.writes.size() - logsBefore) / 2, log.audioTick - originalTick);
|
|
require(rate > 3450 && rate < 4050, "Active foreground sample rate: " + std::to_string(rate));
|
|
std::size_t channelFrames[2] = {}, periods[2] = {}, elapsed[2] = {}, last[2] = {};
|
|
bool high[2] = {};
|
|
for (std::size_t i = 0; i < run.audio.size(); ++i) {
|
|
const auto sample = run.audio[i];
|
|
if (!installed) {
|
|
require(sample.left == sample.right, "Unexpected stereo with cartridge absent");
|
|
continue;
|
|
}
|
|
const float levels[] = {sample.left, sample.right};
|
|
require(sample.left == 0.5f || sample.right == 0.5f, "A stereo note crossed or dropped out");
|
|
for (unsigned side = 0; side < 2; ++side) {
|
|
channelFrames[side] += std::abs(levels[side] - 0.5f) > 0.04f;
|
|
bool nowHigh = levels[side] > 0.55f;
|
|
if (nowHigh && !high[side]) {
|
|
auto distance = i - last[side];
|
|
// The loop has silent gaps between its two notes. Count only
|
|
// adjacent waveform periods inside one note, ignoring gaps.
|
|
if (last[side] && distance < 300) {
|
|
++periods[side];
|
|
elapsed[side] += distance;
|
|
}
|
|
last[side] = i;
|
|
}
|
|
high[side] = nowHigh;
|
|
}
|
|
}
|
|
if (installed) {
|
|
for (unsigned side = 0; side < 2; ++side) {
|
|
require(channelFrames[side] > 2000 && periods[side] > 30, "Missing stereo cycles");
|
|
const double frequency = 44100.0 * periods[side] / elapsed[side];
|
|
const double expected = rate / (side ? 8 : 16);
|
|
require(std::abs(frequency - expected) < expected * 0.05, "Incorrect stereo note pitch");
|
|
std::cout << (side ? "right " : " left ") << frequency << " Hz ";
|
|
}
|
|
std::cout << '\n';
|
|
}
|
|
if (capture) {
|
|
BasicDiskTests::saveActiveFrame(*run.machine.snapshot().frame, "flyby.ppm");
|
|
saveWav("flyby.wav", run.audio);
|
|
}
|
|
run.press(17);
|
|
run.until("FLYBY COMPLETE");
|
|
const auto stopped = log.writes.size();
|
|
verifyBasic(run);
|
|
require(log.writes.size() == stopped, "Background writes continued after BASIC regained control");
|
|
if (disk) require(run.floppy->readEvents(0) > 0 && run.floppy->writeEvents(0) == 0, "Unexpected demo disk I/O");
|
|
std::cout << " " << (installed ? "stereo" : "absent") << " " << (disk ? "disk" : "direct")
|
|
<< ", steering/animation: " << rate << " frames/s\n";
|
|
}
|
|
|
|
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");
|
|
for (auto cpu : {Processor::MC6809, Processor::HD6309}) {
|
|
std::cout << (cpu == Processor::MC6809 ? "6809" : "6309") << std::endl;
|
|
for (bool fast : {false, true}) {
|
|
auto log = std::make_shared<DacLog>();
|
|
Session guest(cpu, system, disk, nullptr, true, log);
|
|
qualify(guest, *log, fast);
|
|
}
|
|
for (bool fromDisk : {false, true}) {
|
|
auto log = std::make_shared<DacLog>();
|
|
Session guest(cpu, system, disk, nullptr, true, log);
|
|
bookExercise(guest, *log, fromDisk, argc == 3 && cpu == Processor::MC6809 && !fromDisk);
|
|
}
|
|
auto log = std::make_shared<DacLog>();
|
|
Session absent(cpu, system, disk, nullptr, false, log);
|
|
bookExercise(absent, *log, false, false, false);
|
|
}
|
|
} catch (const std::exception& e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|