A virtual machine

Emulation is the reproduction of the behavior of a computer's physical circuitry with software. Given that an emulator can translate the actions of one computer onto an other, the same program could be used on both.
This is called emulation.

Someone could devise the actions of a fictional computer that is not necessarily based on existing hardware, write software for this fantastical computer, implement an emulator for it, and use the same program on supported systems.
This is called a virtual machine.

Le Matin des magiciens, by Louis Pauwels and Jacques Bergier. 1963

Over the years, I wrote software over various frameworks for a multitude of peripherals. The vast majority is now defunct after vanishing with the platform they were targetting or by falling behind on the requirements of their ever-changing toolchains. Bitrot is the inability to access digital data because hardware and software no longer exist to read its format. Perhaps it's just a matter of time until people build emulators to make these projects usable again, otherwise these projects were never truly mine, and my learning of these languages only ever belonged to the platforms.

So, why not C? While terminal utilities can be made portable, cross-platform audio and graphical development is not.

I. An Adequate Number Of Bits

During my research into portability, I kept thinking about how frictionless it is to play NES games today. Pulling on that thread led me to projects designed explicitly for virtual machines, such as Another World which is equally easy to play today due to its targeting of a portable virtual machine, instead of any ever-changing physical hardware.

For a time, I thought I ought to be building software for the NES to ensure their survival over the influx of disposable modern platforms — So, I did. Unfortunately, most of the software that I care to write and use require slightly more than an 8-button controller.

So, why not the Commodore 64? While it is of the same era as the NES, not only is implementing a 6502 emulation a difficult task, implementing a c64 emulator is a monumental project beyond my abilities.

Saul Steinberg, Untouched by Human Hands

II. Tarpits & Houses Of Cards

If the focus of this experiment is to ensure the support of a piece of code by writing emulation software for each new platform, the specifications should be painless to implement. Let's use the time one would need to write a passable emulator as a limit in complexity for this system. Could a computer science student implement a mediocre emulation of the 6502 instructions in an afternoon? Could that design be simplified, changed in some way to make it more approachable for would-be implementers?

Subleq is a One-Instruction architecture, an emulator for this platform takes at most 15 minutes to write. Perhaps this could be an interesting target, but what it does away in emulation complexity, it offloads onto the toolchain needed to make intelligible programs.

So, let us also set a limit to the complexity of the toolchain as to not find ourselves in a tarpit. It would be an equally Herculean task to build an emulator and assembler for a machine with thousands of instructions; or a single instruction machine building abstract logic from thousands of primitive parts.

How complex can our virtual machine be if the time shared between the implementation of the runtime and assembly toolchain cannot exceed a single weekend. We shall call this definition of a computer that fits on a napkin, capable of self-hosting in a few hundred lines of its own assembly — A One-page Computer.

III. Things Betwixt

A paper computer was created in the early 1980s, when computer access was not yet widespread, it consists of a piece of paper with 21 lines of code on the left and eight registers on the right. The instruction set of five commands(inc, dec, jmp, isz, stp) is small but Turing complete, meaning that it can approximately simulate the computational aspects of any other real-world general-purpose computer, and is therefore enough to represent all mathematical functions.

In 1977, a programmer wrote a small virtual machine with 36 instructions, 16 registers and 4096 bytes of memory. It has no mouse, the screen is barely capable of displaying readable text, its controller is a nightmare. — But I was able to write an emulator and an assembler for it, in an afternoon.

There is a type of electronics relevant to emulation that has no chips, no instruction set, but instead the programmer designs the hardware circuit in software. As much fun as it would be to create a design that maps well onto these machines, it would make for a poor emulation on my existing devices as it would use nearly none of the optimizations available on the host computer.

Somewhere along this voyage into finding a suitable host for my programs, I began thinking about electronic waste, and I couldn't justify surrounding myself with yet more electronics. This dream platform would therefore be designed to be emulated, its complexity would be designed around the complexity of software and not that of hardware.

IV. Back & Forth

The balancing act of virtual machine instructions, assembler, emulator and the resulting capabilities of its language revealed the stack machines as ideal systems.

Swap operation by Leo Brodie

Forth is a programming language that consists simply of splitting text into a list of words, breaking on whitespace, and to interpret each word, words are often combinations of other words combined to create more complex words.

Brackets and parentheses are unnecessary: the program merely performs calculations in the order that is required, letting the automatic stack store intermediate results on the fly for later use. Likewise, there is no requirement for precedence rules.

LIT a b c [PC]
JCI a b (c8){PC+=[PC]}
JMI a b c {PC+=[PC]}
JSI a b c | PC {PC+=[PC]}

BRK a b c      EQU a b==c             LDZ a b [c8]       ADD a b+c
INC a b c+1    NEQ a b!=c             STZ a {[c8]=b}     SUB a b-c
POP a b        GTH a b>c              LDR a b [PC+c8]    MUL a b*c
NIP a c        LTH a b<c              STR a {[PC+c8]=b}  DIV a b/c
SWP a c b      JMP a b {PC+=c}        LDA a b [c16]      AND a b&c
ROT b c a      JCN a (b8){PC+=c}      STA a {[c16]=b}    ORA a b|c
DUP a b c c    JSR a b | PC {PC+=c}   DEI a b [D+c8]     EOR a b^c
OVR a b c b    STH a b | c            DEO a {[D+c8]=b}   SFT a b>>c8l<<c8h

In Forth, memory is made of blocks of cells, which are typically 16-bits in length, meaning that each piece of data is a number from 0 to 65535. For this specific imaginary system, I wanted the memory to consist of cells of 8-bit, or numbers from 0 to 255. For example, the 12 / (34 - 12) sequence is equivalent to the 6 bytes:

uxntal |  #  12  34 OVR SUB DIV
binary | a0  12  34  07  19  1b

The result is an expressive and extendable virtual machine that can be implemented in an afternoon exposing a user programmable assembly running at a reasonable speed. Using stack-machine operations as primitives, along with a handful of arithmetic and bitwise functions, we find the resulting virtual machine and 32 opcodes.

Uxn cannot error and has no unspecified behaviors or edge cases. Its documentation encourages re-implementation instead of the adoption of a specific implementation. It operates on bytes as to remain portable on small systems, abstracting I/O entirely to the host system via dedicated opcodes.

incoming devlog varvara devlog