Skip to content

About

Virtual machine (and prog lang) implemented in Digital (logic sim) for fun

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Fors Machine

A custom 16-bit CPU built from logic gates in Digital, with its own instruction set, assembler definitions, and a small and simple compiled programming language for this CPU, just for fun.

Instruction set

The CPU is a RISC architecture with a fixed 32-bit length. While the instructions are 32 bits long, the actual data width is 16 bits.

Instructions follow a format of 8-bit opcode followed by 24 bits of operands, which can be immediate values, registers, or addresses.

The first nibble of the opcode determines the instruction family:

Range Family Purpose
0x0n LOAD Loads data into the registers, whether from memory or an immediate value
0x1n STORE Stores data into memory
0x2n ARITHMETIC and, or, xor, not, add, sub, mul, div, mod, shifts
0x4n CMP writes eq/ne/gt/lt/ge/le flags into a target register
0x5n JMP jumps to a point of machine code, absolute or relative, conditional or unconditional

0xFFFFFFFF halts the machine.

Assembler (programs/rules.asm)

Assembly is defined as a customasm ruleset. It maps mnemonics like load, store, add, cmp, and jmp_if to machine code, and adds pseudo-instruction macros (push, pop, push_rpc, leave) that expand into several real instructions for better DX.

Compiler (ForsMachine.Compiler/)

A C# (.NET 8) compiler for a small expression-based language. It has a hand-written tokenizer and recursive-descent parser, a type checker, and a code generator that outputs assembly. It has a handful of features, some of which you might be able to tell is inspired by Lisps:

  • Typed functions (int16, pointers, function pointers)
  • if expressions, with local bindings, and defer
  • Anonymous functions, and recur for anonymous self-recursion
  • Stack frames with a base pointer for arguments and locals

Here is an example of a recursive Fibonacci function:

function fibonacci(n: int16): int16 =
    (function (m: int16, a: int16, b: int16): int16 =
        if (m == 0) a
        else recur(m - 1, b, a + b))
        (n, 0, 1);

function main(): int16 = fibonacci(8);

Emulator (ForsMachine.VirtualMachine/)

This is a C# software model of the same CPU. It runs .bin files and dumps memory after each step. The primary purpose is to test the compiler and run the machine without running Digital.

Usage

dotnet build
cd programs
./compile.sh hello-world.fors
dotnet run --project ../ForsMachine.VirtualMachine -- hello-world.bin

The emulator prints registers and memory after each instruction. The return value of main ends up in rax at the end (e.g., for hello-world.fors, which computes factorial(5), rax = 0x0078).

To run Digital, open dig/Main.dig in Digital and load the .bin file into program memory.

Example programs

programs/ contains hand-written and compiled examples, including recursive Fibonacci, factorial, and early attempts at Advent of Code 2024 and sketches of a memory allocator API.

About

Virtual machine (and prog lang) implemented in Digital (logic sim) for fun

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages