Hacker News new | ask | show | jobs
by cecilpl2 2139 days ago
I have written a virtual CPU from very simple building blocks and highly recommend doing it.

I started with class Wire (which just wraps a bool) and class Transistor (which accepts two input Wire& and has an output Wire). It has an Update() function which sets the output state.

From those I built up gates, then flipflops, registers, mux/demuxes, counters, an ALU, and memory banks. Then I wrote a machine language, connected the cpu together, wrote a loader to load machine language files into ROM, then an assembler to allow me to write assembly code.

I then added VRAM, async buses, and wrote a working implementation of Game of Life.

It has 32bit instructions and runs about 10kHz with 8KB of RAM.

3 comments

Do you happen to have the source for this anywhere?
Cool. If you want to do that the easy way, use an electronics circuit simulator (https://en.wikipedia.org/wiki/List_of_free_electronics_circu...)

If you want a further challenge, write compiler optimization passes that recognize bit arrays a being bytes, figure out what parts are buses, binary adders, etc. and see how fast you can get this to run.

It probably would be fairer to do this on somebody else’s version of this, but I guess doing it on code you know already is hard enough, if you want to get compiler passes that likely would work on other simulated hardware, too.

I did something similar, but with a LogicGate and a Wire class. I would recommend Morris Mano for those who would like to try.
It looks like those authors have a handful of computer engineering books. Is there one particular title you would recommend?
sure, I think what I've used translates to the title "digital design"