m20 · part 4 of 4

Porting notes: four games, one 3D accelerator

Quake was where the 3D accelerator started, so its port was written around it. The other three games were also written for 8-bit screens, palette-based lighting and CPUs that draw every pixel, and two of them had already been ported to m9b that way. Getting them onto the accelerator meant deciding, game by game, how much of the renderer to change.

Quake
Tomb Raider (TRX)
Game Port of Screen 3D accelerator CPU only The accelerator draws
Quake (1996, shareware) quakegeneric 640 × 360 15 fps (timedemo) 3.7 the world’s surfaces, lit, and the models
Tomb Raider (1996) TRX, the decompiled original 640 × 360 13 fps (Vilcabamba timedemo), 20 (Lost Valley) 3.4 opaque textured faces, depth-tested
Descent (1995, shareware) D1X Rebirth 0.40, from m9b’s port 640 × 360 21 fps (level 1, at 54 MHz) 6 opaque wall and robot polygons
System Shock (1994) Shockolate, from m9b’s port 640 × 480 18 fps (level 1, at 54 MHz) 5.2 walls, floors, ceilings

What changed under the games

m20 is m9b with a faster CPU, an FPU, the 3D accelerator, and a different picture: 1280 × 720. The games’ screens are spread over it by the line table, rows repeated, columns doubled by the hardware.

Every port runs 640 pixels wide. That was the target from the start: m9b showed these games at their original 320 × 200, and m20 should look clearly different. The line table’s horizontal doubling puts a 640-pixel, 16-bit line across the 1280 columns, so 640 × 360 maps onto the screen at exactly 2 × 2, and the accelerator is what makes four times m9b’s pixels affordable.

Everything else, the runtime, the SD card, USB input, pre-rendered music and sound mixing in the audio interrupt, is as on m9b (m9b part 6).

Quake: floats, profiles and a NaN

Quake is quakegeneric (id’s GPL source with a small platform layer) and the 3D accelerator’s driver: per surface, its float gradients become the accelerator’s fixed-point planes, and per span, one command (part 3).

At 640 × 360 the frame buffer is drawn straight into one of two display buffers, with sound from Quake’s own mixer at 11 kHz, resampled to the hardware’s 48 kHz in the audio interrupt.

Three games written for 8-bit screens

Tomb Raider, Descent and System Shock draw 8-bit pixels and light them by palette remapping: a 2D table maps (light level, colour) to a darker colour of the same palette. The accelerator writes RGB565 and lights by multiplying the colour. There were two ways to bridge that: change the accelerator to write 8-bit pixels through a lighting table, or change the games. I left the hardware alone, and each port meets the accelerator halfway in its own way. A few things were the same in all three:

Tomb Raider: the decompiled original

Tomb Raider started as OpenLara’s GBA engine: fixed point, small, its levels repacked, and through the accelerator it played the Caves at 27 fps at 640 × 360. But that engine only enables the first four levels, most enemies from Vilcabamba on have no AI in it, and it had no sound on m20. So the port moved to TRX (formerly Tomb1Main), the community’s full decompilation of the original game, at its first standalone build: the game’s own logic, all its levels, and its sound.

TRX draws with OpenGL, so the port puts a software renderer in its place, written without doubles for the single-precision FPU, and a platform layer for video, sound, keys and files. The opaque textured faces go to the 3D accelerator, which maps them with perspective, lights them per pixel from a small lightmap, tests depth and walks their edges itself: the depth test and the EDGE command (part 3) came from this port. The CPU draws the rest, transparent faces, sprites, shadows and the 2D, in the same order. The levels’ 256-wide texture pages are tiled for the accelerator’s texel cache (in OpenLara they had missed 10% of texels, the one game where the accelerator was the limit), and the faces under water use the accelerator’s second palette.

TRX at 640 × 360, timedemos 3D accelerator CPU only
the title 29.8 fps  
City of Vilcabamba 12.7 fps 3.4 fps
Lost Valley 20.2 fps  

Descent: holes in the canvas

D1X’s whole 2D library is 8-bit: fonts, bitmaps, the HUD, menus, sprites. Converting it to 16 bits would be a large port of its own. Instead, the accelerator draws into the 16-bit frame on the screen, and the CPU keeps drawing everything else into the 8-bit canvas as before, with one change: wherever the accelerator draws a span, the canvas gets holes, the transparent colour, which nothing else draws.

The CPU's canvas with holes where the accelerator drew, the accelerator's frame, and the composed result
Whichever drew a pixel last wins: a CPU pixel drawn after an accelerator span overwrites the hole, an accelerator span drawn after a CPU pixel makes a hole over it.

