vm

package
v0.0.0-...-600cb72 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2017 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package vm provides a virtual machine runtime environment.

Index

Constants

View Source
const (
	WORD_SIZE     uint16 = 2
	MAX_CMD_ARGS  uint16 = 0x02
	MAX_MEMORY    uint16 = 0xFFFF
	CODE_POINTER  uint16 = 0x0000
	STACK_POINTER uint16 = 0x0002
	ZERO_FLAG     uint16 = 0x0004
	CARRY_FLAG    uint16 = 0x0006
	REGISTER_AX   uint16 = 0x0008
	REGISTER_BX   uint16 = 0x000A
	REGISTER_CX   uint16 = 0x000C
	REGISTER_DX   uint16 = 0x000E
	INTERRUPT     uint16 = 0x0010
	IR_STATE      uint16 = 0x0012
	IR_KEYBOARD   uint16 = 0x0014
	IR_OVERFLOW   uint16 = 0x0016
	STACK_BASE    uint16 = 0x0100
	STACK_MAX     uint16 = 0x01FF
	OUT_CHARS     uint16 = 0x1000
	OUT_COLORS    uint16 = 0x1F00
	OUT_MODE      uint16 = 0x1FFE
	OUT_MODE_TERM uint16 = 0x0001
	CODE_BASE     uint16 = 0x2000

	FLAG_MASK uint16 = 0xFF00
	FLAG_RR   uint16 = 0x0100
	FLAG_RI   uint16 = 0x0200
	FLAG_RA   uint16 = 0x0300
	FLAG_AA   uint16 = 0x0400
	FLAG_AR   uint16 = 0x0500
	FLAG_IA   uint16 = 0x0600
	FLAG_II   uint16 = 0x0B00
	FLAG_AI   uint16 = 0x0C00
	FLAG_IR   uint16 = 0x0700
	FLAG_I    uint16 = 0x0800
	FLAG_R    uint16 = 0x0900
	FLAG_A    uint16 = 0x0A00
	FLAG_NONE uint16 = 0x0000

	CMD_MASK uint16 = 0x00FF
	CMD_ADD  uint16 = 0x01 // R,R - R,I
	CMD_SUB  uint16 = 0x02 // R,R - R,I
	CMD_MUL  uint16 = 0x03 // R,R - R,I
	CMD_DIV  uint16 = 0x04 // R,R - R,I
	CMD_INC  uint16 = 0x05 // R
	CMD_DEC  uint16 = 0x06 // R
	CMD_AND  uint16 = 0x07 // R,R - R,I
	CMD_OR   uint16 = 0x08 // R,R - R,I
	CMD_XOR  uint16 = 0x09 // R,R - R,I
	CMD_NOT  uint16 = 0x0A // R,R - R,I
	CMD_SHL  uint16 = 0x0B // R,R - R,I
	CMD_SHR  uint16 = 0x0C // R,R - R,I
	CMD_MOV  uint16 = 0x0D // R,R - R,A - A,A - A,R - I,A - I,R
	CMD_PUSH uint16 = 0x0E // R - I
	CMD_POP  uint16 = 0x0F // R
	CMD_CMP  uint16 = 0x10 // R,R - R,I
	CMD_CNT  uint16 = 0x11 // R,R - R,I
	CMD_LGE  uint16 = 0x17
	CMD_SME  uint16 = 0x18
	CMD_JIF  uint16 = 0x12 // R - I
	CMD_JMP  uint16 = 0x13 // R - I
	CMD_CALL uint16 = 0x14
	CMD_RET  uint16 = 0x15
	CMD_HLT  uint16 = 0x16

	IR_OVERFLOW_CODE  uint16 = 0x1
	IR_OVERFLOW_STACK uint16 = 0x2
)

Variables

View Source
var (
	BaseColors = []uint16{
		0x000,
		0xFFF,
		0xF00,
		0x0F0,
		0x00F,
		0xFF0,
		0xF0F,
		0x0FF,
	}
	FlagSize = map[uint16]int{
		FLAG_RR:   2,
		FLAG_RI:   2,
		FLAG_RA:   2,
		FLAG_AA:   2,
		FLAG_AR:   2,
		FLAG_IA:   2,
		FLAG_IR:   2,
		FLAG_I:    1,
		FLAG_R:    1,
		FLAG_NONE: 0,
	}
)
View Source
var (
	ByteOrder = binary.BigEndian
)

