How to design your own CPU from scratch – Part 4

In this part I’ll design the 4-phase-clock of this CPU and I’ll connect all the missing connections between the components.

After I made all the connections, I added some buttons so you can manually test the CPU and everything seems to work fine. I wrote a small program in the ROM that executes a writeback, a jump and an addition. The shifter is missing in the simulation, because it doesn’t seem to work in the simulator and I would simply use a pre-made shift register anyways in a real-world application. However here’s the completed CPU without clock:

The clock

Like I stated in the previous parts of this series, the clock signal will be split into 4 phases:

1.) Fetch instruction
Load the instruction from the ROM and store it in the instruction register.

2.) Load register values
Grab the register values stated in the loaded instruction and store transfer them from the scratchpad to the registers A and B for the upcoming calculation. Also this is where you would load values from the RAM, but that’s not connected in my CPU, simply because the simulator didn’t provide this option.

3.) Execute
Execute the calculation and increment the program counter by 1 or jump to the given address.

4.) Writeback
Store the results in the register. At this point the results have to be calculated. Before this point they might jump around like crazy (for example when changing registers, etc.) , but that doesn’t matter at this point. If you want to add display hardware, you have to get the value either after this point or directly from the register/RAM.

OK, now let’s take a look at the clock:

Figure 1: The 4-phase clock of the CPU

As always, you can click here to view the full-sized image or here to view the example in the online-simulator!

It’s not as complicated, as it looks. What I basically did was divide the clock into 4 cycles, by using two JK-Flip-Flops which toggle with each clock cycle. This way I have 4 states. Now the first 4 AND-Gates check, in which state I currently am. If that state is enabled, the next clock tick will trigger the active section, for example the FETCH-section and it will also make the Flip-Flops flip and therefore switch to the next state.

As this happens, the currently active section has time to work on its task. It has exactly one clock cycle, to finish it, because the next clock cycle will trigger the next section.

I recommend, that you look it up in the simulator step by step by using the single-step mode of the simulated clock.

What now?

I just want to mention, that I didn’t make the connections to the external memory in this simulator. So everything that would be needed there is missing, because the simulator didn’t have an option to add a lot of external memory and I wanted to keep it as simple and easy to understand as possible.

So the following things are missing in this CPU
Data & Address connections to the external memory
A multiplexer to switch between internal registers and the external memory
Different lines to control the external memory (R/W, …). The shifter is also missing for the same reason.

Just wanted to mention it. So if you plan to build a similar thing in real life, don’t forget to include these things!


I’m surprised about how less there was open to say about the clock and yet it caused me so many troubles!

Ok, let’s sum this whole design up: Building a CPU is not that hard, if you understand what parts are involved and how they are connected together. The most important part is the ALU, which takes care of all the calculations that happen in the CPU. Another important thing are the registers, where the results from previous calculations and values, that are needed for calculations, are stored. And the last really important part is the ROM, which holds the information about what the CPU is supposed to do.

The ALU is connected to the registers via three busses. I called them A, B and R. A and B are the input values for the next calculation and R is the result. Furthermore we have some flags, that can be enabled, depending on the last result.

I hope that I could give you a good overview of this topic and entry point for you to start from, if you’re interested in learning more about this. My CPU might not be the fastest, or the one with the best and most features, but I planned it myself and it works, which is a really good feeling!


Table of contents

Part 1 – Basics and the ALU
Part 2 – Registers and memory
Part 3 – Applications
Part 4 – The completed CPU (You are here)
Part 5 – Further improvements and advanced topics (Not published yet)


6 thoughts on “How to design your own CPU from scratch – Part 4

    1. You are welcome and thank you! 🙂
      Years ago when I wanted to learn more about these topics I couldn’t find a good point at where to start from, so it was really hard for me to get into the topic and I decided to write my own series of articles about it. You know, may others are struggling with it too and I wanted to provide a good starting point for them.


Leave your two cents, comment here!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s