Documentation
¶
Overview ¶
Package cpu contains the CPU for our virtual machine interpreter.
We should probably use the constants defined in `opcodes/opcodes.go` instead of the literal hex-constants for our bytecode, but that's a minor issue.
Package cpu contains a number of registers, here we implement them.
Index ¶
- Variables
- func LoadProgTrap(c *CPU, num int) error
- func ReadStringTrap(c *CPU, num int) error
- func RemoveNewLineTrap(c *CPU, num int) error
- func StrLenTrap(c *CPU, num int) error
- func TrapNOP(c *CPU, num int) error
- type CPU
- type Flags
- type IntegerObject
- type Object
- type Register
- type Stack
- type StringObject
- type TrapFunction
Constants ¶
This section is empty.
Variables ¶
var TRAPS [0xffff]TrapFunction
TRAPS is an array of our trap-functions.
Functions ¶
func LoadProgTrap ¶
LoadProgTrap loads a program into memory
Input: Memory address in register 0.
Output: None
func ReadStringTrap ¶
ReadStringTrap reads a string from the console
Input: None
Output:
Sets register 0 with the user-provided string
func RemoveNewLineTrap ¶
RemoveNewLineTrap removes any trailing newline from the string in #0
Input:
The string operate upon in #0.
Output:
Sets register #0 with the updated string
func StrLenTrap ¶
StrLenTrap returns the length of a string.
Input:
The string to measure in register 0.
Output:
Sets register 0 with the length
Types ¶
type CPU ¶
type CPU struct { // STDIN is an input-reader used for the input-trap. STDIN *bufio.Reader // STDOUT is the writer used for outputing things. STDOUT *bufio.Writer // contains filtered or unexported fields }
CPU is our virtual machine state.
func (*CPU) LoadBytes ¶
LoadBytes populates the given program into RAM. NOTE: The CPU-state is reset prior to the load.
func (*CPU) LoadFile ¶
LoadFile loads the program from the named file into RAM. NOTE: The CPU-state is reset prior to the load.
func (*CPU) Reset ¶
func (c *CPU) Reset()
Reset sets the CPU into a known-good state, by setting the IP to zero, and emptying all registers (i.e. setting them to zero too).
func (*CPU) Run ¶
Run launches our intepreter. It does not terminate until an `EXIT` instruction is hit.
func (*CPU) SetContext ¶
SetContext allows a context to be used as our virtual machine is running. This is most used to allow our caller to setup a timeout/deadline which will avoid denial-of-service problems if user-supplied script(s) contain infinite loops.
type Flags ¶
type Flags struct {
// contains filtered or unexported fields
}
Flags holds the CPU flags - of which we only have one.
type IntegerObject ¶
type IntegerObject struct {
Value int
}
IntegerObject is an object holding an integer-value.
func (*IntegerObject) Type ¶
func (i *IntegerObject) Type() string
Type returns `int` for IntegerObjects.
type Object ¶
type Object interface {
Type() string
}
Object is the interface for something we store in a register.
This is done to allow future expansion, and also for neatness.
type Register ¶
type Register struct {
// contains filtered or unexported fields
}
Register holds the contents of a single register, as an object.
This means it can hold either an IntegerObject, or a StringObject.
func (*Register) GetInt ¶
GetInt retrieves the integer content of the given register. If the register does not contain an integer that is a fatal error.
func (*Register) GetString ¶
GetString retrieves the string content of the given register. If the register does not contain a string that is a fatal error.
func (*Register) SetInt ¶
SetInt stores the given integer in the register. Note that a register may only contain integers in the range 0x0000-0xffff
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack holds return-addresses when the `call` operation is being completed. It can also be used for storing ints.
type StringObject ¶
type StringObject struct {
Value string
}
StringObject is an object holding a string-value.
func (*StringObject) Type ¶
func (i *StringObject) Type() string
Type returns `string` for StringObjects.
type TrapFunction ¶
TrapFunction is the signature for a function that is available as a trap.