DEV Community

MozzarellaMonster
MozzarellaMonster

Posted on

CPU Simulation Project

Hi! I have been working my way through the Computer Science course on Codecademy's website, and have just finished the portion about Computer Architecture. To end this portion, I had to complete a portfolio project that simulates the functions of a CPU, so I wrote a program that simulates the work of a Control Unit, registers, and ALU.

I was free to decide exactly how I was to complete my project, with lots of room for interpretation. Since I was fascinated with the part of the lesson that explained how instructions are read from a sequence of binary characters, I decided to simulate that.

My program was written to read the instructions in the form of a 32-bit string based loosely on the actual MIPS R-type. I wrote three classes - Binary Reader, Registers, and ALU, each of which are meant to act as the hardware responsible for the inner functioning of a computer - the Control Unit and binary reader, the Arithmetic Logic Unit (ALU), and the registers used to store data.

The Binary Reader class was in charge of reading the instruction to determine what data was to be worked on, what kind of processing was to take place, and would later output the results to the terminal. The ALU class was to provide the correct arithmetic functions such as addition, subtraction, multiplication, and division. The Registers class was to provide a place to store several pieces of data. It held two different functions for storing data, a load function, and was built on a list data structure.

I ran into several different problems while writing this program, including writing checks to test if the instructions sent were actually 32-bit binary strings, retrieving data from an 'address' (in this case, the index of the list containing the data), correctly converting the data from decimal to binary, and keeping track of the oldest data in the registers. At one point, I had even introduced a logic error by directly converting a binary substring into an integer for calculation when I was supposed to have used it as an address!

All in all, this was a very fun project that I had a blast working on, and I learned a lot in the process, both in terms of Python code that helped with the binary conversion and how computer architecture actually functions. I actually ended up writing another small program to generate randomized binary instructions to help with debugging, which was also fun to work on.

Testing out my program:
Testing out my program

Feel free to look over my code and try it out for yourself. Feedback is very appreciated!

Github: https://github.com/MozzarellaMonster/Binary-Reader

Top comments (0)