TMS9118 VDP · Part 2 · latest

How the title crawl works

A detailed look at the TMS9118 title crawl: Graphics II set up as a bitmap, the ground-plane geometry and the tables precomputed from it, how one subtraction per row scrolls the text and fixed-point sampling scales it, and the C++ and AVR assembly that feed the VDP 20 times a second.

  • Published
  • Worked Nov 24, 2026
  • Claude Opus 5.5
  • tms9918aperspectivefixed-pointavrdemos

The brief, and why it’s hard

Me

The brief was demos that show off the VDP’s capabilities, starting with a perspective title crawl in the style of the Star Wars opening. This post explains how that crawl works: the VDP registers and commands it uses, the C++ the Nano runs, and above all how the scroll and the scale are achieved, and the math behind them.

The TMS9118 is a tile machine. It has no bitmap mode as such, no scaling and no hardware scrolling, and everything it shows is written through one byte-wide port. The display and the CPU share its video RAM, so its data manual caps how often the CPU may access it: once every 8 µs while Graphics II draws the active picture, and every 2 µs while the display is blanked or during the 4.3 ms of vertical retrace (TMS9118 data manual, p. 4-4).

Claude

So the AVR computes every pixel of the crawl. Each update rewrites 3,184 bytes of the picture. At 8 µs a byte, that alone is 25.5 ms, longer than a 16.7 ms frame, so the crawl updates every third frame: 20 times a second. The intro, the title and the crawl’s words are original, about this board’s own bring-up.

Graphics II as a bitmap

Me

Start with the VDP: the registers and the commands.

How does a CPU talk to a TMS9118?

The VDP has one 8-bit data port, and a MODE pin that says what each byte means. The CPU latches a byte in by pulsing ~CSW.

  • MODE high: the byte goes to the control port.
    • A register write is two bytes: the value, then 0x80 plus the register number.
    • A VRAM write address is also two bytes: its low 8 bits, then 0x40 plus its top 6 bits.
  • MODE low: the byte is VRAM data. It’s written at the current address, and the address then increments by itself.

The registers can’t be read back; only a status register can (TMS9918A data manual, Table 2-1; Programmer’s Guide, Tables 4-1 to 4-4).

Claude

The crawl puts the VDP in Graphics II, then turns it into a 256×192 bitmap. The registers are TI’s Graphics II example (Programmer’s Guide, Table 6-2), with R1 and R7 changed:

Register Value Meaning
R0 02 M3 set: Graphics II
R1 E0 / A0 16K, display on (or blanked), frame interrupt on
R2 0E Name table at 0x3800
R3 FF Colour table at 0x2000 (only its top bit counts; the rest must be 1s)
R4 03 Pattern table at 0x0000
R5, R6 76, 03 Sprites at 0x3B00 and 0x1800, switched off by a terminator
R7 01 Black backdrop

Graphics II splits the screen into thirds, each with its own 256 patterns. Filling the name table with 0–255 in each third gives all 768 tiles a pattern of their own, so the pattern table becomes a bitmap.

Claude

That bitmap’s layout decides the drawing order. Pixel row l of the tile at tile row tr and column c is at:

address = tr × 256 + c × 8 + l

So a tile row is 256 consecutive bytes, running column by column, with each tile’s 8 rows together. The crawl sets the write address once per tile row and then streams bytes. That means all 8 screen rows of a tile row must be ready before its first byte goes out.

There is one colour byte per 8×1 strip. They are all written once, when the crawl starts, as dark yellow on black. After that, every update writes only patterns. Setup writes about 14 KB of tables with the display blanked, at 2 µs a byte.

The geometry: where the scale comes from

Me

How is the scale computed, and what is the math behind it?

Why does a receding plane scale like this?

Picture an eye at height H above a flat plane, looking at a screen a distance f in front of it. A point on the plane at distance D projects onto the screen h = f·H/D below eye level, the vanishing line. So a screen row h below the vanishing line shows the plane at distance D = f·H/h.

Two scales follow:

  • Across: anything at distance D appears at f/D times its size, which is proportional to h.
  • Down: one screen row spans f·H/h² of the plane, so one unit of the plane covers h²/(f·H) screen rows, proportional to h².

Near the bottom, the text is stretched tall. Near the vanishing line, it’s squashed flat. How far back a row looks is the sum of what every row below it covers. Integrating 1/h² gives a depth proportional to 1/h − 1/h_b, measured from the bottom row, h_b.

Claude

The generator, scripts/make_crawl.py, evaluates exactly that for each crawl row y, from 40 to 191, in screen pixels and source rows:

h   = y + 0.5 + 16        rows below the vanishing line, which is 16 px above the screen
h_b = 207.5               the bottom row
f   = 0.6 × h_b = 124.5
sx  = 1.45 × h / h_b      screen px per source px, across
sy  = sx × h / f          screen rows per source row, down
D   = (h_b × f / 1.45) × (1/h − 1/h_b)  =  17,816 × (1/h − 1/207.5)

