Classic Computer Magazine Archive COMPUTE! ISSUE 32 / JANUARY 1983 / PAGE 208

Commodore 64 Architecture

Jim Butterfield, Associate Editor

This guided tour of the new Commodore 64 allows you a peek inside the computer's structure and demonstrates some of its extraordinary features.

Let's build a Commodore 64 — at least in principle. We'll put the memory elements together and see how they all fit.

RAM—64K

We start with a full 64K of RAM. That's the maximum amount of memory that the 6510 chip can address.

If we stopped at this point, we'd have problems. First of all, the screen is fed from memory, but it would contain nonsense. We'll need to put in two extra things: a video chip, and a character generator for the video chip to use. Then again, we have no programs of any sort, and no way to get them into RAM.

Building It Out

Here's what we will do: we'll add the extra features we need by piling them on top of RAM. That way, RAM will be "hidden" — if we look at that part of memory, we will see the new memory elements. But we'll include a set of switches which will allow us to "flip away" the overlaying material and expose the RAM beneath any time we choose. More about these later.

Keep in mind: the RAM is still there, but it's hidden behind the new memory chips.

Input/Output

We'll take the block of memory at hexadecimal D000 to DFFF and reserve it for our interface chips. This includes: two CIA's for timing and input/output, a SID chip for sound, and a video chip to deliver a screen to the television set.

About the 6566 video chip: its "registers" are located at hex D000 to D02E; these locations control how the chip works. But when the video chip needs information to put on the screen, it gets it directly from RAM memory. For example, the usual place for the screen characters is hex 0400 to 07E7. There's a distinction here: we control or check the chip by using its register addresses, but the chip gets information from almost anywhere it likes.

The video chip needs to look at RAM to get characters for the screen. It also needs to look somewhere else to get a "picture" of each character; this allows it to light up the individual dots, or "pixels," that make up a character. There needs to be a table which gives details of each character: what it looks like, and how to draw it. This table is called the "Character Base" table; hardware types may just call it the "character generator."

We could put this character base table in RAM and point the video chip to it. In fact, we are likely to do this if we want to define our own graphics. But on a standard 64, we'd just as soon have these characters built in — in other words, we'll put the character base table into ROM memory.

