Home /Modules/cpu/SLAdder.sleela

SLAdder.sleela

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

<< lib/cpu/SLAdder.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLAdder — an N-bit ripple-carry adder built from a chain
     of full adders. This is the arithmetic heart of the ALU: it adds two
     machine words bit-by-bit, propagating carry from the least significant bit
     to the most significant, and reports an overflow/carry-out.
   
   The ripple-carry structure is the textbook adder; faster CPUs use
   carry-lookahead or carry-select variants (documented in CPU.md), but the
   semantics are identical and this models the canonical case.
   
   Family: cpu. Composes SLFullAdder over a word width. >>
#sleela 2.5
material;
class SLAdder {
  < ---- 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 = 10198;
  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; }
  < bits per word (e.g. 32 or 64) >
  int width;
  < one reusable full-adder cell, applied bit by bit >
  SLFullAdder fa;
  < numeric sum, truncated to `width` bits >
  int result;
  < carry out of the most significant bit >
  int carryOut;

  void configure(int bits) {
    width = bits;
    fa = new SLFullAdder(); fa.build();
    result = 0; carryOut = 0;
  }

  int bitAt(int value, int i) {
    int shifted = value / pow2(i);
    return shifted % 2;
  }

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

  < Add two words of `width` bits; ripple the carry through each full adder. >
  int add(int a, int b) {
    int carry = 0;
    int sum = 0;
    int i = 0;
    while (i < width) {
      fa.add(bitAt(a, i), bitAt(b, i), carry);
      if (fa.sumBit() == 1) { sum = sum + pow2(i); }
      carry = fa.carryBit();
      i = i + 1;
    }
    result = sum;
    carryOut = carry;
    return result;
  }

  int sum() { return result; }
  int carry() { return carryOut; }
  int bitWidth() { return width; }
}