cpu

package
v0.0.0-...-68e8ff9 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 17 Imported by: 0

README

μCAPP CPU

Assembly Language

  • [ASSEMBLY.md](μCAPP Assembler Handbook).

CPU Opcodes

  • [OPCODES.md](μCAPP CPU Opcode Layout)

Reset

Initial CPU state

The reset state of the machine is:

  • All CAPP values are randomized.
  • CPU registers are preloaded with the following instructions:
    • r0: .list.of.immz.immz ; Select all of the CAPP
    • r1: .list.all.immz.immz ; Tag all items
    • r2: .list.write.immnz.immnz ; Replace all values with 0xFFFFFFFF
    • r3: .io.fetch.rom.immnz ; Load boot ROM into CAPP
    • r4: .list.not.immz.immz ; Now, only the program is tagged
    • r5: .alu.set.ip.immz ; Set IP to 0x00000000 (exec from CAPP)
  • CPU IP is set to 0x8000000 (execute-from-registers)
Bootstrap
  • CPU executes code in registers
  • CPU's IP is set to 0x00000000 by code in r5
  • OS boot code is as follows:
    • Select Drum 0, Ring 255 from the Depot
    • Read into CAPP as a program in IO arena.
    • Select boot program in CAPP
    • Write trampoline into registers:
      • r0: .list.write.immnz.immnz ; Free boot program
      • r1: .imm_hi32 0x8000 ; Arena ID for program
      • r2: .imm_hi32 0xc000 ; Arena mask
      • r3: .list.of.immz.imm32 ; Select IO program in IO area
      • r4: .list.write.immnz.imm32 ; Write program ID to list
      • r5: .alu.set.ip.immz.immnz ; Set IP to 0x00000000 (exec from CAPP)
    • CPU IP is set to 0x8000000 (execute-from-registers)
    • Control is transferred to the program from Drum 0, Ring 255.

Documentation

Overview

Package cpu implements the microprocessor and assembler for the μCAPP system.

The CPU consists of an instruction pointer (IP) with three execution modes, six 32-bit general-purpose registers (r0-r5), an ALU, a stack, and conditional execution flags. The processor coordinates with the Content Addressable Parallel Processor (CAPP) through Match/Mask registers.

The assembler provides a custom assembly language for the μCAPP instruction set, supporting macros, labels, equates, and compile-time expression evaluation.

Index

Constants

View Source
const (
	ARENA_MASK = 0xc_000_0000 // Mask of the arena CAPP data bits.
	ARENA_IO   = 0x0_000_0000 // Input/Output.
	ARENA_DATA = 0x4_000_0000 // User data.
	ARENA_CODE = 0x8_000_0000 // User code.
	ARENA_OS   = 0xc_000_0000 // OS area

	CAPP_FREE = 0xf_fff_ffff // Unallocated memory
)

Memory arena bit patterns for the CAPP memory space. These constants define the upper bits used to partition CAPP memory into logical regions for different purposes.

View Source
const (
	IP_MODE_CAPP  = uint32(0b00 << 30) // Execute from CAPP
	IP_MODE_STACK = uint32(0b01 << 30) // Execute from stack
	IP_MODE_REG   = uint32(0b10 << 30) // Execute from register bank
	IP_MODE_MASK  = uint32(0b11 << 30) // Mask of execute modes.
)

Instruction Pointer execution mode constants. The upper 2 bits of the IP determine the source for instruction fetch.

