cpu

package
v0.0.0-...-be83087 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package cpu provides an emulator for the unnamed time travel control computer/wearable from AoC 2018.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Inst

type Inst struct {
	Op      Op
	A, B, C int
}

Inst represents a single instruction: an opcode, and the A, B and C operands.

func ParseInst

func ParseInst(s string) (i Inst, ok bool)

ParseInst converts the textual format of an instruction (e.g., "seti 5 0 1") to an instruction.

type Op

type Op int

Op represents one of the device's 16 opcodes.

const (
	// AddR (add register), rC = rA + rB.
	AddR Op = iota
	// AddI (add immediate), rC = rA + #B.
	AddI
	// MulR (multiply register), rC = rA * rB.
	MulR
	// MulI (multiply immediate), rC = rA * #B.
	MulI
	// BanR (bitwise AND register), rC = rA & rB.
	BanR
	// BanI (bitwise AND immediate), rC = rA & #B.
	BanI
	// BorR (bitwise OR register), rC = rA | rB.
	BorR
	// BorI (bitwise OR immediate), rC = rA | #B.
	BorI
	// SetR (set register), rC = rA, B ignored.
	SetR
	// SetI (set immediate), rC = #A, B ignored.
	SetI
	// GtIR (greater-than immediate/register), rC = #A > rB.
	GtIR
	// GtRI (greater-than register/immediate), rC = rA > #B.
	GtRI
	// GtRR (greater-than register/register), rC = rA > rB.
	GtRR
	// EqIR (equal immediate/register), rC = #A == rB.
	EqIR
	// EqRI (equal register/immediate), rC = rA == #B.
	EqRI
	// EqRR (equal register/register), rC = rA == rB.
	EqRR
	// FirstOp is the numerically first opcode; you can iterate from it to LastOp.
	FirstOp = AddR
	// LastOp is the numerically last opcode; you can iterate to it from FirstOp.
	LastOp = EqRR
)

func OpNamed

func OpNamed(s string) (op Op, ok bool)

OpNamed returns the opcode with the given mnemonic, or false as the second argument if the mnemonic is not valid.

func (Op) String

func (op Op) String() string

type Prog

type Prog struct {
	Code    []Inst
	IPBound bool
	IPR     int
}

Prog represents an entire program: a sequence of instructions, together with IP binding instructions.

func ParseProg

func ParseProg(lines []string) (p Prog, err error)

ParseProg reads the text of a program (one symbolic instruction per line, plus an optional IP binding header).

type State

type State struct {
	R       [6]int
	IP      int
	IPBound bool
	IPR     int
}

State holds the entire CPU state: 6 registers and the instruction pointer, as well as the current IP/register binding state.

func (*State) Run

func (s *State) Run(p Prog)

Run executes an entire program until it halts. The IP binding state is reset to be that of the program, but the IP (and other registers) are itself not reset to zero.

func (*State) Step

func (s *State) Step(op Op, a, b, c int)

Step executes one CPU cycle, given the operation to execute.

Jump to

Keyboard shortcuts

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