PHLEGM / Phlegmulator · Architecture & Delivery Specification

Unified PPU & APU Graphics-Backend Micro-Codes

Pre-implementation design for adding data-driven GPU graphics and optional GPU compute to PHLEGM v9, with shared micro-code primitives, cross-backend portability, deterministic CPU fallbacks, and staged revisions.

Design proposal — not implementedProposed start: v9 r14Reference baseline: v9 r13Offline / self-contained
Reading controls:

1. Proposal and goals

Introduce a single, generic micro-code compilation model with three execution targets: CPU, GPU Graphics, and GPU Compute. The emulated machine's hardware rules stay in .phlegm definitions; generic native primitives and graphics drivers provide efficient execution.

Design decision: The SNES Mode 7 engine remains part of SNES-PPU, not an invented extra support chip. The S-SMP/SPC700 remains a CPU-executed processor; only carefully selected S-DSP computations become candidates for GPU offload.

Data-driven behavior

Registers, mode selection, fixed-point rules, tile interpretation, and DSP scheduling derive from machine/support-chip definitions.

Backend-agnostic programs

The same validated generic intermediate representation (IR) can run through the CPU, OpenGL, D3D11, or Vulkan paths where supported.

Correctness first

Retain an authoritative CPU reference path for debugging, deterministic comparisons, and systems without compatible GPU features.

This document authorizes no source edits by itself. It defines a plan to review before implementation, with incremental, testable deliveries rather than a single all-at-once rewrite.

2. Starting baseline and non-negotiable invariants

The intended starting point is PHLEGM v9 r13 (SNES Mode 7 compile fix), subject to checking whether a newer successful revision exists at implementation time. The proposed first implementation milestone is v9 r14; later revision labels must follow actual completed milestones, not speculative work.

  • Micro-code compilation boundary: compile active machine-specific programs on machine load and when a session restore requires it; do not compile them during developer builds, bootstrap, or distribution packaging.
  • No persistent compiled machine state: invalidate machine-specific compiled programs when changing machines or restoring sessions. Backend shader objects may be cached within the current active machine lifetime only.
  • Native genericity: keep SNES/PET/NES behavior in definitions rather than adding machine-name conditionals to microcode.lib, compute.lib, or gfx_*.lib.
  • CPU fallback: no machine, video mode, or audio device may require a GPU compute shader.
  • Platform contract: maintain Windows 32/64 OpenGL/D3D11 and Linux x86/x86_64 OpenGL/Vulkan target support.
  • Packaging contract: preserve existing bootstrap/compiler UI behavior and FreeBASIC toolchain. Do not ship Python scripts or dependencies. Use canonical forward slashes in definition paths.
  • Hardware/legal contract: do not bundle proprietary SNES firmware; preserve machine-declared external firmware sourcing.
Known regressions are not proof of a performance problem: ActRaiser's opening Mode 7 sequence renders, but the game subsequently becomes stuck/blank. Mystic Quest is blank before its expected opening Mode 7 sequence. GPU offload alone must not be presented as a fix for either issue.

3. Unified execution pipeline

The dispatcher selects an implementation based on the primitive, capability, accuracy contract, and measured performance. A graphics backend is a route to GPU computation; sound is not passed through image presentation.

4. Library and component responsibilities

Module / layerOwnsMust not own
microcode.libShared typed IR, generic operation validation, compilation, dispatch metadata, lifecycle.SNES-specific register meanings or hard-coded SPC700/PPU pipelines.
Proposed compute.libGeneric buffers, compute kernel scheduling interface, capability probes, sync abstractions.SNES DSP voice/envelope semantics.
gfx_opengl.libGLSL graphics/compute translation, resource management, shader compilation in capable contexts.Emulated-machine branching.
gfx_d3d11.libHLSL graphics/compute compilation and device access.Assumptions about every D3D11 feature level supporting compute.
gfx_vulkan.libSPIR-V pipelines, command/compute dispatch, barriers and device lifetime.Undeclared implicit CPU↔GPU synchronization.
Machine .phlegm definitionsChip connections, registers, raster/sample rules, hardware algorithms assembled from generic primitives.Host API calls or proprietary driver-specific shader blobs as the only implementation.
Existing host audio outputPCM queue/ring buffer, device interaction, underrun reporting.Emulated DSP timing semantics.

The proposed compute.lib is a new optional abstraction, not an existing confirmed module. Before edits, inspect the actual r13 library interfaces and reuse existing host/graphics integration where practical. Keep aa.lib's associative-array/FBVM responsibilities unchanged.

5. Definition schema and generic primitive contracts

Extend the definitions with typed micro-code operations and explicit execution constraints. Each operation must describe its inputs/outputs, integer width or sample format, state dependencies, memory effects, precision/rounding, and allowed execution targets.