The bottom row is drawn at 1.45× across. D counts source rows back from the bottom row, and it’s the integral of 1/sy up the screen.

Side view of the geometry: rays from an eye at the vanishing line to equally spaced points on the text plane cross a vertical screen at heights that bunch together towards eye level

Claude

Some real rows from the generator:

Row y sx (across) sy (down) D (source rows back) Step du Rows ORed
40 0.39 0.18 229.5 2.53 4
60 0.53 0.33 147.0 1.87 3
90 0.74 0.64 81.4 1.34 2
120 0.95 1.05 44.7 1.05 1
150 1.16 1.56 21.1 0.86 1
191 1.45 2.42 0.0 0.69 1

The far end of the crawl is 229.5 source rows back: about 23 lines of text are in view at once. At the near edge, one source row covers 2.4 screen rows.

The crawl's footprint on the 256×192 screen, drawn from the generator's tables: a trapezoid narrowing towards a vanishing point 16 px above the screen, with alternate lines of text shaded as bands that thin towards the top, and a stepped outline of the tiles redrawn every update

Claude

I chose the parameters with a Python prototype that drew the geometry with the real font, in variants side by side. A focal length equal to the bottom row’s distance gives square letters at the bottom, but they flatten to unreadable slivers by mid-screen. At 0.6 of that distance, the near letters stretch to 2.4 screen rows per source row. In exchange, the text stays readable much further up.

Three prototype renders of the crawl at the same scroll position, with different vanishing lines and focal lengths; the middle one, with the vanishing line 16 px above the screen and a focal length of 0.6, became the demo

Scroll: one subtraction per row

Me

How is the scroll achieved?

Claude

D(y) never changes, so the generator stores it for every row, and scrolling is one subtraction: row y shows source row ⌊scroll − D(y)⌋. As scroll grows, each source row moves to rows with a larger D, higher up the screen, so the text recedes. Here is how draw_crawl sets up line l of tile row tr:

const uint8_t i = static_cast<uint8_t>(tr * 8 + l - crawl::kTop);  // crawl row 0..151
CrawlLine& cl = g_crawl_lines[l];
const int16_t first = static_cast<int16_t>((scroll_q8 - vdp_pgm_word(&crawl::kDepth[i])) >> 8);
const uint8_t flags = vdp_pgm_byte(&crawl::kFlags[i]);
cl.buf = g_row_cache.get(crawl::kSrc, crawl::kSrcBytes, crawl::kSrcRows, first, flags, x0, crawl::kBlank);
cl.u = vdp_pgm_word(&crawl::kU0[i]);
cl.du = vdp_pgm_word(&crawl::kDu[i]);
cl.mask = vdp_pgm_byte(&crawl::kMask[i]);

Each row’s perspective is a table lookup: the AVR subtracts, shifts and reads flash.

Every one of these tables holds 8.8 fixed point: the value times 256, in 16 bits. The high byte is the integer part, and the low byte is the fraction. Shifting right by 8 drops the fraction, and two 8.8 values add as ordinary integers.

Claude

The scroll position follows the clock, not the update count, so the crawl moves at the same speed however long an update takes:

const uint32_t ms = (start - t0) / 1000;
const int32_t scroll = crawl_cfg::kStartScroll * 256L +
                       static_cast<int32_t>(ms * crawl_cfg::kCrawlRowsPerSecQ8 / 1000);

That’s scroll = −2 + 8t source rows, with t in seconds, starting with the text just below the bottom edge. Multiplied by sy, 8 source rows a second is 19.3 screen pixels a second at the bottom, about 1 px per update, and 1.4 px a second at the far end. The crawl ends at scroll 521. That’s its 290 source rows, plus the 230 the last row needs to pass the top, plus one.

Scale: sampling at a fixed-point step

Claude

Across, each screen row steps through its source row. The step is du = 1/sx source pixels per screen pixel, in 8.8. The source is 168 pixels wide, and is unpacked to one byte per pixel (0x00 or 0xFF) at entry 44 of a 256-byte buffer, so its centre sits at entry 128. Each row’s first sample is placed so that screen x = 128 lands on the source’s centre:

u0 = (8·c0 + 0.5 − 128) × du + 128

Here c0 is the first tile column the row’s tile row redraws. Pixel k of the row then samples entry ⌊u0 + k·du⌋. Samples past either edge of the text read the zeros around it, so the trapezoid’s slanted edges come for free.

Claude

The buffer is aligned to a 256-byte page, so a sample’s address is the page in the high byte and u’s integer part in the low byte. In C:

