5 ms·
So the core of this trick is to point the stack pointer into video memory, because pull/push instructions can deliver faster throughput than traditional load/st
by T-hawk 11y ago
So the core of this trick is to point the stack pointer into video memory, because pull/push instructions can deliver faster throughput than traditional load/stores.
The Atari 2600 has hardware designed to take advantage of such a trick. It has some 1-bit graphical objects that are turned on or off by writing to a certain register. The hot bit of that register is located at bit 1.
Why that, why not the more obvious low or high bit? Because bit 1 corresponds to the location of the 6502's zero flag in the flags register. So you can correctly turn the object on or off with literally just a compare on its coordinate then a push-flags instruction. You'll end up pushing a 1 or 0 to enable or disable the object. No branches or conditionals, and the code is even time-invariant, a helpful property in the tightly cycle-counted world of this machine.
Finally, the video chip registers are laid out with several such objects in succession, so you can continue pushing into several of them without resetting the stack pointer. (This machine has no interrupts, so hijacking the stack pointer into video memory is perfectly safe as long as you never issue a call or other push-pop instruction.)
The machine also aligns some of its read registers the same way. The joystick button is mapped to bit 7 of its input register, so you can read and then immediately branch on the sign flag without a compare instruction.