When the frame is done, the canvas’s pixels that aren’t holes go over the accelerator’s, through the palette. The CPU never waits for the accelerator until then. Descent’s texture mapper keeps doing its edge walk, a run of rows at a time, and hands each run to the accelerator as an EDGE command, so the accelerator covers exactly the pixels the CPU would have (checked against the mapper’s own rows in the PC build).

That was the first version. Punching holes under every span cost CPU time, so now the canvas starts each frame all holes, and what the CPU draws in the view is recorded as up to eight boxes. When an accelerator polygon comes over one, the accelerator finishes first, the CPU’s pixels in the box go onto the accelerator’s frame and are left as holes, and the accelerator carries on: the back-to-front order holds without touching the canvas under every span.

The details that took time:

Descent, level 1 (54 MHz) 3D accelerator CPU only
640 × 360 21.1 fps 6.1 fps

The accelerator is busy only about 30% of the time, with under 1% texel misses: Descent’s 64-wide textures are the best case for its cache. The rest is CPU time: composing, the holes, each polygon’s setup, and the game itself.

System Shock: an SVGA mode, and not enough memory

On m9b, System Shock ran at its 320 × 200, where its 3D view is a small window inside the interface. The game had an official SVGA mode in 1994, and Shockolate kept it: the m9b port had simply chosen 320 × 200. At 640 × 480, with the high-resolution interface art, the CPU manages 5.2 fps, and its three texture mappers take 71% of the time. That’s exactly the accelerator’s job.

The design is Descent’s: every 3D texture map in Shock goes through one function after clipping, and that’s where the accelerator takes the polygons whose texture is one of the level’s wall, floor or ceiling textures. The port does its own scan conversion, and the first version forgot the 2D clip rectangle: the polygons reach under the frame around the view, and the accelerator painted holes over the interface.

Then the frame art went black for a different reason: memory. System Shock was already near the end of its heap, and three 16-bit display buffers of 614 KB each took 900 KB more than the 8-bit ones. Resources quietly failed to load. The answer was a feature of the hardware nobody had needed before: every line of the line table can be 8-bit or 16-bit, from anywhere in memory.

System Shock's line table: interface rows from the game's 8-bit screen, view rows from 16-bit display buffers
The rows the 3D view covers come from small RGB565 buffers; the rows above and below are shown straight from the game's own 8-bit screen.

The display buffers now hold only the view’s 259 rows. The interface rows above and below are shown straight from the game’s own screen, which rarely changes (and may tear for a frame when it does), and the interface columns beside the view are converted only when a row’s checksum says they changed. Three buffers fit again, in 93 KB more than the 8-bit ones.

System Shock, level 1 (54 MHz) fps
640 × 480, the CPU 5.2
640 × 480, the 3D accelerator 10.2

Later the same room, with the on-line help off (its pick-up rendering costs about 2.5 fps), went from 12.9 to 18.1 fps in steps: Descent’s boxes instead of holes under every span, the composing done in one pass where the game copies its view to the screen, the RSD bitmaps unpacked once, boxes composed only under accelerator polygons, and the rows as EDGE commands.

Shock is where the accelerator’s cache shows its limits: its 128 × 128 floor textures, seen at an angle, miss on up to half the pixels, while the walls its vertical wall mapper draws miss on 7%. Giving the accelerator the 64 × 64 versions halved the misses but gained only 3%, because, again, the CPU is the limit: each polygon’s setup, the composing, and the game’s on-screen help scanning every object every frame.

How the ports were tested

As on m9b, everything runs from scripts over the serial port: upload a build, press keys at set times, take screenshots, read the frame rate, the accelerator’s counters and a profile every few seconds, and finally reset the board quietly. That last part matters on the 20K: its BL616 USB bridge hangs if the board keeps printing when nobody is reading, and only a replug brings it back.

Writing the games’ data to the SD card in the board, without taking it out, found another BL616 quirk: a file whose last 128 KB window held between 5 and 63 blocks wedged the bridge on its last frame, every time. Padding such windows to the full 64 frames fixed it. With two boards on one PC (they report the same USB serial number), every tool is pinned to a board by its USB port.

Each port also builds as a 32-bit PC program over the accelerator’s emulator (part 3), with the board’s frame and heap sizes: scripted input, screenshots, and checks for what the hardware would draw differently. Quake’s glowing models and two Descent rows one pixel past the bitmap were found there, before any board saw them.