Wednesday, September 18, 2013

The Software Tool Chain

In which our protagonist rekindles the Editor War and then introduces tools for writing 65c02 code even before the first chip has been unpacked.

We have lots of old electronic junk in our attic, cellar, and garage, but what we don't have is an working 8-bit computer. Which leaves us with the problem of writing an operating system (to use a very grand word for a very humble piece of code) for the Übersquirrel. How do you write programs for a machine you haven't built yet?

Chuck Norris, of course, would just use two wires connected to an EEPROM on the breadboard to tap-tap-tap in the whole OS directly, because that's how Chuck Norris writes operating systems. The rest of us need some way to develop, code, and test the thing. This is what cross-assemblers and emulators are for.

(I'll be using the terms "emulator" and "simulator" interchangeably. The formal difference is that a simulator tries to model the internal state of an object as closely as possible, while an emulator doesn't give a damn and just works like it.)

Because the 6502 is such a popular processor, there are quite a number of tools available for free -- see this list, for example. For the Übersquirrel, we'll be using three programs:

Editor: Vim. This is obvious and does not require further discussion.

[Well, maybe it does if you spent your youth with healthy outdoor sports and studying Pre-Raphaelite art instead of fooling around with computers in the basement. The Editor War is the name given to the, ah, discussion between the users of the vim (historically "vi") and Emacs editors on Unix-like systems. All you need to know here is that vim is the right choice, GlaDOS was written with Emacs, and you can trust me because I've been using vi-clones for almost 30 years now.]

Vim comes with highlight support out of the box (on OS X) for files that end with ".asm".


A fragment of the Übersquirrel kernel (version alpha 008) in vim, coded in the Ophis syntax. Note that the hash sign triggers color coding for comments. Probably easy to fix, but I can't be bothered at the moment. Note also the BRA "branch always" instruction that is not available on the original 6502.

Cross-Assembler: Ophis. Written in Python by Michael Martin, it should run on most platforms. The assembler syntax is slightly different than that of most assemblers, but easy enough to learn. Most importantly, Ophis supports the extended commands of the 65c02. In our case, we invoke it with

ophis -65c02 sqos.asm sqos.prg

where sqos.asm is the assembler text written with vim and sqos.prg is the binary output. Ophis supports macros, which can be extremely useful.

Emulator: py65mon. Another Python program, written by Mike Naberezny and others, and again picked for its support of the 65c02. The documentation is a bit sparse, but a look at the code shows us where the hardwired I/O-addresses are:

$F001 - put character
$F004 - get character
$F005 - blocking get character

We invoke py65mon with

py65mon --mpu 65C02 —rom sqos.prg

and a first ghost of the computer that will be the Übersquirrel comes to life.

Now all you have to know is that the 65c02 looks for a jump vector at $FFFC and $FFFD after a reset, and then you can begin to merrily code away at your very own operating system for the 65c02.

So much for software. In the next entry, we'll discuss the hardware toolchain.