Jump to content
IGNORED

Peek and Poke help


Recommended Posts

I recently got my old Atari 800 up and running after having it packed in my mom's attic for the last 20 years.

 

Ive had a blast showing my son all the old basic games that i wrote, and we are actually considering the undertaking of writing a new game together.

 

Here's my question, i remember that way back when, i never really understood how to use peek and poke to draw something on the screen that i would be able to then move with a joystick. I remember that it gave me so much trouble that i used to program my games with just drawing and erasing a figure to make it move, which wasnt too efficient or looked too good either.

 

Is there anyplace on the web i can browse to really let a dummy like me learn how to use peek and poke, or anyone here who can give me a crash course? i'd love to be able to understand this so i can use it to build a game with my son.

 

thanks in advance!

Link to comment
Share on other sites

Page 6 Magazine Issues 4 and 5 have an article on Memory Mapped Screens :)

 

Post your old basic games for us to play :D

 

thanks for setting me in the right direction.

 

i actually thought about posting them, but i think you guys wouldnt think they were worth your time. Now that i've got about 20 more years of programming under my belt, i was examining the code of these old babies and some of it is cringeworthy.

 

still missing 3 of my programs, but i still have a ton of disks to get through... hopfully i'll find them all.

Link to comment
Share on other sites

I can give you a basic on PEEK and POKE insofar as drawing things on your screen.

 

First and most important location is SAVMSC, which is pointed to by PEEK(88)+256*PEEK(89) ... depending on the graphics mode, POKEing to SAVMSC (and add in the number of screen spaces past the top of the screen) will plot a character, or a 2-4 pixel combination depending on the graphics mode.

 

Text mode is easy: In Graphics 0 or any text mode based off 40 columns, the formula is POKE SAVMSC+(row*40)+column, char

 

Keep in mind, char is arrived at using the Atari's internal character set layout, not ATASCII. This means for instance, 0 corresponds to the space char, not a heart.

 

Using bitmap mode is different, you are still working at 20-40 columns but anywhere from a 4-8 pixel combination, when in any of the hi-res modes (Graphics 8, 15, 7)

 

In graphics 8, the table is like:

 

128 64 32 16 8 4 2 1

--- --- -- -- - - - -

 

The value to poke is arrived at by which screen column (of 8) you want the pixel in. then you poke using the same formula as for Graphics 0 shown above.

 

In graphics 7 and 15, you get a 4 column table, with 4 colours:

 

64 16 4 1

0 --- --- --- ---

1

2

3

 

The graphics 7/15 color # is arrived at by multiplying the color # by whatever numbers you see in the columns you want to plot in. Again, the same formula as in Graphics 0.

Link to comment
Share on other sites

I can give you a basic on PEEK and POKE insofar as drawing things on your screen.

 

First and most important location is SAVMSC, which is pointed to by PEEK(88)+256*PEEK(89) ... depending on the graphics mode, POKEing to SAVMSC (and add in the number of screen spaces past the top of the screen) will plot a character, or a 2-4 pixel combination depending on the graphics mode.

 

Text mode is easy: In Graphics 0 or any text mode based off 40 columns, the formula is POKE SAVMSC+(row*40)+column, char

 

Keep in mind, char is arrived at using the Atari's internal character set layout, not ATASCII. This means for instance, 0 corresponds to the space char, not a heart.

 

Using bitmap mode is different, you are still working at 20-40 columns but anywhere from a 4-8 pixel combination, when in any of the hi-res modes (Graphics 8, 15, 7)

 

In graphics 8, the table is like:

 

128 64 32 16 8 4 2 1

--- --- -- -- - - - -

 

The value to poke is arrived at by which screen column (of 8) you want the pixel in. then you poke using the same formula as for Graphics 0 shown above.

 

In graphics 7 and 15, you get a 4 column table, with 4 colours:

 

64 16 4 1

0 --- --- --- ---

1

2

3

 

The graphics 7/15 color # is arrived at by multiplying the color # by whatever numbers you see in the columns you want to plot in. Again, the same formula as in Graphics 0.

 

thanks synth. i think this will help, but first i fear i am going to need an even more 'dumbed-down' explanation of how to start. I understand the basic concept, but i just dont think im understanding exactly what peek and poke "do" enough to know how to get started.

 

like, for example, if i wanted to draw a small smiley face, and be able to move it around the screen with the joystick. based on what you wrote, and some of the documentation others have mentioned, i have an idea, but im not sure exactly how i would start getting that done.

Link to comment
Share on other sites

 

