Thursday, November 19, 2015

Atari Hardware Explained

The Atari 2600 is a very compact system, even though this may not be reflected in the standard woodgrain case in which it is housed.  Inside each system are three chips, the CPU, the Television Interface Adapter (TIA) and the RAM, Input, Output & Timer (RIOT).  These three chips work together along with a cartridge, switches and a controller to display games to a TV.  Lets discuss how the design of these chips influences the games that were produced.

CPU

First, lets start with the CPU, which is a MOS 6507 (a.k.a. C010745).  The 6507 is a budget version of the 6502 an in the Atari 2600 it runs at 1.19MHz (1/3 of the NTSC color clock frequency).  It is a fully-functional 6502 in a 28-pin package.  The full 6502 comes in a 40-pin package.  As a result of the size reduction there is no support for hardware interrupts and the 6507 only has 13 (of a maximum of 16) address lines.  This limits the amount of addressable memory that the CPU can access directly to 8KB (kilobytes).  Unlike the NES's 6502 variant, the 6507 has a functional binary-coded decimal mode.  The TIA is connected to the CPU's RDY line, so it can halt the processor.

RIOT

Next comes the RIOT chip, which is an MOS 6532 Peripheral Interface Adapter (a.k.a. C010750).  This 40-pin chip contains a programmable timer, 128 bytes of RAM and two 8-bit parallel input and output ports.  It has 9 registers, some of which are read only, some write and some can do both.  The timer counts down an 8-bit value by one of four orders of magnitude.  128 bytes of RAM substantially limits the complexity of games because that is where all a program's variables must live.  In addition, this RAM is also used by the stack.  The stack is vital for subroutines, which can be many in a game.  On the plus side, the RAM is located in the 6507's zero page, so accessing it will be fast.

One of the I/O ports is exclusively used for input from the five switches on the console, one bit for each switch.  Each bit of the second I/O port can be set to be an input or an output.  As an input, it reads the four directions from each joystick (one direction per bit), the paddle button from each of the four paddles or the position of the driving controller.  As an output, it strobes the keyboard controllers.  This I/O port can also be used to write to and read from more modern port peripherals like the AtariVox.

TIA - Input and Sound

Finally we come to the TIA, the most complex chip in the system.  This is a custom 40-pin Atari chip C010444D which Atari strangely never got around to patenting, leading to cloned chips from Mattel and Coleco later in the 2600's life.  It was never an off-the-shelf chip, unlike the 6507 and 6532.  The TIA handles all graphics and interfacing with the TV, all sound and handles the remainder of the inputs.  It is also mapped into the zero page of the 2600's memory map.  It contains 45 write-only registers and 14 read-only registers.  Let's discuss its features in inverse order of complexity.

The TIA has four input ports, each dedicated to the input from a single paddle.  Paddles are read in a way similar to how a joystick is on the IBM PC.  A write to a certain register discharges the a capacitor and the position of a connected potentiomer dictates how long it takes to charge the capacitor.  The length of time it takes the capacitor to charge dictates the position of the paddle.  These registers also are used to read the keyboard controllers.  Essentially the keyboard controllers act like paddles set to a specified resistance value.  It also has two ports for reading the state of the joystick switches or the driving controller buttons.

Sound on the 2600 is geared more toward creating interesting sound effect than music.  Each of the two audio channels can emit a pure square wave tone, but there are only five bits to divide the frequency. The range of the 2600 is from 30,000Hz (divide by 1) to 937.5Hz (divide by 32).  Even the less than impressive TI SN 76489/96 has 11-bits of frequency control.  There are 4-bits of volume control, but the 2600 is just not very musical.  Even so, some games like Gyruss, Mountain King and Supercharger Frogger have impressive music.  The noise is based off a polynomial counter tweaked by various settings. There are 16 combinations (10 unique) of tone and noise to choose from in the 2600, and they are much more geared to sound effects.  You can hear what each selection and frequency sounds like here : http://www.randomterrain.com/atari-2600-memories-batari-basic-music-toy.html

TIA - Graphics

Most of the CPU's time is spent drawing the graphics, line by line.  The 2600 does not have a frame buffer and it does not have dedicated video memory.  More modern game consoles write the graphics data into a frame buffer and into sprite memory and let the video display chip take care of drawing it to the screen.

