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:
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.
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.) , 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:
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.
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!
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 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!