vm

package
v0.0.0-...-0b3ecdf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2013 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package vm implements the drones virtual machine, a 16 bit VM intended to drive fictional robots.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpcodeNames

func OpcodeNames() map[Opcode]string

OpcodeNames returns a map of opcode values to their name.

Types

type Opcode

type Opcode uint

Opcode represents the executable opcodes for the VM.

const (

	// Does nothing for 1 clock cycle.
	Nop Opcode = iota

	// Jumps to the argument address.
	Jmp
	// Jumps to the argument address only if a == 0.
	Jz
	// Jumps to the argument address only if a != 0.
	Jnz

	// Swaps the a and b registers.
	Sab
	// Swaps the a and p registers.
	Sap
	// Swaps the a and i registers.
	Sai
	// Loads the b register into p.
	Lbp

	// Loads a constant argument into a.
	Ldc
	// Loads from memory at the argument address into a.
	Ldm
	// Loads from memory at the address in p into a.
	Ldp
	// Loads from memory at the address in (p + i) into a.
	Ldi

	// Pushes the content of a onto the stack.
	Push
	// Pops the top of the stack into a.
	Pop

	// Reads from the bus number in the argument into a.
	Rb
	// Writes from a into the bus number in the argument.
	Wb

	// Jumps to the address in the argument preparing a new stack
	// frame for a function call.  Does not automatically preserve
	// registers, push them manually if you want to save them.
	Call
	// Returns from a function called with call.
	Ret

	// a <- a + b
	Add
	// a <- a - b
	Sub
	// a <- a * b
	Mul
	// a <- a / b, b <- a % b
	Div
	// a <- a / b, b <- a % b (signed)
	Sdiv

	// a <- a & b
	And
	// a <- a | b
	Or
	// a <- a ^ b
	Xor
	// a <- a << b
	Shl
	// a <- a >> b
	Shr
	// a <- ~a
	Not

	// a <- a < b ? 0xffff : 0
	Lt
	// a <- a < b ? 0xffff : 0 (signed)
	Lts
	// a <- a <= b ? 0xffff : 0
	Le
	// a <- a <= b ? 0xffff : 0 (signed)
	Les
	// a <- a > b ? 0xffff : 0
	Gt
	// a <- a > b ? 0xffff : 0 (signed)
	Gts
	// a <- a >= b ? 0xffff : 0
	Ge
	// a <- a >= b ? 0xffff : 0 (signed)
	Ges
	// a <- a == b ? 0xffff : 0
	Eq
	// a <- a != b ? 0xfff : 0
	Neq
)

type VM

type VM struct {

	// The number of 16-bit words in memory.
	MemSize uint16

	// Externally accessible buses.
	Buses []uint16
	// contains filtered or unexported fields
}

VM is an instance of the virtual machine. It stores the VM's current state, and its methods can be used to mutate its state. The buses are directly addressable, and allow the VM to communicate with external components.

func New

func New(memSize uint16, buses uint16) (vm *VM)

New allocates and initializes memory, buses and registers for a new VM.

func (*VM) Clock

func (vm *VM) Clock()

Clock advances the VM by one clock cycle. The opcode located at the memory location at index ip will be executed with the opcode located at memory location ip + 1 used as an argument. See the Opcode constants for more information.

func (*VM) ClockN

func (vm *VM) ClockN(n int)

ClockN runs Clock() on the vm n times.

func (*VM) Debug

func (vm *VM) Debug() string

Debug returns a string that fully specifies the current state of the VM.

func (*VM) LoadBinary

func (vm *VM) LoadBinary(r io.Reader) error

LoadBinary reads a binary data file into the VM's memory. The binary file should begin with a single 16-bit value designating the number of 16-bit words in the file, followed by that many values. Byte-order is expected to be little-endian.

func (*VM) LoadMem

func (vm *VM) LoadMem(memory []uint16)

LoadMem copies a slice of uint16 into the VM's memory.

func (*VM) LoadOpcodes

func (vm *VM) LoadOpcodes(memory []Opcode)

LoadOpcodes copies a slice of Opcodes into the VM's memory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL