hn.today

A great old-timey game-programming hack (2013)

blog.moertel.com283 points71 comments
Screenshot of A great old-timey game-programming hack (2013)

A programmer has nearly completed a side-scrolling game for an 8-bit PC, but its software-rendered graphics run too slowly. The game must copy background tiles pixel data into a 320-by-240 screen buffer, and the 6809 processor has no graphics hardware to do the work. A straightforward routine copies each tile row with a loop; unrolling that loop removes its overhead, and replacing auto-increment addressing with fixed offsets cuts the estimated time per row from 157 to 98 cycles. The resulting tile-copy routine is about 60 percent faster, yet still cannot make the game playable.

After initially finding no further improvement, the narrator consults a 6809 manual and notices that its stack instructions can move multiple registers at once. The proposed hack repurposes the stack pointers: point one at the tile and the other at the screen buffer, then use PULS and PSHU to transfer the D, X, and Y registers as six-byte chunks. Since a tile row contains 14 bytes, the plan uses two six-byte transfers and a final two-byte transfer, with the loops unrolled. This approach exploits an unusual processor feature to treat stack operations as fast bulk-memory copying, rather than merely as a way to save and restore registers. The excerpt ends as the narrator sketches the pointer adjustments for the routine.

Read on blog.moertel.com71 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Ggml v1.0.0

Ggml v1.0.0

ggml 1.0.0 removes deprecated public APIs and legacy DIAG_MASK operations, while adding configurable YaRN truncation and FP8 support. It also improves backend performance with quantized matrix multiplication, fused MoE operations, and new attention and scheduling paths. (github.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.