Let's Build A CPU!
Welp, Iām building a CPU. At least, I hope. But, letās rewind a bit.
A few months back, I realized most of the projects I had in mind and wanted to try at some point had something in common: I really wanted to build a CPU, somehow. I wanted to make a GBC emulator, and thatās obviously a CPU. I wanted to build some kind of simulation of automated behaviour, and thatās also a CPU. I wanted to make a disassembler and decompiler, and guess what? Making a CPU includes those, if youāre not starting from scratch.
So, I decided to make a CPU. That was months ago, and I didnāt really make any kind of progress until today, except some basic research. Why? Iām convinced the main reason was that I had no one to hold me to my decision, since I told no one like most of my projects. Here I am now, telling you, yes, you specifically, to hold me to this.
I want to make a CPU. From scratch - or at least reimplement āclean room styleā an existing CPU, first in software then maybe on a FPGA board if I ever get there. The roadmap currently looks something like:
- Find an easy to replicate and understand architecture - all signs are currently pointing to the 6502
- Find as much reference material on it as possible
- [Weāre here]
- Learn to code in ASM for the selected architecture, and get really comfortable with it
- Write a disassembler for the selected architecture, and get it working really well
- Write an emulator for the selected architecture, and get it working perfectly
- Pick the simplest instruction for the architecture and think about how it was implemented
- Implement the instruction in a logic simulator
- Repeat the last two steps for a few more instructions
- Build the rest of the instructions upon the easy ones implemented in the simulator
- Optimize the design, if possible
- Build the design into an FPGA, if possible - while learning about FPGAs since I know nothing about them :D
Of course, this is going to take a while - Iām hoping to get at least a complete emulator done in a few months, if I stick with this, and then decide where to go from there. There is a massive amount of work to do, and a lot of stuff to learn, considering what very little I know about ASM programming I learned back in high school and I know basically nothing about CPU design and low level architecture.
Iām probably going to use C# for the non ASM parts - the disassembler and emulator - since itās the language I know best and this is not a language learning exercise, so I donāt want to waste time on useless stuff. The code I write is probably not going to be open source, since it doesnāt really contribute anything to anyoneās project except maybe causing some headaches for experienced ASM coders asking themselves why I did stuff the way I did⦠but Iām gonna post snippets here, and probably dump the complete projects (ASM programs, disassembler, emulator, ā¦) on Sourcehut when theyāre done. I just donāt want to maintain incomplete stuff out there on the internet, since that causes significant overhead. I might stream parts of the process on Twitch, if I ever get around to setting up my channel :^.
The first goal Iām setting for myself is to finalize the architecture decision - though that seems pretty final since the 6502 looks simple enough and the instruction set is very small and extremely well documented - and build a few of the āusual suspectsā to familiarize myself with the ASM syntax: Fibonacci sequence, Collatz conjecture, Project Eulerās solutions and so on.
Iāll post back here for the big milestones and for interesting stuff I find out, and Iāll keep Twitter updated with anything noteworthy - if I remember Twitter exists for long enough this time, so subscribe to the RSS or follow me on Twitter for updates on the project!
Oh, and the āBCPā tag stands for Basic CPU Project. I couldnāt come up with anything better, and a quick search didnāt turn up any conflict. And BCP sounds nice as an architecture name, right?