SLMachine.sleela
lib/cpu/SLMachine.sleela · 380 line(s) · sleela
<< lib/cpu/SLMachine.sleela
SLeeLa Standard Library Definition.
Definition: Defines SLMachine — the complete computer: it composes the entire
/lib/cpu stack into one bootable system and demonstrates the full chain the
library exists to express:
gates -> adders/ALU -> registers/RAM/cache -> cores -> SLCPU (hardware)
-> SLBootloader loads the kernel (boot)
-> SLOperatingSystem / SLKernel comes up with drivers + scheduler (OS)
-> SLAssembler builds a Sleela program (program)
-> SLProgramLoader loads it as a process (load)
-> SLKernel schedules and runs it on the Sleela CPU (run)
This is the Sleela CPU built from Sleela source, booting a Sleela
Class-Driven OS, which then loads and runs Sleela programs on top of it.
Family: cpu. Top-level composition of the whole package. >>
#sleela 2.5
material;
class SLMachine {
< ---- 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 = 10263;
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; }
SLRAM memory;
SLHardDrive drive;
SLCPU cpu;
SLBootloader bootloader;
SLOperatingSystem os;
SLProgramLoader loader;
SLInstructionSet isa;
< the machine's adjustable operating point (optional) >
SLTimingProfile timing;
int kernelEntry;
int syscallHandler;
< Build the hardware: main memory, boot disk, and a multi-core CPU. >
void buildHardware(String vendor, String model, int cores, int bits, int mhz) {
< 1M words >
memory = new SLRAM(); memory.configure(1048576, bits); memory.allocate();
drive = new SLHardDrive(); drive.configure("boot0", 65536, 4096, SLHardDrive.MEDIA_SSD);
drive.mount("boot0.img");
cpu = new SLCPU(); cpu.configure(vendor, model, cores, bits, mhz);
cpu.power(memory);
isa = new SLInstructionSet(); isa.configure();
}
< Boot: firmware loads the kernel image, then the OS installs and comes up. >
void boot() {
< kernel loads high above the vector table >
kernelEntry = 4096;
< kernel's syscall handler entry (modeled) >
syscallHandler = 4096;
bootloader = new SLBootloader();
bootloader.configure(cpu, buildBootDiskDriver(), memory);
bootloader.loadKernel(0, 32, kernelEntry);
os = new SLOperatingSystem();
os.configure("SleelaOS", cpu, memory);
os.install(drive, syscallHandler);
loader = new SLProgramLoader();
loader.configure(os, memory);
< enable multi-language source loading >
loader.attachCompiler(isa);
}
< Compile and load a source program in any supported language, returning the >
< spawned process. Language ids: 0 Sleela, 1 C, 2 C++, 3 Java. >
SLProcess loadLanguageProgram(String path, int langId, String procName, int loadAddr) {
return loader.loadSource(path, langId, procName, loadAddr);
}
< Reduce a program in any supported language to the canonical SLeeLa opcode >
< substrate and execute it there, returning its printed result. This runs the >
< program as a stream of the Turing-complete canonical opcodes — the common >
< ground C/C++/Java/Sleela all share. >
int runOnSubstrate(int langId, String path, String name) {
SLTuringBridge bridge = new SLTuringBridge(); bridge.configure();
return bridge.runLanguage(langId, path, name);
}
< Prove the four languages are 1:1 in Turing effect: identical observable >
< result after each is reduced to the Sleela opcode substrate. >
boolean proveLanguageEquivalence(String c, String cpp, String java, String sleela) {
SLTuringBridge bridge = new SLTuringBridge(); bridge.configure();
return bridge.proveEquivalence(c, cpp, java, sleela);
}
< Boot a Linux-style guest OS on top of this Sleela CPU via the hypervisor, >
< and run it for a bounded number of guest instructions. Returns the guest >
< instruction count executed. (Architectural model — see SLGuestLinux.) >
int runLinuxGuest(int guestEntry, int budget) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
< timer IRQs drive the guest scheduler >
vm.attachKernel(linux);
linux.boot(guestEntry, guestEntry + 256);
< program the guest timer heartbeat >
linux.startScheduling(8);
return hv.runGuest(0, budget);
}
< Boot a Linux-style guest whose kernel is GENUINELY COMPILED from source in >
< any supported language (C/C++/Java/Sleela) via the real toolchain (step 4), >
< then run it. Returns the guest instruction count executed. >
int runLinuxGuestFromSource(String kernelSourcePath, int langId, int guestEntry, int budget) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
< timer IRQs drive the scheduler >
vm.attachKernel(linux);
< compile the guest kernel >
linux.buildKernel(kernelSourcePath, langId);
linux.boot(guestEntry, guestEntry + 256);
linux.startScheduling(8);
return hv.runGuest(0, budget);
}
< Boot a guest, then have its init process (PID 1) load and spawn two user >
< programs compiled from source (any supported language). The timer-driven >
< scheduler then multitasks them. Returns the guest instruction count. >
int runLinuxGuestWithPrograms(String kernelSrc, int kernelLang,
String progA, String progASrc, int progALang,
String progB, String progBSrc, int progBLang,
int guestEntry, int budget) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
vm.attachKernel(linux);
linux.buildKernel(kernelSrc, kernelLang);
linux.boot(guestEntry, guestEntry + 256);
SLGuestInit init = linux.startInit();
< spawn user program A >
init.exec(progA, progASrc, progALang);
< spawn user program B >
init.exec(progB, progBSrc, progBLang);
linux.startScheduling(8);
return hv.runGuest(0, budget);
}
< Boot a guest, then compile a program, install it to the guest filesystem as >
< an on-disk executable, load it back FROM DISK, and run it. Demonstrates the >
< full compile -> install -> load-from-disk -> execute cycle. Returns the >
< guest instruction count executed. >
int runLinuxGuestDiskProgram(String kernelSrc, int kernelLang,
String progName, String progSrc, int progLang,
int fileId, int guestEntry, int budget) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
vm.attachKernel(linux);
linux.buildKernel(kernelSrc, kernelLang);
linux.boot(guestEntry, guestEntry + 256);
SLGuestInit init = linux.startInit();
< compile->disk->load->run >
init.installAndExec(progName, progSrc, progLang, fileId);
linux.startScheduling(8);
return hv.runGuest(0, budget);
}
< Boot a guest, launch its shell as init's first child, and run a short >
< command script (each entry is a leading-char + arg pair). Returns the >
< number of shell commands executed. Demonstrates boot -> init -> shell. >
int runLinuxGuestShell(String kernelSrc, int kernelLang, int guestEntry) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
vm.attachKernel(linux);
linux.buildKernel(kernelSrc, kernelLang);
linux.boot(guestEntry, guestEntry + 256);
SLGuestInit init = linux.startInit();
SLGuestShell sh = init.launchShell();
linux.startScheduling(8);
< A tiny boot script: help, ps, echo 'A', exit. >
< help >
sh.run(104, 0);
< ps >
sh.run(112, 0);
< echo 'A' >
sh.run(101, 65);
< exit >
sh.run(120, 0);
return sh.commandCount();
}
< Boot a guest, then exercise the full PROCESS-CREATION cycle — fork, execve, >
< and wait — through the guest's real syscall ABI (SLGuestSyscall). init >
< spawns a parent task, forks a child, execs the child onto a freshly >
< compiled image, the child exits, and the parent reaps it with wait(). >
< Returns the reaped child's tid (>0 on success). Demonstrates boot -> init >
< -> fork/exec/wait: a Linux-style guest creating and reaping processes. >
int runLinuxGuestForkExec(String kernelSrc, int kernelLang,
int childLang, int guestEntry) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
vm.attachKernel(linux);
linux.buildKernel(kernelSrc, kernelLang);
linux.boot(guestEntry, guestEntry + 256);
SLGuestInit init = linux.startInit();
linux.startScheduling(8);
< fork -> execve -> wait >
return init.forkExecWaitDemo(childLang);
}
< Same process-creation cycle, but driven through the guest's OPCODE path: >
< the canonical service opcodes OP_SPAWN (fork) and OP_JOINALL (wait) are >
< routed by SLGuestVM.serviceOpcode() -> syscallNumberFor() -> the syscall >
< ABI, exactly as a guest program's VM-exits are. This exercises the >
< opcode->syscall mapping (not just the syscall handlers). Returns the reaped >
< child's tid (>0 on success). >
int runLinuxGuestForkExecOpcodes(String kernelSrc, int kernelLang, int guestEntry) {
SLHypervisor hv = new SLHypervisor();
hv.configure(cpu, memory);
hv.attachBootDisk(drive);
SLGuestVM vm = hv.createGuest("linux-guest", 262144, 65536, guestEntry);
SLGuestLinux linux = new SLGuestLinux();
linux.configure("sleela-linux", vm);
vm.attachKernel(linux);
linux.buildKernel(kernelSrc, kernelLang);
linux.boot(guestEntry, guestEntry + 256);
SLGuestInit init = linux.startInit();
linux.startScheduling(8);
SLGuestScheduler sched = linux.taskScheduler();
SLSleelaOpcode op = new SLSleelaOpcode(); op.configure(0, "OP_NOP");
< A parent task must be the running task for fork()/wait() to have a caller. >
SLGuestProgram parentProg = new SLGuestProgram(); parentProg.configure("parent", 64);
< (modeled parent body) >
parentProg.append(op.OP_SPAWN, 0);
SLGuestTask parent = init.spawn(parentProg);
< parent becomes running >
sched.onTimerTick();
< OP_SPAWN arg 0 -> fork() >
int childTid = vm.serviceOpcode(op.OP_SPAWN, 0, 0);
if (childTid < 0) { return childTid; }
< Child exits via OP_HALT (-> SYS_EXIT) while it is the running task. >
sched.markRunningSlot(sched.slotOfTid(childTid));
< child exit(0) >
vm.serviceOpcode(op.OP_HALT, 0, 0);
< Parent reaps the child via OP_JOINALL (-> wait()). >
sched.markRunningSlot(sched.slotOfTid(parent.taskId()));
< OP_JOINALL -> wait() >
return vm.serviceOpcode(op.OP_JOINALL, 0, 0);
}
< The bootloader needs a disk driver before the OS driver manager exists; >
< build a minimal one bound to the boot drive. >
SLDiskDriver buildBootDiskDriver() {
SLIOBus bus = new SLIOBus(); bus.configure();
SLIOPort port = new SLIOPort(); port.configure(1, SLIOPort.DIR_INOUT); bus.registerPort(1, port);
SLDMAController dmaEngine = new SLDMAController(); dmaEngine.configure(memory);
SLDiskDriver d = new SLDiskDriver(); d.init(bus, 1, drive, dmaEngine);
return d;
}
< Author, assemble, load, and run a demonstration Sleela program: >
< compute 2 + 3 into r1, print a character via SYS_WRITE, then exit. >
SLProcess loadDemoProgram(int loadAddr) {
SLProgram prog = new SLProgram(); prog.configure("demo", 64);
SLAssembler asm = new SLAssembler(); asm.configure(isa, prog);
< r1 = 2 >
asm.mov(1, 2);
< r2 = 3 >
asm.mov(2, 3);
< r1 = r1 + r2 = 5 >
asm.add(1, 2);
< r2 = 'A' >
asm.mov(2, 65);
< SYS_WRITE: print r2 >
asm.syscall(0);
< SYS_EXIT >
asm.syscall(4);
asm.halt();
return loader.load(prog, loadAddr);
}
< Run the whole chain end to end: build -> boot -> load program -> run. >
int powerOnAndRun(int cores, int bits, int mhz, int rounds) {
buildHardware("SleelaWorks", "SL-Core-X", cores, bits, mhz);
boot();
loadDemoProgram(8192);
return os.run(rounds);
}
< Select the machine's adjustable timing/voltage operating point for a given >
< expertise tier (SLTimingClass.TIER_NOVICE/BASIC/ADVANCED/EXPERT) and brand >
< or medium. The profile owns the actual voltages, the variable bus/core/IO >
< clocks, and the expert CPU/RAM/I/O timings; it is sealed into a manifest >
< that every program compiled through the loader's compiler then carries. >
SLTimingProfile selectTiming(int tier, String brandOrMedium) {
timing = new SLTimingProfile();
timing.configure(tier, brandOrMedium);
return timing;
}
< Compile a program with the machine's selected timing profile stamped into >
< the resulting .sleela image, so the VM/loader honor the declared operating >
< point instead of assuming hardware quality. Returns the compiled program. >
SLProgram compileWithTiming(String path, int langId, String programName) {
SLCompilerDriver driver = new SLCompilerDriver(); driver.configure(isa);
if (timing != null) { driver.useTimingProfile(timing); }
return driver.compile(path, langId, programName);
}
< Is the machine's selected operating point internally honored — every expert >
< timing met on every rail at the dialed speeds, with the tier's headroom? >
boolean timingHonored() { return timing != null && timing.honored(); }
SLCPU processor() { return cpu; }
SLOperatingSystem operatingSystem() { return os; }
SLProgramLoader programLoader() { return loader; }
SLRAM mainMemory() { return memory; }
SLTimingProfile timingProfile() { return timing; }
}