Code Self Study Forum

"I don't know how CPUs work so I simulated one in code"

This might interest people:

A few months ago it dawned on me that I didn’t really understand how computers work under the hood. I still don’t understand how modern computers work.

However, after making my way through But How Do It Know? by J. Clark Scott, a book which describes the bits of a simple 8-bit computer from the NAND gates, through to the registers, RAM, bits of the CPU, ALU and I/O, I got a hankering to implement it in code.

While I’m not that interested in the physics of the circuitry, the book just about skims the surface of those waters and gives a neat overview of the wiring and how bits move around the system without the requisite electrical engineering knowledge. For me though I can’t get comfortable with book descriptions, I have to see things in action and learn from my inevitable mistakes, which led me to chart a course on the rough seas of writing a circuit in code and getting a bit weepy about it.

The fruits of my voyage can be seen in simple-computer; a simple computer that’s simple and computes things.

1 Like

Looks interesting! I wonder how much the approach in Scott’s book differs from that in Nand2Tetris. I tried out some of the very first projects in Nand2Tetris on Logisim, but didn’t pursue it very much. Maybe I will look back at that, or maybe it would be fun to implement simulations of at least some parts in JavaScript. Don’t know if this is mentioned among the links above, but Code by Charles Petzold is another good book on beginning CPU fundamentals, but it’s more for reading than for implementing I’d say.

Wow. I got my start in all this about seven days after the IBM PC came out. My first programming experience was on a HeathKit which I had to assemble and solder together. Then ultimately, you’d position eight switches ON/OFF to correspond with the 8086 assembly code then push the button to submit that byte to the processor. So you’d have to write the assembly on paper then laboriously enter it byte by byte into the computer, finally telling it to run all that. There was no way of persisting it to any sort of media so you’d power-cycle it to reset it. Things have come a long way since then in convenience. :wink:

1 Like