How to design your own CPU from scratch – Part 4

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

Afterward, I added some buttons so the CPU can be tested manually. And so far, 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 anyway in a real-world application. However, here’s the completed CPU without a clock:

https://simulator.io/board/j6VkQodrTT/3

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 transfer them from the scratchpad to the registers A and B for the upcoming calculation. This is, where you would load values from the RAM. But I didn’t connect an external memory to the 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.). 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:

CPU-phase-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 allows the control circuit to have four states. The first 4 AND-Gates check in which state the clock currently is in. 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. The next clock cycle will trigger the next section.

I recommend, that you use the single-step mode of the clock in the simulator to go through each cycle once if you have problems understanding it.

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.

Summary

Ok, let’s sum this whole design up: Building a (very very basic) CPU is not that hard if you understand what parts are involved and how they are connected. The most important component is the ALU, which takes care of the calculations in the CPU. The register file is important too. This is, where the results from previous calculations and values, that are needed for computations, 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 set 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)

comment-banner

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

  1. I just wanted to let you know that your guide was extremely helpful, I am now going on to build a motherboard to manage user input and provide an actual programing language for the OS. Thanks for the guide.

    Like

  2. Thank you for the interesting design. We currently learn this in college and i was not able to completely follow everything. But thanks to you i have a better understanding now.

    Liked by 1 person

    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.

      Like

Leave your two cents, comment here!

This site uses Akismet to reduce spam. Learn how your comment data is processed.