cpu

package
v0.0.0-...-acc6289 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var TRAPS [0xffff]TrapFunction

TRAPS is an array of our trap-functions.

Functions

func LoadProgTrap

func LoadProgTrap(c *CPU, num int) error

LoadProgTrap loads a program into memory

Input: Memory address in register 0.

Output: None

func ReadStringTrap

func ReadStringTrap(c *CPU, num int) error

ReadStringTrap reads a string from the console

Input: None

Output:

Sets register 0 with the user-provided string

func RemoveNewLineTrap

func RemoveNewLineTrap(c *CPU, num int) error

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

func StrLenTrap(c *CPU, num int) error

StrLenTrap returns the length of a string.

Input:

The string to measure in register 0.

Output:

Sets register 0 with the length

func TrapNOP

func TrapNOP(c *CPU, num int) error

TrapNOP is the default trap-function for any trap IDs that haven't explicitly been setup.

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 NewCPU

func NewCPU() *CPU

NewCPU returns a new CPU object.

func (*CPU) LoadBytes

func (c *CPU) LoadBytes(data []byte)

LoadBytes populates the given program into RAM. NOTE: The CPU-state is reset prior to the load.

func (*CPU) LoadFile

func (c *CPU) LoadFile(path string) error

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

func (c *CPU) Run() error

Run launches our intepreter. It does not terminate until an `EXIT` instruction is hit.

func (*CPU) SetContext

func (c *CPU) SetContext(ctx context.Context)

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 NewRegister

func NewRegister() *Register

NewRegister is the constructor for a register.

func (*Register) GetInt

func (r *Register) GetInt() (int, error)

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

func (r *Register) GetString() (string, error)

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

func (r *Register) SetInt(v int)

SetInt stores the given integer in the register. Note that a register may only contain integers in the range 0x0000-0xffff

func (*Register) SetString

func (r *Register) SetString(v string)

SetString stores the supplied string in the register.

func (*Register) Type

func (r *Register) Type() string

Type returns the type of a registers contents `int` vs. `string`.

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.

func NewStack

func NewStack() *Stack

NewStack creates a new stack object.

func (*Stack) Empty

func (s *Stack) Empty() bool

Empty returns true if the stack is empty.

func (*Stack) Pop

func (s *Stack) Pop() (int, error)

Pop removes a value from the stack.

func (*Stack) Push

func (s *Stack) Push(value int)

Push adds a value to the stack.

func (*Stack) Size

func (s *Stack) Size() int

Size retrieves the length of the stack.

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

type TrapFunction func(c *CPU, num int) error

TrapFunction is the signature for a function that is available as a trap.

Jump to

Keyboard shortcuts

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