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
- Variables
- type Assembler
- type Channel
- type Code
- func MakeCodeAlu(cond CodeCond, op CodeAluOp, target, arg CodeIR, imms ...uint16) Code
- func MakeCodeCapp(cond CodeCond, op CodeCappOp, src_v, src_m CodeIR, imms ...uint16) Code
- func MakeCodeCond(cond CodeCond, op CodeCondOp, arg_a, arg_b CodeIR, imms ...uint16) Code
- func MakeCodeExit(cond CodeCond) Code
- func MakeCodeIo(cond CodeCond, op CodeIoOp, channel CodeChannel, arg CodeIR, imms ...uint16) Code
- func (code Code) AluDecode() (op CodeAluOp, target, arg CodeIR)
- func (code Code) CappDecode() (op CodeCappOp, match, mask CodeIR)
- func (code Code) Class() CodeClass
- func (code Code) Cond() CodeCond
- func (code Code) CondDecode() (op CodeCondOp, arg1, arg2 CodeIR)
- func (code Code) ImmediateNeed() int
- func (code Code) IoDecode() (op CodeIoOp, channel CodeChannel, arg CodeIR)
- func (code Code) String() (out string)
- type CodeAluOp
- type CodeCappOp
- type CodeChannel
- type CodeClass
- type CodeCond
- type CodeCondOp
- type CodeIR
- type CodeIoOp
- type Cpu
- func (cpu *Cpu) Close() (err error)
- func (cpu *Cpu) Defines() iter.Seq2[string, string]
- func (cpu *Cpu) Execute(code Code) (err error)
- func (cpu *Cpu) FetchCode() (code Code, err error)
- func (cpu *Cpu) GetChannel(ch CodeChannel) (channel Channel, response chan uint32, err error)
- func (cpu *Cpu) Reset(boot CodeChannel) (err error)
- func (cpu *Cpu) SetChannel(index CodeChannel, channel Channel)
- func (cpu *Cpu) String() (text string)
- func (cpu *Cpu) Tick() (err error)
- type CpuChannel
- type Debug
- type ErrLabelMissing
- type ErrMacro
- type ErrOpcode
- type ErrParseCharacter
- type ErrParseExpression
- type ErrParseNumber
- type ErrSyntax
- type Macro
- type Opcode
- type Program
- type Stack
Constants ¶
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.
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.
const ( COND_ALWAYS = CodeCond(0) // . COND_TRUE = CodeCond(1) // + COND_FALSE = CodeCond(2) // - COND_NEVER = CodeCond(3) // ~ )
const ( OP_ALU = CodeClass(0) // alu OP_COND = CodeClass(1) // if OP_CAPP = CodeClass(2) // list OP_IO = CodeClass(3) // io )
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 )
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 )
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 )
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 )
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 )
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 )
const (
STACK_LIMIT = 16
)
STACK_LIMIT is the maximum stack depth.
Variables ¶
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.
type Code ¶
Code represents a single instruction word with optional immediate values.
func MakeCodeAlu ¶
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 ¶
MakeCodeExit creates an end-of-program instruction that sets IP to 0xffffffff.
func MakeCodeIo ¶
MakeCodeIo creates an I/O operation instruction.
func (Code) AluDecode ¶
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 ¶
Class returns the operation class (ALU, COND, CAPP, or IO) 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 ¶
ImmediateNeed returns the number of 16-bit immediate values required by this instruction.
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 (*Cpu) FetchCode ¶
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.
type CpuChannel ¶
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 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 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 ¶
Opcode represents a line of assembled code with its source location and generated instructions.
type Program ¶
Program is a list of opcodes.