Sunday, 17 November 2019

6502 CPU Computer

This blog describes what I have accomplished in developing an 8-bit computer based on what would be described as a mix of old legacy and new technology.

Contents:

  • Introduction
  • Overview
  • Project Approach
  • Boards
  • Display options
  • Assemblers/Disassemblers
  • Future experiments/developments
  • Thanks


Introduction:
----------------
The project (which is on-going), came about as a result of being distracted while experimenting with current technology, including the Raspberry Pi, Arduino, ESP8266 and other TTL & CMOS digital circuitry. As a consequence of making this 8-bit computer I have learned so much:

 - Digital circuit design using legacy TTL/CMOS circuits.
 - 6502 addressing, timing, parallel and serial interfacing and logic control.
 - EEPROM usage, programmer design, and programming with Arduino.
 - 6502 assembler.

Initially, the first distraction came from watching and being engrossed in Ben Eater's YouTube videos, where he works through building a computer using discrete logic integrated circuits, supported in part by some modern technology - more of that later.
I then stumbled across Joe's Computer Museum and got hooked into the construction of an 8-bit computer based on the 6502 CPU, and got completely hooked after visiting a number of other 6502 related web-sites; including but not limited to - 6502.org, WilsonMinesCo.com and grappendorf.net

I had some limited experience of the 6502 CPU as many years ago I constructed from a kit, a very early Acorn Atom computer. I regret parting with this machine. Later computers included the Dragon 32 based on the 6809 CPU, where I dabbled in 6809 assembler. Turning full circle I recently acquired an Acorn Electron (based on the 6502 CPU) and I'm now occupied with assembly language coding and interfacing experiments. [more distraction...]

Prior to starting this project I had amassed a range of TTL/CMOS Integrated Circuits, some of which were to prove very useful in my 6502 project.

I started searching for and buying some components needed for the project.

These included the following:

6502 - CPU
AS6C62256 - 32kb Static RAM
AT28C64B - 8kb EEPROM
6551 - Asynchronous Communications Interface Adapter (ACIA)
and a few CMOS ICs for interfacing and logic address mapping.

Overview of 6502 project capabilities
----------------------------------------------
Based on what I had seen and read on various web-sites, I decided on the following:

The main CPU (6502) running at 1mhz clock speed.
Main volatile memory (SRAM) 32kBytes.
Non volatile memory (EEPROM) 8kBytes.
Serial Input/Output communications using the 6551.
Parallel Input/Output using the 6821 Peripheral Interface Adapter
(I had one of these from my earlier Dragon 32 experiments).
A capability to program the EEPROM.

The Computer memory mapping:

E000 - FFFF [EEPROM/ROM]
D200 - DFFF [Unused]
D000 - D1FF [Input/Output ACIA & PIA]
8000 - CFFF [Unused]
0000 - 7FFF [SRAM]

You will note some unused address space, but I wasn't too concerned at the start as I had plenty of learning and development to do if I was to succeed in putting this first attempt together and get it to work!


Project Approach
---------------------
After I watched Ben Eater's videos, I decided the best approach for me was to construct each element of the project in turn on breadboards.
My reason for this was so I could wire up individual elements, test and experiment with them to better understand and learn how the circuits worked before integrating the single elements together as a whole. This worked well for each single element but as I progressed, the amount of point to point breadboard wiring became almost unmanageable, and in some cases unreliable. On the positive side, I gained a much better understanding of how each Integrated Circuit worked, and the interfacing needs of each. I also became appreciative of the host of documentation on the Internet, particularly the availability of the ICs datasheets.

Thanks to a number of Ben Eater videos I was able to put together an EEPROM based address and data bus monitor, and an EEPROM programmer based
on a NANO Arduino. Additionally I'm in a very fortunate position of owning an oscilloscope which has proved invaluable to check the correct operation of the circuits and identifying problems.

I did succeed in getting a working project, comprising all the main elements (CPU, EEPROM, SRAM, ACIA & PIA), although became frustrated with some unreliability.