Primitive familyProposed operationsKey contract
Raster / affineAFFINE_2D, COORD_WRAP, COORD_CLIPExact fixed-point coordinate math, overflow/wrapping behavior and line-local state.
Graphics fetchTILE_FETCH, TEXEL_FETCH, PALETTE_LOOKUPCorrect tilemap layout, palette resolution, bitplane/VRAM interpretation.
Composite / outputLAYER_COMPOSE, COLOR_MATH, RASTER_OUTPUTOrder, priority, transparency, window masks and brightness rules.
Audio sourceAUDIO_BRR_DECODE, AUDIO_SAMPLE_INTERPOLATEHistory samples, filter/interpolation tables, saturation and deterministic arithmetic.
Audio state / mixAUDIO_ENVELOPE_UPDATE, AUDIO_VOICE_MIX, AUDIO_FIR_FILTER, AUDIO_ECHO_FEEDBACKState ordering and history dependencies across samples.
Audio outputAUDIO_SAMPLE_OUTPUTOrdered PCM samples under the emulated DSP clock.
Illustrative, not implemented syntax: Primitive names in this document are design candidates. They must be reconciled with the actual .phlegm grammar, existing compiled CFG execution model, and FreeBASIC interfaces before creating files or claiming that a schema already parses.
Illustrative IR intent (not valid .phlegm syntax):
PROGRAM SNES_PPU_MODE7
  INPUT: raster_register_snapshot, vram_snapshot, cgram_snapshot
  OPS:   AFFINE_2D, COORD_WRAP, TILE_FETCH,
         TEXEL_FETCH, PALETTE_LOOKUP, RASTER_OUTPUT
  MATH:  deterministic fixed-point
  TARGET: CPU_REQUIRED, GPU_GRAPHICS_OPTIONAL
  SYNC:  raster-state-boundary
END

PROGRAM SNES_DSP_SELECTED_KERNEL
  INPUT: dsp_register_snapshot, voice_state, sample_clock
  OPS:   AUDIO_SAMPLE_INTERPOLATE, AUDIO_VOICE_MIX
  TARGET: CPU_REQUIRED, GPU_COMPUTE_OPTIONAL
  SYNC:  ordered-sample-block
END

6. PPU graphics micro-codes & SNES Mode 7

Keep SNES-PPU as one support chip. Its mode selection, register latches, scroll/center coordinates, tilemap/palette rules, pixel priority, and special effects remain in definitions compiled into generic raster operations.

Rendering pipeline

  1. Schedule emulated PPU register, VRAM, CGRAM and OAM writes at the correct emulated time.
  2. Snapshot or segment state at scanline/pixel boundaries where hardware changes affect pixels.
  3. Compile Mode 7 affine/tile fetch into generic primitives, using SNES-accurate fixed-point and overflow behavior.
  4. Execute CPU reference rasterization first; build a GPU Graphics translation after pixel-level tests pass.
  5. Integrate color math, sprites, priority, windows and HDMA-dependent raster effects incrementally.
  6. Present the validated framebuffer; report rendering backend and checksums for comparison.

Mode 7 operations are highly parallelizable per pixel, but mid-frame register writes and HDMA mean a single immutable frame-wide uniform state may be insufficient. Raster state and read-after-write visibility must be modeled explicitly.

Correctness gate: First compare CPU vs GPU framebuffer pixels and register-side effects for fixed test scenes. Only then profile speed. A visually recognizable ActRaiser logo does not by itself establish Mode 7 or full PPU accuracy.

7. APU micro-codes & optional GPU Compute

Keep the S-SMP/SPC700 instruction engine, IPL handshake, APU port communication, timers and CPU↔APU synchronization on the timing-controlled CPU micro-code path. The S-DSP produces roughly 32 kHz audio and is the candidate for selected general-purpose GPU compute workloads.

Candidate kernels

BRR decoding, interpolation, bounded mixing, and selected signal operations after profiling and exactness checks.

CPU-first operations

S-SMP instruction execution, register-side effects, envelope transitions, echo feedback and history-sensitive DSP state until safe equivalence is proved.

Audio path

GPU compute writes generic PCM/result buffers, which the host audio system consumes in sample-clock order. No audio-through-framebuffer path.

Small SNES workloads can be slower on a GPU because dispatch, state transfer, synchronization and readback cost more than the work saved. GPU compute is therefore optional and disabled by default until a tested kernel is both accurate and faster under realistic load.

Do not hide current game stalls: ActRaiser appears to encounter an APU-related polling stall after its logo sequence. Accelerating DSP math will not repair incorrect IPL/port semantics. Preserve and expand diagnostic visibility for that separate issue.

