Building a 6502 Computer: The System Clock

Intro

In the last article we learnt all about the various pins of the 6502, today  we’re going to look at the various ways of supplying a system clock, power up and see perhaps things you were not expecting. The you tube video is below and the main article follows.

Parts list/affiliate and none affiliate links

Affiliate links
US
Breadboards : https://amzn.to/33w1NYp (Limited Stock!)
Arduino Nano: https://amzn.to/3O1ayfz
Board wire (red, black, white, green, blue, yellow): https://amzn.to/3LIvWVx
Various colour LEDs : https://amzn.to/3Knrsma
DS1054Z Rigol Scope (the one I use) : https://amzn.to/3LIHHdU

UK
Arduino Nano: https://amzn.to/3v6NROk
Board wire (red, black, white, green, blue, yellow): https://amzn.to/34IBeA5
Purple board wire : https://amzn.to/3rX3OGn
Various colour LEDs : https://amzn.to/3KeSbS1

Breadboards : https://amzn.to/3oTBJOo
Note: These are not the ones I’m using in the videos, I’m using BusBoard ones not Elegoo. Busboard are much more expensive but are highly rated. The Elegoo are probably fine, I’ve used other cheap breadboards lots of times and been fine but for full disclosure I cannot vouch for these particular ones. If you can, order from US Amazon, which does Busboard. That’s where I get mine from. The process is just like ordering from UK.

Other none affiliate links

W65C02 CPU from Mouser
UK : https://www.mouser.co.uk/ProductDetail/Western-Design-Center-WDC/W65C02S6TPG-14?qs=opBjA1TV903lvWo9AEKH5w%3D%3D

US : https://www.mouser.com/ProductDetail/Western-Design-Center-WDC/W65C02S6TPG-14?qs=opBjA1TV903lvWo9AEKH5w%3D%3D

Epson Clock Modules from RS components : https://uk.rs-online.com/web/c/passive-components/crystals-oscillators-resonators/crystal-oscillators/?searchTerm=epson+oscillator&searchType=CATCH_ALL_DEFAULT&applied-dimensions=4294512007

The system clock.

A computer’s clock is just a voltage that goes high for a time and then low for a time and repeats this over and over again. In the case of our beloved  6502 we connect this changing voltage into pin 37.

So a single clock pulse is one complete wave, a high and then a low. The clock signal repeats this over and over again, below you can see the top diagram has two complete waves and the bottom four occurring in one second.

The point of a clock is to ensure all internal elements of the processor and external parts of your design all do their operations at a given time. This stops different parts of the processor and your design from clashing, keeping everything in step. So for every clock pulse the processor performs some sort of action – whatever that may be.

What can we use to generate a clock? Well there are a lot of choices. If you’re not running at fast clock speeds then simple flip flop circuits using transistors, capacitors and resistors would do the job. Or you can go a bit up from that and use a 555 timer – as seen in Ben Eater’s series of videos, here’s a link to his playlist. They are an excellent series of videos on building a 6502 based system. 

The correct way of doing it for higher speeds and accuracy is to use a quartz crystal in an oscillator circuit. Crystals can come in many forms and in many frequencies as seen in the video.

These types need additional components to get them to resonate at their frequency and give a suitable output. I show an example from a design I used in the early 90’s in the video but it’s not the best clock circuit as it produces a sign wave. For this reason I’ve not included the circuit here as I would not want you to copy it. It works great with the 6502 as that will clean it up and output a decent square wave from it on pin 39 which you can then use with your circuits. But let’s go a bit more modern, simpler and better…

Oscillator/Clock Modules

These are clock modules, now don’t get confused, these may look like just crystals in a big can but they’re not.

Inside is a crystal and all the supporting circuitry similar to what you need with the single crystal just mentioned. The design is no doubt a little more sophisticated as the manufacturer generally lists tolerances for not only frequency but also how much that may change with temperature and how much the symmetry may change, that is to say, does it drift and be higher than it is low or vice versa. Again they come in different packages but generally you will often see them in metal cans or in these plastic packages and many many different frequencies are available. 

