Home /Modules/cpu/SLGate.sleela

SLGate.sleela

lib/cpu/SLGate.sleela · 100 line(s) · sleela

<< lib/cpu/SLGate.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGate, a single Boolean logic gate โ€” the foundational
     primitive of the SLeeLa CPU. Every circuit, adder, register, and ALU is
     ultimately composed from gates, so the whole CPU is Sleela source that
     bottoms out here.
   
   A gate takes one or two 1-bit inputs (0 or 1) and produces one 1-bit output
   according to its kind. Kinds: AND, OR, NOT, NAND, NOR, XOR, XNOR, BUF.
   NAND/NOR are universal โ€” any logic can be built from them alone โ€” which is
   why real silicon favors them; this class exposes them directly.
   
   Family: cpu. Pure Sleela logic; no native bridge needed at this layer. >>
#sleela 2.5
material;
class SLGate {
  < ---- 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 = 10224;
  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; }
  static final int AND  = 0;
  static final int OR   = 1;
  static final int NOT  = 2;
  static final int NAND = 3;
  static final int NOR  = 4;
  static final int XOR  = 5;
  static final int XNOR = 6;
  static final int BUF  = 7;

  < one of the constants above >
  int kind;
  < most recent evaluated output bit >
  int lastOutput;
  < how many times this gate has been evaluated >
  int evaluations;

  void configure(int gateKind) { kind = gateKind; lastOutput = 0; evaluations = 0; }

  int clampBit(int v) { if (v != 0) { return 1; } return 0; }

  < Evaluate the gate for the given input bits. For unary gates (NOT, BUF), >
  < only `a` is used. >
  int eval(int a, int b) {
    int x = clampBit(a);
    int y = clampBit(b);
    int out = 0;
    if (kind == AND)  { if (x == 1 && y == 1) { out = 1; } }
    if (kind == OR)   { if (x == 1 || y == 1) { out = 1; } }
    if (kind == NOT)  { if (x == 0) { out = 1; } }
    if (kind == NAND) { if (x == 1 && y == 1) { out = 0; } else { out = 1; } }
    if (kind == NOR)  { if (x == 0 && y == 0) { out = 1; } else { out = 0; } }
    if (kind == XOR)  { if (x != y) { out = 1; } }
    if (kind == XNOR) { if (x == y) { out = 1; } }
    if (kind == BUF)  { out = x; }
    lastOutput = out;
    evaluations = evaluations + 1;
    return out;
  }

  int output() { return lastOutput; }
  int kindOf() { return kind; }
}