blog Building a CPU

In university, I had a class “Computer Organization and Assembly Language” where we had to write programs in 8086 assembler. I’m sure I also had a lesson or two somewhere on basic logic gates, but I still had a big gap in knowledge. Recently I was curious and decided to learn, how does a CPU actually work? How does an op code in assembler language translate to physical bits in the system?

Finding videos like How TRANSISTORS do MATH was helpful in understanding how some logic gates where made out of transistors. How a CPU Works was another great video, introducing me to the “Scott CPU”. The series Exploring How Computers Work was also excelent and I originally tried to use their Digital Logic Sim to try to build my test CPU before quickly hitting limits.

I ended up buying the book But How Do It Know . Reading through the book, I started using the circuit verse simulator to build up my own CPU model to match the model on the book’s website. The two largest things that helped a CPU finally click for me, was understanding how the stepper and how the selectors worked.

Stepper

Alternate Link

The stepper reads from the clock and decides what “step” the CPU is currently on. Many CPUs use a fetch-execute loop, so in this model CPU, the first 3 steps are used to fetch an instruction and the next 3 steps are used to execute an instruction.

Selector

Alternate Link

The selector is hooked up to various bits of the instruction register. Depending on which bits are on, this might trigger an ALU instruction or select a specific register.

Full Simulator

Alternate Link

While reading through the book, I attempted to build my own CPU to match the model on the website. I think I’m near the limits of what the online simulator can easily handle, so my CPU does not quite work, but while building it I was able to better understand how everything fits together.

Next?

More modern CPUs are a lot more complicated, supporting larger addresses and interupt systems. For the purpose of learning how a CPU works, I think this is a good stopping point. Next I’m curious to revisit how an OS works. I recently read that the original Linux kernel is only about 10,000 lines of code.