8. Target platform and graphics backend matrix

Build targetRaster accelerationOptional GPU ComputeRequired fallback
Windows 32/64 · OpenGLGL graphics shadersGLSL compute if context supports it, generally OpenGL 4.3+CPU raster and CPU DSP
Windows 32/64 · D3D11HLSL graphics shadersHLSL compute if the device/feature level permitsCPU raster and CPU DSP
Linux x86/x86_64 · OpenGLGL graphics shadersGLSL compute where supportedCPU raster and CPU DSP
Linux x86/x86_64 · VulkanSPIR-V graphics pipelineCompute pipeline/queue only if supportedCPU raster and CPU DSP

Availability is a runtime device capability, not a promise made by the build name. Preserve other existing native backend behavior; do not expand the required target list to Windows/Vulkan or native Linux/D3D11 without a separate scope decision.

9. Runtime compilation, caching and cleanup

  1. Machine load: resolve chipset/support-chip paths and validate active graphics/DSP definitions.
  2. Compile generic IR: produce typed programs, explicit state descriptors, hazard metadata and deterministic CPU executor plans.
  3. Probe backend: query graphics and compute capabilities, supported integer types, buffer limits and synchronization features.
  4. Prepare optional GPU executors: translate eligible IR to backend shader code and compile pipelines once per active definition state—not per frame/sample.
  5. Run: schedule PPU operations by raster state and DSP operations by emulated sample time, dispatching only proved-safe GPU work.
  6. Session restore / machine change: discard old compiled machine-specific IR, shader bindings and buffers as needed; reconstruct from the active definitions.
  7. Backend loss / unsupported feature: switch to CPU at a safe state boundary, keeping emulated time and state coherent.

Any runtime shader cache is session-local to the active machine/definitions; compiled machine micro-codes must not persist across machine loads or restores. Hashes may be logged for reproducibility without persisting compiled machine programs.

10. Timing, state ordering and determinism

DomainDeterministic contractFailure to prevent
PPU rasterApply state changes at the correct cycle/scanline/pixel boundary.Wrong Mode 7 transform or HDMA/window effect within a frame.
CPU ↔ PPUHonor VRAM/CGRAM/OAM access and DMA timing relative to video rendering.Rendering stale video memory or out-of-order writes.
CPU ↔ S-SMPPreserve port latches, handshake state and emulated-clock progression.Infinite startup polling or prematurely acknowledged transfers.
S-DSPRespect sample-clock ordering, voice state, echo history and clipping precision.Changed PCM, broken feedback or audio drift.
GPU schedulerExplicit barriers/fences and bounded transfers; never advance emulated time because a draw/dispatch completed early.Audio/video desynchronization or host-frame-dependent emulation.

Use an integer-exact CPU executor as the parity oracle. GPU code is acceptable only if it meets the chosen strict pixel/sample equivalence target and doesn't alter emulated register behavior. Performance cannot supersede fidelity.

11. Sequential implementation revisions

Each revision is a meaningful delivered milestone with its own new Developer Distribution, Bootstrap Archive, regression notes and logs. Upload completed revision artifacts to the ChatGPT Library without replacing prior revisions. Labels beyond r14 are provisional; assign the next number when a milestone is actually produced.

Planned initial milestone · v9 r14

Shared typed IR + deterministic CPU executors

Inspect the current APIs; specify generic raster/audio operations, validation, state hazards and CPU execution. Keep r13 gameplay behavior unchanged. Exit: native builds where available, IR unit tests, baseline regression results, and preserved fallback behavior.

Next completed revision

GPU Graphics backend bridge + Mode 7 parity

Translate a minimal raster IR subset to OpenGL/D3D11/Vulkan graphics paths on supported targets. Exit: CPU/GPU matching Mode 7 test frames including coordinate wrap, fixed-point cases and raster-state changes.

Subsequent completed revision

PPU raster integration & instrumentation

Broaden GPU rendering to validated PPU operations, expose backend selection and capture per-frame mismatches. Exit: regression matrix passes with CPU fallback and no new rendering artifacts.

Subsequent completed revision

Optional GPU Compute interface + one S-DSP kernel

Add compute.lib only if the audited existing architecture warrants it; prototype one measured DSP compute kernel. Exit: sample/state parity, bounded latency and safe feature negotiation.

Subsequent completed revision

Cross-target qualification & measured defaults

Run each supported OS/architecture/backend combination, failure/fallback tests and real game checks. Enable GPU paths only when independently validated. Exit: a release-grade compatibility matrix with test evidence, known gaps and performance numbers.

Revision discipline: A code skeleton, ZIP integrity test, or successful build on one host is not by itself proof of correctness. Each revision must state exactly which targets were compiled, runtime-tested, and not yet verified.

12. Test and acceptance matrix

Test tierCasesAcceptance gate
Definition/IRSchema errors; unknown opcodes; unsupported types; invalid buffer ranges; hazards; wrong targets.Deterministic diagnostics; fail invalid definitions before machine execution.
CPU referenceMode 7 arithmetic, tile lookup, palette, DSP interpolation/mixing, state restore.Golden-state/known-test-vector parity for specified operations.
GPU parityRender-frame checksum and pixel diffs; PCM checksums and per-sample state diffs.Meets explicit exactness target; mismatch disables optional path.
TimingVBLANK/NMI, raster writes, HDMA/DMA, CPU/APU port handshakes, audio sample timing.No changed externally observable emulation timing.
Host combinationsWindows 32/64 GL/D3D11; Linux x86/x86_64 GL/Vulkan, capable and incapable GPU devices.Build/runtime evidence per target; CPU fallback functions without GPU compute.
RecoveryMachine switching, session restores, lost GPU context/device, shader compile errors.No leaked machine code, corrupt state, hangs or silent blank output.
Game checksActRaiser opening and post-logo sequence; Mystic Quest opening; NES and PET baseline video/audio.No regressions; existing startup failures remain separately reported until fixed.

For a failed game, capture two or more time-separated core dumps plus the console log and CPU/PPU/APU traces. Distinguish a rendering omission, forced blank, and CPU/APU polling stall; all three can produce a black picture for different reasons.

13. Instrumentation and performance evidence

Benchmark after correctness checks, always against the same machine state and host conditions. Record both emulated fidelity and host-side cost.

PPU metrics

Mode, scanline/frame counters, VRAM/CGRAM versions, raster segment count, framebuffer checksum, CPU vs GPU mismatch pixels, PPU time/frame.

APU metrics

S-SMP instruction clock, CPU↔APU ports, DSP sample count, state hash, PCM checksum, dispatch/readback time, underruns and queued samples.

Backend metrics

Selected device/backend, feature checks, IR/program hash, shader compile time, transfer/fence time, device loss, CPU fallback reason.

Report median and worst-case execution cost, frame pacing, A/V latency and power/CPU/GPU utilization where measured. Do not assume GPU acceleration wins just because a shader compiles; the S-DSP may be faster on a modern host CPU.

14. Risks, non-goals and rollback

  • Incomplete hardware models: GPU acceleration doesn't supply missing HDMA, color math, S-SMP IPL, S-DSP or register timing behavior.
  • Precision differences: shader floating point, signed shifts, overflow and rounding can differ from SNES hardware; specify integer semantics and parity tests.
  • Synchronization hazards: CPU writes might race GPU reads; DSP echo introduces history dependencies; use explicit barriers and narrow offload.
  • Performance inversions: tiny compute kernels may lose to dispatch overhead; always measure complete processing cost including transfers.
  • Feature fragmentation: 32-bit hosts and older devices may lack compute or appropriate shader features; an equivalent CPU path is mandatory.
  • Migration risk: maintain a selectable CPU reference path, preferably as the initial default, throughout implementation.
Rollback rule: On shader compilation failure, capability mismatch, state parity failure or runtime device loss, stop dispatching the affected optional GPU program and return to CPU execution at a safe synchronization boundary. Record the reason and do not silently skip raster or DSP work.

15. Pre-implementation review checklist

  1. Confirm r13 is the intended base and inspect the current microcode.lib, PPU/DSP code, build scripts, graphics APIs, and actual definition grammars.
  2. Approve the generic IR operation list and define fixed-point/integer semantics, state hazards and explicit target capabilities.
  3. Define which existing SNES PPU behavior will first be expressed as data-driven programs, rather than blindly rewriting all graphics code.
  4. Choose one constrained, benchmarkable DSP primitive for optional GPU Compute, while keeping S-SMP/ports on the CPU path.
  5. Specify CPU-vs-GPU parity test vectors and the performance measurement procedure.
  6. Implement the first small revision, preserve bootstrap/compiler equivalence, compile targets actually available, and publish test evidence.
  7. Create and link a new sequential revision archive, upload it to Library, then proceed only after reviewing failures and known limitations.
Recommended go/no-go: Proceed with v9 r14 as a generic IR + CPU-reference milestone. Defer GPU activation until correctness and cross-backend parity are demonstrable. Maintain a separate bug track for Mystic Quest's missing opening and ActRaiser's post-logo black screen.

Related documents: PHLEGM-Graphics-Backend-Micro-Codes.html and PHLEGM-APU-GPU-Compute-Micro-Codes.html. This document stands on its own; neither file is required for viewing.