// SPDX-License-Identifier: GPL-3.0-or-later #include "TestMachine.h" #include "../../src/core/VideoHost.h" #include "../../src/core/VideoRegisters.h" #include "../../src/core/FrameRunner.h" #include "../../src/core/tcc1014graphics.h" #include #include using namespace VCCTest; namespace { constexpr unsigned width = 640, height = 480, guard = 16; constexpr unsigned sentinel = 0xdeadbeef; struct GraphicsHost final : Host, VCC::VideoHost { GimeGpu* gpu = nullptr; VCC::VideoState surface; std::vector pixels = std::vector(width * height + 2 * guard, sentinel); bool ntsc = false; VCC::VideoRect active; void ActiveVideoArea(int x,int y,int w,int h) override { active.include(x,y,w,h,surface.WindowSize); } unsigned horizontalFalls = 0, horizontalRises = 0; std::vector> vertical; GraphicsHost() { surface.BitDepth = 3; surface.PTRsurface32 = pixels.data() + guard; surface.SurfacePitch = width; surface.WindowSize = {width, height}; ClearSurface(); } VCC::VideoState& Surface() override { return surface; } void ClearSurface() override { std::fill_n(surface.PTRsurface32, width * height, 0); } bool UseNtscArtifacts() const override { return ntsc; } void VideoWrite(VCC::VideoRegister reg, unsigned int value, unsigned char index) override { VCC::ApplyVideoRegister(*gpu, reg, value, index); } void TimerWrite(VCC::TimerRegister reg, unsigned int value) override { VCC::ApplyTimerRegister(*gpu, reg, value); } unsigned int pixel(unsigned x, unsigned y) const { return pixels[guard + y*width + x]; } void checkGuards() const { require(std::all_of(pixels.begin(), pixels.begin()+guard, [](auto v) { return v == sentinel; }) && std::all_of(pixels.end()-guard, pixels.end(), [](auto v) { return v == sentinel; }), "Renderer stays inside 640x480 surface"); } }; struct GraphicsFixture { GraphicsHost host; Debugger debugger; GimeGpu gpu; explicit GraphicsFixture(bool scheduled = false, bool use6309 = false) : gpu(scheduled ? GimeGpu::SyncFunction([this](int level) { if (level) ++host.horizontalRises; else ++host.horizontalFalls; irq_hs(level); }) : GimeGpu::SyncFunction{}, scheduled ? GimeGpu::SyncFunction([this](int level) { host.vertical.emplace_back(level, VCC::gEvents.GetClocks()); irq_fs(level); }) : GimeGpu::SyncFunction{}) { host.gpu = &gpu; host.use6309 = use6309; VCC::SetMachineHost(&host); VCC::SetVideoHost(&host); VCC::SetCpuDebugHooks(&debugger); VCC::SetCpuMemoryBus(nullptr); VCC::gEvents.Reset(); MC6809Init(); HD6309Init(); require(MmuInit(1) != nullptr, "Graphics fixture RAM/ROM allocation"); host.surface.RamBuffer = Get_mem_pointer(); host.surface.WRamBuffer = reinterpret_cast(Get_mem_pointer()); std::fill_n(Get_mem_pointer(), 0x80000, 0); GimeRegistersReset(); PiaReset(); mc6883_reset(); MC6809Reset(); HD6309Reset(); MC6809SetBreakpoints({}); HD6309SetBreakpoints({}); gpu.GimeReset(); gpu.SetMonitorType(1); gpu.SetDefaultPalette(); } ~GraphicsFixture() { gpu.StopEvents(); VCC::gEvents.Reset(); MmuShutdown(); MC6809SetBreakpoints({}); HD6309SetBreakpoints({}); VCC::SetCpuDebugHooks(nullptr); VCC::SetVideoHost(nullptr); VCC::SetMachineHost(nullptr); } void clockTicks() { VCC::gEvents.SetClocksPerCycle(1); VCC::gEvents.Rebase(); } void renderLine(unsigned line = 0) { host.surface.LineCounter = line; gpu.UpdateScreen32(&host.surface); } void loopProgram() { MemWrite8(0x20, 0x4000); MemWrite8(0xfe, 0x4001); // BRA $4000 } }; } void CheckVideoPixels() { GraphicsFixture f; // 320-pixel, 4-bpp RGB graphics. Pixel pairs are doubled to 640 pixels. f.gpu.SetGimeVmode(0x80); f.gpu.SetGimeVres(0x1e); f.gpu.SetGimePalette(1, 36); // full red f.gpu.SetGimePalette(2, 18); // full green f.gpu.SetGimePalette(3, 9); // full blue f.gpu.SetGimePalette(4, 63); // white Get_mem_pointer()[0] = 0x12; Get_mem_pointer()[1] = 0x34; f.renderLine(); const unsigned expected[] = {0xff0000,0xff0000,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0xffffff,0xffffff}; const auto y = f.gpu.VertCenter * 2; for (unsigned x = 0; x < 8; ++x) require(f.host.pixel(x,y) == expected[x] && f.host.pixel(x,y+1) == expected[x], "RGB pixel byte order, nibble order and 2x scaling"); f.gpu.SetScanLines(1); f.renderLine(); for (unsigned x = 0; x < 8; ++x) require(f.host.pixel(x,y) == expected[x] && f.host.pixel(x,y+1) == 0, "Scanline option leaves alternating row black"); f.gpu.SetScanLines(0); f.gpu.SetGimeBoarderColor(63); f.gpu.SetGimeVres(0x18); // 128 bytes at 1 bpp derives an unsupported 1024-pixel row f.renderLine(10); require(std::all_of(f.host.pixels.begin()+guard, f.host.pixels.end()-guard, [](auto pixel) { return pixel == 0; }), "Unsupported wide layout leaves framebuffer intact"); f.host.checkGuards(); // Explicit raster metadata is independent of green/black content. Both // resolutions and a mixed-width frame are represented without clipping. for (unsigned resolution : {0u, 0x1eu, 0x3eu, 0x5eu}) { f.host.active = {}; f.gpu.SetGimeVmode(0x80); f.gpu.SetGimeVres(resolution); for (unsigned row=0;row(i * 37 + 3); ram[(wrappedStart + i) & mask] = ram[contiguousStart + i] = value; } auto renderedRow = [&](unsigned start) { f.gpu.NewStartofVidram = start; f.renderLine(); const auto* row = f.host.surface.PTRsurface32 + 2 * f.gpu.VertCenter * width; return std::vector(row, row + width); }; require(f.gpu.BoarderColor32 == 0xffffff && f.gpu.BytesperRow == 32 && renderedRow(wrappedStart) == renderedRow(contiguousStart), "PMODE 4 composite row wraps individual RAM reads at the installed capacity"); // 80 columns with attributes: the fourth attribute is the last physical // byte, and the fifth character and attribute must both come from low RAM. f.gpu.SetCompatMode(0); f.gpu.SetGimeVmode(3); f.gpu.SetGimeVres(0x15); require(f.gpu.ExtendedText == 2 && f.gpu.BytesperRow == 80 && renderedRow(wrappedStart) == renderedRow(contiguousStart), "CoCo 3 text and attribute pairs wrap at the installed RAM capacity"); } void CheckRasterTiming() { for (bool use6309 : {false,true}) { GraphicsFixture f(true, use6309); f.loopProgram(); auto execute = use6309 ? HD6309Exec : MC6809Exec; auto first = VCC::RunFrame(f.gpu, execute, 20000); require(first.reason == VCC::FrameStop::Complete && first.cycles == 14994, "First raster frame completes after 263 lines with instruction overshoot"); require(f.host.horizontalFalls == 263 && f.host.horizontalRises == 263, "Raster emits one HSYNC pulse per 912-clock line"); auto second = VCC::RunFrame(f.gpu, execute, 20000); require(second.reason == VCC::FrameStop::Complete && second.cycles == 14991 && f.host.horizontalFalls == 526, "Subsequent frame preserves 239856-clock period"); require(f.host.vertical.size() >= 2 && f.host.vertical[0].first == 0 && f.host.vertical[1].first == 1 && f.host.vertical[1].second - f.host.vertical[0].second == 4*912, "VSYNC pulse lasts four scanlines"); f.host.checkGuards(); } } void CheckRasterPixels() { GraphicsFixture f(true); f.loopProgram(); f.gpu.SetGimeVmode(0x80); f.gpu.SetGimeVres(0x1e); f.gpu.SetGimePalette(1, 36); f.gpu.SetGimePalette(2, 18); f.gpu.SetGimePalette(3, 9); f.gpu.SetGimePalette(4, 63); f.gpu.SetGimeBoarderColor(9); for (unsigned y = 0; y < 192; ++y) for (unsigned x = 0; x < 160; ++x) { const auto color = (x / 20) % 4 + 1; Get_mem_pointer()[y*160+x] = (color << 4) | color; } require(VCC::RunFrame(f.gpu, MC6809Exec, 20000).reason == VCC::FrameStop::Complete, "CPU-driven color-bar frame completes"); const unsigned colors[] = {0xff0000,0x00ff00,0x0000ff,0xffffff}; for (unsigned y = 50; y < 434; ++y) for (unsigned x = 0; x < 640; ++x) require(f.host.pixel(x,y) == colors[(x/80)%4], "Full raster RGB bars preserve placement and pixels"); require(f.host.pixel(0,0) == 0x0000ff && f.host.pixel(639,477) == 0x0000ff, "Raster draws top and bottom border"); // The first scheduled frame established the mode and the red first bar. // A guest palette write is visible to a paused snapshot before HSYNC // consumes it for the live raster. const auto livePixels = f.host.pixels; const auto clock = VCC::gEvents.GetClocks(); const auto lineClock = f.gpu.GetLineStartClock(); const auto hsyncs = f.host.horizontalFalls; f.gpu.SetGimePalette(1, 54); // red + green = yellow require(f.gpu.Palette32Bit[1] == colors[0], "Palette write is queued for the beam"); std::vector snapshot; const auto area = f.gpu.RenderSnapshot32(f.host.surface, snapshot); require(snapshot.size() == width * height && area.x == 0 && area.y == 50 && area.width == width && area.height == 384, "Detached color bars cover the established active raster"); for (unsigned y : {50u, 433u}) { require(snapshot[y * width] == 0xffff00 && snapshot[y * width + 80] == colors[1], "Detached raster applies the queued palette to the whole bar"); } require(f.host.pixels == livePixels && f.gpu.Palette32Bit[1] == colors[0], "Detached render preserves all live pixels and the pending palette write"); require(VCC::gEvents.GetClocks() == clock && f.gpu.GetLineStartClock() == lineClock && f.host.horizontalFalls == hsyncs, "Detached render leaves the scheduler and beam at the stop"); const auto next = VCC::RunFrame(f.gpu, MC6809Exec, 20000); require(next.reason == VCC::FrameStop::Complete && next.cycles == 14991 && f.host.horizontalFalls == hsyncs + 263, "The next scheduled frame retains its normal period"); require(f.gpu.Palette32Bit[1] == 0xffff00 && f.host.pixel(0, 50) == 0xffff00 && f.host.pixel(0, 433) == 0xffff00, "Regular rendering consumes the queued write and redraws the bar"); f.host.checkGuards(); } void CheckGimeTimers() { GraphicsFixture f(true); f.clockTicks(); GimeWrite(0x90, 0x20); GimeWrite(0x95, 1); GimeWrite(0x92, 0x20); GimeWrite(0x91, 0x20); // fast clock; restart at +8, period (1+2)*4 VCC::gEvents.Tick(36); require(!f.host.lines[IS_GIME], "Fast timer respects delayed IRQ and exclusive event boundary"); VCC::gEvents.Tick(1); require(GimeRead(0x92) == 0x20, "Fast timer IRQ occurs after reload and issue delay"); VCC::gEvents.Tick(12); require(GimeRead(0x92) == 0x20, "Short-period timer retains overlapping delayed IRQs"); GimeWrite(0x94, 0); GimeWrite(0x95, 0); GimeWrite(0x94, 0); VCC::gEvents.Tick(100); (void)GimeRead(0x92); VCC::gEvents.Tick(100); require(GimeRead(0x92) == 0, "Zero timer stops retriggering"); // Reset and run the line-clock source, including the initial decrement. VCC::gEvents.Reset(); f.clockTicks(); f.gpu.GimeReset(); GimeRegistersReset(); GimeWrite(0x90, 0x20); GimeWrite(0x95, 1); GimeWrite(0x94, 0); GimeWrite(0x92, 0x20); VCC::gEvents.Tick(2736); require(!f.host.lines[IS_GIME], "Line timer waits for the third decrement and next HSYNC"); VCC::gEvents.Tick(1); require(GimeRead(0x92) == 0x20, "Line-clock timer fires at HSYNC after reaching zero"); } void CheckVideoLifecycle() { GraphicsFixture f(true); f.clockTicks(); f.gpu.SetGimeBoarderColor(63); GimeWrite(0x95, 1); GimeWrite(0x91, 0x20); f.gpu.GimeReset(); require(VCC::gEvents.GetSize() == 4, "GPU reset replaces callbacks instead of duplicating raster/timers"); VCC::gEvents.Tick(913); require(f.host.horizontalFalls == 1, "Repeated GPU reset leaves a single raster clock"); f.gpu.StopEvents(); require(VCC::gEvents.GetSize() == 0, "GPU stop cancels all owned work"); VCC::gEvents.Tick(10000); require(f.host.horizontalFalls == 1, "Stopped GPU produces no further callbacks"); { GimeGpu transient([](int) {}, [](int) {}); transient.GimeReset(); // deliberately rely on lifetime guard, not StopEvents } VCC::gEvents.Tick(10000); require(VCC::gEvents.GetSize() == 0, "Destroyed GPU callbacks expire without dereferencing it"); } void CheckFrameStops() { GraphicsFixture f(true); f.loopProgram(); MC6809SetBreakpoints({0x4000}); auto result = VCC::RunFrame(f.gpu, MC6809Exec, 20000); require(result.reason == VCC::FrameStop::Halted && result.cycles == 0, "Frame runner returns immediately when CPU hits a breakpoint"); MC6809SetBreakpoints({}); f.debugger.halted = false; result = VCC::RunFrame(f.gpu, MC6809Exec, 100); require(result.reason == VCC::FrameStop::Budget && result.cycles == 102, "Frame runner bounds work with final instruction overshoot"); result = VCC::RunFrame(f.gpu, MC6809Exec, 20000); require(result.reason == VCC::FrameStop::Complete && result.cycles == 14892, "Partial frame resumes without restarting the raster"); result = VCC::RunFrame(f.gpu, [](int budget) { return budget; }, 100); require(result.reason == VCC::FrameStop::Stalled, "Frame runner detects a CPU that makes no progress"); }