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.
| 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).
- Single-precision floats. The FPU only does single precision, and Quake uses
doublefar more than it needs to. Built with-fsingle-precision-constant, the math library calls mapped to their float versions, and the hot renderer’sdoublelocals asfloat, it went from 6.4 to 9.3 fps on the CPU alone. Game time staysdouble, which Quake does need. - Profile-guided builds. GCC’s freestanding gcov counts branches on the board and prints the counters over the UART at the end of a timedemo; a script turns them back into
.gcdafiles for the next build. Together with LTO it was worth 5 to 10%. - A freeze that looked like hardware. One demo froze on one bitstream and not on others, for weeks of suspicion about the CPU. It was Quake:
R_EmitEdgekept a stale value that gave a NaN edge step, and the span stepper spun forever. Real-time demo playback means a different machine renders different frames, so the bug came and went with the hardware. -
A deterministic timedemo. Fixed time steps, a fixed random sequence and a hash of every frame turned “it finished” into a frame-by-frame comparison between builds, clocks and boards, and found the 60 MHz problem in part 2.
- Models in the accelerator. Later the accelerator took the models too: each triangle becomes three planes, the skin read in place through the accelerator’s any-width lit mode, and its Gouraud light fitted as a plane into a small lightmap; the rows go as EDGE commands and the depth test keeps them behind the world. Two things were worth knowing. Quake’s per-vertex light is a darkness, not a brightness (colormap row 0 is the brightest), so a model in shadow first came out glowing. And the frame base mustn’t move while the accelerator still writes depths (part 3): Quake crashed within minutes until the frame’s start waited for it.
- The menu and the settings. With the menu up the frame rate fell from about 16 to 6: Quake asks for a full screen update every frame then, so the whole 640 × 360 screen was tiled under the 3D view, and the fade behind the menu looked its colour up for every pixel. Tiling only the border and fading two pixels at a time doubled it. Quake also only wrote its settings on a clean shutdown, which never happens on a board that’s switched off: they’re now saved on leaving the Options menu.
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:
- Light rows become brightness. Each row of the game’s lighting table is fitted, by least squares over the palette, to the one brightness factor that best turns each colour into its remapped one. The accelerator then lights from a small lightmap fitted to the polygon’s corners. It comes within a few percent of the CPU’s picture.
- The accelerator’s fullbright colours. Colours 224 to 255 are never lit (Quake’s lamps and lava). These games use them as ordinary colours, so as the textures load, those texels are moved to the nearest other palette colour: 3% of Descent’s texels.
- Perspective for free. Through the accelerator every face is perspective-correct, whatever the game’s own mapper did.
- The accelerator only takes what it can draw: opaque textures. Transparent walls, sprites, flat-shaded polygons and everything 2D stay on the CPU, exactly as before.
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.
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:
- Textures written at run time. Descent pages textures in from its data file, merges wall overlays into new bitmaps, and expands compressed ones into a cache. Each of those writers now cleans what it wrote from the data cache and tells the accelerator to empty its texel cache, or the accelerator draws stale texels.
- The composing pass reads the whole canvas every frame. Testing 16 pixels with one compare (four words ANDed together, since holes are all ones) and flushing only the display lines it wrote brought it down to about 7 ms at 640 × 360.
- Menus over the game still have holes from the last 3D frame, so they’re composed over a copy of it; menus, briefings and fades without holes stay 8-bit, and the palette fades keep working.
| 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.
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.