I’m going to be using the Epson ones, ranging from 1 Mhz up to 14, so I can just pop different ones in and out of my circuit and see just how fast I can push my design.

The circuit for the epson modules

Here’s the circuit I used in the video for the Epson module.

The 0.1 uF capacitor is optional but recommended. In simple systems with not many chips you’ll probably get away without it but to be safe and to stop hard to track down problems it’s best to put it in. It acts like a tiny reservoir of energy for the chip as it pulls voltage from the power connections. The capacitor should places as near to the power connections of the chip as possible. Note that many other clock modules have very similar connections and as such should be able to use the exact same circuit above. For example in the photo above the silver metal clock module is from a different manufacturer but has identical 4 connections (just in different positions).

Using a debugger

Moving on, the last way I’m going to show you for  generating a clock signal is with a debugger. A debugger is a system set up to intimately control and monitor a processor or system. For this, like in Ben Eaters videos I’m going to use an Arduino. To start with a simple Arduino Nano but this will be expanded to a full Arduino Mega in the future. 

Here is the circuit I used in the video.

The Push Button
This will be our button to control the system clock. The code in the Arduino will look for it being pressed and released. When pressed it will ground (set to 0v) the Clock output on D3, when released it will raise D3 high (set to 5V). All the messiness of de-bouncing the switch is done in the software running on the Nano. With this set up we can single step the processor easily.

The Code
I’m not going through the details of this code now, that’s for the next episode when we will add a bit more to it. But for completeness and in case your following along here it is.

// Basic Clock Driver for WDC65C02 system
// For XTroncial 6502 computer, see www.xtronical.com

// Constants

#define CLOCK  3      // This pin connected to clock in pin of 6502 (Pin 37)
#define CLOCK_BUT A0  // The button you press to control the clock of the 6502
#define DEBOUNCE_TIME 20     // Amount of time (millis) to wait for any spurios signals from a push button (the time that we are going to debounce for)
#define CLOCK_BUT_UP HIGH    // value for ClockButtonStatus if button is raised
#define CLOCK_BUT_DOWN LOW   // value for ClockButtonStatus if button is pressed


bool DebouncingClockButton=false;   // are we debouncing the clock push button
uint32_t DebounceStartTime;         // Start of when button state changed
uint8_t PreviousClockButtonStatus;  // current state of the clock push button, i.e. up or down

void setup() {
  // By default pins on Arduino are initially set to INPUTS. However we will explicitly set them all here
  pinMode(CLOCK,OUTPUT);
  pinMode(CLOCK_BUT,INPUT_PULLUP);
}

void loop() {
  // Very simple code just looking for the change in the manual clock button, note this could have been interupt (on pin change) driven
  // but for our purposes this is fine.
    CheckClockButton();
}

void CheckClockButton()
{
  // Checks the current status of the clock button, performing any debounce if it changes
  uint8_t CurrentClockButtonStatus;
  if(DebouncingClockButton==false)          // only check button change if not currently debouncing a change of button state (press or release)
  {  
    CurrentClockButtonStatus=digitalRead(CLOCK_BUT);
    if(CurrentClockButtonStatus!=PreviousClockButtonStatus)
    {
      // button has changed, set the 6502 clock pin to match, set debounce variables
      digitalWrite(CLOCK,CurrentClockButtonStatus);
      DebouncingClockButton=true;
      DebounceStartTime=millis();
      PreviousClockButtonStatus=CurrentClockButtonStatus;
    }      
  }
  else
  {
    // debouncing clock button, basically we ignore any changes of the push button until a certain time (determined by DEBOUNCE_TIME) has passed
    // here we just check if the time is up and if so clear the DebouncingClockButton flag so we can check the button again
    if(millis()>=DebounceStartTime+DEBOUNCE_TIME)
      DebouncingClockButton=false;
  }
}

That wraps it up for now. 

In the next article/episode we’ll look in detail at the code running on this Nano, the 6502’s reset sequence, busses, hexadecimal and binary. If you want to be notified then subscribe to my YouTube channel, link in the video description.