// SPDX-License-Identifier: GPL-3.0-or-later #include "CoreChecks.h" #include "../../src/core/FramePacing.h" #include "../../src/core/MachineSession.h" #include "../../src/core/tcc1014mmu.h" #include #include using namespace VCC::Mac; namespace { void require(bool value, const char* message) { if (!value) throw std::runtime_error(message); } } void CheckFramePacing() { using namespace std::chrono; FramePacer pacer; const auto origin = FramePacer::Clock::time_point{}; // Irregular host callbacks for thirty simulated minutes; no wall-clock wait. auto elapsed = nanoseconds::zero(); std::uint64_t frames = 0; for (unsigned i = 0; elapsed < minutes(30); ++i) { if (pacer.ready(origin + elapsed)) ++frames; elapsed += microseconds(1000 + (i % 7) * 500); } const double expected = duration(elapsed).count() * masterClockHz / clocksPerFrame; require(std::abs(double(frames) - expected) < 1.1, "Irregular host ticks must not accumulate frame drift"); auto late = origin + hours(1); require(pacer.ready(late) && !pacer.ready(late), "Long stall must not replay a backlog of frames"); require(!pacer.ready(late + frameInterval - nanoseconds(1)) && pacer.ready(late + frameInterval), "Late wake establishes a full new frame interval"); pacer.reset(); require(pacer.ready(late) && !pacer.ready(late), "Resume resets stale deadlines without double execution"); // Short lateness is caught up, preserving average speed rather than dropping time. pacer.reset(); pacer.ready(origin); unsigned catchup = 0; while (pacer.ready(origin + frameInterval * 3)) ++catchup; require(catchup == 3, "Short host jitter retains scheduled guest frames"); } void CheckAudioProgress() { using namespace std::chrono; AudioProgress progress; const auto start = AudioProgress::Clock::time_point{}; require(!progress.stalled(start, 0) && !progress.stalled(start + milliseconds(999), 0), "Audio startup has a bounded grace period"); require(progress.stalled(start + seconds(1), 0), "A live graph with no consumption must be detected"); require(!progress.stalled(start + seconds(2), 1), "Sample consumption clears a stalled interval"); require(!progress.stalled(start + milliseconds(2999), 1) && progress.stalled(start + seconds(3), 1), "Watchdog follows the last progress, not graph age"); progress.reset(); require(!progress.stalled(start + hours(1), 1), "Pause/device restart discards the previous watchdog deadline"); AudioProgress startup; require(!startup.stalled(start, 0), "Prefill may precede the first output graph"); // A slow host may spend longer than the watchdog interval creating its // first graph. That setup time cannot count as a stalled render callback. const auto graphReady = start + seconds(2); startup.reset(); require(!startup.stalled(graphReady, 0), "A newly started graph gets its own progress deadline"); require(!startup.stalled(graphReady + milliseconds(999), 0), "Slow startup retains the normal consumption grace period"); require(startup.stalled(graphReady + seconds(1), 0), "Startup reset must not disable or extend the live-graph watchdog"); require(!startup.stalled(graphReady + milliseconds(1100), 4410), "Real output consumption clears the startup stall"); } void CheckAcceleratedTiming() { for (auto cpu : {Processor::MC6809, Processor::HD6309}) { MachineSession machine; // Authored BRA-to-self ROM gives an independently known three-cycle loop. std::vector rom(32768, 0x12); rom[0] = 0x20; rom[1] = 0xfe; rom[0x7ffe] = 0x80; rom[0x7fff] = 0; machine.load(rom, cpu); machine.resume(); for (unsigned multiplier : {1u, 2u, 4u, 2u, 1u}) { MemWrite8(multiplier == 4 ? 1 : 0, 0xff96); MemWrite8(0, multiplier == 1 ? 0xffd8 : 0xffd9); machine.advance(); machine.takeAudio(); // settle a transition within a scanline const auto before = machine.snapshot(); unsigned samples = 0; for (unsigned frame = 0; frame < 60; ++frame) { machine.advance(); samples += machine.takeAudio().size(); } const auto after = machine.snapshot(); const auto cycles = after.cycles - before.cycles; require(after.frames - before.frames == 60 && after.cpu.PC == 0x8000, "Accelerated guest must execute complete frames"); require(std::abs(double(cycles) - 60.0 * clocksPerFrame / 16 * multiplier) <= 3, "Mapped normal/double/turbo switches must change CPU throughput by 1/2/4"); require(samples > 44300 && samples < 44400, "CPU acceleration must not accelerate audio or raster time"); } } } void CheckConfiguredOverclock() { for (auto cpu : {Processor::MC6809, Processor::HD6309}) { MachineSession machine; std::vector rom(32768, 0x12); rom[0] = 0x20; rom[1] = 0xfe; rom[0x7ffe] = 0x80; rom[0x7fff] = 0; machine.load(rom, cpu); machine.resume(); for (unsigned factor : {1u, 3u, 16u, 1u}) { auto config = machine.snapshot().configuration; config.overclock = factor; machine.configure(config); for (unsigned speed : {1u, 2u, 4u}) { MemWrite8(speed == 4 ? 1 : 0, 0xff96); MemWrite8(0, speed == 1 ? 0xffd8 : 0xffd9); machine.advance(); machine.takeAudio(); const auto before = machine.snapshot(); unsigned samples = 0; for (unsigned i = 0; i < 6; ++i) { machine.advance(); samples += machine.takeAudio().size(); } const auto after = machine.snapshot(); // Retained Windows timing uses 1/256 master-clock resolution; // non-power-of-two multipliers quantize the clock divider. const unsigned divider = speed == 1 ? 4096 : 2048 / (factor * (speed / 2)); const auto expected = 6.0 * clocksPerFrame * 256 / divider; require(std::abs(double(after.cycles - before.cycles) - expected) <= 3, "Configured overclock matches Windows guest-gated throughput including turbo"); require(samples >= 4432 && samples <= 4434 && after.frames - before.frames == 6, "Overclock preserves complete video frames and 44.1 kHz guest audio"); } } } }