psxgpu

26 definitions in 6 files, 26 with an encoding diagram. src/isa/psxgpu

Files

FileDefinitionsOtherWhat
package.lisp00the ISA.PSXGPU package: the PS1's GPU (GP0/GP1 commands) described as an ISA.
state.lisp015the PS1 GPU as a machine: VRAM, GPUSTAT, the command FIFO, transfers, the display.
defgpu.lisp06GP0 as an ISA: DEFGP0, the words each command takes, the executor.
raster.lisp023the reference rasterizer: PS1 polygons, lines, rectangles, fills and copies into VRAM.
gp0.lisp1515the GP0 commands: drawing, VRAM transfers, the drawing environment; the GP0 port.
gp1.lisp116the GP1 commands (display control) as an ISA, and the GPU's reset.

README

src/isa/psxgpu: the PS1's GPU, described as an ISA

The PlayStation's GPU (CXD8514Q/CXD8561Q) takes 32-bit words on two ports: GP0 (1F801810h), rendering and VRAM transfer commands of 1 to 12 words, and GP1 (1F801814h), one-word display control commands ([PSXSPX] "Graphics Processing Unit (GPU)"). Both command sets are described as ISAs with the kit's defisa/definsn (src/core/isa.lisp), their semantics as effects on the abstract graphics state (src/core/gfx.lisp) where it has the concept, and on the GPU's own 1 MiB VRAM.

Package ISA.PSXGPU (uses CL and CORE). Pair name: psxgpu.

