Source

You can find the source code for the MicroBlocks VM, MicroBlocks standalone IDE and Websockets to serial connector in our BitBucket repository.

The Snap! version of the MicroBlocks IDE can be found in this other BitBucket repository.

Supported Devices

Technical Requirements

The MicroBlocks virtual machine is designed for 32 bit microcontrollers. It requires at least 8Kb of RAM and 40Kb of flash memory. For most supported boards, the virtual machine is built using the Arduino IDE and libraries, but it has been ported to vanilla Linux. In the past, it was also built using the mbed libraries, although that option is no longer maintained.

MicroBlocks Capable Boards

The following boards are officially supported by the MicroBlocks system:

The following boards are supported by the MicroBlocks system but present some limitations or problems:

For the latest list of supported boards and their status, see Device List on our WIki.

How it Works

The MicroBlocks Virtual Machine

Instructions

MicroBlocks code is executed by a simple interpreter or virtual machine (VM) running on the microcontroller board. The instructions for this virtual machine are 32-bits consisting of an 8-bit operation (opcode) and a 24-bit operand:

<operand (24 bits)><opcode (8-bits)>

The opcode tells the virtual machine what to do. The operand is used to in different ways by different opcodes. For example, it can be used to encode constant values or jump offsets. Some opcodes don't use the operand at all, but both the interpreter and the compiler are simplified by using the same instruction format for all opcodes.

The interpreter is a stack machine. To execute a given opcode, its arguments (i.e. the values of any parameters) are first pushed onto the stack, then the code for the opcode is run. That code pops any arguments off the stack and, optionally, pushes a return value onto the stack.

For example, the instructions for "set user LED <true>" are:

pushImmediate true		// push the boolean value 'true'
setLEDOp			// pop 'true' off the stack and set the state of the user LED to it

The instructions for "set user LED (tilt-x < 10)" are:

mbTiltX				// pushes the value of the tilt X sensor
pushImmediate 10		// pushes the number 10
greaterThan			// remove the two arguments and pushes true if tilt-x is less than 10
setLEDOp			// pop a boolean value and and set the state of the user LED to it

Tasks

MicroBlocks appears to run many scripts at the same time, but that is an illusion. The virtual machine actually runs a few instructions of one script, then a few instructions of the next script, and so on. It only executes one script at a time, but switches between those scripts (or "tasks") so quickly that they appear to be running simultaneously.

Unlike many programming languages, MicroBlocks can only switch to the next task at certain well-defined points in the code: at the end of a loop or at an instruction that explicitly waits, such as "wait 10 milliseconds". This avoids many of the concurrency issues (or race conditions) that one encounters in other languages. It also means advanced users can implement their own higher-level concurrency mechanisms (e.g. locks, semaphores, or monitors) in MicroBlocks because a test followed by some action such as setting a flag cannot be interrupted by another MicroBlocks task partway through. (Such uninterruptible sequences are sometimes called critical sections. In MicroBlocks, every command sequence that does not contain a loop or a block that explicitly waits is an implicit critical section.)

Between running user scripts, the MicroBlocks virtual machine handles a few system chores. For example, it checks for incoming commands from the programming environment when the board is tethered. On some devices, it may also perform additional system tasks. For example, on the BBC micro:bit the virtual machine periodically updates the 5x5 LED display.

Protocol

The MicroBlocks VM communicates with IDEs and other types of software (such as the Mozilla Web of Things gateway) by means of a home brewed serial protocol.

You can find specifications for this protocol here.

The MicroBlocks Programming Environment (IDE)

There are currently two blocks-based programming environments for MicroBlocks, a stand-alone system based on GP and one integrated into Snap! that allows the MicroBlocks programs and Snap! programs to interact with each other when the board is tethered. In the future, there could be other MicroBlocks programming environments, perhaps some using textual code representations rather than blocks.

The programming environment includes a compiler that translates user scripts into a sequence of MicroBlocks opcodes and a code manager that downloads scripts to the board, starts and stops scripts, and keeps track of the running status of every script so it can display graphical feedback (e.g. a glowing border around scripts that are running). Both environments can display the instructions generated by the compiler, allowing users to see how their scripts get turned into low level machine code for the virtual machine.