Classic Computer Magazine Archive COMPUTE! ISSUE 76 / SEPTEMBER 1986 / PAGE 93

Fast Data For 64

Bob Kodadek

This handy Commodore 64 routine offers a speedy alternative to READing large amounts of information from DATA statements and POKEing it into memory. By using this automatic technique, you can cut program initialization delays dramatically. Use it for new programs or convert all your old ones—either way, you'll be delighted at the difference it makes.


Have you ever waited for a BASIC program to READ loads of data from DATA statements and POKE it into memory? This has always been the traditional way to store data for sprite images or custom characters, to set up musical note tables, and for many other purposes. No matter what the goal, there are few experiences more tedious than staring at a PLEASE WAIT message while BASIC executes hundreds (or even thousands) of READ and POKE statements. "Fast Data For 64" can perform such operations in a flash, at the speed of machine language. Yet, it becomes part of your BASIC program and is simply called with a GOSUB. For example, 2000 bytes of data can be read and POKEd into memory in only 6/10 second—about 3000 bytes per second. It takes BASIC over 27 full seconds to do the same job. Best of all, this routine automatically appends itself to any BASIC program and can be used even if you don't know anything about machine language.

A Speedy Alternative

Type in and save the program as it appears in the listing. When you run it, the program installs a machine language routine in memory, then displays several instructions on the screen. Next, load the BASIC program you wish to convert. After the load is finished, enter SYS 49152 and press RETURN. When the word LIGHTNING appears on the screen, a special routine has been added to your program. If you list the program, you will notice that it now contains four extra lines, numbered 63996-63999. (These line numbers are used because the routine must be located at the very end of your program, and BASIC will not allow line numbers higher than 63999.)

Now locate the very last DATA statement in your program and add a comma followed by —1. For instance, say that the last DATA line in the program looks like this:

5000 DATA 224,169,255,96

You'd change it to:

5000 DATA 224,169,255,96,-1

The value —1 marks the end of the data. (Because —1 is used as a marker, you cannot use this program for data that contains the value —1 elsewhere. This shouldn't pose any problems when the program is used for its intended purpose, since it's impossible to POKE a negative value into a memory location.)

To call the routine, add a line which sets the variable D equal to the beginning of the memory area where you want to store the data and then executes GOSUB 63997. For example, to move a block of data into screen memory, which normally begins at location 1024, you could use this line:

100 D=1024:GOSUB 63997

The same procedure is used whether you're writing a new program or enhancing an existing one. If you're updating an existing program, be sure to remove the old lines that previously did the POKEing. (Of course, you must not remove the DATA lines themselves, since the ML routine still needs something to read.) This routine uses the variable names D, D%, and A, so you must not use those variables anywhere in your own program. When you're finished making the changes, save the modified version of the program with a new filename.

If you're interested in how all this works: Line 63997 of the conversion routine changes the variable D into a low-byte/high-byte address and sets up a pointer at 253-254 ($FD-$FE) for the machine language routine to use in storing the data. Line 63998 updates the DATA pointer at 65 ($41) by reading and POKEing the first byte of data from BASIC. It then calculates the location of the machine language routine in BASIC memory and calls it with the resultant SYS number. Line 63999 contains the actual machine language in a REM statement. This technique works fine as long as the code is relocatable and does not contain any zero bytes or control characters. Note that this special line contains more than the usual 80 characters. Do not attempt to edit or change this line in any way; the BASIC editor will shorten the line and scramble the machine language it contains.

Fast Data For 64

For instructions on entering this listing, please refer to "COMPUTE!'s Guide to Typing In Programs" in this issue of COMPUTE!.

XB 10 PRINT "{CLR}{DOWN)PLEASE WAIT":FOR I=0 TO 386:READ BY:POKE 49152+I,BY:CK=CK+BY:NEXT
RF 20 IF CK <> 38541 THEN PRINT "ERROR IN DATA STATEMENT!":END
GG 30 DATA 162,0,189,101,193,240,6,32
XC 40 DATA 210,255,232,208,245,169,77,133
GS 50 DATA 170,169,192,133,171,32,51,165
FQ 60 DATA 160,0,177,170,201,3,240,18
EM 70 DATA 145,34,230,34,208,2,230,35
SD 80 DATA 230,170,208,2,230,171,160,0
MG 90 DATA 240,232,32,51,165,165,34,24
QF 100 DATA 105,2,144,2,230,35,133,45
FJ 110 DATA 133,47,133,49,165,35,133,46
GA 120 DATA 133,48,133,50,96,58,8,252
MR 130 DATA 249,128,58,143,32,82,38,80
SG 140 DATA 32,82,79,85,84,73,78,69
BB 150 DATA 46,70,73,82,83,84,32,83
JP 160 DATA 69,84,32,68,61,84,79,32
QR 170 DATA 68,69,83,84,32,84,72,69
FM 180 DATA 78,32,71,79,83,85,66,32
DC 190 DATA 54,51,57,57,55,0,103,8
DR 200 DATA 253,249,68,37,178,68,173,50
KA 210 DATA 53,54,58,151,50,53,52,44
QX 220 DATA 68,37,58,151,50,53,51,44
CB 230 DATA 68,171,68,37,172,50,53,54
PM 240 DATA 58,135,32,65,58,151,32,68
PB 250 DATA 44,65,0,156,8,254,249,158
RD 260 DATA 32,194,40,52,54,41,172,50
JX 270 DATA 53,54,170,194,40,52,53,41
RJ 280 DATA 171,32,49,49,56,32,58,142
KK 290 DATA 58,143,32,70,73,78,68,32
CX 300 DATA 49,83,84,32,66,89,84,69
FK 310 DATA 32,79,70,32,77,47,76,0
KG 320 DATA 22,9,255,249,143,34,230,253
XR 330 DATA 208,2,230,254,160,255,200,132
GP 340 DATA 98,132,99,132,100,230,65,208
MS 350 DATA 02,230,66,177,65,208,014,165
CF 360 DATA 65,24,105,5,133,65,144,44
MM 370 DATA 230,66,208,40,234,201,44,240
EK 380 DATA 35,201,32,240,224,201,45,208
FK 390 DATA 12,165,65,24,105,2,133,65
RX 400 DATA 144,2,230,66,96,56,233,48
QE 410 DATA 166,99,134,98,166,100,134,99
FP 420 DATA 133,100,176,193,162,100,165,98
FC 430 DATA 240,9,201,1,240,2,162,200
BF 440 DATA 138,133,98,165,99,240,8,162
XX 450 DATA 9,24,101,99,202,208,250,24
QR 460 DATA 101,98,24,101,100,145,253,144
JE 470 DATA 141,0,0,0,3,76,73,71
AF 480 DATA 72,84,78,73,78,71,33,013
FJ 490 DATA 0,40,67,41,49,57,56,54
DC 500 DATA 66,79,66,75,79,68,65,68,69,75,0