1. Overview
Yes: the SNES PPU's Mode 7 can be accelerated using graphics-backend micro-codes. Mode 7 maps screen pixels through an affine coordinate transformation, then fetches tilemap/texture data. That pixel-level workload maps well to GPU shaders.
.phlegm definitions. No standalone Mode 7 support chip is needed: Mode 7 belongs to the existing SNES-PPU support chip.GPU acceleration is a performance strategy, not a fix for inaccurate PPU registers, DMA/HDMA, synchronization, or missing rendering features. ActRaiser's partially working intro and Mystic Quest's black screen must still be diagnosed against hardware behavior.
2. Proposed execution architecture
.phlegm definitionsmicrocode.lib front end / graphics IRgfx_opengl.lib / gfx_d3d11.lib / gfx_vulkan.libThe intermediate representation (IR) describes what the rendering program does, not which graphics API it uses. Backend libraries translate this shared IR into supported shader programs and GPU resource operations. A CPU fallback executes equivalent primitives for reference testing and unsupported backend features.
3. Responsibility boundaries
| Layer | Responsible for | Must avoid |
|---|---|---|
.phlegm definitions | Hardware registers, register latches, mode selection, fixed-point and address rules, tile layout, layer priorities, scanline behavior. | Embedding backend API calls. |
microcode.lib | Generic micro-code parsing, validation, IR generation and runtime dispatch contracts. | Hard-coded SNES/Mode 7 instruction branches. |
| Generic graphics primitives | Coordinate transforms, fetches, masks, arithmetic, blend/compose, raster output. | Hardware-specific assumptions. |
gfx_*.lib | Backend shader compilation, resources, uploads, rendering, synchronization and present. | Reinterpreting SNES register semantics. |
| Host / CPU runtime | PPU state updates and timing, machine life cycle, scheduling, diagnostics. | Per-pixel CPU rendering when a verified GPU path is available. |
Existing support chips remain the source of device state. A graphics program consumes snapshots of that state; it does not create a second competing set of SNES registers.
4. Candidate graphics micro-code primitives
These are proposed instruction names for the design, not a claim that PHLEGM already implements them.
AFFINE_2D
Apply a supplied fixed-point 2D transformation matrix.
COORD_WRAP / COORD_CLIP
Resolve out-of-bounds coordinates using definition-selected rules.
TILE_FETCH
Address tilemap entries using a configurable format and stride.
TEXEL_FETCH
Sample tile pixel indices from emulated VRAM data.
PALETTE_LOOKUP
Translate indexed colors using current palette state.
MASK_TEST
Evaluate layer, window or transparency masks.
LAYER_COMPOSE
Combine eligible background and sprite layers in priority order.
COLOR_MATH
Apply definition-selected addition, subtraction, half intensity or related math.
RASTER_OUTPUT
Write final output pixels to the selected render target.
5. SNES Mode 7 as an example
Mode 7 is part of the SNES PPU, controlled by background mode register $2105 and associated Mode 7 registers including $211A–$2120. The PPU definitions specify register widths, write-latch behavior, matrix values, center offsets, scrolling, repeat modes, pixel lookup, and final layer/color composition.
SNES-PPU definition (illustrative pseudocode; not current .phlegm syntax)
on BGMODE == 7:
load matrix = { M7A, M7B, M7C, M7D }
load origin = { M7X, M7Y }
load scroll = { M7HOFS, M7VOFS }
for each visible raster segment:
AFFINE_2D screen_xy -> sample_xy
COORD_WRAP sample_xy using configured repeat rules
TILE_FETCH tilemap, sample_xy -> tile
TEXEL_FETCH tile, sample_xy -> color_index
PALETTE_LOOKUP color_index -> rgb
MASK_TEST visibility/priority
LAYER_COMPOSE backgrounds and sprites
COLOR_MATH configured screen/subscreen operations
RASTER_OUTPUT framebuffer
Conceptually, affine mapping uses a matrix such as x' = A·x + B·y, y' = C·x + D·y. This is not sufficient for cycle-accurate SNES behavior: the hardware's origin, scrolling, signed fixed-point calculations, intermediate truncation, and repeat/out-of-range semantics must also be modeled.
The graphics program should use the existing PPU's VRAM/CGRAM/OAM and registers. It should not require a fictional separate “Mode 7 chip.”
6. Graphics backend mapping
| Graphics library | Target execution | Notes |
|---|---|---|
gfx_opengl.lib | GLSL fragment shaders; compute where supported | Prefer broadly supported raster paths; choose compute only when capabilities permit. |
gfx_d3d11.lib | HLSL pixel shaders or compute shaders | Windows D3D11 backend. |
gfx_vulkan.lib | SPIR-V graphics or compute pipelines | Linux Vulkan backend. |
| CPU reference path | Software execution of equivalent generic primitives | Deterministic validation and fallback, not a graphics API backend. |
Keep backend capability negotiation explicit. The same IR should produce equivalent output regardless of the selected supported backend. Graphics backend support must not change the machine's externally visible behavior.
7. Compilation, caching, and lifecycle
- On machine load: Read that machine's current
.phlegmdefinitions, validate operations, and compile its graphics micro-code into a machine-scoped IR. - On session restore: Rebuild graphics micro-code from the active definitions when needed; restore only emulated hardware state from the session.
- On first backend use: Translate validated IR and compile backend GPU programs; reuse the resulting objects during the same machine session.
- On machine change / reload: Dispose of machine-specific compiled programs and recompile for the newly active definitions.
- During native build / bootstrap / packaging: Do not precompile machine-specific micro-codes.
Compiled micro-codes are non-persistent across machine loads and session restores. Any backend shader object cache must observe that boundary as well; it must never silently reuse a program for changed definitions. An optional, future persistent cache would require a separate explicit architecture decision.
8. Raster timing and accuracy
SNES games can alter PPU registers during a frame. A single whole-frame set of register values can therefore be incorrect. For HDMA and mid-frame changes, capture a sequence of scanline/raster-segment state snapshots, then dispatch corresponding GPU batches.
- Fixed-point fidelity: Reproduce hardware signedness, precision, clipping, wrapping, and intermediate rounding/truncation. Avoid floating-point shortcuts unless proved equivalent.
- Register effects: Match PPU write order and latching semantics, including Mode 7's shared write behavior.
- Temporal accuracy: Preserve VBLANK/HBLANK, NMI and DMA/HDMA timing independently of GPU rendering speed.
- Composition: Mode 7 output still passes through layer priority, windows, backdrop, color math, and forced-blank behavior.
- Synchronization: Ensure CPU writes to emulated VRAM/registers are visible to the correct GPU raster segments.
9. Validation and regression tests
Keep a deterministic CPU reference implementation. Compare software and GPU output frame-by-frame using matching PPU snapshots and pixel-level checksums. Where exact equality is expected, use exact comparisons rather than approximate image similarity.
| Test category | Acceptance target |
|---|---|
| Register tests | Correct write latches, signed matrix math, scroll/center coordinates and wrap behavior. |
| Raster tests | Mode 7 rotation/zoom; visible scanline-dependent changes; consistent forced-blank output. |
| Backend parity | Same reference frames on OpenGL, D3D11 and Vulkan where supported. |
| Lifecycle tests | Recompile on machine load/restore; invalidate programs after definition changes. |
| Performance | Measure CPU time, GPU time, upload cost, shader/IR compile cost and actual FPS separately. |
| Game regressions | ActRaiser and Mystic Quest startup sequences; do not equate absence of a black screen with full accuracy. |
Suggested diagnostic fields: background mode, matrix coefficients, overscan status, raster segment count, active backend, shader compilation status, VRAM/CGRAM revision counters, CPU-versus-GPU frame hash, and per-stage GPU timings.
10. Recommended implementation order
- Define the IR contract. Instruction typing, resource access, format descriptions, integer precision, deterministic validation, and explicit capability requirements.
- Implement a software reference path. Use the same validated IR and establish pixel-perfect Mode 7 tests.
- Move Mode 7-specific rules into
.phlegm. Retain only generic, hardware-neutral primitives in native libraries. - Add OpenGL execution first. Verify fixed-point correctness, uploads, shader outputs, and performance on the current OpenGL runtime.
- Add D3D11 and Vulkan. Match reference output; handle feature variation without changing emulated hardware behavior.
- Add raster-segment dispatch. Support mid-frame register updates, then broader PPU layers and color math.
- Expand beyond SNES. Reuse the graphics IR for NES backgrounds, Commodore displays, sprite engines and other emulated devices where appropriate.
11. Non-goals and cautions
- Not a new SNES support chip: Mode 7 remains functionality of
SNES-PPU. - Not shipped firmware: No proprietary ROM images are needed for the graphics micro-code design.
- Not backend-specific machine behavior: SNES semantics must live in definitions, not GLSL/HLSL/SPIR-V branches that only one backend implements.
- Not automatic speedup: Profile first. Uploads, synchronizations or per-scanline dispatch overhead can erase GPU gains.
- Not an implemented feature: This document proposes an architecture; it does not attest that PHLEGM currently has a shader IR, generic graphics micro-code compiler, or validated GPU Mode 7 path.