Classic Computer Magazine Archive COMPUTE! ISSUE 71 / APRIL 1986 / PAGE 94

Screen Saver 64

Stephen E. Masters

Here are two fast, useful routines for storing and retrieving high-resolution graphics screens with a disk drive. They work with the Commodore 64 or Commodore 128 in 64 mode.


Taking advantage of the Commodore 64's high-resolution graphics can be a time-consuming process at best. Even with extra commands such as those found in Simons' BASIC, it may take many minutes or even hours to plot a detailed screen. Utilities for dumping a high-resolution screen to your dotmatrix printer are readily available. At times, however, you may wish to save your graphics screen in a disk file so you can display it later without rerunning the program that created it. And if you own an Okimate 10 or similar color printer, the ability to save multicolor graphics screens is particularly useful.
    "Screen Saver 64" provides two machine language (ML) routines that let you quickly save and retrieve hi-res graphics screens-both standard and multicolor- from disk. Though they're written in ML, you can use them without knowing the ins and outs of ML yourself. And we've included two demonstration programs that show exactly how to use the ML routines for real applications.
    To get started, type in and save Programs 1 and 2. Program 1 puts the screen save routine into memory, and Program 2 creates the screen retrieval routine. Since both ML routines go into the same memory area, they must be used separately. If you have an ML monitor and wish to examine the routines, note that each is broken into two parts, located from memory locations 679-738 and 828-1023 (decimal).

Saving A Graphics Screen
Here are the steps for saving a graphics screen with Screen Saver 64:
1. Run Program 1 to place the ML screen save routine in memory.
2. Create your hi-res or multicolor screen as usual. If you don't know how to do this, the Commodore 64 Programmer's Reference Guide and many other books explain the required steps. Program 3 (see below) contains a simple demonstration.
3. Execute a statement like OPEN 2,8,2,"filename,P,W" to open a disk file for writing (replace filename with the name of your own file). You must open the file as a PRG (program format) file using the P suffix as shown above. The W suffix indicates that you're opening the file for a write operation, and the first numeral 2 sets the logical file number (2 in this case) for that file.
4. Execute SYS 1007 to activate the ML save routine. This must be done while you are in hi-res or multicolor mode. The ML routine finds the currently defined graphics screen and associated color memory, and stores their contents in the disk file.
5. Execute a statement like CLOSE 2 to close the file. It is very important that you end the procedure by CLOSEing the file, specifying the same logical file number (2 in this case) which was used to open it. If you omit this vital step, you may end up with a poison (unclosed) file on the disk that could damage other files or render the whole disk unreadable.

Retrieving A Graphics Screen
Once you have saved the screen to disk, it's easy to retrieve. Here are the steps to follow for bringing a graphics screen back into memory:
1. Run Program 2 to put the ML retrieval routine in memory.
2. Perform the steps needed to enter the appropriate hi-res or multicolor graphics mode.
3. Execute a statement like OPEN 2,8,2,"filename,P,R" to open the disk file for reading (input). Again, the P suffix specifies a PRG file, and the R suffix opens the file for reading.
4. Execute SYS 881. The ML routine loads the graphics data back into the right memory locations.
5. Execute a statement like CLOSE 2 to close the disk file. Again, you should use the same logical file number (2 in this case) used when opening the file.
6. At this point you can continue with a BASIC program or do whatever else you like.

Graphics Demonstrations
Programs 3 and 4 contain practical demonstrations of how to use these two routines from BASIC. Type in and save both programs, then load and run Program 1 to put the ML save routine in memory. Now load and run Program 3. This program uses the hi-res drawing example from pages 123-126 in the Commodore 64 Programmer's Reference Guide. Lines 110-140 define the hires screen and color memory to start at locations 8192 and 1024, respectively, then clear the graphics screen. Lines 150-230 draw a simple sine wave pattern. (Be patient; it takes a few minutes to complete the drawing.) Line 270 opens the disk file using 2 as the logical file number and SINEWAVE.HIRES as the filename. After checking the disk error channel, the program calls the ML save routine.
    The sine wave disappears as the hi-res memory is moved temporarily to a new location and stored in the disk file. Then the routine moves the picture back to its original location, saves color memory, and returns control to BASIC. After checking the error channel again, the BASIC program restores the normal screen display and ends.
    Program 4 shows how to use the ML retrieval routine. Since it looks for a file named SINEWAVE .HIRES on the disk, you can run it only after you've used Program 3 to create the file. Run Program 2 to put the ML retrieval routine in memory, then load and run Program 4. Lines 110-130 define the hi-res screen starting at location 24576, a different area than the one it was saved from. Lines 140-150 fill the screen with a uniform pattern. (Note that this is done only for the purpose of demonstration, to confirm that the retrieval routine puts new information on the screen. It is not necessary to clear the graphics screen before using this routine.)
    Lines 160-190 open a disk file for reading, using the same name as Program 3 (SINEWAVE.HIRES). After checking the error channel (180-190), the retrieval routine is called with SYS 881. The hi-res screen is restored right before your eyes: First the graphics information appears, then color memory is brought in as well. After a brief pause, the program restores the screen to normal and ends.

