Bits and Bytes
Question: How do you put four elephants in a Volkswagen?
Answer: Two in the front and two in the back.
It is a sad fact of life that every device yet made by mankind is subject to limits. We know that a Volkswagen is not designed to transport elephants. The Atari computer is a device with a lot of ability and thousands of potential applications, many of them probably beyond the expectations of the designers, but it does have limits.
The most fundamental limitation of the Atari as a general purpose computer concerns its memory constraints. In order to fit a great many capabilities in a small device at a reasonable price; decisions, compromises, and tradeoffs had to be made. This is especially true of the graphics capabilities of the Atari. Great flexibility, even at the cost of complexity, was one of the design objectives of the computer.
If you design a device so that a user has few options, it should be reasonably simple to operate. When you turn on a light at home, all you have to do is throw a switch, and perhaps occasionally change a light bulb. You do not have to be aware of hundreds of miles of light poles and wire cables, of transformer substations, and perhaps a complex nuclear power plant that make it possible for you to have light when you throw the switch.
The Atari is capable of operating on this level. You open the door on the top, plug in a Missile Command cartridge, plug a joystick into the front, press the Start button, and you are ready to defend your cities against nuclear attack. You need to know absolutely nothing about programming.
But the Atari also allows you to create your own programs, and every trick that is used in Star Raiders and Eastern Front is available to you as well. There is no way to give you so many options and so much power and keep the process as simple as throwing a switch.
It would be possible to simplify the process more than the Atari does by converting every instruction into plain English. Perhaps you could have a computer that could interpret a series of instructions like this:
- draw a man with blue eyes and blond hair
- make him a little taller
- give him a white shirt with a red and gold striped tie
- make the gold stripes thinner
This kind of graphics, while possible, would require a vast amount of development expense and lots of memory, and would lead to its own restrictions. In order to fit many capabilities into a small amount of memory, the Atari had to omit features that would make the graphics easier to use and understand. Instead,the programmer (that means you!), must gain a basic understanding of how the computer works.
The most basic element of information in any computer can be thought of as a two position switch. You can picture it as a light bulb that is either on or off, as a box that is empty or full, as an electrical circuit that is charged or not charged, or in any of several other ways. But the important thing is that there are only two possibilities. This fundamental unit of memory is called a bit. There are many bits of memory in your Atari.
(The 16K memory cartridge that came with your computer has 131,072 bits of memory. You can add two more such cartridges to an Atari 800,for a total of 393,216 bits. The operating system cartridge contains 81,920 more bits of memory, and your Basic cartridge another 65,536.)
Since any bit in memory can function as a two way switch, with possibilities as dramatic as turning the screen on or off, the computer can be very complex. In fact, a single bit in a fixed location can turn the screen display of players on or off. Much of the process of learning how to do fancy graphics on the Atari is learning how to find these special locations in memory and set the "switches" the way you want them.
In most of the memory locations in your Atari computer, a bit of memory is actually a tiny electrical circuit. This circuit is either charged with a voltage, or has no charge. As an easy way of referring to the state of one of these circuits, we use the numbers O and 1. If a bit of memory has a charge, it represents a "1" bit. If there is no charge, it represents a "0" bit. If you were to hook up a tiny light bulb to a single memory cell (one bit), it would actually glow when the bit represented a "1" and not glow when the bit was a "0". Many older computers had lights on the front to do just this; and the user could see what was in each memory location.
A group of 8 bits of computer memory organized into one unit is called a byte. The 8 bits of memory usually are in different locations in memory, but the computer is designed to treat them all as a single unit. The bits are organized so that position is significant. Each byte has a bit pattern that is a series of eight ones or zeroes representing the value of each bit.
A bit has two possible electrical states, on or off. How many does a byte have? There are 256 possible combinations of ones and zeroes in the eight bits that make up a byte. Just as a bit can represent either 0 or 1, a byte can represent a number from 0 to 255. (That is 256 numbers. Programmers start counting at zero.)
That may seem strange. Imagine a blackboard that was only wide enough for one digit to be written upon it. How many different numbers could it hold? The answer is 10, ranging from 0 to 9. If it had room for two digits, the possibilities would range from 0 to 99, giving 100 different possibilities. Since there are ten different possibilities for each digit, there are 10 times 10, or 100 different possibilities for 2 digits, 10 times 10 times 10 or 1000 different possibilities for 3 digits, and so on for numbers with more digits.
Since there are only two possibilities for a bit, there are only 2 times 2, or 4 possibilities for 2 bits. This means that the eight bits in a byte can represent 2 times 2 times 2 times 2 times 2 times 2 times 2 times 2, or 256 different numbers.
There are thousands of different bytes in the memory of your Atari. Each of them is organized and assigned a number, which is called its address. Each byte has eight bits assigned to it, and every bit is a completely separate electrical circuit, or memory cell. You might wish to think of the memory of your computer as a very long street, with houses on only one side of the street, each house numbered in order, 0, 1, 2, 3, 4, etc. Each of these houses (a byte) has eight rooms (eight bits). In every room, the lights are either turned off or turned on (0 or 1).
You can locate any byte in memory to examine it by its number. The possible range of numbers with the current operating system is from 0 to 65,535. There is at least one device, the Axlon Ramdisk, that expands the Atari's memory to 262,144 bytes. These numbers may seem strange at first. Why not a "round" number like 100,000? The answer is that with two possibilities for each bit, the possible addresses must be a power of two.
How large a number can be represented by a certain number of bits? Here is a table for 0 to 18 bits. If you would like to check it out, just write down every possible combination and count them. For example, with three bits you could have 000, 001, 010, 011, 100, 101, 110, or 111; eight different possibilities.
Bits............Combinations 0..............0 1..............2 2..............4 3..............8 4..............16 5..............32 6..............64 7..............128 8..............256 (one byte) 9..............512 10..............1024 (also known as "1K") 11..............2048 (2K) 12..............4096 (4K) 13..............8192 (8K) 14..............16384 (16K) 15..............32768 (32K) 16..............65536 (Atari memory size -- 2 bytes) 17..............131072 (128K -- Note this is NOT "131K") 18..............256144 (The Atari can be expanded to this)
Because the processor in the Atari uses 16 bits to address memory, it can address 65,536 memory locations, numbered in order from 0 to 65,535.
How Your Atari Works
The "brain" of your Atari computer is a micro-processor, an integrated circuit. This processor is called a #6502, and its major function is to communicate with the memory in your computer. It receives instructions from memory and reads and writes instructions from and to memory and other devices (such as the cassette recorder, joysticks, the disk drive, and the screen) that appear to the processor (as if the other devices were also memory).
The 6502 starts at a location in memory and interprets the content of that first location as a command. From then on, the instructions tell it what to do with the other locations in memory and where to look next. The processor can only do this with the main memory, so every program must be in the random access memory at the time it is executed, or run.
Since we are limited to 65.536 locations in memory, and have to do everything in those locations, we must discover a way to store characters, displays, Basic variables and anything else as bytes of data.
Here are some ways information is stored:
Character strings. Each character (a letter, number, symbol, punctuation mark or space) is stored by representing that character as a number and storing the number into a byte. Since there are 256 different possible patterns in a byte, the Atari has 256 different characters. Each character is represented as a unique byte, and the computer looks up the number on a table in the operating system to determine what pattern to display on the screen.
Basic variables. Atari Basic is not limited to numbers between 0 and 65536, although it is limited to line numbers between 0 and 32767. This is because line numbers are stored in one bit less than two bytes, while Basic variables are stored as a group of 6 bytes. Basic encodes numbers in a way that makes them easy to work with, not in the manner we have been discussing.
Basic programs are stored in coded bytes, not as text. Each possible Basic statement type is given a unique number, and that number is stored as a single byte instead of as the letters that you typed into the computer. Whenever you list a program, those codes are translated back into letters that you can read. The details of this coding will be explained later in this book.
An important point to remember is that the 6502 processor sees data as bits and bytes. We think of data as characters, variables, or language keywords. Sections of memory are dedicated to different functions. For example, the operating system ROM holds 6502 machine language instructions, data for the display of characters on the screen, and text messages.