An 80s style, 6502 computer.

August 18th, 2013

This is actually a project I started ages ago but many other things got in the way and I ended up not doing anything on it for months. I have now come back to it. Basically the aim is to build my own 6502 based computer from scratch. Now I have actually done this before. Bender’s Brain in my Beer Brewing Bender project uses a 6502 machine I built as his brain. I basically borrowed the existing circuit out of a Commodore 64 floppy drive and modified it slightly and used that. It’s a very specialised use though only doing one thing (playing samples).

This new machine though will be a general purpose computer. It will basically be a machine with similar capabilities to the very first 80s home computers, the ZX81, Vic20 (my first machine) and so on. I decided the machine needs a goal, there needs to be a reason for it to exist, so my plan is to make a machine capable of running Basic programs. In particular I want a machine that I can use to run the type of basic programs that were described in a series of books published in the 80s by Usborne.

Anyone who grew up playing with computers in the 80s will remember these books. I had a number of them and remember getting them out from the library as a kid. Unfortunately all my old books were thrown out by my dad after I left home (apparently a common issue!) so I have been buying them up again off TradeMe and eBay.

20130818_123741 Usborne computer books.

I am collecting all of them I can but the ones I particularly want are the programming ones. So far I have the ‘Write your own adventure programs‘ one. I have several others on the way, such things as ‘Computer Battle Games’ and ‘Computer Space Games’. There are also ‘Write your own fantasy games‘ and ‘Creepy Computer Games‘, ‘Weird Computer Games’ and ‘Computer Spy Games’ I need to find. I also have several other books such as ‘Introduction to computer programming’, ‘Practice your basic’ and so on. Great books and I am amazed at how much of them I remember. It was using these books I wrote my first Basic programs back when I was about 9.

Anyway, these books contain listings for very simple (all text based) programs. They have variations for the different flavours of computer but all are very simple. So my goal is a machine that can run them.

A general purpose computer needs a few things. Input, output, memory, storage and so on. I won’t go into the details of exactly how a 6502 computer works since other people explain it far better than I can. The best tutorial that tells you almost everything you need to know is Garth Wilson’s 6502 primer. I am using Garth’s simple address decode scheme in my own project. The other place you really must go if you’re interested in this stuff is

My own machine will have 32k or ROM (I am using 27C256) and 16K of RAM (using half of a 62256 SRAM). I/O will be handled by 6522 VIAs. I may include other I/O handling devices too. Input will be from a keyboard. Output was an interesting one. I want to output to a screen of course but doing video can be a tricky problem. My original idea was to do something using the TV Out circuit and code I used in my Arduino version of Taipan! It allows fairly low res, black and white video output using a standard Arduino chip. I want video out rather than something more modern (like VGA) since the machine needs to be similar to an 80s spec one. I found it was easy to start getting feature creep when looking into all the different ways I could do the output. You can very easily end up using a much more powerful modern processor just to do video output which seems a bit wrong when you’re driving it with a simple (relatively) 6502. In my investigations though I came across the Batsocks Telemate.

Basically it is very similar to my Arduino TV Out I used for Taipan! but it uses a beefier microcontroller so can display more characters on the screen (a massive 38×25 characters). It outputs simple black and white composite video, emulates VT52 + H19 control codes and can do simple graphics via custom character sets. Colour would have been nice but for my 80s machine this is pretty much perfect. It’s also cheap! I ordered a kit from the UK and assembled it and have tested with using an Arduino to control it and it works great. The examples page shows what it can do.

20130818_124806 Batsocks Telemate.

Since it outputs simple composite video I did the same thing I did on Taipan! and bought a little car LCD screen to use as a monitor. These small screens are typically used for reversing cameras and DVD playing. They are simple and cheap. I was going to buy a whole unit then I found that DealExtreme sell just the guts of them. I bought this 7 inch DIY LCD screen.

20130818_124858 7 inch LCD screen.

It has 2 channel input, selectable aspect ratio and orientation and it even came with a tiny remote control! There is no audio but I will be doing that separately anyway. This is it hooked up to the Telemate running the Portal demo on an Arduino.

20130813_215851 Running the Telemate Portal demo.

The keyboard was something I also wanted to be 80s style. I found on TradeMe an old Tandata TD4000 viewdata terminal I was looking to pull apart to use but when I bought the thing I discovered it was brand new and unused and working perfectly so I couldn’t bring myself to destroy it! Hunting about though I found a nice, 80s keyboard on eBay and bought that. It’s from Radioshack and is an Archer keyboard originally from a TI 99/4 computer. I guess Radio Shack in the states bought a bunch surplus and repackaged them. Mine is unused and came in original packaging with the original receipt. $4.22US including tax in 1986!

