The KimKlone: Bride of Son of Cheap Video
X-Indirect-Y Addressing Using the W Register
|Another use for
W, besides its role in the NEXT
instruction, is to give the 65C02 an extra address mode (more or less).
Of course 65xx chips already have Indexed
Indirect address mode and Indirect
Indexed address mode, but the
W register lets
us emulate Indexed
— Ie; addressing which is indexed both before and after
the fetch of the indirect pointer.
This oh-so-esoteric capability is actually startlingly useful — especially in the Forth context, where it's very common for an address on stack to point to a multi-byte structure. This implies two steps. We want to index via X into the Forth data stack to find the pointer to the structure. Then, since the pointer only indicates the base of the structure, we index from there to access the other byte(s).
Here is a simple example.
The Forth word @ (pronounced "fetch") treats the top-of-stack value TOS
as an address, returning the value "at" that address. The value at
the address is simply a 16-bit number — a basic instance of
a multi-byte structure.
... except no extra cycles are consumed, and A is actually not involved. (See the upper-left chart if the underlying hardware behavior interests you.)
Thereafter the top-of-stack byte pair is accessible without any need for X indexing, because KK address decoding provides access to W at Wreg, a fixed pair of addresses in Zero Page. Of course a fixed pair of addresses in Zero Page can be used for indirect-Y addressing, and that's how the example concludes. (Incidentally, KK Forth sets Y=1 on startup and, by convention, restores Y=1 if it has ever gotten set otherwise.)
To match LDAW, the KK also has a STAW instruction, which for example is handy for accelerating Forth's ! ("store") operation. But LDA and STA aren't the only op's that can use the new address mode. There's always the option of using a "dummy" LDAW or STAW for the sole purpose of rapidly copying a pointer to W. Then subsequent code can use ADC or EOR or whatever Indirect-Y instructions it needs to get the job done.
X-Indirect-Y addressing is a significant asset for applications (including Forth) which need to index both before and after the fetch of an indirect pointer. In our example the performance boost is 89%.
If you need reentrancy you'll have to save and restore W. Another limitation is that W can only address one item at a time. But loading a new value to W only takes 6 cycles.
|<Previous Page KK Index Next Page >|
Servicing the unserviceable