• Aucun résultat trouvé

Daniel R. Widyono

Dans le document 64/128 (Page 93-97)

• •

• •

• •

• •

• •

Slowpoke

Daniel R. Widyono

This very short machine language program, from a 12-year-old author and programmer, allows you to control the

speed of the PRINT statement. We've added some sugges-tions for using this unique routine. For the Commodore 64

and 128 in 64 mode.

When a 64 or 128 is printing something or listing a program, you can slow it down by pressing the CTRL key. You have a choice of two speeds, regular and slow. If you've ever used an Apple II, you probably know that it gives you a wider choice;

it has 256 printing speeds.

Can a similar feature be added to the 64 and 128? You could use a BASIC subroutine, although it would be rather complicated. You would have to define a string, do a FOR-NEXT loop for the length of the string, use MID$ to pull out a character, print the character, use another FOR-NEXT loop for the delay, and then continue until the message is done. The subroutine would work only as part of a program. You'd still have to press CTRL to slow down program listings.

A Better Way

There's a short, simple, and effective machine language alter-native to the BASIC subroutine. Enter and save "Slowpoke."

N ow run the program and type LIST. It lists as you would expect. Enter

POKE 251,255:LIST

and you'll see a very slow listing. The program takes more than a minute to list (compared to less than a second at the regular speed). POKE a zero into 251, and the computer re-turns to normal.

You now have 256 different printing speeds, from 0, the fastest, to 255, the slowest. There's no need to use a BASIC subroutine or to SYS. Just POKE the speed you want into memory location 251. It works within programs and also in immediate mode. It even slows down error messages and the

85

CHAPTER 2

READY prompt. If you use a printer while "Slowpoke" is on, it will wait between lines, but not between characters. You'll notice, however, that your typing is not affected; the computer still reads the keyboard at the regular speed.

Pressing RUN/STOP-RESTORE disables Slowpoke. To get it back, you'll have to run the loader program again or type POKE 806,167: POKE 807,2

If you use these POKEs, make sure you put them on the same line, separated by a colon.

How It Works

I was looking through a memory map and discovered a vector at 806-807 that points to the character-out (CHROUT) routine in the operating system. The Kernal CHROUT vector in ROM (which cannot be POKEd) uses this vector in RAM (where it can be changed) to find the routine that sends a character to screen memory, to a printer, or to a tape or disk file.

When the computer gets an instruction to PRINT, it checks the Kernal vector, goes to this vector, and finally ends up at the instructions for printing. The computer has to look up the address for every character it prints, even if the charac-ters are part of a long string. The address is stored in the usual low-byte/high-byte format.

Vectors are like highway signs, pointing the way to a des-tination (in this case, the CHROUT routine). By changing the numbers in memory locations 806 and 807, you can change the route, adding a slight detour, a machine language delay loop. When the computer tries to PRINT or LIST, the vector at 806-807 sends it to the delay loop, which is followed immedi-ately by a jump to the usual CHROUT part of the operating system.

Since machine language is so fast, you need two delay loops, one inside the other. First, the X register is loaded from location 251. If 251 holds a zero, the rest of the loop is skipped (using BEQ, Branch if EQual to zero). Then the Y register is loaded with the number 255. Y is decremented (DEY) until it becomes zero. Next, X is decremented and a BNE instruction (Branch if Not Equal) loops back to the DEY loop. The higher the number in 251, the longer the loops take. At the slowest printing speed, X has to be decremented 255 times, while Y is decremented 255 X 255 times (over 65,000 times).

86

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

SCREEN CONTROL

The values of X and Yare always zero after the loop has been done. So, to be safe, the values of the A, X, and Y regis-ters, as well as the processor status, are saved. They are pushed onto the stack before the routine is executed and pulled off after the delay loop.

The delay loop occupies a small area of memory at loca-tions 679-767. If you need this RAM for other machine lan-guage programs, Slowpoke is relocatable. You can move it to the cassette buffer, beginning at 828, or other free sections of memory. If you have a program which uses zero-page location 251, change the 251 in line 30 to another available zero-page location.

Practical Ideas

One useful application of Slowpoke is to slow down program listings. Load and run Slowpoke; then load the program you're working on. You can control the speed of the listing with a single POKE.

If you are working on a game and the rules take up a couple of screens, you can use Slowpoke to make them scroll up slowly, rather than the usual method of printing a screen and waiting for the user to press a key to get the next page.

This same idea could be adapted to a story program: At the end of the story, roll the credits (just like at the movies). Many programs that put a lot on the screen could use a printing speed control.

The program is a good debugging tool if you are having problems getting the screen graphics just right. You should note, however, that only the PRINT statement is affected;

POKEing to screen memory is no slower than usual.

Slowpoke can be very useful with a program that exam-ines tape or disk files. You would open the file, read an item, print it to the screen, and then use a GET statement to see whether a key has been pressed. If not, then continue with the next item. If f1 has been pressed, speed up the printing; if f7 has been pressed, make it slower. You might even adapt it to a speed-reading program.

Finally, if you're familiar with machine language pro-gramming, you could make a slight modification to add sound effects. Just before the delay loop, turn on the noise generator and turn it off at the end of the loop. Each time you print

87

CHAPTER 2

something, you will hear, say, the clicking of a typewriter. If you want to get really fancy, you can add a bell sound at the end of each line.

Slowpoke

For mistake-proof program entry, use "The Automatic Proofreader," Appendix B, to type in this program.

AX 10 CK=0:POKE251,0

FK 20 FORJ=679T0703:READA:CK=CK+A:POKEJ,A:NEXT HD 25 IFCK<>3615THENPRINT"CHECK DATA STATEMENTS":E

ND

PS 30 DATA72,138,72,152,72,8,166,251,240,8,160,255 ,136,208,253,202,208,248,40

GG 40 DATA104,168,104,170,104,76

HD 50 IFPEEK(807)<>2THENPOKE704,PEEK(806):POKE705, PEEK(807)

GE 60 POKE806,167:POKE807,2

88

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

Dans le document 64/128 (Page 93-97)