thanks synth. i think this will help, but first i fear i am going to need an even more 'dumbed-down' explanation of how to start. I understand the basic concept, but i just dont think im understanding exactly what peek and poke "do" enough to know how to get started.

 

like, for example, if i wanted to draw a small smiley face, and be able to move it around the screen with the joystick. based on what you wrote, and some of the documentation others have mentioned, i have an idea, but im not sure exactly how i would start getting that done.

 

Very dumbed down explanation of peek and poke:

 

Peek and poke are used to directly manipulate memory locations. Peek is there to retrieve a value from a location, poke is there to set a value to a location. Each location can hold a number, ranging between 0 and 255 inclusive. Each number is dependent on interpretation.

 

A 64kB computer has 65536 of these locations, but most of the times, not all are used for storage of fluid data. Only RAM normally holds fluid data. ROM has set locations, which therefore can be peeked, but not poked. Also there are locations that are reserved for device control. Poking a value in one of those locations may change properties of these devices, but peeking them may not always retrieve that same information again. In most cases, peeking a location will give data relevant to the device though.

 

In the specific case of the Atari computers, there are special areas you should be aware of.

 

The first 256 bytes are called the zero-page. These locations are used a lot by the Operating System, and this program code will maintain and use the data contained here for specific purposes. For example, locations 88 and 89 are used by the screen handler portion of the Operating system to hold the starting location of the editable screen, which may or may not be the displayed screen, but in normal operation, it does.

 

Yes, these are 2 locations, that give 1 address, since each location can only hold numbers from 0-255, the total number of locations is 65536 however, which is 256*256. Hence 2 of these numbers are needed to give a full location number.

 

Peek(88)+256*peek(89) will output a number in the range 0-65535 and is in this case the location of the first character on the screen.

 

It is not always needed to use 2 locations in this way though. For example, a character set can be changed by setting the location 756. This is because due to restrictions, a character set always starts on a so-called 1k-boundary.

 

You may for example use poke 756,204 to activate the XL/XE's international character set. These replace the control-characters aka graphics characters. Turn it back by using poke 756,224. The starting location of the international character set can thus be found by 256*204 = 52224 whereas the normal character set starts at 256*224 = 57344. Each character set is 1024 bytes in size.

 

The second set of 256 bytes (locations 256 till 511) also known as page 1, is used by the Central Processing Unit as it's Stack. Avoid using this area directly. There are specialized CPU instructions to manipulate these locations in a consistent manner, for use in programs.

 

Using some of the above, making and using a self-defined character set can also be done. And I think you're aiming for that. Therefore I'll give a small listing below with comments to what each line does.

 

10 RAMTOP=PEEK(106):NEWCHARSET=RAMTOP-4:POKE 106,RAMTOP-4:GRAPHICS 0

- This line asks the OS where the highest available volatile memory location resides, then sets this down 4 pages. Since a page is 256 bytes, this reserves 1024 bytes needed for a new character set. It then invokes the Graphics command to makes this area unused since normally the screen data and display list are just below RAMTOP.

 

20 FOR X=0 TO 1023:POKE NEWCHARSET*256+X, PEEK(224*256+X):NEXT X

- This line copies the standard character set from ROM into the reserved RAM so we have something to work with.

 

30 POKE 756,NEWCHARSET

- This tells the OS to use the newly made character set to display characters on the screen.

 

40 PRINT "!";CHR$(34);"#$%"

- This just puts some characters on screen. We'll change these. Normally the Space is the first character represented in the characterset, followed by these.

 

50 FOR X=0 TO 47:READ CHARDATA:POKE NEWCHARSET*256+X,CHARDATA:NEXT X

- Including space, we redefine 6 characters. Each character is 8 bytes of data, so we need to read and poke 48 locations.

 

60 END

- We stop the program here. Keep in mind, that concurrent running of the program will keep on lowering RAMTOP, thus reserving 1024 of space each time. This will lead to an error when done a lot of times, since the memory available for programs will become less each time. To stop this from happening, press RESET before running the program again.

 

70 DATA 0,0,0,0,0,0,0,0

- Character data for the space. Since we don't want spaces to change, we'll keep them empty.

 

71 DATA 60,90,219,255,189,189,66,60

- Character data replacing the exclamation mark. It's a smiley.

 

72 DATA 0,58,58,116,0,0,0,0

- Character data for ". Similar, but a bit more fancy.

 

73 DATA 60,66,129,129,129,129,66,60

- Character data for #. Makes it into a thin circle.

 

74 DATA 0,60,66,66,66,66,60,0

