Home /Modules/cpu/SLX86CPU.sleela

SLX86CPU.sleela

lib/cpu/x86/SLX86CPU.sleela · 131 line(s) · sleela

<< lib/cpu/x86/SLX86CPU.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLX86CPU โ€” a runnable model of a 32-bit x86 (IA-32 style):
     the general registers EAX ECX EDX EBX ESP EBP ESI EDI, an EFLAGS register,
     and a program counter (EIP). It extends SLCPURuntime and adds the x86
     register set, a decoder for a compact two-operand encoding, the protected-
     mode / control-register and interrupt hooks the skeleton named, and โ€” as a
     worked example of a CPU running ON TOP OF the secondary Sleela VM โ€” a
     lower() path that translates a short x86 sequence into canonical opcodes and
     executes them on the inherited SLSleelaVM substrate.
   
   Instruction model (op in high byte, 4-bit dst + 4-bit src/imm in low byte):
   MOV r,imm / MOV r,r / ADD r,r / SUB r,r / INC r / CMP r,r / MOV r,[m] /
   MOV [m],r / JMP / JE / JNE / INT(interrupt+halt) / HLT / NOP.
   
   Family: cpu. Extends SLCPURuntime. >>
#sleela 2.5
material;
class SLX86CPU extends SLCPURuntime {
  < ---- 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 = 10469;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  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-08";
  < 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; }
  static final int EAX = 0; static final int ECX = 1; static final int EDX = 2; static final int EBX = 3;
  static final int ESP = 4; static final int EBP = 5; static final int ESI = 6; static final int EDI = 7;
  static final int EFLAGS = 8;

  static final int MOV_RI = 1; static final int MOV_RR = 2; static final int ADD_RR = 3;
  static final int SUB_RR = 4; static final int INC_R  = 5; static final int CMP_RR = 6;
  static final int MOV_RM = 7; static final int MOV_MR = 8; static final int JMP = 9;
  static final int JE = 10; static final int JNE = 11; static final int INT = 12; static final int HLT = 13;

  < 0 real, 1 protected (control-register hook) >
  int mode;
  < control register 0 >
  int cr0;

  void configure() {
    < Intel IA-32 (386/486-class) at ~33 MHz; overclockable >
    setSpecClock(33);
    bringUp("Intel IA-32", 32, 32, 9, 262144);
    mode = 1; cr0 = 1;
    writeReg(ESP, 0x40000 - 4);
  }

  int opOf(int w)  { return (w / 256) % 256; }
  int dstOf(int w) { return (w % 256) / 16; }
  int srcOf(int w) { return w % 16; }
  int immOf(int w) { return w % 256; }

  void decode(int word) {
    int opcode = opOf(word);

    if (opcode == MOV_RI) { int v = immOf(word); writeReg(dstOf(word), v); setZN(v); return; }
    if (opcode == MOV_RR) { int v = readReg(srcOf(word)); writeReg(dstOf(word), v); setZN(v); return; }
    if (opcode == ADD_RR) { int r = aluOp(readReg(dstOf(word)), readReg(srcOf(word)), alu.OP_ADD); writeReg(dstOf(word), r); return; }
    if (opcode == SUB_RR) { int r = aluOp(readReg(dstOf(word)), readReg(srcOf(word)), alu.OP_SUB); writeReg(dstOf(word), r); return; }
    if (opcode == INC_R)  { int r = aluOp(readReg(dstOf(word)), 1, alu.OP_ADD); writeReg(dstOf(word), r); return; }
    if (opcode == CMP_RR) { aluOp(readReg(dstOf(word)), readReg(srcOf(word)), alu.OP_SUB); return; }
    if (opcode == MOV_RM) { writeReg(dstOf(word), readMem(immOf(word))); return; }
    if (opcode == MOV_MR) { writeMem(immOf(word), readReg(dstOf(word))); return; }
    if (opcode == JMP)    { setEntryPoint(immOf(word)); return; }
    if (opcode == JE)     { if (zero() == 1) { setEntryPoint(immOf(word)); } return; }
    if (opcode == JNE)    { if (zero() == 0) { setEntryPoint(immOf(word)); } return; }
    if (opcode == INT)    { interrupt(immOf(word)); halted = 1; return; }
    if (opcode == HLT)    { halted = 1; return; }
  }

  < IDT dispatch hook (model no-op) >
  void interrupt(int vector) { cr0 = cr0; }

  void setZN(int v) { flagZ = (v == 0) ? 1 : 0; flagN = (v < 0) ? 1 : 0; }
  int encode(int opcode, int dst, int src) { return (opcode % 256) * 256 + (dst % 16) * 16 + (src % 16); }
  int encodeImm(int opcode, int dst, int imm) { return (opcode % 256) * 256 + (imm % 256); }

  < --- Worked example: run x86 "MOV EAX,a; MOV EBX,b; ADD; PRINT" on top of >
  < the secondary Sleela VM by lowering to canonical opcodes. This demonstrates >
  < a CPU model executing on the native-opcode substrate rather than inline. >
  int lowerAddAndPrint(int a, int b) {
    enableLowering();
    emitConst(a);
    emitConst(b);
    emitAdd();
    emitPrint();
    emitHalt();
    runLowered(100);
    return loweredOutput();
  }

  int reg(int i)       { return readReg(i); }
  int eflags()         { return readReg(EFLAGS); }
  boolean protectedMode(){ return mode == 1; }
}