View Source
const (
	COND_ALWAYS = CodeCond(0) // .
	COND_TRUE   = CodeCond(1) // +
	COND_FALSE  = CodeCond(2) // -
	COND_NEVER  = CodeCond(3) // ~
)
View Source
const (
	OP_ALU  = CodeClass(0) // alu
	OP_COND = CodeClass(1) // if
	OP_CAPP = CodeClass(2) // list
	OP_IO   = CodeClass(3) // io
)
View Source
const (
	ALU_OP_SET = CodeAluOp(0) // set
	ALU_OP_XOR = CodeAluOp(1) // xor
	ALU_OP_AND = CodeAluOp(2) // and
	ALU_OP_OR  = CodeAluOp(3) // or
	ALU_OP_SHL = CodeAluOp(4) // shl
	ALU_OP_SHR = CodeAluOp(5) // shr
	ALU_OP_ADD = CodeAluOp(6) // add
	ALU_OP_SUB = CodeAluOp(7) // sub
)
View Source
const (
	COND_OP_EQ = CodeCondOp(0) // eq
	COND_OP_LT = CodeCondOp(1) // lt
	COND_OP_LE = CodeCondOp(2) // le
	COND_OP_NE = CodeCondOp(4) // ne
	COND_OP_GE = CodeCondOp(5) // ge
	COND_OP_GT = CodeCondOp(6) // gt
)
View Source
const (
	CAPP_OP_SET_SWAP    = CodeCappOp(0) // swap
	CAPP_OP_LIST_ALL    = CodeCappOp(1) // all
	CAPP_OP_LIST_NOT    = CodeCappOp(2) // not
	CAPP_OP_LIST_NEXT   = CodeCappOp(3) // next
	CAPP_OP_LIST_ONLY   = CodeCappOp(4) // only
	CAPP_OP_SET_OF      = CodeCappOp(5) // of
	CAPP_OP_WRITE_FIRST = CodeCappOp(6) // wfirst
	CAPP_OP_WRITE_LIST  = CodeCappOp(7) // wlist
)
View Source
const (
	IO_OP_FETCH = CodeIoOp(0) // fetch
	IO_OP_STORE = CodeIoOp(1) // store
	IO_OP_AWAIT = CodeIoOp(2) // await
	IO_OP_ALERT = CodeIoOp(3) // alert
)
View Source
const (
	IR_REG_R0         = CodeIR(0)  // r0
	IR_REG_R1         = CodeIR(1)  // r1
	IR_REG_R2         = CodeIR(2)  // r2
	IR_REG_R3         = CodeIR(3)  // r3
	IR_REG_R4         = CodeIR(4)  // r4
	IR_REG_R5         = CodeIR(5)  // r5
	IR_IP             = CodeIR(6)  // ip
	IR_STACK          = CodeIR(7)  // stack
	IR_REG_MATCH      = CodeIR(8)  // match
	IR_REG_MASK       = CodeIR(9)  // mask
	IR_REG_FIRST      = CodeIR(10) // first
	IR_REG_COUNT      = CodeIR(11) // count
	IR_CONST_0        = CodeIR(12) // immz
	IR_CONST_FFFFFFFF = CodeIR(13) // immnz
	IR_IMMEDIATE_16   = CodeIR(14) // imm16
	IR_IMMEDIATE_32   = CodeIR(15) // imm32
)
View Source
const (
	CHANNEL_ID_TEMP    = CodeChannel(0) // temp
	CHANNEL_ID_DEPOT   = CodeChannel(1) // depot
	CHANNEL_ID_TAPE    = CodeChannel(2) // tape
	CHANNEL_ID_VT      = CodeChannel(3) // vt
	CHANNEL_ID_MONITOR = CodeChannel(7) // monitor
)
View Source
const (
	STACK_LIMIT = 16
)

STACK_LIMIT is the maximum stack depth.

Variables

