Home /Modules/cpu/SLRegister.sleela

SLRegister.sleela

lib/cpu/SLRegister.sleela · 103 line(s) · sleela

<< lib/cpu/SLRegister.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLRegister — an N-bit register: a word of storage that
     loads on a clock edge. Conceptually a row of SLFlipFlop cells sharing one
     clock and one load-enable. Registers hold the CPU's working values (the
     general-purpose registers, the program counter, the status word).
   
   This class stores the word as an integer for efficiency but models the
   clocked, load-enabled flip-flop behavior exactly.
   
   Family: cpu. Clocked storage. >>
#sleela 2.5
material;
class SLRegister {
  < ---- 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 = 10276;
  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; }
  String name;
  int width;
  int value;
  int loadEnable;
  int lastClk;
  int writes;

  void configure(String regName, int bits) {
    name = regName; width = bits; value = 0;
    loadEnable = 0; lastClk = 0; writes = 0;
  }

  int pow2(int n) { int p = 1; int i = 0; while (i < n) { p = p * 2; i = i + 1; } return p; }
  int mask() { return pow2(width); }

  < combinational input staging >
  void setInput(int v) { value = v; }
  void enableLoad() { loadEnable = 1; }
  void disableLoad() { loadEnable = 0; }

  < Clocked load. On the rising edge, if load is enabled, capture the input >
  < (already truncated to the register width). >
  int clock(int clk, int input) {
    if (lastClk == 0 && clk == 1 && loadEnable == 1) {
      int m = mask();
      int v = input % m;
      if (v < 0) { v = v + m; }
      value = v;
      writes = writes + 1;
    }
    lastClk = clk;
    return value;
  }

  < Direct load helper (one clocked write), convenient for drivers. >
  int load(int input) {
    enableLoad();
    clock(0, input);
    clock(1, input);
    disableLoad();
    return value;
  }

  int read() { return value; }
  String regName() { return name; }
  int bitWidth() { return width; }
}