Files

82 lines
3.6 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
#include "../../../src/core/MachineSession.h"
#include "../../../src/devices/Orchestra90Device.h"
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <stdexcept>
using namespace VCC::Mac;
static std::vector<std::uint8_t> readFile(const char* path) {
std::ifstream input(path, std::ios::binary);
if (!input) throw std::runtime_error("Cannot open a test input");
return {std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()};
}
static void measure(Processor processor, const std::vector<std::uint8_t>& system,
const std::vector<std::uint8_t>& binary) {
MachineSession machine;
auto orchestra = std::make_shared<VCC::Devices::Orchestra90Device>();
machine.cartridges().configure({nullptr, orchestra, nullptr, nullptr, nullptr}, true, 4);
machine.load(system, processor);
machine.resume();
for (unsigned i = 0; i < 150; ++i) machine.advance();
machine.takeAudio();
machine.quickLoad(binary);
std::vector<StereoSample> pcm;
for (unsigned i = 0; i < 120; ++i) {
machine.advance();
auto chunk = machine.takeAudio();
pcm.insert(pcm.end(), chunk.begin(), chunk.end());
}
// Marker 32/96 establishes channel order; each following block must contain
// 32 alternating plateaus on one side, with the other side fully silent.
std::size_t cursor = 0;
for (auto clock : {"normal", "fast"}) {
auto isMarker = [](StereoSample p) { return p.left == 0.125f && p.right == 0.375f; };
while (cursor < pcm.size() && !isMarker(pcm[cursor])) ++cursor;
if (cursor == pcm.size()) throw std::runtime_error("Raw stereo marker missing");
while (cursor < pcm.size() && isMarker(pcm[cursor])) ++cursor;
for (unsigned side = 0; side < 2; ++side) {
const auto start = cursor;
unsigned plateaus = 0;
float previous = -1;
while (cursor < pcm.size()) {
const auto sample = pcm[cursor];
float active = side ? sample.right : sample.left;
float other = side ? sample.left : sample.right;
if (other != 0 || (active != 0.25f && active != 0.5f)) break;
if (active != previous) {
if (active != (plateaus % 2 ? 0.25f : 0.5f))
throw std::runtime_error("Wrong alternating sample amplitude");
++plateaus;
previous = active;
}
++cursor;
}
if (plateaus != 32) throw std::runtime_error("Missing sample transitions or channel bleed");
const double rate = 44100.0 * 256 / (cursor - start);
std::printf("%s %-6s %s: %.1f frames/s\n", processor == Processor::MC6809 ? "6809" : "6309",
clock, side ? "right" : "left", rate);
if (std::abs(rate - 4000.0) > 200.0) throw std::runtime_error("Playback rate outside 5% tolerance");
}
}
if (cursor == pcm.size() || pcm.back().left != 16 / 256.0f || pcm.back().right != 80 / 256.0f)
throw std::runtime_error("Guest fixture did not reach the final stereo marker");
}
int main(int argc, char** argv) {
try {
if (argc != 3) throw std::runtime_error("Usage: measure COCO3_ROM PLAYBACK_BIN");
const auto system = readFile(argv[1]), binary = readFile(argv[2]);
for (auto cpu : {Processor::MC6809, Processor::HD6309}) measure(cpu, system, binary);
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
return 0;
}