Now comes the tricky bit. We will put our ROM character base (it's 4K long when we allow for both graphics and text) into locations hex D000 to DFFF. Wait a minute! We just put our interface chips there!

No problem. We just pile the memory elements higher. The ROM character base sits above the RAM, and then we put the I/O on top. Any time we peek these locations, we'll see the I/O. The video chip, by the way, has a special circuit allowing it to go directly to the ROM character base, so there's no confusion there.

If you wanted to look at the character ROM, you'd have to flip it to the top somehow. It turns out you are allowed to do this: clearing bit two (mask four) of address one to zero will do the trick. But be sure you disable the interrupt first, or you're in serious trouble. After all, the interrupt routines expect the I/O to be in place. Bit 2 of address 0 is called the CHAREN control line.

Let's look at a small part of the character base — in BASIC! Be sure to do this on a single line, or as part of a program. First, to turn the interrupt off and back on again:

POKE 56333,127: ... ...: POKE 56333,129

Now, while the interrupt is in force, flip in the character base:

POKE 56333,127 : POKE 1,51: ... POKE 1, 55 : POKE 56333,129

Finally, let's PEEK at part of a character:

POKE 56333,127 : POKE 1, 51 : X = PEEK(53248) : POKE 1, 55 : POKE 56333,129 : PRINT X

You should see a value of 60; this is the top of the "@" character. To see its pixels, we would write it in binary as ..xxxx.. and to see the next line of pixels we would repeat the above code with X = PEEK(53249).

Remember that this is ROM; we can PEEK but can't POKE. If we wanted a new character set, we would point the video chip to some new location.

Kernal ROM

To allow the computer to work at all, we must have an operating system in place. The 64's system is called the Kernal: it's in ROM, and placed above RAM at addresses E000 to FFFF.

We can flip the Kernal away and expose the RAM beneath by clearing bit one (mask two) of address one. Be very careful! The computer can't exist for long without an operating system. Either put one into the RAM, or be prepared for a crash.

Even if you flip out the Kernal for a moment, you must be sure to disable the interrupt. The interrupt vectors themselves are in the Kernal; if the interrupt strikes while the Kernal is flipped away, we'll have utter confusion.

Flipping out the Kernal automatically flips out BASIC as well. So bit 1 of address 1, called the HIMEM control bit, switches out both ROMs. We can switch BASIC alone, however, by using bit 0 – the LOMEM control bit.

BASIC ROM

To run BASIC, we have another ROM which is placed above RAM at addresses A000 to BFFF. We may flip it out by clearing bit zero (mask one) of address one.

This is a very useful thing to do. When a word processor, spread sheet calculator, or other program is in the computer, we may not need BASIC at all. Flip it away, and we have extra memory for our program.

Do Your Own BASIC

We can do even more. If we copy BASIC – carefully! – from its ROM into the RAM behind it, we can get BASIC-in-RAM ... a BASIC we can change to meet our own needs.

Let's do this, just to show how. Write the following program into your Commodore 64:

100 FOR J = 40960 TO 49151
110 POKE J, PEEK(J)
120 NEXT J

Run the program. It will take a minute or so. While it's running, let's talk about that curious line 110. What's the point in POKEing a value into memory identical to what's already there? Here's the secret: when we PEEK, we see the BASIC ROM; but when we POKE, we store information into the RAM beneath.

The program should say READY; now we have made a copy of BASIC in the corresponding RAM. Flip the ROM away with POKE 1,54. If the cursor is still flashing, we're there. BASIC is now in RAM. How can we prove this?

Let's try to fix one of my pet peeves (PET peeves?). Whenever I try to take the ASC value of a null string, BASIC refuses. Try it:

PRINT ASC("")
.. will yield an ?ILLEGAL QUANTITY ERROR.

Now, it's my fixation that you should be able to take the ASCII value of a null string, and have BASIC give you a value of zero. (Don't ask why; that would take a couple more pages). By peering inside BASIC, I have established that the situation can be changed by modifying the contents of address 46991. There is usually a value of eight there. Normally, we couldn't change it: it's in ROM. But now BASIC is in RAM, and we'll change the ASC function slightly by:

POKE 46991, 5

Now try PRINT ASC(""); it will print a value of zero. In every other way, BASIC is exactly the same.

Just for fun: you can change some of BASIC'S keywords or error message to create your own style of machine. For example, POKE 41122,69 changes the FOR keyword ... you must type the new keyword to get the FOR action. Say LIST and see how line 100 has changed. Alternatively, POKE 41230,85; now you must say LUST instead of LIST.

You may go back to ROM BASIC at any time with a POKE 1,55.

Combination Switch

When we use the HIMEM control to flip out the Kernal, BASIC ROM is also removed. Is there any point in flipping both HIMEM and LOMEM? If you do, the I/O and character generator also disappear, giving you a solid 64K of RAM. You can't talk to anybody, since you have no I/O ... but you can do it.

We have named three control lines: CHAREN, which flips I/O with the character base; HIRAM, which flips out Kernal and BASK ROMs; and LORAM, which controls BASIC. In my memory maps (COMPUTE! #29, October 1982), I've called them D-ROM switch, EF-RAM switch, and AB-RAM switch in an attempt to make them more descriptive.

But there are two other control lines, and your program cannot get to them. They are called EXROM and GAME and may be changed only by plugging a cartridge into the expansion slot. When these lines are switched by appropriate wiring inside the cartridge, the memory map changes once again.

But that's another story.

For the first time, the machine's architecture is at your disposal. If you don't like BASIC, throw it out and replace it with your own. The same is true of the Kernal operating system; it's accessible or replaceable.

New horizons are opening. We'll need to do a lot of traveling to reach them.

Commodore 64 Memory
Addresses shown in hexadecimal.