"Some Useful PEEKs and POKEs" (from Vol. 1 No. 11) All of us received a "Guide to the Tandy 1000" when webought our computers. After reading the third section ofthat manual, "BASIC -- A Reference Guide", you probably feltthat you had a rudimentary understanding of all of thecommands that were introduced. You may not have been ableto sit down and write a professional style arcade game, butyou at least understood the purpose of graphics commandssuch as COLOR, SCREEN, CIRCLE, LINE, DRAW, and so on.Similarly, you may not have an immediate need for SIN, COS,LOG, SQR, and so -- but at least you knew the purpose ofeach and might imagine a time in the future when you mayneed them. On the other hand, two commmands -- PEEK and POKE -- mayhave seemed rather mysterious. Most of the other BASICcommands are fairly obvious, but why would anybody want towrite or retrieve data bytes to or from various memorylocations? For that matter, what is a memory location andwhat kind of data is stored there? We can answer those questions by constructing a rathercomplicated allegory to illustrate the purpose of PEEK andPOKE. All of our Grandy computers contain a certain amountof memory -- from 128K (or 128,000 bytes or units of memory)to 640K. Imagine that you are in the post office of a largecity. In front of you are 640,000 post office boxes. Eachof the boxes is assigned a number from 0 to 640,000. Eachof those boxes contains a slip of paper; a number from 0 to255 is written on each slip of paper. It should be obviousby now that each of those imaginery post office boxes isequivalent to a byte of memory -- each has an address or amemory location number assigned to it, and each contains anumber. PEEK and POKE are no longer as mysterious as they werebefore. When you PEEK at a certain address, you areelectronically taking a PEEK through the glass window inthat post office box and seeing what number is written onthat slip of paper. And when you POKE a number into anaddress, you are writing a new number on an imaginery slipof paper and pushing it into that post office box. The oldslip of paper is pushed out the back of the box to be lostforever, and the box now contains the value that youinserted. Some boxes are special -- they are sealed so that you canPEEK in, but you cannot POKE anything in to change theircontents. Those "boxes" are equivalent to ROM -- or ReadOnly Memory. In the Grandy, ROM addresses are used to storea minimum amount of permanent information that enables thecomputer to load MS-DOS from the disk at start-up. (Each ofthe numbers stored in the ROM area represents a certainaction or operation. When you turn on your Grandy or whenyou press the reset button, the computer executes each ofthe operations indicated by the "code numbers" found in theROM. Most of the rest of your computer's memory is RAM -- orrandom access memory. You can use both PEEK and POKE toexamine and/or alter those memory locations. Those "boxes"are used to store the portion of MS-DOS that is read fromthe disk, the BASIC language, and a great deal of additionalinformation. In fact, if you know what is stored where, youcan use PEEK and POKE to great advantage in your BASICprograms. The BASIC program in the listing that follows contains anumber of very interesting and useful PEEKs and POKEs.Before we actually begin cruising through memory, however,there is one other important point to discuss, the DEF SEGcommand in line 140. When BASIC encounters the commands DEFSEG = X and PRINT PEEK(Y), it actually takes a PEEK ataddress number X times 16 plus Y and prints its contents.Thus, the DEF SEG = 0 of line 140 is vitally important. Ifyou use these PEEKs and POKEs in your programs, be sure toinclude DEF SEG = 0. Lines 130 through 320 illustrate how you can determine thememory size, the number of disk drives, the type of monitor,the presence of an RS-232 card, and the status of the CAPSLOCK and NUM LOCK keys of the computer executing yourprogram. You have probably already thought of some uses foreach of these items. For example, if your program requires320K to operate, you can check at the top of the program forthat amount. If the user has only one drive, you need notask "which drive will you use (A or B)?" If the computer isattached to a monochrome monitor, you might use a differentcolor scheme. And so on. A better way to check for all upper case or mixedupper/lower case entry is to evaluate PEEK(1047) AND 64. Ifthat value is zero, then the computer is in mixed upper andlower case; if it is 64, then the computer is in all capitalmode. You can change to mixed case with a POKE 1047,PEEK(1047) AND 191. You can insure all capital letters witha POKE 1047, PEEK(1047) OR 64. Similarly, the computer is in numeric mode if PEEK(1047) AND32 equals 32. It is in non-numeric mode if that PEEK equalszero. You can switch to numeric mode with a POKE 1047,PEEK(1047) OR 32. Switch to the non-numeric mode with aPOKE 1047, PEEK(1047) AND 223. Line 340 is especially useful and deserves special mention.The POKE 1050, PEEK(1052) command clears the keyboard bufferso that even if any keys were pressed before that line wasreached, they will not register with any INKEY$ or INPUT$statement which follows. There are a few keys that cannot be detected through the useof the INKEY$ statement, such as the SHIFT keys, CTRL, andALT. However, the PEEK statements presented in lines 380through 530 prove that you CAN detect those keys -- if youknow where to look. Lines 570 through 660 show that there is a way to disablethe CTRL-BREAK sequence which normally interrupts executionof your BASIC program. The values in addresses 108 through111 are saved in lines 570 and 580. New values are POKEd inline 590, and once they are in place, CTRL-BREAK no longerworks. The original values in locations 108 through 111 arerestored in lines 650 and 660. Obviously, PEEK and POKE enable you to do things in BASICthat you never dreamed possible. They can make a very goodprogram into a truly amazing one. Go ahead -- experimentwith the tricks presented in this article and see what youcan do. If you have any questions or comments, write to mein care of ONE THOUSAND. See you next month!