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 ¶
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 (*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) LoadBinary ¶
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) LoadOpcodes ¶
LoadOpcodes copies a slice of Opcodes into the VM's memory.