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.
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, orgfx_*.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.
3. Unified execution pipeline
*.phlegm definitionsmicrocode.lib — parse / validate / compile typed generic IRThe 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 / layer | Owns | Must not own |
|---|---|---|
microcode.lib | Shared typed IR, generic operation validation, compilation, dispatch metadata, lifecycle. | SNES-specific register meanings or hard-coded SPC700/PPU pipelines. |
Proposed compute.lib | Generic buffers, compute kernel scheduling interface, capability probes, sync abstractions. | SNES DSP voice/envelope semantics. |
gfx_opengl.lib | GLSL graphics/compute translation, resource management, shader compilation in capable contexts. | Emulated-machine branching. |
gfx_d3d11.lib | HLSL graphics/compute compilation and device access. | Assumptions about every D3D11 feature level supporting compute. |
gfx_vulkan.lib | SPIR-V pipelines, command/compute dispatch, barriers and device lifetime. | Undeclared implicit CPU↔GPU synchronization. |
Machine .phlegm definitions | Chip 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 output | PCM 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 family | Proposed operations | Key contract |
|---|---|---|
| Raster / affine | AFFINE_2D, COORD_WRAP, COORD_CLIP | Exact fixed-point coordinate math, overflow/wrapping behavior and line-local state. |
| Graphics fetch | TILE_FETCH, TEXEL_FETCH, PALETTE_LOOKUP | Correct tilemap layout, palette resolution, bitplane/VRAM interpretation. |
| Composite / output | LAYER_COMPOSE, COLOR_MATH, RASTER_OUTPUT | Order, priority, transparency, window masks and brightness rules. |
| Audio source | AUDIO_BRR_DECODE, AUDIO_SAMPLE_INTERPOLATE | History samples, filter/interpolation tables, saturation and deterministic arithmetic. |
| Audio state / mix | AUDIO_ENVELOPE_UPDATE, AUDIO_VOICE_MIX, AUDIO_FIR_FILTER, AUDIO_ECHO_FEEDBACK | State ordering and history dependencies across samples. |
| Audio output | AUDIO_SAMPLE_OUTPUT | Ordered PCM samples under the emulated DSP clock. |
.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
- Schedule emulated PPU register, VRAM, CGRAM and OAM writes at the correct emulated time.
- Snapshot or segment state at scanline/pixel boundaries where hardware changes affect pixels.
- Compile Mode 7 affine/tile fetch into generic primitives, using SNES-accurate fixed-point and overflow behavior.
- Execute CPU reference rasterization first; build a GPU Graphics translation after pixel-level tests pass.
- Integrate color math, sprites, priority, windows and HDMA-dependent raster effects incrementally.
- 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.
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.
8. Target platform and graphics backend matrix
| Build target | Raster acceleration | Optional GPU Compute | Required fallback |
|---|---|---|---|
| Windows 32/64 · OpenGL | GL graphics shaders | GLSL compute if context supports it, generally OpenGL 4.3+ | CPU raster and CPU DSP |
| Windows 32/64 · D3D11 | HLSL graphics shaders | HLSL compute if the device/feature level permits | CPU raster and CPU DSP |
| Linux x86/x86_64 · OpenGL | GL graphics shaders | GLSL compute where supported | CPU raster and CPU DSP |
| Linux x86/x86_64 · Vulkan | SPIR-V graphics pipeline | Compute pipeline/queue only if supported | CPU 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
- Machine load: resolve chipset/support-chip paths and validate active graphics/DSP definitions.
- Compile generic IR: produce typed programs, explicit state descriptors, hazard metadata and deterministic CPU executor plans.
- Probe backend: query graphics and compute capabilities, supported integer types, buffer limits and synchronization features.
- Prepare optional GPU executors: translate eligible IR to backend shader code and compile pipelines once per active definition state—not per frame/sample.
- Run: schedule PPU operations by raster state and DSP operations by emulated sample time, dispatching only proved-safe GPU work.
- Session restore / machine change: discard old compiled machine-specific IR, shader bindings and buffers as needed; reconstruct from the active definitions.
- 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
| Domain | Deterministic contract | Failure to prevent |
|---|---|---|
| PPU raster | Apply state changes at the correct cycle/scanline/pixel boundary. | Wrong Mode 7 transform or HDMA/window effect within a frame. |
| CPU ↔ PPU | Honor VRAM/CGRAM/OAM access and DMA timing relative to video rendering. | Rendering stale video memory or out-of-order writes. |
| CPU ↔ S-SMP | Preserve port latches, handshake state and emulated-clock progression. | Infinite startup polling or prematurely acknowledged transfers. |
| S-DSP | Respect sample-clock ordering, voice state, echo history and clipping precision. | Changed PCM, broken feedback or audio drift. |
| GPU scheduler | Explicit 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.
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.
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.
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.
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.
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.
12. Test and acceptance matrix
| Test tier | Cases | Acceptance gate |
|---|---|---|
| Definition/IR | Schema errors; unknown opcodes; unsupported types; invalid buffer ranges; hazards; wrong targets. | Deterministic diagnostics; fail invalid definitions before machine execution. |
| CPU reference | Mode 7 arithmetic, tile lookup, palette, DSP interpolation/mixing, state restore. | Golden-state/known-test-vector parity for specified operations. |
| GPU parity | Render-frame checksum and pixel diffs; PCM checksums and per-sample state diffs. | Meets explicit exactness target; mismatch disables optional path. |
| Timing | VBLANK/NMI, raster writes, HDMA/DMA, CPU/APU port handshakes, audio sample timing. | No changed externally observable emulation timing. |
| Host combinations | Windows 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. |
| Recovery | Machine switching, session restores, lost GPU context/device, shader compile errors. | No leaked machine code, corrupt state, hangs or silent blank output. |
| Game checks | ActRaiser 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.
15. Pre-implementation review checklist
- Confirm r13 is the intended base and inspect the current
microcode.lib, PPU/DSP code, build scripts, graphics APIs, and actual definition grammars. - Approve the generic IR operation list and define fixed-point/integer semantics, state hazards and explicit target capabilities.
- Define which existing SNES PPU behavior will first be expressed as data-driven programs, rather than blindly rewriting all graphics code.
- Choose one constrained, benchmarkable DSP primitive for optional GPU Compute, while keeping S-SMP/ports on the CPU path.
- Specify CPU-vs-GPU parity test vectors and the performance measurement procedure.
- Implement the first small revision, preserve bootstrap/compiler equivalence, compile targets actually available, and publish test evidence.
- Create and link a new sequential revision archive, upload it to Library, then proceed only after reviewing failures and known limitations.
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.