- Character data for $. Similar circle, but smaller.

 

75 DATA 0,0,0,24,24,0,0,0

- Character data for %. A centered dot.

 

Some extra hints for your program:

- Try using GRAPHICS 1 or GRAPHICS 2 or another text mode, in conjunction with POSITION and PRINT #6 to display the characters in color.

- A GRAPHICS 1 or GRAPHICS 2 character set, only uses 512 bytes. The other characters are represented as a different color.

- The order of the characters in a character set is the same as the order of the characters when POKEd into screen memory (remember 88,89) but are different than the order in PRINTing. Examine these differences for yourself.

- Make binary representations of the numbers in the DATA lines, in an 8x8 grid, each row is 1 number.

 

I hope this will set you on the right path to make what you want.

Link to comment
Share on other sites

Alpha,

 

thank you so much for taking the time to put that together for me.

 

i just read about half of it, but im going to save the rest of it for tonight when i am at home so i can really digest it. too many distractions here in the office :)

 

again, thanks so much. hopefully this will give even a dummy like me the basic understanding i will need to get my feet wet with peeking and poking.

Link to comment
Share on other sites

Alpha,

 

thank you so much for taking the time to put that together for me.

 

i just read about half of it, but im going to save the rest of it for tonight when i am at home so i can really digest it. too many distractions here in the office :)

 

again, thanks so much. hopefully this will give even a dummy like me the basic understanding i will need to get my feet wet with peeking and poking.

 

You're very welcome. Once you grasp the basic concepts behind peek and poke, you may want to look at List of Atari 400 800 XL XE Books on AtariMania. Those books listed there are downloadable as PDF files.

 

Special recommendations:

Dr. C. Wacko Presents Atari Basic

Dr. C. Wacko's Miracle Guide to Creating Arcade Games

Mapping the Atari and/or De Re Atari

 

The first two will give you more in depth knowledge about peeking and poking, and are a nice read aswell. No special knowledge is required for these books and they will explain a lot in a comprehensive manner. Specially the Miracle Guide will take you right through peeks, pokes, graphics, including player-missile graphics and even some machine language.

 

Mapping and De Re are like programmer's bibles. They'll list all memory locations, and how they are used. Darn good reference materials when you want to tap into the full potential of your Atari.

 

Keep in mind though, that there are differences between the 400/800 and XL/XE series of computers, although most of it is the same. The subtle differences will eventually become apparent, and can trip you up, especially when you're going in depth.

Edited by Alphasys
Link to comment
Share on other sites

thanks again for the links, i'll definitly look at those when im ready to begin my journey.

 

and thanks for pointing out that there are differences between models. as i said, i have an 800 but am looking to buy either an xl or xe so thats good to know.

 

Alpha,

 

thank you so much for taking the time to put that together for me.

 

i just read about half of it, but im going to save the rest of it for tonight when i am at home so i can really digest it. too many distractions here in the office :)

 

again, thanks so much. hopefully this will give even a dummy like me the basic understanding i will need to get my feet wet with peeking and poking.

 

You're very welcome. Once you grasp the basic concepts behind peek and poke, you may want to look at List of Atari 400 800 XL XE Books on AtariMania. Those books listed there are downloadable as PDF files.

 

Special recommendations:

Dr. C. Wacko Presents Atari Basic

Dr. C. Wacko's Miracle Guide to Creating Arcade Games

Mapping the Atari and/or De Re Atari

 

The first two will give you more in depth knowledge about peeking and poking, and are a nice read aswell. No special knowledge is required for these books and they will explain a lot in a comprehensive manner. Specially the Miracle Guide will take you right through peeks, pokes, graphics, including player-missile graphics and even some machine language.

 

Mapping and De Re are like programmer's bibles. They'll list all memory locations, and how they are used. Darn good reference materials when you want to tap into the full potential of your Atari.

 

Keep in mind though, that there are differences between the 400/800 and XL/XE series of computers, although most of it is the same. The subtle differences will eventually become apparent, and can trip you up, especially when you're going in depth.

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

Just found this thread and like it!

 

Just got back to Basic at NOMAM 2011, where we had a 10 lines Basic competition. I´ve never used Peek and Poke much, but with these explanations here I start having fun with Basic and finally really learn it. :-)

 

Keep on explaining. :-)

Link to comment
Share on other sites

Just found this thread and like it!

 

Just got back to Basic at NOMAM 2011, where we had a 10 lines Basic competition. I´ve never used Peek and Poke much, but with these explanations here I start having fun with Basic and finally really learn it. :-)

 