Atari 2600 graphics have to be fed into the TIA each and every line that the TV draws.  Whatever time is left over during horizontal blanking (the time it takes for the TV's electron beam to return from the right side of the screen to the left) is spent on setting up the graphics for the next line.  This is what is called racing the beam.  The vertical blanking period, (the time it takes for the beam to return to the top left portion of the screen from the bottom right) is typically used for game logic, sound and input reading.

The Atari 2600 is capable of displaying 128 colors, 8 of which are solely grayscale from black to white.  The remainder are spread across the NTSC color wheel with a great variety of variation.  The horizontal resolution of the Atari 2600 is always 160 pixels.  The vertical resolution of the Atari 2600 is not fixed but typically is 192 lines.  It is up to the programmer to start the vertical retrace period.

The graphics are made up of seven elements, a background color, a playfield, two players, two missiles and a ball object.  The background and the playfield make up what would be known in later consoles as the background graphics and the player, missile and ball objects would later be known as the sprite graphics.

Combat - Note the reflected playfield and the two players.  Each player fires a missle with the same color
The background color a single color that is set for the entire TV area, unless changed on a particular scanline.  When the color is changed it can be masked by the playfield.  Air-Sea Battle has frequent changes of the background color.

The playfield consists of 20 bits per line.  If a bit is on, the playfield color is displayed.  If the color is off, then the background color is displayed.  Each bit of the playfield is four pixels wide, giving the backgrounds to Atari 2600 games a characteristic low resolution look.  Additionally, those 20-bits only cover the left side of the screen, so the right side of the screen displays a duplicate or a mirror image of the left side of the screen.  This tends to give the backgrounds a symmetrical look in many Atari 2600 games.

Combat - Note the second copy of Player 2, the repeated playfield and that the playfield has priority over the players
When you see counters or scores on the top of the screen, these are usually playfield graphics.  But as I mentioned, there is only enough room in the playfield registers for half the screen, so what happens when one side's 0 turns into a 1?  You can time your writes to the playfield registers in mid scanline so you get a different playfield on the right side as opposed to the left side.  You can also set the colors of each side to the colors of the player 1 and player 2 objects.

Video Olympics - The ball object is used, and the scores are playfield bits changed in midscanline
The ball object is a one pixel object that can be placed anywhere on a scanline.  It uses the same color as the playfield on the same scanline.  The ball was intended to make Pong games like Video Olympics possible on the 2600.

The player objects are up to eight pixels wide, with a 1 bit showing the player color and a 0 bit showing a background or playfield color.  Two players, two colors.  Each player has a one pixel missile object associated with it which is the same color as the player.  Each player and missle object can also be placed anywhere on a scanline.  Multi-colored objects are accomplished by changing the color of the player object each line.  These graphics are best suited for Tank-line games like Combat.

Space Invaders - The player cannon is stretched, and each invader is a player copied twice, the copy is removed when shot
Ball and missile objects can be stretched to 2, 4 or 8 pixels.  Each player object can have one or two additional copies spaced at set intervals.  This is what makes Space Invaders possible.  The player can be stretched 2x or 3x horizontally and be reflected (to face left instead of right).  Players and missiles are typically displayed over the playfield graphics, but can be set to have the playfield graphics displayed over them.

In many games like Space Invaders and Ms. Pac Man, you will see a serrated series of lines across the left border of the screen.  These lines are used to delay the start of the graphics drawing, giving the programmer more time to write graphics to the next line.  In the 2600, you always have to be one line ahead of the raster. In fact, the standard Activision practice was to blank out the entire left column of the screen for 8 pixels or so.

Pitfall - Player, missile, ball and playfield graphics all combine on various scanlines to make a complex background
In Video Checkers, there are four pieces per row, three of which are provided by a player object and one provided by a stretched missile object.  Lucky for Atari standard checkers is played with a maximum of four pieces per row.  However, the same effect is insufficient for Chess.  Atari used a Venetian Blinds effect used in Video Chess.  This is necessary because each player object can only draw three objects on a line, and you need eight in chess.  So in Video Chess, they alternated the drawing of the chess pieces every other line. This eliminated flickering.  They also had to alter the player graphics in mid-frame in order to make the king's row pieces distinct for each side.

Yar's Revenge - The Neutral Zone is a visual representation of the game's code
Color cycling was one of the hallmarks of Atari graphics, from the Chalice in Adventure to the Neutral Zone in Yar's Revenge.  The color registers are often updated many times per second.  In Yar's Revenge, the bit pattern of the code in ROM was used to give a pseudo-random appearance to the Neutral Zone.  Color cycling was also done for many of the early 2600 games because this was believed to reduce screen burn-in if the TV and the 2600 were left on.

Video Checkers - Each Player has two copies and stretched missiles make up the fourth checker on each row
Scrolling the screen was uncommon on the 2600 because there were no scrolling registers, everything had to be done in software.  Vertical scrolling was easier due to the line-based nature of Atari graphics and because the programmer could tell the TIA when to stop drawing scanlines.  Raiders of the Lost Ark, Jr. Pac-Man and Haunted House all have vertical scrolling.  Horizontal scrolling was a bit trickier, but it could be done.  Mountain King, Stargate/Defender II and Dragonstomper all do it well.

Video Checkers - Each piece is a Player object is staggered compared to the next and drawn every other line.  The background is very large to give the programmer more time to update the individual pixel patterns without flickering
There are hardware collision registers for each combination between the six objects.  Reading a register will tell you if there was an intersection between player 1 and missile 2, for example.  While this saves the programmer from having to determine the collision by having to compare the position values of the two characters, it can also lead to too-strict hit control.  If a missile hit a non-visible player pixel, it will still register a hit.

The Stella emulator can turn on and off each of the objects at will.  If you ever wonder how so few objects can make up the screen, try turning off some.  Collisions can be disabled as well, people tend to use this to cheat.

Cartridges and Memory

Standard Atari 2600 Cartridges contain a 2KB or 4KB ROM chip.  4KB is the maximum amount of ROM the 2600 can address without any bankswitching hardware.  To address more ROM, you had to include additional hardware in the cartridge to handle the swapping.  Most bankswitching schemes reserve certain addresses.  When you write a value to the reserved address, it swaps in another 4KB section of the ROM. Some schemes are more complicated, but this is the general idea.

Asteroids was the first commercially released game to use more than 4KB.  Approximately 20% of Atari 2600 games used more than 4KB of ROM, and almost none used more than 16KB.  However, most Atari 2600 games did not have title screens because they always require precious space in ROM to program.  The title on the cartridge should have been enough for everybody, or so the thinking went.  Also, if you wanted to know what the differences between the various game options are, you have to look in the manual or figure it out through observation.  There was no space for the game itself to explain options.

Some Atari 2600 Cartridges also include additional RAM, usually 128 or 256 bytes.  This RAM is mapped somewhere into the cartridge space and one set of 128 or 256 byte addresses is used for reading and a second set is used for writing.  This is because there is no read/write line sent to the cartridge slot.

The most unique game device for the 2600 was the Starpath Supercharger, which transferred binary data played on cassette tapes to a large cartridge which contained 6KB of RAM and a ADC.  I have discussed this device's operation in more detail elsewhere : http://nerdlypleasures.blogspot.com/2014/06/arcadiastarpath-supercharger-cassette.html

The B&W switch on the 2600 does nothing if the program ignores it.  If the program responds to the B&W switch as it was meant to be used, it will set the graphics to use colors that will show up better on a B&W TV.  B&W TVs only understand the eight levels of luminance the Atari 2600 provides.  However, there is nothing special about the B&W switch or any of the other non-power switches, and the B&W switch was used for other purposes on occasion : http://nerdlypleasures.blogspot.com/2015/03/the-forgotten-switch-atari-2600s-b.html

Enhancements

The TIA contains two audio channels that operate identically but are mixed together outside the chip.  The 2600 was originally planned to be a stereo console with two speakers installed in the 2600's case, but this idea was scrapped as being too costly.  Some of the earliest games (Combat) and a few homebrews (Medieval Mayhem) support stereo audio, but you have to modify your system to hear stereo audio.

The Atari 2600 outputs RF video only, which is less than desirable in the year 2015 for many people.  The chip outputs the color, c-sync and three luminance levels on separate lines.  The luminance levels are mixed and balanced with an R2R network and mixed with the other signals and sent to the RF modulator.  Composite and S-Video mods have existed for years.  However, S-Video is the best you can get from the TIA without substantial additional hardware.

There is an Atari 2600RGB adapter which will provide RGB video. It requires removing the TIA chip and inserting it into a custom board.  The board has an FPGA that will take the values in the color registers and translate them into RGB values.  It will also provide S-Video and Composite Video output.  Because there are only six graphical objects and the background color and four color palettes to keep track of, the FPGA can keep track of this by snooping on writes to these color palette registers.  In order to keep track of the objects as the pixels are displayed, each set of objects is assigned a different black and white palette and intercepts all writes to the palette registers.

Because of the non-standardized method (every game can do it differently) the 2600 uses to indicate that it is time to do vertical blank to the TV, some games may have some trouble when upscaled with an X-RGB Mini Framemeister. The original Warlords is one such game and there is a patch available.

1 comment:

Aloan said...

Very nice stuff and informative!
check out my take over here: http://aloan.site90.net/atari_graphics_simplified.html