Inside The ML Routines
The ML save routine saves the currently defined graphics screen and its associated color memory wherever they are located-even if the hi-res screen is stored in the RAM underneath a ROM area. The ML retrieval routine brings the stored screen back into whatever area you have currently defined as the graphics screen, even if that's a different location from the area from which it was saved. This lets you create and store a complex graphics display using one particular graphics aid (Simons' BASIC, etc.) and retrieve it for use by any other program.
    Since sprites are independent of other graphics, these routines can't store or retrieve sprite shapes that appear on the screen.
    To make this routine compatible with as many programs as possible, memory usage is restricted to three areas. It uses memory locations 679-738 (normally unused) and 828-1023 (the cassette buffer) to store the routines, and also zeropage locations 2 and 251-254. To save a screen, the ML routine first looks in locations 56576 and 53272 to locate the graphics screen and normal screen memory (which becomes the hi-res color memory). It then swaps the 8K bytes of hi-res RAM memory with the contents of locations 24576-32767 ($6000-$7FFF). This is done by "turning off" the computer's ROM chips temporarily so the swapping routine can see hi-res memory no matter where it's located. Then the routine switches the Kernal ROM back in to write the 8,000 bytes of hi-res information to the disk file, and moves the hi-res screen back to its original location. The 1,000-byte screen memory area is written directly to the disk file.
    Note that since no memory swapping is done for color memory, this part of your screen must be located in a memory area that's not normally hidden by ROM.
    Finally, the normal color memory at 55296-56295 (used in multicolor mode) as well as the screen background byte at 53281 is written to disk. The final disk file is 10,003 bytes (40 blocks) long. Two extra bytes are added at the beginning of the file to make it compatible with version 3.0 of the Okimate Color Print program.
    The retrieval routine works in reverse, finding where the graphics screen and color memory are located in the current configuration, then restoring everything to the correct memory locations. Since RAM can be POKEd even if it's under ROM, no memory swapping is required and the contents of the disk file are moved directly into the appropriate memory areas.

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

Program 1: Screen Saver 64

CS 100 REM PROGRAM 1 SCREEN SA
       VER 64
PE 110 SU=0:FORI=688TO738:READ
       X:SU=SU+X:POKEI,X:NEXT
CK 120 IFSU<>4855THENPRINT"ERR
       OR IN DATA IN LINES 170
       -220":STOP
QX 130 SU=0:FORI=828TO1022:REA
       DX:SU=SU+X:POKEI,X:NEXT
PQ 140 IFSU<>31598THENPRINT"ER
       ROR IN DATA IN LINES 24
       0-470":STOP
SR 150 PRINT"SCREEN SAVE INSTA
       LLED":END
MS 170 DATA173,0,221,41,3,73,3
       ,10,10,10
QP 180 DATA10,10,10,133,254,17
       3,24,208
QJ 190 DATA41,8,10,10,101,254,
       133,2,105
DA 200 DATA31,141,169,2,173,24
       ,208,41,240
DB 210 DATA74,74,234,234,101,2
       54,141,168
XM 220 DATA2,105,3,141,167,2,9
       6
KB 240 DATA160,0,132,251,132,2
       53,165,2,133
PH 250 DATA252,169,96,133,254,
       120,165,1,72
MG 260 DATA41,253,133,1,177,25
       1,170,177
DA 270 DATA253,145,251,138,145
       ,253,200
RK 280 DATA208,243,230,252,230
       ,254,165
HK 290 DATA254,201,12E,208,233
       ,104,133,1
JH 300 DATA88,234,234,234,96,1
       62,2,32,201
MD 310 DATA255,1 :,0,32,210,25
       5,165,2,32
SX 320 DATA210,255,160,0,132,2
       51,169,96
RK 330 DATA133,252,234,234,177
       ,251,32,210
XS 340 DATA255,165,252,201,127
       ,240,7,200
PX 350 DATA208,242,230,252,208
       ,238,200
MX 360 DATA152,201,64,208,232,
       96,160,0
FC 370 DATA132,251,173,168,2,1
       33,252,177
FA 380 DATA251,32,210,255,165,
       252,205,167
SH 390 DATA2,240,7,200,208,241
       ,230,252
MD 400 DATA208,237,200,152,201
       ,232,208
JB 410 DATA231,160,0,132,251,1
       69,216,133
XS 420 DATA252,177,251,32,210,
       255,165,252
JD 430 DATA201,219,240,7,200,2
       08,242,230
XE 440 DATA252,208,238,200,152
       ,201,232
XC 450 DATA208,232,173,33,208,
       32,210,255
PH 460 DATA32,204,255,96,32,17
       6,2,32,60
MX 470 DATA3,32,113,3,32,60,3,
       32,163,3,96


Program 2: Screen Retriever 64

PR 100 REM PROGRAM 2 SCREEN RE
       TRIEVE