Functions

This section is empty.

Types

type DemoDisplay

type DemoDisplay struct {
	TermboxDisplay
}

DemoDisplay renders a variety of colors.

func (DemoDisplay) Draw

func (DemoDisplay) Draw(width, height int, data []byte)

type Display

type Display interface {
	// Draw the data on a display with a specific width and height.
	Draw(width, height int, data []byte)
	// Init the display driver.
	Init() error
	// Close and dispose the display.
	Close()
}

Display is a drawable display.

type Interrupt

type Interrupt struct {
	Type uint16
	Code uint16
}

Interrupt with a type and a value code.

type Machine

type Machine struct {
	Memory
	// contains filtered or unexported fields
}

Machine is a sixteen bit virtual machine.

func New

func New() *Machine

New instantiates a new virtual machine.

func (*Machine) Boot

func (machine *Machine) Boot(code []byte) error

Boot copies the bytecode into the program segment and starts the virtual machine.

func (*Machine) EnableDebug

func (machine *Machine) EnableDebug(debug bool)

EnableDebug prints verbose debugging logs.

func (*Machine) Halt

func (machine *Machine) Halt()

Halt sets the running flag to false. The machine will shutdown after the current operation.

func (*Machine) Interrupt

func (machine *Machine) Interrupt(code, reason uint16)

Interrupt sends a interrupt event to the virtual machine.

func (*Machine) PerformArithmetic

func (machine *Machine) PerformArithmetic(carry func(int, int) int) error

PerformArithmetic executes a arithmetic function with two parameters.

func (*Machine) PerformCall

func (machine *Machine) PerformCall() error

PerformCall pushes the current code pointer onto the stack and jumps to the specified memory point.

func (*Machine) PerformJump

func (machine *Machine) PerformJump(jumpAlways bool) error

PerformJump jumps two the specified code point. If JumpAlways is set to false, the code pointer will only be changed if the zero flag is 1.

func (*Machine) PerformLogic

func (machine *Machine) PerformLogic(base func(uint16, uint16) uint16) error

PerformLogic executes a logic function with two parameters.

func (*Machine) PerformMove

func (machine *Machine) PerformMove() error

PerformMove executes a copy operation on registers, values and addresses.

func (*Machine) PerformPop

func (machine *Machine) PerformPop() error

PerformPop copies data from the stack into a register.

func (*Machine) PerformPush

func (machine *Machine) PerformPush() error

PerformPush pushes an argument value onto the stack.

func (*Machine) PerformReturn

func (machine *Machine) PerformReturn() error

PerformReturn fetches a memory pointer from the stack and jumps to the code point.

func (*Machine) PerformSimpleArithmetic

func (machine *Machine) PerformSimpleArithmetic(carry func(int) int) error

PerformSimpleArithmetic executes a simple arithmetic function with only one parameter.

func (*Machine) PerformSimpleLogic

func (machine *Machine) PerformSimpleLogic(base func(uint16) uint16) error

PerformSimpleLogic executes a simple logic function with only one parameter.

func (Machine) String

func (machine Machine) String() string

String visualizes the register segment of the virtual machine.

type Memory

type Memory interface {
	Load(addr uint16) (uint16, error)
	Store(addr, value uint16) error
	StoreByte(addr uint16, value byte) error
	Segment(from, to uint16) []byte
	Convert(value uint16) []byte
	InRange(addr uint16) bool
}

Memory is a virtual representation of a RAM.

func NewMemory

func NewMemory(size int) Memory

NewMemory creates a new memory with a given range.

type OutOfRangeError

type OutOfRangeError struct {
	Address uint16
}

OutOfRangeError is thrown if a address is out of memory range.

func (OutOfRangeError) Error

func (err OutOfRangeError) Error() string

type TermboxDisplay

type TermboxDisplay struct{}

TermboxDisplay is a generic display based on go-termbox.

func (TermboxDisplay) Close

func (TermboxDisplay) Close()

func (TermboxDisplay) Init

func (TermboxDisplay) Init() error

type TextDisplay

type TextDisplay struct {
	TermboxDisplay
}

TextDisplay is a simple termbox console display.

func (TextDisplay) Draw

func (TextDisplay) Draw(width, height int, data []byte)

Jump to

Keyboard shortcuts

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