Home /Modules/cpu/SLCache.sleela

SLCache.sleela

lib/cpu/SLCache.sleela · 106 line(s) · sleela

<< lib/cpu/SLCache.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLCache — a CPU cache level (L1/L2/L3) sitting between a
     core and main memory. It models the properties that matter for behavior:
     total size, line size, associativity, and the hit/miss statistics that
     drive the memory hierarchy. On a miss it falls through to a backing SLRAM
     (or a slower cache level).
   
   This is a behavioral model: it tracks a modeled hit rate and forwards misses;
   the exact line contents live in the backing SLRAM below the bridge.
   
   Family: cpu. Composes SLRAM as its backing store. >>
#sleela 2.5
material;
class SLCache {
  < ---- 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 = 10205;
  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; }
  < "L1", "L2", "L3" >
  String level;
  int sizeBytes;
  int lineBytes;
  < ways >
  int associativity;
  < next level down (main memory or a lower cache) >
  SLRAM backing;
  int accesses;
  int hits;
  int misses;
  < modeled count of resident lines >
  int warmLines;

  void configure(String cacheLevel, int size, int line, int ways) {
    level = cacheLevel; sizeBytes = size; lineBytes = line; associativity = ways;
    accesses = 0; hits = 0; misses = 0; warmLines = 0;
  }

  void attachBacking(SLRAM ram) { backing = ram; }

  int lineCount() { if (lineBytes == 0) { return 0; } return sizeBytes / lineBytes; }
  int lineOf(int addr) { if (lineBytes == 0) { return 0; } return addr / (lineBytes / 4); }

  < Access a word address. A line already warm is a hit; otherwise a miss that >
  < warms the line (bounded by the cache's line capacity) and reads from the >
  < backing store. >
  int access(int addr) {
    accesses = accesses + 1;
    int line = lineOf(addr);
    boolean resident = line < warmLines;
    if (resident) {
      hits = hits + 1;
    } else {
      misses = misses + 1;
      if (warmLines < lineCount()) { warmLines = warmLines + 1; }
      if (backing != null) { return backing.readWord(addr); }
    }
    if (backing != null) { return backing.readWord(addr); }
    return 0;
  }

  int accessCount() { return accesses; }
  int hitCount() { return hits; }
  int missCount() { return misses; }
  < hit rate in percent (0..100), integer >
  int hitRatePercent() { if (accesses == 0) { return 0; } return (hits * 100) / accesses; }
  String levelName() { return level; }
}