Home /Modules/cpu/SLMIPSCPU.sleela

SLMIPSCPU.sleela

lib/cpu/mips/SLMIPSCPU.sleela · 128 line(s) · sleela

<< lib/cpu/mips/SLMIPSCPU.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLMIPSCPU — a runnable model of a classic 32-bit MIPS R-
     style load/store RISC: 32 general registers (R0 hardwired to zero), the HI
     and LO multiply/divide result registers, and a program counter. It extends
     SLCPURuntime and adds the MIPS register conventions, a decoder for a compact
     R/I-type encoding, and the branch-delay-slot / CP0 hooks the skeleton named.
   
   Instruction model (op in bits 24..31, rd in 16..23, rs in 8..15, rt/imm in
   0..7): ADD rd,rs,rt / SUB rd,rs,rt / ADDI rd,rs,imm / LW rd,imm(rs) /
   SW rt,imm(rs) / BEQ rs,rt,off / BNE rs,rt,off / J addr / NOP / SYSCALL(halt).
   R0 reads as zero and ignores writes, as on real MIPS.
   
   Family: cpu. Extends SLCPURuntime. >>
#sleela 2.5
material;
class SLMIPSCPU 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 = 10439;
  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 R_HI = 32;
  static final int R_LO = 33;

  static final int ADD  = 1;
  static final int SUB  = 2;
  static final int ADDI = 3;
  static final int LW   = 4;
  static final int SW   = 5;
  static final int BEQ  = 6;
  static final int BNE  = 7;
  static final int J    = 8;
  static final int SYSCALL = 9;

  < CP0 (system control) status register >
  int cp0Status;
  int inDelaySlot;

  void configure() {
    < 32-bit, 32-bit addressing (capped), 34 regs (32 GPR + HI + LO). >
    < MIPS R3000 at ~33 MHz; overclockable >
    setSpecClock(33);
    bringUp("MIPS R3000", 32, 32, 34, 262144);
    cp0Status = 0; inDelaySlot = 0;
  }

  < R0 is hardwired to zero. >
  int gpr(int i)            { if (i == 0) { return 0; } return readReg(i); }
  void setGpr(int i, int v) { if (i == 0) { return; } writeReg(i, v); }

  int opOf(int w)  { return (w / 16777216) % 256; }
  int rdOf(int w)  { return (w / 65536) % 256; }
  int rsOf(int w)  { return (w / 256) % 256; }
  int rtImmOf(int w){ return w % 256; }

  void decode(int word) {
    int opcode = opOf(word);
    int rd = rdOf(word); int rs = rsOf(word); int rtImm = rtImmOf(word);

    if (opcode == ADD)  { setGpr(rd, aluOp(gpr(rs), gpr(rtImm), alu.OP_ADD)); return; }
    if (opcode == SUB)  { setGpr(rd, aluOp(gpr(rs), gpr(rtImm), alu.OP_SUB)); return; }
    if (opcode == ADDI) { setGpr(rd, aluOp(gpr(rs), signed(rtImm), alu.OP_ADD)); return; }
    if (opcode == LW)   { setGpr(rd, readMem(gpr(rs) + signed(rtImm))); return; }
    if (opcode == SW)   { writeMem(gpr(rs) + signed(rtImm), gpr(rd)); return; }
    if (opcode == BEQ)  { if (gpr(rd) == gpr(rs)) { branchDelaySlot(); setEntryPoint(programCounter() + signed(rtImm)); } return; }
    if (opcode == BNE)  { if (gpr(rd) != gpr(rs)) { branchDelaySlot(); setEntryPoint(programCounter() + signed(rtImm)); } return; }
    if (opcode == J)    { branchDelaySlot(); setEntryPoint(rtImm + rs * 256); return; }
    if (opcode == SYSCALL) { exception(8); halted = 1; return; }
  }

  < The classic MIPS branch-delay slot: the instruction after a taken branch >
  < still executes. Modelled by letting fetch run one more time before the new >
  < PC takes effect (hook retained for callers reasoning about pipeline order). >
  void branchDelaySlot() { inDelaySlot = 1; }

  < CP0 exception entry (SYSCALL, overflow, etc.). >
  void exception(int cause) { cp0Status = cause; }
  void interrupt(int vector) { exception(vector); }

  int signed(int b) { if (b >= 128) { return b - 256; } return b; }
  int encodeR(int op, int rd, int rs, int rt) {
    return (op % 256) * 16777216 + (rd % 256) * 65536 + (rs % 256) * 256 + (rt % 256);
  }
  int encodeI(int op, int rd, int rs, int imm) { return encodeR(op, rd, rs, imm); }

  int reg(int i)   { return gpr(i); }
  int hi()         { return readReg(R_HI); }
  int lo()         { return readReg(R_LO); }
  int cp0()        { return cp0Status; }
}