I Built a CPU Emulator From Scratch - Here's What I Learned

24. Juli 2025

For my Maturaarbeit (a final-year research project at the gymnasium in Switzerland) I wanted to learn about the fundamentals of computing. Rather than just studying dry material, I decided to build my own CPU emulator along with a custom instruction set and an assembler in Python.

Emulator GUI

My Motivation

The Maturaarbeit is a big challenge but also a huge opportunity. For me, it was the biggest project I ever worked on in school. I got to choose a subject I was interested in and work on it for about half a year. The gymnasium was where I got into coding and since I was always curious about what hides behind abstraction, understanding how computers work at a low level was naturally something I was interested in. I decided that building my own computer would be the best way to really understand how they work.

What I built

The Computer

The main component of my program is of course the computer itself. Like actual computers, it consists of different components. I built a CPU consisting of an arithmetic and logic unit (ALU) and a control unit (CU) to let the computer execute instructions and store data in registers, a RAM to store data and instructions and a clock to orchestrate the Von-Neumann-Cycle. In order to program my CPU I designed my own ISA and for the sake of not having to write every program in ones and zeros, I built an assembler that translates mnemonics into machine code.

The Screen

In order to have some sort of output I decided to build a pixel-based display. The way it works is fairly simple: It reads the last 2 KiB in the RAM and translates the data to color values for the corresponding pixel.

The GUI

Since I also wanted to have a use case for my program, I used tkinter to build a GUI where the user can see exactly what's going on inside of the computer: the registers in the CPU, the calculations in the ALU, the clock, the RAM and a log showing what parts are doing what. Someone interested in how computers work could use the program to observe step for step what a computer actually does when executing a program.

What I learned

While working on this project and immediately after it I was aware that I learned a lot about how to keep a bigger project organized and keep code modular and (obviously) how computers work on a lower level. However after a while I started noticing that this project taught me a lot more.

Because I built this emulator from scratch, wrote programs for it and had to work with it, I learned what a computer "knows" and what capabilities it actually has. I learned that a computer has to somehow keep track of everything it wants to work with. It has the program counter to keep track of the location of the next instruction, the stack pointer to keep track of the top of the stack and so on. I learned how memory is just an array of cells containing data. This all might seem trivial but working with it completely changed the way I think about programming, data structures and computers in general.

After working on this project it was way more intuitive to think about how arrays and linked lists differ and work or how pointer arithmetic works. When I learn about a new concept or datastructure on a low level I often think about how I would implement it in my CPU emulator.

Conclusion

Working on this project taught me much more than just how a computer works but gave me a new perspective on low level systems. I can recommend looking at the code or even rebuilding it from scratch to anyone trying to learn about computers on a low level or programming and computer science in general.


GitHub

If you're curious about the project, my ISA or just want to try out the emulator, then you can visit my GitHub: https://github.com/maliknie/emulator-in-python