Task: Learn About Scanlines Using PEEK in Atari BASIC
Needed: Atari BASIC
Time: 5-10 minutes
Introduction
The Atari 8-bit computers generate the video display one horizontal line at a time, starting at the top of the screen and progressing to the bottom approximately 60 times per second on NTSC systems (50 times per second on PAL systems). This rapid refresh creates the illusion of a stable, continuously displayed image.
The Atari video system is a collaboration between two custom chips. ANTIC reads the display list and screen memory to determine what graphics should appear on each line, while GTIA converts that information into the final color video signal sent to the monitor or television. As the electron beam (or the equivalent process in a modern display) moves down the screen, the operating system continuously updates a hardware register containing the current scan line number. This is essential for timing in assembly language programs where may you need to execute code during the horizontal and vertical blanks when the beam is moving to the next line or the top of the screen to start over.
In this project, we’ll use a simple PEEK command at address 54283 in Atari BASIC to read the current scan line in real time. Watching this value change reveals how the Atari constructs each video frame and provides a foundation for more advanced programming techniques such as display list interrupts (DLIs).
Note that PEEK(54283) returns the vertical line counter value which corresponds to a value from 0 to 130 or 131 possible values. This is exactly half the 262 lines per TV frame for NTSC displays. The values range from 0 to 155 for PAL. You can read about this address at The Atari Memory Map. The reason for the half approximation of the 262 scanlines is that the number 262 is too big to fit into an 8-bit byte which can store numbers from 0 to 255.
Instructions
Here is a simple BASIC program to PEEK and see what approximate scanline (accurate to two scanlines) the Atari is drawing when the command is executed. Remember, the entire screen is drawn 60 times per second, so this happens really fast.
10 REM Returns scanlines 1 to 262 (NTSC)
20 PRINT ((PEEK(54283)+1)*2)
30 FOR I=1 to 200:NEXT I
40 GOTO 10

Comments
Programming in assembly language on the Atari 2600 requires counting scanlines and keeping track of which pixel is being drawn. This is because the CPU actually draws the screen due a lack of a GPU on the 2600. Programmers need to wait until the scanline is done to execute game code during the horizontal blank and later the vertical blank.