Crate strawberryvm

source ·
Expand description

A fantasy virtual machine with limits on resources.

Instructions

NameArgumentsDescription
No OperationNoneDoes nothing.
Pushu8 (8-bit value to push)Pushes an 8-bit value onto the stack.
Pop RegisterRegister (destination register)Pops a value from the stack into the specified register.
Push RegisterRegister (source register)Pushes the value of the specified register onto the stack.
Add StackNoneAdds the top two values on the stack.
Add RegisterTwo Registers (operands)Adds the values of two registers and stores the result in the destination register.
Signalu8 (signal value)Sends a signal with an 8-bit value.
Jumpu8 (target address)Jumps to the specified address in the program.

Reserved symbols

SymbolUse
$Hexadecimal value
%Binary value
^Label value

Hopes for this project

  • Turing completion
  • Custom assembly
  • Possible language implementation (C or Lua)
  • Video card
  • Ability to render games

Example usage:

use strawberryvm::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut vm = Machine::new();

    vm.define_handler(0xF0, |machine| machine.machine_halted = true);

    write_memory!(vm,
       0 => 0x1,
       1 => 0xA,
       2 => 0x1,
       3 => 0x8,
       4 => 0x4,
       5 => 0x0,
       6 => 0x2,
       7 => 0x0,
       8 => 0x6,
       9 => 0xF0
    );

    while !vm.machine_halted {
        vm.step()?;
    }

    println!("A = {}", vm.get_register(Register::A));

    Ok(())
}

Modules

  • Can be included to get everything useful for the machine

Macros

  • Simple macro to write instructions into the machines memory. Usually used in tests or very very simple programs