LRVM: a dead-simple virtual machine to learn about computers

52 points ClementNerma

Hi there!

I'm introducing to you today a little project I've made: LRVM

It stands for Lightweight Rust Virtual Machine, and is designed for beginners to learn how the basics of low-level programming work.

It has a fully-custom 31-instruction ISA and assembly language, MMIO (memory mapping), 32x 32-bit registers, support for common features like exceptions, and even an MMU!

If you don't know what all of these mean, you're the kind of person I made this project for ;)

TL;DR: you can take a look at the repo or check out the tutorial. To have a pretty complex example of the assembly language, go here.

There is a base crate that contains everything that's required to run the VM, which is only 1.5k lines of code. It's extremely simple to read, and I voluntarily decided to not implement certain optimizations in order to make the code more readable by beginners. It still has very good performance, on my 9th gen i7, I get about 80 million instructions per second when running in release mode.

Then there is the tools crate which contains all kind of tools to simplify the usage of the VM: a program to convert assembly to machine code, debug tools, functions to convert raw data like registers' ID to readable human representation.

And finally the auxiliaries' crate contains a few components that you can plug into the virtual motherboard to get some common features: BootROM, RAM, persistent memory that remains after reboot, file-backed memory that remains after program exits, etc.

If you're a bit more experienced and want to know directly how the VM's architecture works, there is a specifications document for that!

I hope this project is useful (or at still kind of interesting) to some of you :)