ROM Computer Magazine Archive ROM MAGAZINE ISSUE 7 — AUGUST/SEPTEMBER 1984 / PAGE 30

PAGE FLIPPING

by Bob Cockroft

    Have you ever made a game that required several displays, only to be disappointed with the slow speed in which every screen was drawn? The monotonous motion of the cursor or the gradual drawing of graphics is enough to detract from any program. Fortunately, there is a simple method of avoiding all this. By flipping through pages of memory, one is able to instantaneously replace one screen of information with another.
    When one creates a graphic mode with the GRAPHICS `X' command, the computer allocates a certain amount of memory space for screen data. The screen pointers indicate where the screen is, so that the computer is able to display it. The significant fact is that the GRAPHICS `X' command uses empty memory, so that the screen appears originally blank. This means that data cannot be placed in memory locations that are to be used for display before the screen is created. Text or graphics must be drawn after the display is presented on the screen. As a result, much time is wasted while the screen is being drawn.
    When using page flipping, the screen is best thought of as a block of memory with a specific location and size. The computer can have many of these blocks stored. The exact number depends on both the size of the block and the memory capacity of the computer. Page flipping is simply changing the screen pointers so that they indicate different blocks of screen data in the memory. Once the screen containing your information is stored as a block in the RAM, changing the pointers so that they point to this block will cause the screen to appear instantaneously. What is important is that all the screens need to be drawn only once and then stored in the RAM. Time does not need to be wasted recreating screens. The amount of memory reserved for screen data blocks depends on the resolution of the graphic mode. For example, GR.8 uses much more memory than GR.0. The following table gives the byte requirement for all the graphic modes.

Table 1
Mode type Byte Requirements
0
993
1
513
2
261
3
 273
4
537
5
1017
6
2025
7
3945
8
7900

    Blocks of screen data must be placed in different places in the memory so that they do not erase each other. The position in the memory that is allotted for screen data is determined by the value contained in RAMTOP. RAMTOP (dec 106) gives the total amount of available RAM. This value will vary with the amount of memory already used and your computers RAM capacity(16K,32K or 48k etc). RAMTOP (dec 106) indicates the amount of RAM available in terms of PAGEs. A PAGE, when referring to memory capacity, means 256 bytes. In other words, by multipling the value in RAMTOP by 256, one can find the amount of bytes available. In addition, this value is also the bottom of the screen. By subtracting the number of bytes used in the graphic mode under consideration(table 1) from the total availability bytes(PEEK(106),256), the top of the screen can be found. (see below)

Screen Data

    PEEK(106)-bytes use by Graphic mode.


  PEEK(106)
 
