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:
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.
Execute the calculation and increment the program counter by 1 or jump to the given address.
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:
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.
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.
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!