Home /Modules/cpu/SLSiFiveU74CPU.sleela

SLSiFiveU74CPU.sleela

lib/cpu/sifive-u74/SLSiFiveU74CPU.sleela · 133 line(s) · sleela

<< lib/cpu/sifive-u74/SLSiFiveU74CPU.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLSiFiveU74CPU โ€” a runnable model of the SiFive U74 (RISC-V RV64GC). It extends
     SLCPURuntime, so it inherits register storage, a word-addressable memory
     interface, an SLALU, the fetch-decode-execute loop, and โ€” crucially โ€” the
     resource-aware workload path (admit()/runWorkload()) that runs C, C++ or
     Sleela programs on this CPU within an SLCPUResource grant, mapped onto the
     canonical SLeeLa opcode substrate.
   
   This file supplies the SiFive U74 (RISC-V RV64GC) register set and a decode() over a compact
   instruction encoding (load/move/arithmetic/compare/branch/halt). It is a
   functional model sufficient to run the CPU as a CPU and as a target for
   further .sleela inputs, not a cycle-accurate emulator; per-arch bus/timing
   detail remains in this folder's markdown docs.
   
   Family: cpu. Extends SLCPURuntime. >>
#sleela 2.5
material;
class SLSiFiveU74CPU 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 = 11080;
  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-13";
  < 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_ZERO = 0;
  static final int R_SP = 31;
  static final int R_LINK = 30;

  < r0 is hardwired to zero (reads 0, writes discarded). >
  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); }

  void configure() {
    < the model's rated clock (MHz); overclockable >
    setSpecClock(1500);
    bringUp("SiFive U74 (RISC-V RV64GC)", 64, 39, 32, 262144);
  }

  < dst <- imm >
  static final int I_LOADI = 1;
  < dst <- src >
  static final int I_MOVE  = 2;
  < dst <- mem[imm] >
  static final int I_LOAD  = 3;
  < mem[imm] <- dst >
  static final int I_STORE = 4;
  < dst <- dst + src >
  static final int I_ADD   = 5;
  < dst <- dst - src >
  static final int I_SUB   = 6;
  < dst <- dst * src >
  static final int I_MUL   = 7;
  < dst <- dst & src >
  static final int I_AND   = 8;
  < dst <- dst | src >
  static final int I_OR    = 9;
  < flags <- dst - src >
  static final int I_CMP   = 10;
  < pc <- imm >
  static final int I_JMP   = 11;
  < if Z: pc <- imm >
  static final int I_JZ    = 12;
  static final int I_HALT  = 13;

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

  void decode(int word) {
    int opc = opOf(word);
    int dst = dstOf(word);
    int src = srcOf(word);

    if (opc == I_LOADI) { setGpr(dst, src); setZN(gpr(dst)); return; }
    if (opc == I_MOVE)  { setGpr(dst, gpr(src)); setZN(gpr(dst)); return; }
    if (opc == I_LOAD)  { setGpr(dst, readMem(src)); setZN(gpr(dst)); return; }
    if (opc == I_STORE) { writeMem(src, gpr(dst)); return; }
    if (opc == I_ADD)   { setGpr(dst, aluOp(gpr(dst), gpr(src), alu.OP_ADD)); return; }
    if (opc == I_SUB)   { setGpr(dst, aluOp(gpr(dst), gpr(src), alu.OP_SUB)); return; }
    if (opc == I_MUL)   { setGpr(dst, aluOp(gpr(dst), gpr(src), alu.OP_MUL)); return; }
    if (opc == I_AND)   { setGpr(dst, aluOp(gpr(dst), gpr(src), alu.OP_AND)); return; }
    if (opc == I_OR)    { setGpr(dst, aluOp(gpr(dst), gpr(src), alu.OP_OR)); return; }
    if (opc == I_CMP)   { aluOp(gpr(dst), gpr(src), alu.OP_SUB); return; }
    if (opc == I_JMP)   { setEntryPoint(src); return; }
    if (opc == I_JZ)    { if (zero() == 1) { setEntryPoint(src); } return; }
    if (opc == I_HALT)  { halted = 1; return; }
  }

  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) * 65536 + (dst % 256) * 256 + (src % 256);
  }

  int reg(int i) { return gpr(i); }
  int registers() { return registerCount(); }
}