For this project, I wrote a C program that emulates or mimics an old family of processors, called x6502.
Before I started this project, I was trying to find resources to understand how computers work "under the hood". I had done some research into operating systems, and had previously read textbooks on assembly and computer architecture. A mentor of mine at the company I was working for at the time suggested writing an emulator.
This project was a great suggestion, I learned a lot doing this. Some of the highlights doing this project are:
An emulator is essentially a virtual construction of a physical computer. There is a family of chips called x6502, that were built and used by different devices. For example, the original Nintendo was based on these chips. The program I wrote will read a series of opcodes (or instructions), and then execute them the same way a chip would execute those instructions.
In theory, if I wrote a basic program in assembly, it would run on my emulator just like it would run on an actual chip, which is pretty cool.
Almost all the work in my project is done by vcpu.c
. To be honest, it works mostly with a huge switch statement, which does something different for each of the different opcodes defined by the chip. So I spent a lot of time researching the opcodes and what they did, and then it was just a matter of implementing the various opcodes. This tends to be pretty simple operations, like left or right shift bits, add with overflow, etc.
The only other consideration was memory. I used an array to simulate the memory, which is pretty easy to do considering the chip has a limited memory space.
There were some excellent resources I found to help bootstrap me. This project here was a huge inspiration, and I found some other really great resources online.
You can find my project here: https://github.com/bknoechel/x6502-emulator. Overall, a very interesting project, I would definitely recommend writing an emulator to those looking as a way to dive into lower level program. And the cool thing is, you can write it in any language you want, if I were to redo this project I would probably pick a higher level language like JavaScipt.
Last updated 2018-10-02