Technology / ARTICLE

JIT Bitmap Text Rendering for Microcontrollers

Raph Levien describes a JIT bitmap text rendering technique for the RP2350 microcontroller. Generated ARM code fills a 1-bit buffer within scanline budgets, supporting proportional spacing and kerning.

Raph Levien has implemented a JIT bitmap text renderer for the vic-2350, a re-envisioning of an 80s home computer built around the RP2350 microcontroller. He chose bitmap fonts for efficiency and because, at low resolutions, a deliberately designed bitmap can look crisper than antialiased output. The renderer also supports proportional spacing and kerning, which rules out the fixed character cells common in RP2350 text projects.

Racing the Beam and Display Lists

vic-2350 uses race-the-beam rendering: software fills a video buffer for each scanline as it is scanned out. The RP2350 drives DVI through its HSTX peripheral, but with only 520kB of SRAM, a full framebuffer would constrain resolution and bit depth. Levien’s approach leaves a budget of roughly 6 cycles per pixel. Rust could not meet that target, so the renderer is written mostly in symbolasm, a language he created for high-performance Cortex-M33 programming.

The interface between application and renderer is a double-buffered display list. Objects are sorted by y0, and the renderer maintains an active list of objects intersecting the current scanline. When a text object enters or leaves the active list, its start or end method runs. The start method allocates space for JIT code and a 1-bit-per-pixel intermediate buffer, then invokes the JIT compiler. Rendering fills the 1-bit buffer, and a second pass expands it to 16-bit RGB565 at 1.75 cycles per pixel.

JIT and Glyph Packing

Font ink is stored in a wide bitmap image with the same height as the font. The packing guarantees no glyph straddles a 32-pixel boundary, so one row of pixels can be read with a single 32-bit load. Space on either side of a glyph is kept in per-glyph metadata instead of the bitmap. Overlap detection further reduces the font bitmap from 1140 to 960 bytes. The font bitmap must reside in SRAM to avoid flash stalls during video scanout.

In generated code, r0 points to the font bitmap row, r1 is the output buffer, r2 is the accumulator, r3 holds extracted glyph bits, and r4 loads 32 bits of ink. The simple path uses ldr, ubfx, and orr. Reordering the last instruction of one glyph to follow the ldr of the next fills the result-use delay slot, cutting the per-glyph cost from 4 cycles to 3. The JIT compiler’s inner loop, written in symbolasm, compiles to 23 cycles per iteration. In vic-2350, the JIT runs in under 100 microseconds before scanout.

For the textdemo, Levien reports about 21k cycles for JIT compilation and 42k cycles to run the generated 1-bit code, together roughly 2.6% of a single RP2350 core’s throughput. Expanding to RGB565 costs about three times as much. JIT output is not cached because it expands the input string by about 10x, and running the JIT is cheap.

Dot Product Kerning and Prior Art

Kerning uses two bitvectors per glyph, one for each side. The amount to kern is the popcount of the AND of the right vector of the first glyph and the left vector of the second. Each glyph needs only two bytes for kerning data, and evaluation happens at display list build time. When a kern value is nonzero, a negative space character is inserted into the string. Levien built the kerning table for this font by hand; each bit represents a specific outline feature, such as bit 0 marking a top-right projection on one glyph and a matching lack of ink on the next.

Prior work includes on-the-fly bitblt code generation for the Blit around 1982, which achieved about 2500 characters per second. Levien estimates the new renderer’s raw 1bpp output is almost three orders of magnitude faster. David Fuchs pointed to US Patent 6377261, filed in 1996, which compiled glyphs to machine code but relied on a cache to manage RAM. Levien also notes that the initial JIT core was hand-written in Rust, then translated to symbolasm and optimized with Claude; the blog post itself was hand-written.

END
Feedback