20130818_124754 Old keyboard.

It’s an old matrix keyboard so I will have to work out how to  interface it to the 6502. Perhaps with a ASCII interface somehow? Haven’t worked that bit out just yet.

So input and output are sorted (well, sort of) and so I needed to build the core of the machine. To start with I am bread boarding it. This is a process fraught with heartache and disaster. You have wires everywhere and one bad connection or even noise on the lines can make things very difficult to get working right. My machine is only going at 1Mhz though so it is possible to get it working. Enough to test the concept anyway.

As mentioned I am using Garth Wilson’s simple NAND gate decode circuit. In addition to that  is a simple clock generator, a reset generator and on mine a single step circuit. I am using the original circuit designed by Woz used on the Apple 1. Currently the only I/O I have is an 8 bit latch to which I can write values that are then displayed on 8 LEDs. One of the things I want to do is implement a hardware break point on the machine. Basically so in code I can tell the machine to halt then I can use the single stepper to step through the code. It should make debugging much easier. What I am playing with is using on bit on my latch to control the run/stop part of the single stepper. If the bit is set the machine stops. Then you can single step from that point on.

To get all this working of course you don’t only need hardware, you need software. This is where I have had the most problem. Not writing the code itself, that’s fairly easy once you get your head around assembly language. It’s more the whole ‘how do I get the code on the machine’ that is a real pain in the neck. It’s taken me more time trying to remember how to do that than any of the hardware things. Part of  the reason for the post is for me to document how I do it, mainly for my own use since in 6 months time I won’t remember!

First you write you code. I am using the 6502 Macro Assembler, Simulator and Debugger by Michal Kowalski. It is free (you can even get the source code for it) and it is excellent.

All my code is written in that. Now, when you write ASM code you have to specify where things are stored in the computers program space using .ORG directives. For example in the 6502 the reset vector (where the computer first looks when it resets to get the address to start running from) has to be located at address FFFC. Once you assemble your code you then save it in some kind of format that your EEPROM burner can understand. The code you write is burnt into the ROM chip.

The tricky thing is getting the code out of the assembler into the right format so you can load it into your burner and have everything end up in the correct place. This took me AGES to get right.

The EEPROM burning I am using is a standard Willem 3.5 type. This is a multi chip burner that can be a real pain to configure and use. It uses a parallel port interface so I actually have it hooked up to my old Apple 2 in an Ammo Tin machine since it’s the only machine I have with an actual parallel port on it still! I am using the Willem Eprom (0.97ja) version of the software.

I spent ages trying to get it to work with Intel hex format files but I could never make it work right. The problem with the Intel hex files is the file itself includes all the offsets you specified with the .org directives. So when you export the code as Intel hex then read it in in the Willem software it uses all those offsets and put the code in those places in memory. This isn’t actually what you want for burning a ROM. The reason is because of this. The code has to specify where things are in relation to the computers memory space. In my machine for example the ROM code starts at address 8000 hex (i.e. the start of the top 32k of the 6502 address space). When you program the ROM chip though your code is starting at address 0000 hex on the chip.

The Willem software, when reading in an Intel hex file, uses the offsets and put the code into the ROM at those offsets rather that starting at 0000. Now the programmer does have a field for setting the offset but this never seemed to work for me. In the end this is how I how I do it. It’s documented here so I remember in the future! These are the exact steps I use for my machine that has 32K of ROM from 0x8000 to 0xFFFF in address space.

  1. Write your code in the 6502 Macro Assembler tool. All .org directives are in relation to the computer address space.
  2. Assemble it.
  3. Select ‘Save code…’ from the File menu.
  4. Set the save as type to be ‘Binary image (*.65b)’.
  5. Enter the file name and use the .65b extension.
  6. In the options set the begin code address to 0x8000, the end address to 0xFFFF and the length to 0x8000.
  7. Save the code.
  8. In the Willem software select ‘Load’ from the File menu. Using the all files filter load your saved *.65b code.
  9. Check in the buffer that the code begins at the start of ROM (0x0000) and the reset vector is in the right place (0x7FFC).
  10. Burn the ROM.

It seems simple once you know it but this took me hours and hours to get right due to all the different options and file types on both the exporting and importing sides.

So far I have the machine doing a simple loop around incrementing a computer that I then output to my 8 bit latch in I/O space. Next I can start making the code more complicated and look at adding proper I/O with the VIAs.

20130818_124108 Computer core running.

The other thing I want to do is make sure this project is fully documented. I am learning how to use KiCad which is free schematic and circuit board designing software. It has a bit of a learning curve but I found this great set of tutorials online that teach the basics. Theoretically I can use this software to draw the schematics then design a PCB for it. I can then send away the PCB files and have a board made for the machine.


Comments are closed.