for (uint8_t k = 0; k < 8; ++k) {
    out = static_cast<uint8_t>((out << 1) | (buf[u >> 8] & 1));
    u = static_cast<uint16_t>(u + du);
}

On the AVR, it’s 6 instructions and 7 cycles a sample:

mov  ZL, uH    ; address = page : integer part of u
ld   t, Z      ; t = 0x00 or 0xFF
lsl  out
sub  out, t    ; 0xFF is -1, so a set pixel adds 1
add  uL, duL   ; step u
adc  uH, duH

Eight samples make one pattern byte, in 56 cycles.

Claude

Down, sy decides which source rows a screen row shows. Above row 117, sy drops below 1, so each screen row stands for more than one source row, and sampling just one would drop thin horizontal strokes. So the row ORs round(1/sy) source rows together, at most 4.

Where du is over 2, a step can jump clean over a 2-pixel stroke. So the ORed row is first widened by a pixel, acc | (acc >> 1) across its bytes. The font helps too. The console’s 5×7 font is emboldened by a pixel, and any one-pixel gap column is doubled first, so m and M keep their counters.

Claude

The fade works by removing pixels, not by changing colour. The TMS9118 has 15 fixed colours and no dim yellow. The prototype’s first fade stepped the text down through dark red and dark blue, and the far text just turned red. Now the colour stays dark yellow, and each row’s bytes are ANDed with a dither mask:

  • 0x88 and 0x22 on alternate rows, for the far 8% of the crawl
  • 0xAA and 0x55 out to 20%
  • solid after that

The C++ the Nano runs

Me

Then the C++ that the Nano runs.

Claude

An update walks the crawl’s 19 tile rows. For each one, it sets up its 8 screen rows as above, taking each one’s unpacked source row from a small cache:

  • Consecutive rows that show the same source rows share one buffer.
  • Blank rows share a buffer of zeros, found through a bitmap of blank source rows.
  • Eight page-aligned buffers are handed out round-robin.

Then it sets the address once, and writes the tile row column by column. Here’s the loop, without the branch that ORs in stars:

p_.addr(bitmap::pattern_addr(tr, c0));
for (uint8_t c = c0; c <= c1; ++c)
    for (uint8_t l = 0; l < 8; ++l) {
        CrawlLine& cl = g_crawl_lines[l];
        p_.data(static_cast<uint8_t>(scaler::sample8(cl.buf, cl.u, cl.du) & cl.mask));
    }

c0..c1 is the tile row’s span from the generator, so nothing outside the trapezoid is redrawn.

Claude

Crawl is a template over a Port with six calls: reg, addr, data, frame, now_us and stop. On the board, the Port writes through vdp_fastio. Each write puts MODE and the data on the pins while the previous gap is still running. It then waits on TCB2, a free-running 8 MHz timer, strobes ~CSW, and notes the time:

VDP_HOT void write(uint8_t b, bool mode, uint8_t wait_after) {
    bus::io::mode(mode);
    bus::io::put(b);
    if (!g_driving) { bus::io::data_outputs(); g_driving = true; }
    gate();               // until TCB2 says the last gap has passed
    bus::io::strobe();    // ~CSW
    g_last = TCB2.CNTL;   // the next gap is timed from this strobe
    g_wait = wait_after;
}

The gap is 8 µs after a data byte, but only 2 µs in three cases:

  • after a control byte
  • after a data byte while the display is blanked
  • after a data byte within 3.87 ms of ~INT falling: the 4.3 ms of retrace, less a margin

Because the gap is timed strobe to strobe, an interrupt can only lengthen it, never shorten it.

Claude

Getting to 20 updates a second took fixes to my own mistakes. The first full run on the board managed 7.6 updates a second, 133 ms an update.

  • A bus write took 20.5 µs. My byte-to-pin mapping looped with variable shifts, which the AVR does one bit per cycle. A 16-entry table and two shifts, proven equal to the pin map for all 256 bytes, made it 4.6 µs.
  • No inlining. The firmware builds with -Os, and the compiler wasn’t inlining the bus helpers. Marking them always_inline saved about 8 ms an update.
  • A slow sampler. The C version took about 12 cycles a sample, so I moved sampling and unpacking to the assembly above.

An update now takes 42.5 ms, 29.1 ms of it drawing, so the crawl moves every third frame. Thirty updates a second would need 33.4 ms, and the 8 µs windows alone take 25.5 ms of that.

Claude

The same Crawl code also runs on the host. In the host tests, a fake Port checks every byte against a per-pixel reference. In the preview, a Port records VRAM at every frame for the reference renderer. Here the preview and the board draw the same frame, at scroll position 150:

The host preview of the crawl at scroll position 150, drawn by the reference renderer: yellow justified text receding on a tilted plane, with stars, on black

The board showing the same frame, through composite video and the capture device: the same lines of text in the same places, slightly soft