
Write your own Virtual CPU in C++ (2014) - andreygrehov
http://megalomaniacbore.blogspot.com/2014/04/virtual-cpu-in-c-4001-cpu.html
======
saagarjha
Writing VMs is a great way to understand how a real CPU might work–not
functionally, of course, but with a fairly good idea of some of the important
parts. I had my students implement one (in C) for a class of mine, which made
teaching a lot of higher-level topics much easier because they had implemented
the levels underneath and understood better what was going on.

------
KMag
Implementing the Beta processor (32-bit RISC processor loosely based on the
DEC Alpha) for 6.004 [0] was one of the most fun projects I've done. We wrote
everything at the netlist level (or at least I'm told it's roughly equivalent
to SPICE's netlists) rather than VHDL or Verilog, with some simple component
libraries (mostly 2-,3-,and 4-input MUXes/DEMUXes) and a register file
implementation provided by the instructors.

These days, I would hope they'd use a simplified ISA based on either RISC-V or
something as close to 64-bit ARM as they could legally get away with.

[0] [https://ocw.mit.edu/courses/electrical-engineering-and-
compu...](https://ocw.mit.edu/courses/electrical-engineering-and-computer-
science/6-004-computation-structures-spring-2009/tools/)

------
eatonphil
Like others say, writing a VM is a great project for your own education! I
thought it would be hard to target a real system like x86 but after trying to
get started on older Nintendo systems I gave up and just did x86. It's simpler
because it's more familiar and the tooling is more easily accessible.

Here is a series of tutorials I wrote on building an x86 emulator in
JavaScript:

[https://notes.eatonphil.com/emulator-basics-a-stack-and-
regi...](https://notes.eatonphil.com/emulator-basics-a-stack-and-register-
machine.html)

[https://notes.eatonphil.com/emulator-basics-system-
calls.htm...](https://notes.eatonphil.com/emulator-basics-system-calls.html)

------
xerox13ster
> The right most is known as the least significant bit, the right most the
> most significant bit.

I can only assume this is meant to say the left is the most significant bit?

------
userbinator
_This first processor is very simplistic, it can do just one logical thing...
Addition_

That's a very good place to start, because historically, computers evolved
from calculators.

