Why I Built This
I Built a CPU From Scratch in Python — And Finally Understood How Computers Think When I started CS104: Computer Architecture on Codecademy, I realized I had been using computers for years without actually understanding what happens inside. We type print("hello") and it works — but how? The portfolio project challenge was: "Research, design, and build a Python program that simulates the…
I built a CPU from scratch using Python for a computer architecture project. Before diving into the project, I realized that I had been using computers for years without truly understanding how they functioned. The challenge was to research, design, and build a Python program that would simulate the functionalities of a CPU. So, I decided to take a hands-on approach and build it myself.
The project involved simulating the functionalities of a CPU by breaking it down into three classes that mimic real hardware: MemoryBus, Cache, and CPU. The MemoryBus represented the RAM, which had 256 addressable locations and could load initial values from a CSV file. The Cache was a simple direct-mapped cache with 8 blocks and a write-through policy, which tracked hits and misses.
The CPU, the brain of the simulator, had 8 registers (R0-R7) and implemented a program counter (PC) to track the next instruction. It also had a fetch-decode-execute loop that performed the necessary steps to process instructions.
Throughout the project, I encountered various challenges, such as import errors, case-sensitivity issues with Git, and missing input files. However, each of these challenges taught me something valuable about real-world development, such as the importance of file structure, case sensitivity, and the strict nature of Git. By the end of the project, I had a deeper understanding of why cache exists (the CPU is fast, and memory is slow), why R0 is always 0, and how a simple ADD instruction travels through fetch, decode, execute, and writeback stages.
After completing the project, I completed the Codecademy Computer Science Career Path (CS104: Computer Architecture) and gained a newfound appreciation for the inner workings of computers. I highly recommend this project for anyone taking CS104, as it provides an invaluable learning experience that goes beyond just reading about concepts.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.