PE 110 SU=0:FORI=688TO738:READ
       X:SU=SU+X:POKEI,X:NEXT
HE 120 IFSU<>4855THENPRINT"ERR
       OR IN DATA IN LINES 160
       -180":STOP
JQ 130 SU=0:FORI=881TO1004:REA
       DX:SU=SU+X:POKEI,X:NEXT
XK 140 IFSU<>21290THENPRINT"ER
       ROR IN DATA IN LINES 19
       0-250":STOP
HM 150 PRINT"SCREEN RETRIEVE I
       NSTALLED":END
KB 160 DATA173,0,221,41,3,73,3
       ,10,10,10,10,10,10,133,
       254,173,24,208
PD 170 DATA41,8,10,10,101,254,
       133,2,105,31,141,169,2,
       173,24,208,41,240
KP 180 DATA74,74,234,234,101,2
       54,141,148,2,105,3,141,
       167,2,96
PC 190 DATA32,176,2,162,2,32,1
       98,255,32,207,255,32,20
       7,255,234,160,0,132,251
AH 200 DATA165,2,133,252,32,20
       7,255,145,251,165,252,2
       05,169,2,240,7,200,208
QK 210 DATA241,230,252,208,237
       ,200,152,201,64,208,231
       ,160,0,132,251,173,168,
       2
FR 220 DATA133,252,32,207,255,
       145,251,165,252,205,167
       ,2,240,7,200,208,241,23
       0
AB 230 DATA252,208,237,200,152
       ,201,232,208,231,160,0,
       132,251,169,216,133,252
       ,32
JE 240 DATA207,255,145,251,165
       ,252,201,219,240,7,200,
       208,242,230,252,208,238
       ,200
SH 250 DATA152,201,232,208,232
       ,32,207,255,141,33,208,
       32,204,255,96,999


Program 3: Screen Saver Demo

DG 100 REM PROGRAM 3 SCREEN SA
       VE DEMO
HS 110 BASE=2*4096:POKE53272,P
       EEK(53272)OR8
BH 120 POKE53265,PEEK(53265)OR
       32
BK 130 FOR I=BASE TO BASE+7999
       :POKEI,0:NEXT
KD 140 FOR I=1024TO2023:POKEI,
       3:NEXT
MS 150 FOR X=O TO 319 STEP.5
RF 160 Y=INT(90+80*SIN(X/10))
KR 170 CH=INT(X/8)
QC 180 RO=INT(Y/8)
RF 190 LN=YAND7
AR 200 BY=BASE+RO*320+8*CH+LN
GX 210 BI=7-(XAND7)
BX 220 POKEBY,PEEK(BY)OR(2TBI)
AP 230 NEXT X
EG 240 POKE1024,16
GR 250 FOR I=1(2 SPACES)TO 100
       0:NEXT(2 SPACES}I
BM 260 OPEN15,8,15
JK 270 OPEN2,8,2,"SINEWAVE.HIR
       ES,P,W"
RJ 280 INPUT#15,Z1,Z2$,Z3,Z4
AG 290 IF Z1<>0 THEN CLOSE2:CL
       OSE15:PRINT Z1;Z2$;Z3;Z
       4:GOTO350
MX 300 SYS 1007
AS 310 CLOSE2
AM 320 INPUT#15,Z1,Z2$,Z3,Z4
RS 330 IF Z1<>0 THEN CLOSE15:P
       RINT Z1;Z2$;Z3;Z4
MH 340 FOR I=1 TO 1000:NEXT I
MM 350 POKE53265,PEEK(53265)AN
       D 223
DF 360 POKE53272,PEEK(53272)
       (2 SPACES}AND 247
DR 370 END


Program 4: Screen Retriever Demo

XF 100 REM PROGRAM 4 SCREEN RE
       TRIEVE DEMO
PH 110 POKE56578,PEEK(56578) 0
       R 3:POKE 56576,(PEEK(56
       576)AND252)OR 2
HD 120 BASE=6*4096:POKE53272,P
       EEK(53272)OR8
FG 130 POKE53265,PEEK(53265)OR
       32
FM 140 FOR I=BASE TO BASE+7999
       :POKE I,66:NEXT
CS 150 FOR I=16384+1024 TO 163
       84+2023:POKE I,77:NEXT
GE 160 OPEN15,8,15
QJ 170 OPEN2,8,2,"SINEWAVE.HIR
       ES,P,R"
CC 180 INPUT#15,Z1,Z2$,Z3,Z4
GG 190 IF Z1<>0 THEN CLOSE2:CL
       OSE15:PRINT Z1;Z2$;Z3;Z
       4:GOTO230
RS 200 SYS 881
PJ 210 CLOSE2
MC 220 FOR I=1 TO 2500:NEXT I
DF 230 POKE56576,(PEEK(56576)A
       ND252) OR 3
FR 240 POKE53272,PEEK(53272)AN
       D247
HD 250 POKE53265,PEEK(53265)AN
       D223