A Simulated Distributed Microprocessor

In case you didn’t already know this, Python is one of the most amazing and fun programming languages ever. It’s incredibly productive, easy to learn, runs on pretty much everything and powers some of the largest sites on the internet — from YouTube to Reddit and Instagram.

Recently, ANTLR4 (which is one of my all-time favorite tools) was extended with a target for Python and I thought it would be fun to try it out and do something a little unusual — inspired by all the fun I had playing Microcorruption:

I built a a simulated, distributed, stack-based microprocessor (a virtual machine) with a hypothetical integer-only instruction set. Programs are written in a simple high-level language which supports limited integer math, while-loops, if-else statements, comments and console output.

Each program’s source code is compiled to bytecodes and placed into a 512-byte memory block, which the virtual machine’s “cpu” operates on. The “cpu” is actually distributed across a number of isolated worker processes, which can scale across physical (or virtual) hardware. The memory blocks are stored in a central Redis cache and the worker threads periodically serialize and de-serialize the blocks in order to execute code. All data from the virtual machine, such as memory, registers, disassembly and console output is streamed back to the user with websockets using a pub/sub pattern.

Here’s what that looks like:

The virtual machine is written in Python 2.7. The simple language is built with the ANTLR4 runtime. The website runs Flask on gunicorn and everything is hosted at DigitalOcean on two small CentOS 6.5 servers.

Please note: This is a toy project, used to examine various architectural ideas and patterns. It is not a production-like implementation of a virtual machine. That said, you are welcome to clone the source code.

Click here to try it and read more about how it all works:

VirtualMachine