The Screen
 

    Because RAMTOP determines the position of the screen data, changing the value in this location will cause the computer to create new screens at different locations in the memory. For instance, if no change was made to the value in RAMTOP(dec 106), the GRAPHICS `X' command would create a screen that had a base of PEEK(106),256. Then, if one were to reduce the value in RAMTOP by 4(POKE 106,PEEK( 106)-4), the base of this new graphics mode would be positioned 1024 (4,256) bytes lower than the previous one. There would be now two screens in the memory. The only limit to the number of screens in the memory is your machines RAM capacity. Below is an example of creating extra GRAPHICS mode 0 screen blocks. The values for RAMTOP are those for a machine with 48K and do not necessarily correspond to computers that have different amounts of RAM.

Memory Configuration

   PEEK(106)*256=40960
 
Screen One
 
 
Screen Two
 
 
Screen Three
 
   PEEK(106-4)*256=39936
   PEEK(106-8)*256=38912

    For every screen created their will be a corresponding Display List. This list is a set of instructions that tell the ANTIC chip where the screen data is, and how to display it. By directing the DL pointers(dec 560,561) to the location of the display list of the screen you wish to have presented, the computer will be able to know where to find the display. The pointers use the (LSB/MSB) byte pattern for storing the start location of the display list. In other words, the second byte must be multipled by 256 and added to the first.

DL = PEEK(560)*256,PEEK(561)

    The Display List can be anywhere between 24 and 256 bytes in length. Each byte or sometimes a group of bytes represents an individual instruction for the screen. In a discussion on Page Flipping, we are not concerned with most of these bytes. In fact, we only need to look at three. Display list bytes 3,4 and 5 are the first Load Memory Scan (LMS) instruction. Byte 3 tells the ANTIC what graphic mode to use, the next two bytes are the location of the first byte of the screen that corresponds to this display list(LSB/ MSB). What this means is that after creating a graphics mode, you POKE the display list pointers so that they indicate the display list of the screen you wish to have presented. This will cause the desired display to immediately appear on the screen. Table 2 is a partial display list. It was included in the article to help indicate how the display list finds screen data.

Partial Display List

DL 112    These 3 bytes draw a
DL+1 112    total of 24 blank lines
DL+2
112    at the top of the screen
DL+3 66
DL+4 76    These 2 bytes indicate the
DL+5 125    location of the screen data


    The following is a step by step application of what has been explained. Although not a program, this section is designed to help you apply Page Flipping in your programs.

Create first screen

1 GRAPHICS 0
2 A = PEEK(106)
3 REM * NEXT:Store display list location  *
4 DL11= PEEK(560):DL12 = PEEK(561)

Repeat this process with the second screen

5 POKE 106,PEEK(106)-4:REM * lower memory location *
6 GRAPHICS 0
7 REM * NEXT:store display list location  *
8 DL21 = PEEK(560):DL22 = PEEK(561)
If you POKEd the display list pointers with DL 11 and DL12 (POKE 560,DL11:POKE 561,131-12) then the T.V. would display the first screen.

If instead, you POKEd the pointers with DL21 and DL22 (POKE 560,DL21:POKE 561,DL22) then the T.V. would display the second screen.

100 GRAPHICS 8
102 COLOR 1:SETCOLOR 2,16,1
110 T=0
120 L=PEEK(106)
140 DL11=PEEK(560):DL12=PEEK(561)
155 AC=236:ED=185:XP=210:RA=625:YP=25
158 GOTO 500
160 AC=60:ED=40:XP=50:RA=100:YP=120
162 GOTO 500
170 AC=310:ED=280:XP=295:RA=225:YP=140
172 GOTO 500
180 AC=35:ED=25:XP=30:RA=25:YP=30
500 FOR X=ED TO XP
510 Y=-SQR(-(X-XP)*(X-XP)+RA)+YP
520 AC=AC-1
530 PLOT X,Y:DRAWTO AC,Y
540 Y1=SQR(-(X-XP)*(X-XP)+RA)+YP
550 PLOT X,Y1:DRAWTO AC,Yi
560 NEXT X
580 T=T+1
600 IF T=1 THEN 160
610 IF T=2 THEN 170
620 IF T=3 THEN 180
700 PLOT 100,50:PLOT 100,100:PLOT 75,75:PLOT 200,50
710 PLOT 200,140:PLOT 300,50:PLOT 100,150:PLOT 80,140
720 PLOT 300,100:PLOT 5,5:PLOT 5,100:PLOT 275,100
730 PLOT 100,120:PLOT 250,90:PLOT 225,140:PLOT 200,150
740 PLOT 100,10:PLOT 120,15:PLOT 300,25:PLOT 150,95
750 PLOT 2,150:PLOT 315,148
755 ?
760 ? " Use Joystick to change picture"
1000 POKE 106,L-32
1010 GRAPHICS 8
1020 COLOR 1:SETCOLOR 2,16,1
1030 T=0
1040 DL21=PEEK(560):DL22=PEEK(561)
1050 AC=100:ED=50:XP=75:RA=625:YP=25
1052 GOTO 1500
1060 AC=310:ED=290:XP=300:RA=100:YP=100
1062 GOTO 1500
1070 AC=270:ED=230:XP=250:RA=400:YP=37
1072 GOTO 1500
1080 AC=125:ED=95:XP=110:RA=225:YP=135
1500 FOR X=ED TO XP
1510 Y=-SQR(-(X-XP)*(X-XP)+RA)+YP
1520 AC=AC-1
1525 Y1=SQR(-(X-XP)*(X-XP)+RA)+YP
1530 PLOT X,Y:DRAWTO AC,Y1
1550 PLOT X,Y1:DRAWTO AC,Y
1560 NEXT X
1570 T=T+1
1580 IF T=1 THEN 1060
1590 IF T=2 THEN 1070
1600 IF T=3 THEN 1080
1700 PLOT 2,4:PLOT 100,95:PLOT 310,75:PLOT 270,50
1710 PLOT 50,140:PLOT 300,150:PLOT 224,120:PLOT 75,75
1720 PLOT 100,10:PLOT 120,40:PLOT 300,5:PLOT 270,75
1730 PLOT 150,20:PLOT 160,100:PLOT 180,50:PLOT 300,150
1740 PLOT 1,100:PLOT 150,150
1745 ?
1748 POKE 752,1
1750 ? " Use Joystick to change picture"
5000 ST=STICK(0)
5010 IF ST=14 THEN POKE 560,DL11:POKE 561,DL12
5020 IF ST=13 THEN POKE 560,DL21:POKE 561,DL22
5100 GOTO 5000

1 REM * CHECK DATA *
100 DATA 8005,13,797,526,996,60,352,839,223,834,393,835,138,280,112,757,731,119
550 DATA 7200,831,781,665,224,227,230,91,184,993,241,97,701,443,453,132,62,845
1030 DATA 9630,576,110,289,930,416,931,384,932,392,329,161,806,171,829,831,830,713
1580 DATA 7026,328,331,325,8,204,93,239,733,491,985,501,123,868,870,927

99 REM **** PAGE 1 ****
100 GRAPHICS 1
104 A=PEEK(106)
106 DL11=PEEK(560)
107 DL12=PEEK(561)
110 COLOR 1
120 SETCOLOR 2,16,1
122 SETCOLOR 1,8,10
124 SETCOLOR 0,18,5
130 POSITION 1,1:? #6;"THIS is"
140 POSITION 1,3:? #6;"PaGe i"
150 ? "Press the page number you want to see"
155 ? "?"
199 REM **** PAGE 2 ****
200 POKE 106,A-4
210 GRAPHICS 1
212 SETCOLOR 2,16,1
214 DL21=PEEK(560)
218 DL22=PEEK(561)
250 POSITION 12,1:? #6;"THIS is"
260 POSITION 12,3:? #6;"PaGe 2"
270 ? "Press the page number you want to see"
275 ? "?"
299 REM **** PAGE 3.****
300 POKE 106,A-8
310 GRAPHICS 1
312 SETCOLOR 2,16,1
314 DL31=PEEK(560)
318 DL32=PEEK(561)
350 POSITION 1,14:? #6;"THIS is"
360 POSITION 1,16:? #6;"PaGe 3"
370 ? "Press the page number you want to see"
375 ? "?"
399 REM **** PAGE 4 ****
400 POKE 106,A-12
410 GRAPHICS 1
412-SETCOLOR 2,16,1
414 DL41=PEEK(560)
418 DL42=PEEK(561)
450 POSITION 12,14:? #6;"THIS is"
460 POSITION 12,16:? #6;"PaGe 4"
470 ? "Press the page number you want to see"
475 ? "?"
499 REM **** MAIN LOOP ****
500 POKE 106,A-16
504 GRAPHICS 1
510 INPUT W
520 IF W=1 THEN POKE 560,DL11:POKE 561,DL12
530 IF W=2 THEN POKE 560,DL21:POKE 561,DL22
540 IF W=3 THEN POKE 560,DL31:POKE 561,DL32
550 IF W=4 THEN POKE 560,DL41:POKE 561,DL42
560 IF W<1 OR W>4 THEN 510
570 GOTO 510
1 REM * CHECK DATA *
99 DATA 6560,327,6,987,170,173,797,275,277,283,71,972,967,568,377,25
214 DATA 5168,171,177,124,26,970,571,379,30,9,278,173,179,127,30,971,572,381
400 DATA 6905,74,10,279,175,181,178,82,972,573,652,79,14,856,689,693,697,701
560 DATA 1494,656,838