Keep on explaining. :-)

 

When there are questions to be answered, I'm game. But there's not much to explain beyond what's been told before, unless you have specific questions. Once you get the idea behind peek and poke, you may want to experiment with it. And keep a copy of DeRe Atari, Mapping the Atari, or for german users, Atari Intern, at hand so you can get information on all the locations and what they're used for.

 

The only problem I see happening, is that with all these memory locations, you don't know where to look to get a certain result. Well, that's why there's forums like this one. Ask and ye shall receive.

 

There are a few things I'd like to make clear though:

 

  • Your Atari is a computer based on a 6502 processor. This means that you have 65536 distinct addresses, each holding a value between 0-255.
  • Once you start your Atari computer, it'll start running a program. This is called the Operating System ROM, which occupies locations 49152-65535, with the exception of 53248-55295, which are the locations to directly manipulate devices such as screen, sound, joystick ports and the serial IO, and in some cases also any extra hardware that's built in or plugged into the cartridge port.
  • Locations 0-1535 are used by the Operating System ROM (and some for Basic) to hold values for its own use, with the exception of locations 256-511, which is the processor stack and should be avoided. These locations are of most interest to Basic programmers, since changing these values will manipulate the Operating System to great effect.
  • After all that we're left with locations 1536-49151, which are free RAM and are used to load user programs. 1536-1791 (the infamous Page 6) is kept free in most cases and this area is commonly used for small machine code routines in Basic. 1792 onwards, is the usual location where DOS loads. Depending on the DOS and settings, the end address differs, but after that, is where Basic will start filling memory with your program.
  • Once you insert a Basic cartridge, or have it enabled on the XL/XE, this will occupy locations 40960-49151 and turn it into ROM. (This is also true for most other cartridges, but there are some different behaviors possible.)

When you start reading a book like DeRe Atari, you'll find that a lot of these "interesting" locations are "shadow registers". I'll briefly explain what these are and why they are there.

 

  • As told above, locations 53248-55295 are locations to directly manipulate devices. These special chips (Antic, GTIA, Pokey) work in a bit of an odd way, different to normal RAM. Peeking a location in this range, will not give the same value as you may have poked into it. In fact, the action of peeking or poking any of these locations may have effect on the workings of these devices already.
  • Since a value, once poked into these locations, will not be retrievable by peeking, they need to be stored in a different location as well which will retain the value until changed. There's your "shadow register".
  • During a VBLANK interrupt, a lot of these "shadow registers" are read and poked into the device chips.

One good example of this is what happens with a SETCOLOR command. Take for example SETCOLOR 2,8,0 which will make your graphics 0 screen a dark blue. Basic interprets this as "Calculate 8*16, add 0, then put this into location 710". So this is equivalent to POKE 710,128

But 710 isn't a hardware location, so how come the screen turns dark blue? That's where the OS and its VBLANK interrupt comes in. It will take the value of 710 (among others) and stuff it into the GTIA register located at 53272. Reading 53272 does however not return the value 128 again. This is where "shadow registers" come in handy. (The Atari's Attract mode color changes are directly linked to several of these shadow registers for example.)

 

Just for completeness sake, I want to mention that these shadow registers are normally only copied into hardware registers in the VBLANK interrupt, since that's the time that the Antic is "done" drawing the screen, and setting registers will not cause any tearing or noise. If you do want to change the color, or any other hardware register, during the screen drawing, you will have to directly manipulate the hardware registers at the appropriate time.

 

Your Basic program thus relies on the Basic ROM to interpret and execute your program, but the Basic ROM in turn relies on the Operating System ROM to do certain tasks.

 

Recap of all what's above: Your computer is basicly a processor, some special chips for device handling and a bunch of memory. It's the OS, Basic and/or other program that gives some of this memory a special meaning. Without a program, these are just locations that the CPU can read and write, but it'll take instructions for it to do so. Memory just holds numbers. The CPU manipulates the numbers. Numbers have no special meaning, unless they are given one by context.

 

(Depending on context, 0 may mean just 0, it may also mean "black", "silence", "equality", the heart-character, the space character, "display one screen-line with the current background color" and even can mean "Stop execution of the current program, set a flag signaling this, retrieve a new program address from location 65534 and 65535 and continue from there". It just depends on what is "using" the 0.)

 

Thusfar this little primer into basic Atari 8-bit operation. I hope you'll get some insight from this. Questions may arise, just post them and they will be answered, not necessarily by me.

Edited by Alphasys
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...