uxn

package
v0.0.0-...-7404ff5 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package uxn provides an implementation of a Uxn CPU, called Machine, that can be used to execute Uxn bytecode.

Index

Constants

This section is empty.

Variables

View Source
var ErrBRK = errors.New("BRK")

Functions

This section is empty.

Types

type Device

type Device interface {
	In(port byte) (value byte)
	InShort(port byte) (value uint16)
	Out(port, value byte)
	OutShort(port byte, value uint16)
}

Device provides access to external systems connected to the Uxn CPU.

type HaltCode

type HaltCode byte

HaltCode signifies the type of condition that halted execution.

const (
	Halt         HaltCode = 0x00
	Underflow    HaltCode = 0x01
	Overflow     HaltCode = 0x02
	DivideByZero HaltCode = 0x03

	// Debug is treated differently by Exec, which will still return a
	// HaltError but the stacks are left unchanged and the program may
	// cotinue to execute normally.
	Debug HaltCode = 0xff
)

func (HaltCode) String

func (c HaltCode) String() string

type HaltError

type HaltError struct {
	HaltCode
	Op   Op
	Addr uint16
}

HaltError is returned by Exec if execution is halted by the program for some reason.

func (HaltError) Error

func (e HaltError) Error() string

type Machine

type Machine struct {
	Mem  [0x100000]byte
	PC   uint16
	Work Stack
	Ret  Stack
	Dev  Device
}

Machine is an implementation of a Uxn CPU.

func NewMachine

func NewMachine(rom []byte) *Machine

NewMachine returns a Uxn CPU loaded with the given rom at 0x100. If the rom is larger than 0xfeff bytes then Mem is grown to accommodate it, even though the Uxn CPU cannot directly address the extra memory.

func (*Machine) Exec

func (m *Machine) Exec() (err error)

Exec executes the intsruction at m.PC. It returns ErrBRK if that instruction is BRK, and otherwise only returns a non-nil error if it encounters a halt condition.

func (*Machine) OpAddr

func (m *Machine) OpAddr(addr uint16) (uint16, bool)

OpAddr returns the address associated with the operation at addr, either from memory or the stack, and reports whether the operation has an associated address (for example, JMP does while POP does not), and whether there are enough bytes on the stack to provide an address (ie, it returns false if executing the instruction would trigger a stack underflow).

type Op

type Op byte

Op represents a Uxn opcode.

const (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CN2k
	JSR2k
	STH2k
	LDZ2k
	STZ2k
	LDR2k
	STR2k
	LDA2k
	STA2k
	DEI2k
	DEO2k
	ADD2k
	SUB2k
	MUL2k
	DIV2k
	AND2k
	ORA2k
	EOR2k
	SFT2k
	LITr
	INCkr
	POPkr
	NIPkr
	SWPkr
	ROTkr
	DUPkr
	OVRkr
	EQUkr
	NEQkr
	GTHkr
	LTHkr
	JMPkr
	JCNkr
	JSRkr
	STHkr
	LDZkr
	STZkr
	LDRkr
	STRkr
	LDAkr
	STAkr
	DEIkr
	DEOkr
	ADDkr
	SUBkr
	MULkr
	DIVkr
	ANDkr
	ORAkr
	EORkr
	SFTkr
	LIT2r
	INC2kr
	POP2kr
	NIP2kr
	SWP2kr
	ROT2kr
	DUP2kr
	OVR2kr
	EQU2kr
	NEQ2kr
	GTH2kr
	LTH2kr
	JMP2kr
	JCN2kr
	JSR2kr
	STH2kr
	LDZ2kr
	STZ2kr
	LDR2kr
	STR2kr
	LDA2kr
	STA2kr
	DEI2kr
	DEO2kr
	ADD2kr
	SUB2kr
	MUL2kr
	DIV2kr
	AND2kr
	ORA2kr
	EOR2kr
	SFT2kr
)

func (Op) Base

func (b Op) Base() Op

Base returns the opcode without any flags set.

func (Op) Keep

func (b Op) Keep() bool

Keep reports whether the opcode has the keep flag set.

func (Op) Return

func (b Op) Return() bool

Return reports whether the opcode has the return flag set.

func (Op) Short

func (b Op) Short() bool

Short reports whether the opcode has the short flag set.

func (Op) StackArgs

func (op Op) StackArgs() (a, b, c StackVal)

StackArgs reports the stack arguments consumed by Op. If any of the args are zero then they not consumed.

func (Op) String

func (o Op) String() string

type Stack

type Stack struct {
	Bytes [255]byte
	Ptr   byte
}

Stack implements a Uxn CPU stack.

func (*Stack) Peek

func (s *Stack) Peek() (byte, bool)

func (*Stack) PeekOffset

func (s *Stack) PeekOffset() (uint16, bool)

func (*Stack) PeekShort

func (s *Stack) PeekShort() (uint16, bool)

func (Stack) String

func (s Stack) String() string

type StackVal

type StackVal struct {
	Index int // from top of stack (1 is first, 0 is none)
	Size  int // 1 for byte, 2 for short
}

StackVal holds the position and width of a value on the stack.

Jump to

Keyboard shortcuts

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