Anyone interested in the beginnings of the IT revolution will have one eye looking at the past. Although the beginnings stretch back to the telegraph system, today we won’t go that far.

In this post we’ll look at the Commodore 64, “why?“, you might ask. Unlike the Sinclair Spectrum it had a full keyboard, could display more colors, unusually for the time, had sprite capability and that combined with bespoke chips, notably the SID sound synthesis chip, made it stand out from the crowd. That however was the selling point of the day, why now? The one true fact for today, you can fully understand a Commodore 64, from the electronics, software to the I/O. All the manuals, guides and software are freely available.

I don’t have access to the actual machine so I’ll be using the very capable VICE emulator. Purists will note that the timings aren’t absolutely perfect but that is just the nature of the beast. In most regards it’s a superb emulation of the system.

I’ll be doing the following through linux, windows users particularly may have to change a few things but it should be quite similar.

First thing to do is to bring up the emulator, you do this with the “x64” binary.

$> x64

You should be greeted with this:

c64

That’s Commodore’s implementation of Microsoft Basic. The observant amongst you may note that the available ram is quite a bit lower than the aforementioned 64K. There’s a simple explanation for that, the Basic interpreter and Commodore’s kernel has to be loaded in first which takes a sizable chunk. This is one of the main reasons that Basic wasn’t the primary language for games, that and the fact it’s quite slow.

Never deterred, we shall proceed in the hands of Microsoft and it’s quirky basic creation. Quirky because in this machine it’s not complete. In order to access certain parts of the system you have to “PEEK” system memory and “POKE” values to change behaviour. Other systems of the day had better implementations of basic, at least when it came to simple graphics commands but we’ll forgive that.

To use basic, you should prefix the command with a number, this will stand as an index to your code. Commonly at this time the number would start at 10, then 20, 30, 40 and so on. This would allow you to insert a line of code at 25 for example, if that became necessary further down the road. When you’ve written your code, you can call it back with “LIST“. In a large sections of code which won’t fit on screen you can do something like “LIST 10-100“. The nice thing about this listing, is it’s active, you can take the cursor up to the line, modify it, press return and it’s stored in memory.

The first thing we’ll learn is how to clear the screen, to do that we’ll use the “PRINT” command. It uses a special character which in VICE you access with shift+home. This will output a heart symbol, which is equivalent to {clr}.

In vice systax it’s:

10 print "{clr}"

On the machine it looks like this:

c64 clr

When you type in “RUN“, the code will be executed and the screen will clear. Now we can start on an actual program.

c64 rand listing

I dumped the program to my filesystem, so it’s easier to read:

   10 print "{clr}"
   20 let x=int(rnd(1)*6)
   30 print "the computer has chosen a number between 1 and 6. can you guess it?"
   40 input g
   50 print "{clr}"
   60 if g=x then print "well done!":goto 90
   80 print "tough luck - you're wrong :("
   90 print "do you want another go? (y/n)"
  100 input a$
  110 if a$="y" then goto 10

Even though the Commodore defaults to uppercase, the listing produced by the petcat command line program is lower. This is because the Commodore uses special characters for uppercase, it uses the PETSCII character set rather than the now more common ASCII.

The first point of interest is on line 20. We create a random number between 0 and 1 and then multiply 6. This is a fractional number so we cast it to an integer with the familiar INT().

The next new point is on line 40, “INPUT G“, it takes an input but on line 100, we use “INPUT A$” the difference being the $ denotes a string.

The IF statement should be very familiar but unlike most modern languages GOTO is used to jump to sections of code as you would in assembly.

If you want to write on your host computer, type it in in a text document, “random.bas” for example and then do the following.

$> petcat -w2 -o random.prg -- random.bas
$> x64 random.prg

If you run the program a couple of times you’ll notice something strange. The random doesn’t seem random at all. That’s because the Commodore didn’t have a an internal clock, so no way of storing time. The closest thing it had was system uptime. So in this case you’re not imagining things, the RND function is a simple progression.

Okay so now we can save the program to a disk image, create a blank disk with VICE and attach it to slot 8, then type the following

SAVE "RANDOM",8,1

We’re doing this so we can reverse the process and get a basic program out of the emulator

$> c1541 random.d64 -read random random.prg

n the above we’re assuming that the disk is “random.d64” and the program on the disk is called “random”, which is dumped to a file “random.prg”. Confused yet?

If you forget the name of a program on a disk, you can view it with:

$> c1541 random.d64 -list

Now we can turn it into a listing with petcat:

$> petcat -2 -o random.bas -- random.prg

So that’s it folks, you’ve taken your tentative steps in becoming a wizkid of the 80s, congratulations. All you need now is a time machine and the plan will be complete. Flux Capacitors will be covered in another episode so stay tuned.