SWEET 16 - INTRODUCTION by Dick Sedgewick Sweet 16 is probably the least used and least understood seed in the Apple ][. In exactly the same sense that Integer and Applesoft Basics are languages, SWEET 16 is a language. Compared to the Basics, however, it would be classed as low level with a strong likeness to conventional 6502 Assembly language. To use SWEET 16, you must learn the language - and to quote "WOZ", "The opcode list is short and uncomplicated". "WOZ" (Steve Wozniak), of course is Mr. Apple, and the creator of SWEET 16. SWEET 16 is ROM based in every Apple ][ from $F689 to $F7FC. It has it's own set of opcodes and instruction sets, and uses the SAVE and RESTORE routines from the Apple Monitor to preserve the 6502 registers when in use, allowing SWEET 16 to be used as a subroutine. It uses the first 32 locations on zero page to set up its 16 double byte registers, and is therefore not compatible with Applesoft Basic without some additional efforts. The original article, "SWEET 16: The 6502 Dream Machine", first appeared in Byte Magazine, November 1977 and later in the original "WOZ PAK". The article is included here and again as test material to help understand the use and implementation of SWEET 16. Examples of the use of SWEET 16 are found in the Programmer's Aid #1, in the Renumber, Append, and Relocate programs. The Programmer's Aid Operating Manual contains complete source assembly listings, indexed on page 65. The demonstration program is written to be introductory and simple, consisting of three parts: 1. Integer Basic Program 2. Machine Language Subroutine 3. SWEET 16 Subroutine The task of the program will be to move data. Parameters of the move will be entered in the Integer Basic Program. The "CALL 768" ($300) at line 120, enters a 6502 machine language subroutine having the single purpose of entering SWEET 16 and subsequently returning to BASIC (addresses $300, $301, $302, and $312 respectively). The SWEET 16 subroutine of course performs the move, and is entered at Hex locations $303 to $311 (see listing Number 3). After the move, the screen will display three lines of data, each 8 bytes long, and await entry of a new set of parameters. The three lines of data displayed on the screen are as follows: Line 1: The first 8 bytes of data starting at $800, which is the fixed source data to be moved (in this case, the string A$). Line 2: The first 8 bytes of data starting at the hex address entered as the destination of the move (high order byte only). Line 3: The first 8 bytes of data starting at $0000 (the first four SWEET 16 registers). The display of 8 bytes of data was chosen to simplify the illustration of what goes on. Integer Basic has its own way of recording the string A$. Because the name chosen for the string "A$" is stored in 2 bytes, a total of five housekeeping bytes precede the data entered as A$, leaving only three additional bytes available for display. Integer Basic also adds a housekeeping byte at the end of a string, known as the "string terminator". Consequently, for convenience purposes of the display, and to see the string terminator as the 8th byte, the string data entered via the keyboard should be limited to two characters, and will appear as the 6th and 7th bytes. Additionally, parameters to be entered include the number of bytes to be moved. A useful range for this demonstration would be 1-8 inclusive, but of course 1-255 will work. Finally, the starting address of the destination of the move must be entered. Again, for simplicity, only the high-order byte is entered, and the program allows a choice between Decimal 9 and high-order byte of program pointer 1, to avoid unnecessary problems (in this demonstration enter a decimal number between 9 and 144 for a 48K APPLE). The 8 bytes of data displayed starting at $00 will enable one to observe the condition of the SWEET 16 registers after a move has been accomplished, and thereby understand how the SWEET 16 program works. From the article "SWEET 16: A 6502 Dream Machine", remember that SWEET 16 can establish 16 double byte registers starting at $00. This means that SWEET 16 can use the first 32 addresses on zero page. The "events" occurring in this demonstration program can be studied in the first four SWEET 16 registers. Therefore, the 8 byte display starting at $0000 is large enough for this purpose. These four registers are established as R0, R1, R2, R3: R0 $0000 & 0001 -SWEET 16 accumulator R1 $0002 & 0003 -Source address R2 $0004 & 0005 -Destination address R3 $0006 & 0007 -Number of bytes to move . . . R14 $001C & 001D -Prior result register R15 $001E & 001F -SWEET 16 Program counter Additionally, an examination of registers R14 and R15 will extend and understanding of SWEET 16, as fully explained in the "WOZ" text. Notice that the high order byte of R14, (located at $1D) contains $06, and is the doubled register specification (3X2=$06). R15, the SWEET 16 program counter contains the address of the next operation as it did for each step during execution of the program, which was $0312 when execution ended and the 6502 code resumed. To try a sample run, enter the Integer Basic program as shown in Listing #1. Of course, REM statements can be omitted, and line 10 is only helpful if the machine code is to be stored on disk. Listing #2 must also be entered starting at $300. NOTE: A 6502 disassembly does not look like listing #3, but the SOURCEROR disassembler would create a correct disassembly. Enter "RUN" and hit RETURN Enter "12" and hit RETURN (A$ - A$ string data) Enter "18" and hit RETURN (high-order byte of destination) The display should appear as follows: $0800-C1 40 00 10 08 B1 B2 1E (SOURCE) $0A00-C1 40 00 10 08 B1 B2 1E (Dest.) $0000-1E 00 08 08 08 0A 00 00 (SWEET 16) NOTE: The 8 bytes stored at $0A00 are identical to the 8 bytes starting at $0800, indicating that an accurate move of 8 bytes length has been made. They are moved one byte at a time starting with token C1 and ending with token 1E. If moving less than 8 bytes, the data following the moved data would be whatever existed at those locations before the move. The bytes have the following significance: A Token$ C1 40 00 10 08 B1 B2 1E --------- ---- -------- --------- -- | | | | | String VN DSP NVA DATA DATA Terminator The SWEET 16 registers are as shown: low high low high low high low high $0000 1E 00 08 08 08 0A 00 00 ---------- ---------- ---------- ---------- | | | | register register register register R0 R1 R2 R3 (acc) (source) (dest) (#bytes) The low order byte of R0, the SWEET 16 accumulator, has $1E in it, the last byte moved (the 8th). The low order byte of the source register R1 started as $00 and was incremented eight times, once for each byte of moved data. The high order byte of the destination register R2 contains $0A, which was entered at 10 (the variable) and poked into the SWEET 16 code. The low-order byte of R2 was incremented exactly like R1. Finally, register R3, the register that stores the number of bytes to be moved, has been poked to 8 (the variable B) and decremented eight times as each byte got moved, ending up $0000. By entering character strings and varying the number of bytes to be moved, the SWEET 16 registers can be observed and the contents predicted. Working with this demonstration program, and study of the text material will enable you to write SWEET 16 programs that perform additional 16 bit manipulations. The unassigned opcodes mentioned in the "WOZ Dream Machine" article should present a most interesting opportunity to "play". SWEET 16 as a language - or tool - opens a new direction to Apple ][ owners without spending a dime, and it's been there all the time. "Apple-ites" who desire to learn machine language programming, can use SWEET 16 as a starting point. With this text material to use, and less opcodes to learn, a user can quickly be effective. Listing #1 >List 10 PRINT "[D]BLOAD SWEET": REM CTRL D 20 CALL - 936: DIM A $ (10) 30 INPUT "ENTER STRING A $ " , A $ 40 INPUT "ENTER # BYTES " , B 50 IF NOT B THEN 40 : REM AT LEAST 1 60 POKE 778 , B : REM POKE LENGTH 70 INPUT "ENTER DESTINATION " , A 80 IF A > PEEK (203) - 1 THEN 70 90 IF A < PEEK (205) + 1 THEN 70 100 POKE 776 , A : REM POKE DESTINATION 110 M = 8 : GOSUB 160 : REM DISPLAY 120 CALL 768 : REM GOTO $0300 130 M = A : GOSUB 160 : REM DISPLAY 140 M = O : GOSUB 160 : REM DISPLAY 150 PRINT : PRINT : GOTO 30 160 POKE 60 , 0 : POKE 61 , M 170 CALL -605 : RETURN : REM XAM8 IN MONITOR Listing #2 300:20 89 F6 11 00 08 12 00 00 13 00 00 41 52 F3 07 FB 00 60 Listing #3 SWEET 16 $300 20 89 F6 JSR $F689 $303 11 00 08 SET R1 source address $306 12 00 00 SET R2 destination address A $309 13 00 00 SET R3 length B $30C 41 LD @R1 $30D 52 ST @R2 $30E F3 DCR R3 $30F 07 BNZ $30C $311 00 RTN $312 60 RTS Data will be poked from the Integer Basic program: "A" from Line 100 "B" from Line 60