SLControlUnit.sleela
lib/cpu/SLControlUnit.sleela · 212 line(s) · sleela
<< lib/cpu/SLControlUnit.sleela
SLeeLa Standard Library Definition.
Definition: Defines SLControlUnit — the conductor of a CPU core. It drives
the fetch-decode-execute cycle for the full SLInstructionSet: fetch the
instruction at the program counter, decode it, route operands through the
register file, ALU, and the hardware stack (SLStack), write results back,
update the program counter (sequential, branch, call, or return), and set
the status flags.
This models a classic single-cycle control unit with a real call/stack
protocol: CALL pushes the return address and jumps; RET pops it; PUSH/POP
move words to and from the stack. That protocol is what makes the ISA a
viable lowering target for compiled C/C++/Java/Sleela (function calls,
locals, spills). Pipelining/out-of-order are described in CPU.md; the
observable semantics here are the same.
Family: cpu. Composes decoder, register file, ALU, PC, status, RAM, and stack. >>
#sleela 2.5
material;
class SLControlUnit {
< ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
<< Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
or catalog can read and (where appropriate) override this record.
Values are faithful to the subject's real origin where publicly known. >>
String metaVersion = "1.0.0";
int metaId = 10210;
String metaAuthor = "Kiro Agent";
String metaDate = "2026-10-16";
String metaCopyright = "SLeeLa project";
String metaTrademark = "none";
String metaCorporation = "none";
< metaOrigin: National | <nation> | International | Galactic >
String metaOrigin = "International";
String metaBirthDate = "2026-10-06";
< metaScienceRating: maturity/robustness on a 1..10 scale >
int metaScienceRating = 9;
< metaNationalSchedule: a real, citable instrument or "none" >
String metaNationalSchedule = "none";
String getMetaVersion() { return metaVersion; }
void setMetaVersion(String v) { metaVersion = v; }
int getMetaId() { return metaId; }
void setMetaId(int v) { metaId = v; }
String getAuthor() { return metaAuthor; }
void setAuthor(String v) { metaAuthor = v; }
String getDate() { return metaDate; }
void setDate(String v) { metaDate = v; }
String getCopyright() { return metaCopyright; }
void setCopyright(String v) { metaCopyright = v; }
String getTrademark() { return metaTrademark; }
void setTrademark(String v) { metaTrademark = v; }
String getCorporation() { return metaCorporation; }
void setCorporation(String v) { metaCorporation = v; }
String getOrigin() { return metaOrigin; }
void setOrigin(String v) { metaOrigin = v; }
String getBirthDate() { return metaBirthDate; }
void setBirthDate(String v) { metaBirthDate = v; }
int getScienceRating() { return metaScienceRating; }
void setScienceRating(int v) { metaScienceRating = v; }
String getNationalSchedule() { return metaNationalSchedule; }
void setNationalSchedule(String v) { metaNationalSchedule = v; }
SLInstructionSet isa;
SLInstructionDecoder decoder;
SLRegisterFile regs;
SLALU alu;
SLProgramCounter pc;
SLStatusRegister status;
SLRAM memory;
SLStack stack;
int halted;
int executed;
< address the OS installed for SYSCALL >
int syscallVector;
< address installed for TRAP (faults / guest exits) >
int trapVector;
< last syscall number requested (for the kernel to read) >
int lastSyscall;
< last trap number requested >
int lastTrap;
void configure(SLInstructionSet instructionSet, SLInstructionDecoder dec,
SLRegisterFile registers, SLALU unit, SLProgramCounter counter,
SLStatusRegister stat, SLRAM ram, SLStack hwStack) {
isa = instructionSet; decoder = dec; regs = registers; alu = unit;
pc = counter; status = stat; memory = ram; stack = hwStack;
halted = 0; executed = 0; syscallVector = 0; trapVector = 0;
lastSyscall = -1; lastTrap = -1;
}
void installSyscallVector(int addr) { syscallVector = addr; }
void installTrapVector(int addr) { trapVector = addr; }
boolean isHalted() { return halted == 1; }
int instructionsRetired() { return executed; }
int pendingSyscall() { return lastSyscall; }
int pendingTrap() { return lastTrap; }
void clearSyscall() { lastSyscall = -1; }
void clearTrap() { lastTrap = -1; }
< Execute exactly one instruction at the current PC. Returns the opcode run, >
< or -1 if the core is halted. >
int step() {
if (halted == 1) { return -1; }
int word = memory.readWord(pc.value());
SLInstruction instr = decoder.decode(word);
int op = instr.op();
int rd = instr.dest();
int rs = instr.src();
int imm = instr.immediate();
executed = executed + 1;
if (op == isa.HALT) { halted = 1; return op; }
if (op == isa.NOP) { pc.increment(); return op; }
< ---- data movement ---- >
if (op == isa.MOV) { regs.write(rd, imm); pc.increment(); return op; }
if (op == isa.MOVR) { regs.write(rd, regs.read(rs)); pc.increment(); return op; }
if (op == isa.LOAD) { regs.write(rd, memory.readWord(imm)); pc.increment(); return op; }
if (op == isa.LOADR) { regs.write(rd, memory.readWord(regs.read(rs))); pc.increment(); return op; }
if (op == isa.LOADX) { regs.write(rd, memory.readWord(regs.read(rs) + imm)); pc.increment(); return op; }
if (op == isa.STORE) { memory.writeWord(imm, regs.read(rs)); pc.increment(); return op; }
if (op == isa.STORER) { memory.writeWord(regs.read(rd), regs.read(rs)); pc.increment(); return op; }
if (op == isa.STOREX) { memory.writeWord(regs.read(rd) + imm, regs.read(rs)); pc.increment(); return op; }
< ---- arithmetic / logic (register-register, result in rd) ---- >
if (isa.isArithmetic(op)) {
int a = regs.read(rd);
int b = regs.read(rs);
int r = alu.execute(a, b, toAluOp(op));
status.setFromALU(alu);
regs.write(rd, r);
pc.increment();
return op;
}
< ---- compare (sets flags only) ---- >
if (op == isa.CMP) {
alu.execute(regs.read(rd), regs.read(rs), alu.OP_SUB);
status.setFromALU(alu);
pc.increment();
return op;
}
< ---- branches ---- >
if (op == isa.JMP) { pc.branchTo(imm); return op; }
if (op == isa.JZ) { if (status.zeroFlag() == 1) { pc.branchTo(imm); } else { pc.increment(); } return op; }
if (op == isa.JNZ) { if (status.zeroFlag() == 0) { pc.branchTo(imm); } else { pc.increment(); } return op; }
if (op == isa.JLT) { if (status.negativeFlag() == 1) { pc.branchTo(imm); } else { pc.increment(); } return op; }
if (op == isa.JGE) { if (status.negativeFlag() == 0) { pc.branchTo(imm); } else { pc.increment(); } return op; }
< ---- call / return / stack ---- >
if (op == isa.CALL) {
< return address = instruction after CALL >
stack.push(pc.value() + 1);
pc.branchTo(imm);
return op;
}
if (op == isa.RET) {
int ret = stack.pop();
< stack underflow fault >
if (ret < 0) { raiseTrap(1); return op; }
pc.branchTo(ret);
return op;
}
if (op == isa.PUSH) { stack.push(regs.read(rs)); pc.increment(); return op; }
if (op == isa.POP) { regs.write(rd, stack.pop()); pc.increment(); return op; }
< ---- system ---- >
if (op == isa.SYSCALL) {
lastSyscall = imm;
status.enterSupervisorMode();
if (syscallVector != 0) { stack.push(pc.value() + 1); pc.branchTo(syscallVector); } else { pc.increment(); }
return op;
}
if (op == isa.TRAP) { raiseTrap(imm); return op; }
< Unknown opcode: fault. >
raiseTrap(2);
return op;
}
< Deliver a trap: record it and vector to the handler (or just advance). >
void raiseTrap(int number) {
lastTrap = number;
status.enterSupervisorMode();
if (trapVector != 0) { stack.push(pc.value() + 1); pc.branchTo(trapVector); } else { pc.increment(); }
}
< Map ISA arithmetic opcodes onto SLALU operation selectors. >
int toAluOp(int op) {
if (op == isa.ADD) { return alu.OP_ADD; }
if (op == isa.SUB) { return alu.OP_SUB; }
if (op == isa.MUL) { return alu.OP_MUL; }
if (op == isa.DIV) { return alu.OP_DIV; }
if (op == isa.MOD) { return alu.OP_MOD; }
if (op == isa.NEG) { return alu.OP_NEG; }
if (op == isa.AND) { return alu.OP_AND; }
if (op == isa.OR) { return alu.OP_OR; }
if (op == isa.XOR) { return alu.OP_XOR; }
if (op == isa.NOT) { return alu.OP_NOT; }
if (op == isa.SHL) { return alu.OP_SHL; }
if (op == isa.SHR) { return alu.OP_SHR; }
return alu.OP_PASS;
}
< Run until HALT or until the instruction budget is exhausted (safety bound). >
int run(int budget) {
int ran = 0;
while (halted == 0 && ran < budget) { step(); ran = ran + 1; }
return ran;
}
}