View Source
var (
	// Cpu errors
	ErrIpEmpty        = errors.New(f("ip empty"))
	ErrIpMultiple     = errors.New(f("ip multiple"))
	ErrIpTrap         = errors.New(f("ip trap"))
	ErrIpKey          = errors.New(f("ip key unknown"))
	ErrStackEmpty     = errors.New(f("stack empty"))
	ErrStackFull      = errors.New(f("stack full"))
	ErrChannelInvalid = errors.New(f("channel invalid"))
	ErrChannelPartial = errors.New(f("partial channel read"))
	ErrChannelFull    = errors.New(f("channel full"))

	// Instruction decode errors
	ErrOpcodeDecode = errors.New(f("decode"))
	ErrOpcodeAlu    = errors.New(f("alu"))
	ErrOpcodeCond   = errors.New(f("cond"))
	ErrOpcodeCapp   = errors.New(f("capp"))
	ErrOpcodeIo     = errors.New(f("io"))
	ErrOpcodeOp     = errors.New(f("op"))
	ErrOpcodeArg1   = errors.New(f("arg1"))
	ErrOpcodeArg2   = errors.New(f("arg2"))
	ErrOpcodeImm    = errors.New(f("imm"))

	// Assembler errors
	ErrDwSyntax           = errors.New(f(".dw syntax"))
	ErrDwInvalid          = errors.New(f(".dw invalid value"))
	ErrEquateSyntax       = errors.New(f(".equ syntax"))
	ErrEquateDuplicate    = errors.New(f(".equ duplicated"))
	ErrLabelDuplicate     = errors.New(f("label duplicated"))
	ErrMacroSyntax        = errors.New(f(".macro syntax"))
	ErrMacroNesting       = errors.New(f(".macro in .macro prohibited"))
	ErrMacroDuplicate     = errors.New(f(".macro duplicated"))
	ErrMacroLonely        = errors.New(f(".macro wihtout .endm"))
	ErrMacroLonelyEndm    = errors.New(f(".endm without .macro"))
	ErrOpcodeExtraArgs    = errors.New(f("excessive arguments"))
	ErrOpcodeMissing      = errors.New(f("opcode missing"))
	ErrOpcodeValueMissing = errors.New(f("value missing"))
	ErrOpcodeInvalid      = errors.New(f("opcode invalid"))
	ErrRegisterInvalid    = errors.New(f("register invalid"))
	ErrTargetMissing      = errors.New(f("target missing"))
	ErrTargetInvalid      = errors.New(f("target invalid"))
	ErrInstructionInvalid = errors.New(f("instruction invalid"))
)

Functions

This section is empty.

Types

type Assembler

type Assembler struct {
	Verbose bool     // If set, verbosely logs the assembler actions.
	Opcode  []Opcode // List of generated opcodes.
	Data    []uint32 // Data words to append.

	Label  map[string]int      // Map of jump labels to opcode indexes.
	Equate map[string]string   // Map of equates.
	Macro  map[string](*Macro) // Map of macros.
	// contains filtered or unexported fields
}

Assembler is a single pass macro assembler for the μCAPP system.

func (*Assembler) Parse

func (asm *Assembler) Parse(input io.Reader) (prog *Program, err error)

Parse parses an input stream into a Program containing opcodes.

func (*Assembler) Predefine

func (asm *Assembler) Predefine(equ string, value string)

Define defines a new equate or redefines an existing equate.

type Channel

type Channel sio.Channel

Channel is an I/O channel interface.

type Code

type Code struct {
	Word       uint16
	Immediates []uint16
}

Code represents a single instruction word with optional immediate values.

func MakeCodeAlu

func MakeCodeAlu(cond CodeCond, op CodeAluOp, target, arg CodeIR, imms ...uint16) Code

MakeCodeAlu creates an ALU operation instruction.

func MakeCodeCapp

func MakeCodeCapp(cond CodeCond, op CodeCappOp, src_v, src_m CodeIR, imms ...uint16) Code

MakeCodeCapp creates a CAPP operation instruction.

func MakeCodeCond

func MakeCodeCond(cond CodeCond, op CodeCondOp, arg_a, arg_b CodeIR, imms ...uint16) Code

MakeCodeCond creates a conditional comparison instruction.

func MakeCodeExit

func MakeCodeExit(cond CodeCond) Code

MakeCodeExit creates an end-of-program instruction that sets IP to 0xffffffff.

func MakeCodeIo

func MakeCodeIo(cond CodeCond, op CodeIoOp, channel CodeChannel, arg CodeIR, imms ...uint16) Code

MakeCodeIo creates an I/O operation instruction.

func (Code) AluDecode

func (code Code) AluDecode() (op CodeAluOp, target, arg CodeIR)

AluDecode decodes and returns the ALU operation, target register, and argument.

