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 😀

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?