I was tricked into writing a VM while doing Advent of Code last year and it really deepened my understanding of programming (e.g., on how to implement simple coroutines). Advent of Code is supposed to be 25 daily programming puzzles leading up to christmas. I went in thinking they were going to be algo/datastructure tasks but half of them were actually about implementing and using a VM for a made up assembly language…
Funny that VM related days are all prime numbers :). EDIT: No they're not, I wasn't fully awake, sorry.
Write Your Own Virtual Machine
21–30 of 44 posts
Re: Write Your Own Virtual Machine
#22Re: Write Your Own Virtual Machine
#23I 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.
Re: Write Your Own Virtual Machine
#24If you think you need to develop a VM, I recommend to reconsider, and think how you can reuse something that’s already there. For instance, modern .NET VM is open source with MIT license, the code quality is more or less OK, and it’s relatively easy to generate .NET bytecode from something else, Reflection.Emit from the inside, or Mono.Cecil from outside.
Re: Write Your Own Virtual Machine
#25I 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, con…
Re: Write Your Own Virtual Machine
#26Never wrote virtual machines, because I know why Sun (now Oracle) and Microsoft both spent a billion each to create theirs. You can write something that works over a weekend, but the performance won’t be good without JIT, generational GC, and many other extremely complicated optimizations. If you think you need to develop a VM, I recommend to reconsider, and think how you can reuse something that’s already there. For…
It will teach you skills that are applicable in other contexts and give a deeper understanding of what makes a VM tick.
And sometimes, a tiny VM is just what you need.
Re: Write Your Own Virtual Machine
#27Never wrote virtual machines, because I know why Sun (now Oracle) and Microsoft both spent a billion each to create theirs. You can write something that works over a weekend, but the performance won’t be good without JIT, generational GC, and many other extremely complicated optimizations. If you think you need to develop a VM, I recommend to reconsider, and think how you can reuse something that’s already there. For…
Re: Write Your Own Virtual Machine
#28It‘s great for following along self-paced and has hands-on exercises for each topic. You should be a bit familiar with compilers already though.
Re: Write Your Own Virtual Machine
#29Never wrote virtual machines, because I know why Sun (now Oracle) and Microsoft both spent a billion each to create theirs. You can write something that works over a weekend, but the performance won’t be good without JIT, generational GC, and many other extremely complicated optimizations. If you think you need to develop a VM, I recommend to reconsider, and think how you can reuse something that’s already there. For…
It's a learning experience, it doesn't have to be production quality. It will teach you skills that are applicable in other contexts and give a deeper understanding of what makes a VM tick. And sometimes, a tiny VM is just what you need.
Re: Write Your Own Virtual Machine
#30If you want to go really deep on this I highly recommend NAND To Tetris, a book which starts from the basics of combining NAND gates to build basic logic. You’ll gradually work through building a CPU from those components, building an assembler to program it, and finally putting a virtual machine on top of that.
It's also realllllly satisfying to play Tetris on something you "built" all the way up from basic logic gates.
It's a great (and free) course during a pandemic lockdown or a fun project over a holiday break.