func (Code) CappDecode

func (code Code) CappDecode() (op CodeCappOp, match, mask CodeIR)

CappDecode decodes and returns the CAPP operation, match value, and mask.

func (Code) Class

func (code Code) Class() CodeClass

Class returns the operation class (ALU, COND, CAPP, or IO) from the instruction word.

func (Code) Cond

func (code Code) Cond() CodeCond

Cond returns the condition code from the instruction word.

func (Code) CondDecode

func (code Code) CondDecode() (op CodeCondOp, arg1, arg2 CodeIR)

CondDecode decodes and returns the conditional operation and its two arguments.

func (Code) ImmediateNeed

func (code Code) ImmediateNeed() int

ImmediateNeed returns the number of 16-bit immediate values required by this instruction.

func (Code) IoDecode

func (code Code) IoDecode() (op CodeIoOp, channel CodeChannel, arg CodeIR)

IoDecode decodes and returns the I/O operation, channel, and argument.

func (Code) String

func (code Code) String() (out string)

String returns the assembly language representation of this instruction.

type CodeAluOp

type CodeAluOp int

CodeAluOp is an ALU operation type.

type CodeCappOp

type CodeCappOp int

CodeCappOp is a CAPP operation type.

type CodeChannel

type CodeChannel int

CodeChannel is an IO channel index type.

type CodeClass

type CodeClass int

CodeClass is the type of opcode class.

type CodeCond

type CodeCond int

CodeCond is a condition code.

type CodeCondOp

type CodeCondOp int

CodeCondOp is a conditional operation type.

type CodeIR

type CodeIR int

CodeIR is an Immediate-or-Register decode type.

func (CodeIR) Writable

func (ir CodeIR) Writable() bool

Writable returns true if the CodeIR represents a writable destination.

type CodeIoOp

type CodeIoOp int

CodeIoOp is an IO operation type.

type Cpu

type Cpu struct {
	Verbose bool // Set to enable verbose logging.

	Capp *capp.Capp // Reference to the CAPP simulation.

	Ip       uint32    // Current instruction pointer.
	Register [6]uint32 // Register bank.
	Stack    Stack     // Stack simulation.
	Match    uint32    // Match value sent to the CAPP.
	Mask     uint32    // Mask value sent to the CAPP.
	Cond     bool      // Current conditional execution state.

	Power int // Power (bits flipped) counter.
	Ticks int // CPU ticks counter.
	// contains filtered or unexported fields
}

Cpu is the simulation context for the control CPU attached to the CAPP

func NewCpu

func NewCpu(count uint) (cpu *Cpu)

NewCpu creates a new CPU with a specifically sized CAPP.

func (*Cpu) Close

func (cpu *Cpu) Close() (err error)

Close closes all I/O channels associated with the CPU.

func (*Cpu) Defines

func (cpu *Cpu) Defines() iter.Seq2[string, string]

Defines for the cpu

func (*Cpu) Execute

func (cpu *Cpu) Execute(code Code) (err error)

Execute executes a single decoded instruction.

func (*Cpu) FetchCode

func (cpu *Cpu) FetchCode() (code Code, err error)

FetchCode fetches the next instruction to execute based on the IP mode and address.

func (*Cpu) GetChannel

func (cpu *Cpu) GetChannel(ch CodeChannel) (channel Channel, response chan uint32, err error)

GetChannel gets the channel simulation model by index.

func (*Cpu) Reset

func (cpu *Cpu) Reset(boot CodeChannel) (err error)

Reset the CPU state. - Clears the registers, stack, and CAPP. - Zeros statistics counters. - Resets all IO channels. - Installs trampoline to boot from CAPP in register bank. - Sets CPU execution mode to boot-from-register bank.

func (*Cpu) SetChannel

func (cpu *Cpu) SetChannel(index CodeChannel, channel Channel)

SetChannel sets a channel index to a channel simulation model.

func (*Cpu) String

func (cpu *Cpu) String() (text string)

String returns the current CPU state as a string.