FileRole
package.lispthe package and its exports
state.lispthe machine (gpu): VRAM, GPUSTAT, the command FIFO, transfers, the display registers; gpu-status, the display's size and format, display-rgb, gpu-vram-hash
defgpu.lispdefisa gp0, defgp0 (a command's first-word encoding and the number of words it takes), the generated gp0-words
raster.lispthe reference rasterizer: triangles, lines, rectangles, fills, copies, pixel by pixel into VRAM
gp0.lispthe GP0 commands, gp0-execute, the GP0 port (gp0-write, polylines, CPU-to-VRAM data), GPUREAD (gpu-read)
gp1.lispdefisa gp1, the GP1 commands, gpu-reset, gp1-write

The command form

(defgp0 polygon "001 iip:1 vtx:1 tme:1 abe:1 tge:1 color:24"
    (:words (+ 1 (* (if (= vtx 1) 4 3) (+ 1 tme)) (* iip (if (= vtx 1) 3 2))))
  (:doc "GP0(20h-3Fh) Render Polygon ...")
  ...)                                   ; (word i): parameter word i, CPU the GPU, GFX its state

(defgp0 draw-mode "11100001 .......... flip:2 tbx2:1 dfe:1 dtd:1 tpf:2 abr:2 tby:1 tbx:4" ()
  (gpu-set-texpage cpu insn) ...)        ; GP0(E1h): the abstract state's texture 0 and combine

The encoding is the first word's (MSB first, as every ISA's); :words is a form over its fields. gp0-write collects that many words and runs the command (gp0-execute, a decision tree like a CPU's decoder). Words that match no encoding are the NOPs of "GP0(04h..1Eh,E0h,E7h..EFh)". Polylines (after their first vertex) and CPU-to-VRAM data stream in two modes of the port: each vertex draws a segment until the 5xxx5xxxh terminator; each data word stores two pixels.

The abstract graphics state

Every command says what it means in the core's abstract graphics state (docs/gfx.md, src/core/gfx.lisp), the one model of rasterization the RSX and the GS write too; the reference rasterizer draws what the state says, and so does a presenter (src/present):

PS1Abstract state
drawing area (E3h, E4h)scissor 0 (inclusive corners to x, y, width, height)
drawing offset (E5h)coordinates offset-x, offset-y (already added to the vertices)
texture page (E1h, a polygon's)texture 0: address (64-halfword and 256-line steps), format index4, index8 or a1b5g5r5, linear, pitch 2048, 256 x 256; zero-discard (texel 0000h)
CLUT (a textured primitive's)texture 0 clut-address, clut-format a1b5g5r5
texture window (E2h)sampler 0: region-repeat, region-u0 = NOT mask, region-u1 = offset AND mask
semi-transparency (E1h bits 5-6)combine: ((A - B) * C >> 7) + D on 5-bit channels; B/2+F/2, B+F, B-F, B+F/4 are its cases; select source-alpha-msb for textured primitives (the texel's bit 15)
mask set and check (E6h)mask-bit force, test (pass 0)
dithering (E1h bit 9)raster dither for a primitive that is shaded or modulated; dither-matrix the 4x4 matrix
VRAMcolor-target 0 a1b5g5r5, attachment 0 at 0, pitch 2048, linear
a primitiveimmediate vertices (x y with the offset, colour, u v), primitive topology (triangle list; a quad a strip; line list; rectangle-list of two corners), texture 0 enable and function (decal raw, modulate), raster shade-model, combine enable; then gfx-draw

gpu-primitive writes a primitive's part, gpu-init-gfx (at GP1(00h)) what never changes. GPUSTAT keeps the bits that are not drawing state (drawing to the display area, texture disable); GP1(10h) reads the texture window back as written (gpu-window).

The reference rasterizer (raster.lisp)

Every drawing command draws at once into VRAM, by the rules of [PSXSPX] "GPU Render Polygon/Line/Rectangle Commands" and "GPU Rendering Attributes": top-left fill (right and bottom edges excluded), no draw past 1023x511 extents, quads as triangles 1-2-3 and 2-3-4, nearest texels through the texture window and the 4/8-bit CLUT or 15-bit direct, texel 0000h transparent, modulation texel x colour / 128, the 4x4 dither matrix on shaded and modulated pixels, the four semi-transparency modes on 5-bit channels (textured pixels only with bit 15), the mask bits, lines with both ends. Colours and texture coordinates are interpolated over the triangle's plane in 20.12 fixed point, as [XPS1EMU]'s gpu_raster.c does: the two compare exactly (test psxgpu_rastdiff). The hardware's own interpolator differs from both in the last bit at places (not modelled).

Fills ignore the drawing area, offset and mask; copies and CPU-to-VRAM transfers honour the mask bits and wrap at VRAM's edges ("Wrapping", "Masking for COPY Commands parameters"). xps1emu clips copies at the edges and does not apply the mask bits to them: the differential test avoids those cases.

GPUSTAT and GP1

gpu-stat keeps GPUSTAT's stored bits; gpu-status adds the computed ones: 26 (ready for a command) and 28 (ready for a DMA block) always set between commands (commands complete at once), 27 while a VRAM-to-CPU transfer has data, 25 by the DMA direction (GP1(04h)), 24 IRQ1 (GP0(1Fh) to GP1(02h)). The machine adds 31 (odd line) and 13 (interlace field) from its video timing (src/sys/ps1/bus.lisp). GP1(05h) counts a change of the display start as a flip (gpu-flips). GP1(10h) answers 2-5 (texture window, drawing area, offset) and 7 (version 2).

Tests (tests/isa/psxgpu)

TestWhat
psxgpu_cmdhand-made cases: transfers (wrapping, the download's packing), fills (masking, rounding), copies with the mask bits, GPUSTAT's bits, GP1(10h), display modes, IRQ1, the words of every command
psxgpu_rastdiff, lower_psxgpu_rastdiffrandom command streams (every polygon, line and rectangle flag; environment commands; fills; copies) from rastdiff.c, run on xps1emu's GPU built on the host from XSPE_XPS1EMU_DIR and through gp0-write; all of VRAM compared at checkpoints. Larger runs: rastdiff N SEED EVERY (2000 commands over five seeds, a checkpoint after each, match)

Not described yet

The GPU's timing (commands take no time; GPUSTAT's busy bits are never set), the texture cache (GP0(01h) does nothing, so self-modifying textures show at once), interlaced drawing (GP1(08h) bit 5 does not skip lines), 24-bit drawing, the v2 GPU's 2 MiB VRAM and texture flips.


Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.