After much thought and further research, I discovered a modular computer called the RC2014. Although based on the Z80 CPU, it was the approach to
using modular boards that clarified how I could move my breadboard based project to a more semi-permanent and reliable solution.

So it was that I began to design and migrate the components from a breadboard to a more modular form, using veroboard, loosely based on a backplane with separate cards for each element of the 6502 project.

Modular Boards
-------------------
The following image is the current build, working well and reliable, comprising a single backplane with multiple single boards.

From left to right the boards are:

6821 PIA / 6551 ACIA / USB Keyboard-to-serial-ASCII / Address Logic / EEPROM / SRAM / CPU - with power socket (5v) bottom right.


Current build - 6502 computer 

The following image is a reverse view of the first image.


Current build - 6502 computer


The following image is the unpopulated backplane. This has a few unused sockets as I modified the board when I moved from a ZIF socket solution for the EEPROM to a single modular board.


Unpopulated 6502 backplane


The next image is the main CPU board. The main clock runs at 1mhz. I've provided a small jumper connection that can be removed to allow a separate clock source to be connected directly to the 6502 CPU. This is to allow me to use slower clocks for trouble shooting. With this particular CPU, I've managed to clock at 1Hz. With my EEPROM based address and data bus monitor attached to the back-plane bus it's possible to monitor address and data values as any code runs on the 6502 CPU.

6502 main CPU board

The following image is the address and chip select logic board. It provides control signals for the EEPROM/SRAM/ACIA & PIA boards.



Address/chip select logic board



The next image is the EEPROM board, utilising the AT28C64B, 8kByte EEPROM. I've designed this board so it can be removed from the backplane and inserted into an EEPROM programmer board. The jumper is used to place the EEPROM in to either READ only mode, or enable for READ and WRITE actions.


EEPROM board

The following image is the SRAM board (32kByte), utilising the AS6C62256. I've employed an additional logic gate to ensure the SRAM is in WRITE mode
only when the CPU addressing is stable and the PH2 clock is in the correct phase.

SRAM board