func (*Cpu) Tick

func (cpu *Cpu) Tick() (err error)

Tick executes a single CPU instruction cycle.

type CpuChannel

type CpuChannel struct {
	Channel  Channel
	Response chan uint32
}

CpuChannel represents an I/O channel attached to the CPU with its response channel.

type Debug

type Debug struct {
	*Opcode     // Opcode for the index.
	Index   int // Index into the Program's Opcodes array.
}

Debug contains debugging information for a program instruction pointer.

type ErrLabelMissing

type ErrLabelMissing string

ErrLabelMissing indicates a missing jump label.

func (ErrLabelMissing) Error

func (el ErrLabelMissing) Error() string

type ErrMacro

type ErrMacro struct {
	Macro string
	Line  int
	Err   error
}

ErrMacro locates a macro error in the assembly listing.

func (ErrMacro) Error

func (err ErrMacro) Error() string

func (ErrMacro) Unwrap

func (err ErrMacro) Unwrap() error

type ErrOpcode

type ErrOpcode Code

ErrOpcode indicates an invalid opcode.

func (ErrOpcode) Error

func (eo ErrOpcode) Error() string

func (ErrOpcode) Is

func (eo ErrOpcode) Is(err error) (ok bool)

type ErrParseCharacter

type ErrParseCharacter string

ErrParseCharacter indicates a character ('x') parsing failure.

func (ErrParseCharacter) Error

func (err ErrParseCharacter) Error() string

type ErrParseExpression

type ErrParseExpression string

ErrParseExpression indicates a $(..) expression parsing failure.

func (ErrParseExpression) Error

func (err ErrParseExpression) Error() string

type ErrParseNumber

type ErrParseNumber string

ErrParseNumber indicates a numeric parsing failure.

func (ErrParseNumber) Error

func (err ErrParseNumber) Error() string

type ErrSyntax

type ErrSyntax struct {
	LineNo int
	Line   string
	Err    error
}

ErrSyntax locates an error in the assembly listing.

func (ErrSyntax) Error

func (err ErrSyntax) Error() string

func (ErrSyntax) Unwrap

func (err ErrSyntax) Unwrap() error

type Macro

type Macro struct {
	LineNo int      // Line number of the macro definition.
	Args   []string // Arguments for the macro.
	Lines  []string // Lines of macro text to expand.
}

Macro represents a macro definition in the assembly language.

type Opcode

type Opcode struct {
	LineNo    int
	Ip        int
	Words     []string
	Codes     []Code
	LinkLabel string
}

Opcode represents a line of assembled code with its source location and generated instructions.

type Program

type Program struct {
	Opcodes []Opcode // Opcodes and metadata
	Data    []uint32 // Data section
}

Program is a list of opcodes.

func (*Program) Binary

func (prog *Program) Binary() (bins []uint32)

Binary returns the program as a list of 32-bit CAPP memory words.

func (*Program) Codes

func (prog *Program) Codes() iter.Seq2[uint16, Code]

Codes returns an iterator over all instruction pointer addresses and their codes.

func (*Program) Debug

func (prog *Program) Debug(ip uint16) (dbg Debug)

Debug returns debugging information for the instruction at the given IP address.

type Stack

type Stack struct {
	Data []uint32
}

Stack represents the CPU's call/data stack.

func (*Stack) Empty

func (s *Stack) Empty() bool

Empty returns true if the stack is empty.

func (*Stack) Full

func (s *Stack) Full() bool

Full returns true if the stack is full.

func (*Stack) Peek

func (s *Stack) Peek() (value uint32, ok bool)

Peek returns the top value from the stack without removing it. Returns ok=false if the stack is empty.

func (*Stack) Pop

func (s *Stack) Pop() (value uint32, ok bool)

Pop removes and returns the top value from the stack.

func (*Stack) Push

func (s *Stack) Push(value uint32)

Push pushes a value onto the stack.

func (*Stack) Reset

func (s *Stack) Reset()

Reset clears all values from the stack.

Jump to

Keyboard shortcuts

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