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.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.