Apple II User Manual

Page of 257
+------------------------------------------------------------------------
|  TOPIC -- Apple II -- WOZPAK Sweet-16 article by Steve Wozniak
+------------------------------------------------------------------------
SWEET 16: A Pseudo 16 Bit Microprocessor
by Steve Wozniak
Description:
------------
While writing APPLE BASIC for a 6502 microprocessor, I repeatedly
encountered a variant of MURPHY'S LAW. Briefly stated, any routine
operating on 16-bit data will require at least twice the code that
it should. Programs making extensive use of 16-bit pointers (such
as compilers, editors, and assemblers) are included in this
category. In my case, even the addition of a few double-byte
instructions to the 6502 would have only slightly alleviated the
problem. What I really needed was a 6502/RCA 1800 hybrid - an
abundance of 16-bit registers and excellent pointer capability.
My solution was to implement a non-existant (meta) 16-bit
processor in software, interpreter style, which I call SWEET 16.
SWEET 16 is based on sixteen 16-bit registers (R0-15), which are
actually 32 memory locations. R0 doubles as the SWEET 16
accumulator (ACC), R15 as the program counter (PC), and R14 as the
status register. R13 holds compare instruction results and R12 is
the subroutine return stack pointer if SWEET 16 subroutines are
used. All other SWEET 16 registers are at the user's unrestricted
disposal.
SWEET 16 instructions fall into register and non-register categories.
The register ops specify one of the sixteen registers to be used as
either a data element or a pointer to data in memory, depending
on the specific instruction. For example INR R5 uses R5 as data
and ST @R7 uses R7 as a pointer to data in memory. Except for the
SET instruction, register ops take one byte of code each. The
non-register ops are primarily 6502 style branches with the second
byte specifying a +/-127 byte displacement relative to the address
of the following instruction. Providing that the prior register op
result meets a specified branch condition, the displacement is
added to the SWEET 16 PC, effecting a branch.
SWEET 16 is intended as a 6502 enhancement package, not a stand
alone processor. A 6502 program switches to SWEET 16 mode with a
subroutine call and subsequent code is interpreted as SWEET 16
instructions. The nonregister op RTN returns the user program to
6502 mode after restoring the internal register contents
(A, X, Y, P, and S). The following example illustrates how to use
SWEET 16.
300  B9 00 02           LDA   IN,Y     ;get a char
303  C9 CD              CMP   #"M"     ;"M" for move
305  D0 09              BNE   NOMOVE   ;No. Skip move
307  20 89 F6           JSR   SW16     ;Yes, call SWEET 16