The next image is the ACIA (serial comms) board. I used a 65C51 (I couldn't find an original 6551), employing a crystal controlled oscillator for ACIA bit-rate generation.


ACIA - Serial Comms board


The following image is the PIA (parallel interface) board, using a 6821 as explained above. I've made most of the interfacing pins available for getting data in and out of the CPU.


PIA - Parallel Interface Board

The next image is a USB Host board (obtained from Hobbytronics).
This allows me to attach a USB Keyboard to the computer. The USB host board takes USB keyboard input and converts it to ASCII data, which is connected to the ACIA board serial input. This removes the need to use a PC running a communications terminal program to send ASCII data to the CPU.

USB Host Board - Keyboard Interface

The following image is the breadboard based EEPROM programmer, using the Arduino NANO and a couple of TTL shift registers. This is based on Ben Eater's EEPROM programmer slightly modified to accommodate a larger memory capacity EEPROM.


EEPROM Programmer - Arduino NANO based


The next image is the earlier EEPROM modular board, based on a ZIF socket that plugged directly into the back-plane.
Initially this worked reasonably well, but I had suspicions it was contributing to occasional errors. Also I wasn't happy with physically removing the EEPROM from this board and plugging it into the breadboard programmer every time a code change happened. A redesign followed...

ZIF socket based EEPROM board
The following image is the more permanent and reliable EEPROM programmer board. Based on the circuit of my earlier breadboard version, this allows me to plug in the complete EEPROM modular board when I need to make programming changes.
EEPROM programmer - Arduino NANO based

The next image shows an EEPROM modular board plugged into the EEPROM programmer board. I use a PC plugged into the Arduino NANO micro-usb socket, running an Arduino IDE to write 6502 assembled (HEX) code to the EEPROM.

EEPROM programmer - populated with EEPROM board

The final two images show the address and data bus monitor breadboards. These are based again on Ben Eater's solution and monitors 4-bit 'nibbles' and display the address and data HEX values on 7-segment displays. With these monitors plugged into the 16 address and 8 data bus lines on the main 6502 backplane, it's possible to trace through the 6502 instructions in realtime (only practical at very slow clock rates).

16-bit address bus monitor

8-bit data bus monitor

Display options
-------------------
Initially I used a Windows based PC running RealTerm via a USB/FT232 board connected to the 6502 CPU ACIA board to send and receive ASCII data.
The image below shows a screen shot of the RealTerm terminal with the output of the 6502 project, where I'm running a 6502 monitor based on the code
provide by Joe (Joe's Computer Museum). This only displays 5 bytes per line, formatted to display text on an LCD display (a later project element).
Using RealTerm / USB/FT232 / ACIA connectivity I've managed speeds from 115200bps to 300bps.

RealTerm - ACIA output


Additionally I've experimented with the Serial Monitor function in the Arduino IDE; a simple display (below) but lacking in features.



Arduino Serial Monitor - ACIA output


Doing further experiments I've connected the ACIA (with USB/FT232 interface) to a Raspberry Pi computer, and used some different ways to send and receive ASCII data interacting with the 6502 monitor. The two images below show:

1. CuteCom on the /dev/ttyUSB0 port.

2. The Screen command using: screen /dev/ttyUSB0 9600.


CuteCom - running on Raspberry Pi


Screen - running on Raspberry Pi


Taking this one stage further, I've even managed to use my Windows PC to remotely SSH to the Raspberry Pi, then run the screen function to 'connect' to the ACIA and interact with the 6502 based monitor. This is true remote operation of a 6502 CPU based computer!

In each display output I've shown above, the monitor is displaying the contents of the EEPROM upper addresses:

$FFFA/FB - NMI
$FFFC/FD - RESET
$FFFE/FF - IRQ
programmed with the 6502 monitor code, where the monitor start address is $E000:

The final two images below show an LCD display (20 characters x 4 lines). This LCD module connects to an Adafruit serial/LCD backpack interface.
Serial output from the 6502 ACIA module connects to the Adafruit serial interface running at 9600bds, with the ASCII characters appearing on the LCD display. I've needed to modify the 6502 CPU monitor code to ensure the output is formatted appropriately to fit the LCD display.


LCD display 20 x 4  - Boot-up display 




LCD display 20 x 4 - EEPROM address range $FFFB - $FFFF

Assemblers/Dis-assemblers
-----------------------------------
I generally use Notepad++ to create the assembler file and use the RetroAssembler for Windows to assemble/generate the HEX code.
This HEX code is copied to the Arduino IDE where I use the Arduino NANO based EEPROM programmer to write HEX data to the EEPROM.
For small pieces of code where I need to check the status of the CPU (A, X, Y and Status Register) I use Easy 6502 (skilldrick).
To dis-assemble 6502 HEX code I regularly use the Masswerk-Virtual6502 - I've found this especially useful to step through assembled code to better understand how the 6502 code is running.

Future / Experiments
---------------------------
More permanent mounted LCD display.
TV / RGB / VGA display output.
6502 assembly coding
External interfacing using the 6821 (PIA) and 6551 (ACIA) to other digital circuits/sensors/control.
Interfacing with the Acorn Electron.

... and there will be others .. depending on my levels of distraction!

Hopefully you'll have found this article interesting. I haven't provided much technical detail such as circuits and veroboard layouts as this is likely to take me a significant amount of time. If you have any questions I'll do my best to answer them.

Thanks
---------
As I stated earlier, I have many people to thank and to recognise the significance of the effort many people have put in to providing designs, advice, documentation and videos:

Ben Eater
Joe's Computer Museum
6502.org
WilsonMinesCo.com
grappendorf.net
RetroAssembler
skilldrick Easy 6502
masswerk-virtual6502
KIM-1 documentation
BYTE magazine
stardot.org


and others too.... Many thanks.