HTURTLE revisited. John Harrison; Mary Harrison.
Ever since reading "HTURTLE: A Turtlegraphics Interpreter in Applesoft" by Al Evans in the July 1982 issue of Creative Computing, I have been fascinated by the idea of using Turtlegraphics in Atari Basic. The major drawback has been the limited number of colors availab ein the high-resolution modes. Graphics modes 7 and 8 offer respectable resolution (160 X 160 and 320 X 160, respectively) but only four colors in moe 7 and one in mode 8. Enter the GTIA chip and enhanced graphics modes 9, 10, and 11.
These three additional modes give the capability of up to 16 colors on the screen and a resolution of 80 X 192. However these modes are "map" modes and as such offer no provision for a text window. Through the use of a display list interrupt (DLI) a four-line text window has been provided in graphics mode 11. Thus, a suitable environment exists for implementing a Turtlegraphics interpreter in Atari Basic.
Using this expanded capability, I have converted the original Applesoft code to Atari Basic and added three features to the interpreter. The EDIT command deletes the last entered command and erases it from the screen. The PROGRAM command allows a series of commands to be strung together and then executed by the GO command. A HELP function was added to list the commands available. Each of these is more fully described below.
Listing 1 is HTURTLE for the Atari. It functions similarly to the HTURTLE described in the Al Evans article. The maximum X value is 79 and the maximum Y value is 53. The origin (0,0) is located at the bottom left corner of the screen. The Y value of 53 is corrected to allow for similar distances to be covered by a one-unit move in the X or Y direction. The Y value is multiplied by 3 in the program to make this correction and show the full 160-line resolution.
The turtle understands the following commands:
CLEAR: Clears the screen and text window and resets the turtle to the center of the screen (40,26) heading north (0).
COLOR AVAR: Sets one of the eight available colors as the pencolor.
Possible values for AVAR are black, orange, pink, purple, blue, turquoise, green, and yellow.
TURN ANGLE: Tells the turtle to turn ANGLE degrees. Positive values are clockwise, negative values are counterclockwise.
TURNTO ANGLE: Tells the turtle to turn to the direction ANGLE.
MOVE DISTANCE: The turtle moves DISTANCE in the direction he is facing. If color is not back, a line is drawn.
MOVETO X,Y: The turtle moves to the specified coordinates. If color is not black, a line is drawn.
TURTLEX: Reports the turtle's current X coordinate.
TURTLEY: Reports the turtle's current Y coordinate.
TURTLEANG: Reports the direction the turtle is facing.
PROGRAM: Stores a sequence of commands. The prompt changes from ? to sup.*.? when in the program mode. All other commands may be issued after the PROGRAM command.
GO: Executes the stored sequence of commands. Once the program mode is exited, the program is no longer stored, so GO has no effect.
QUIT: Leaves the interpreter or the program mode, if active.
HELP: Lists the commands for easy reference.
EDIT: Deletes the last command and erases it from the screen. May not be issued twice in succession. How The Program Works
HTURTLE has three main parts: the initialization routines, the command parser, and the subroutines for each of the commands. Table 1 is a list of all variables used in the program.
The initialization routines (lines 60-100, 10000-11080) set the trigonometric functions to accept degrees rather than radians, dimensions all arrays and string variables, stores the allowable commands in CMD$, stores the available colors in COLR$, and sets up the graphics display (lines 10120-10160). This setup is the only interesting portion of the initialization routine.
The screen is initialized by a graphics 8 call. This provides 320 X 160 resolution plus a four-line text window at the bottom of the screen. Placing 11 in memory address 87 and 192 in memory address 623 causes Basic to recognize this mode as graphics 11, one of the GTIA modes. Locations 560 and 561 hold the address of the display list. In the 167th entry of the display list, the display list interrupt is set by placing a 143 in this location.
The 167th location defines the last line of the display above the text window. Examining the display list reveals that each of the first three entries is 112. This leaves 24 blank lines at the top of the screen. Next comes a load memory scan (LMS) instruction (79), followed by a 2 byte address. This identifies the start of screen memory and the mode of the first line.
Since our display consist of 160 graphics lines, 160 entries (including the LMS) are required to define the display. However, whenever the display crosses a 4K boundary, a new LMS and address must be issued. Since graphics 8 requires 7900 bytes of screen memory, one 4K boundary will be crossed.
Consolidating this information reveals 3 bytes for blank lines and plus 160 bytes for the display plus 2 bytes for the initial screen address plus 2 bytes for the 4K boundary crossing screen address for a total of 167 bytes to reach the final screen line. Thus, the start of the display list plus 166 is the 167th entry. This is where the DLI is called. (For more information on display list composition, see the excellent article by David and Sandy Small, "Atari Graphics Unveiled," July 1981, Creative Computing.)
Next a short machine language routine is read in and stored starting at memory location 1536. This is the DLI routine. When called, it causes the bottom of the display to become a text window just as in graphics 8 while the upper portion remains in graphics 11.
Locations 512 and 513 hold the two-byte address of the DLI routine (600 Hex = 1536 decimal). Finally the interrupt is enabled by placing 192 into location 54286. New Commands
The command parser (lines 100-380) accepts the command from the user. If the program mode is active, the command is added to PROGRAM$ (except QUIT, GO, and HELP).
The command is then parsed into the command and parameter srings. If the command is found in the list of allowed commands, the appropriate subroutine is called by line 360. If the command is not allowed, an error message is printed, the command is deleted from PROGRAM$ if added earlier, and control returns to line 120 for the next command.
The command subroutines (lines 3000-7040) perform the tasks requested by the user. These routines check PAR$ for the appropraite parameters, check screen boundaries, and move or turn the turtle as required. Only the GO command routine (lines 6500-6610) requires additional explanation.
In this command the user wishes to execute a stored program. The variable XEQ is set to one, then PROGRAM$ is parsed, looking for the sup.*., that separates the various commands in the list. (Atari Basic does not support string arrays.) As each command is found, it is assigned to CH$ and control is returned to the command parser at line 200. Once the command is executed, control is returned to line 6600 from line 370 as long as XEQ=1. Once the entire PROGRAM$ has been parsed, XEQ is set to zero, and the subroutine returns to the command parser in the program mode. Thus an existing program may be executed and extended numerous times. PROGRAM$ is dimensioned as 1025, so approximately 100 commands may be stored. Once the program mode is exited through the QUOT command, PROGRAM$ is set to the null string and the previous program is deleted.
So there you have it, HTURTLE for the Atari. Now you Atari owners can begin to explore the world of Turlegraphics.