Home /Modules/vm/SLVMMemory.sleela

SLVMMemory.sleela

lib/vm/SLVMMemory.sleela · 132 line(s) · sleela

<< lib/vm/SLVMMemory.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLVMMemory — the flat linear address space of the
     secondary Sleela VM, distinct from the object heap (SLVMHeap). It backs raw
     loads/stores, string/array blobs, and the module code image over SLRAM, with
     word- and byte-granular access and simple bounds checking. The heap carves
     its arena from this space; native bindings may map device windows into it.
   
   Keeping a byte-addressable linear memory separate from the typed heap lets the
   secondary VM model both managed objects and the raw memory a guest OS/CPU
   model expects — the same split the native layer draws between SLRAM and the
   managed runtime.
   
   Family: vm. Composes SLRAM; composed by SLVM. >>
#sleela 2.5
material;
class SLVMMemory {
  < ---- 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 = 929;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  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-09-28";
  < 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; }
  < Backing mode. >
  < SLRAM (bridge-backed, accounted) >
  static final int MODE_SIMULATED = 0;
  < SLNativeMemory (real host RAM, direct) >
  static final int MODE_NATIVE    = 1;

  int mode;
  < used in MODE_SIMULATED >
  SLRAM store;
  < used in MODE_NATIVE >
  SLNativeMemory native;
  int capacityWords;
  int reads;
  int writes;

  < Default (simulated) backing over SLRAM. >
  void configure(int words) {
    mode = MODE_SIMULATED;
    capacityWords = words;
    store = new SLRAM(); store.configure(words, 32); store.allocate();
    reads = 0; writes = 0;
  }

  < Native-RAM backing: the linear address space lives in REAL host memory, for >
  < running outside the simulated store (procedural layment). Same interface. >
  void configureNative(int words) {
    mode = MODE_NATIVE;
    capacityWords = words;
    native = new SLNativeMemory(); native.configure(words, 32); native.allocate();
    reads = 0; writes = 0;
  }

  boolean nativeBacked() { return mode == MODE_NATIVE; }

  boolean inRange(int addr) { return addr >= 0 && addr < capacityWords; }

  int readWord(int addr) {
    if (!inRange(addr)) { return 0; }
    reads = reads + 1;
    if (mode == MODE_NATIVE) { return native.readWord(addr); }
    return store.readWord(addr);
  }
  void writeWord(int addr, int value) {
    if (!inRange(addr)) { return; }
    writes = writes + 1;
    if (mode == MODE_NATIVE) { native.writeWord(addr, value); return; }
    store.writeWord(addr, value);
  }

  < Byte view packed four-per-word, little-endian within the word. >
  int readByte(int byteAddr) {
    int w = byteAddr / 4; int b = byteAddr % 4;
    int word = readWord(w);
    return (word >> (b * 8)) & 0xFF;
  }
  void writeByte(int byteAddr, int value) {
    int w = byteAddr / 4; int b = byteAddr % 4;
    int word = readWord(w);
    int shift = b * 8;
    int cleared = word & ~(0xFF << shift);
    writeWord(w, cleared | ((value & 0xFF) << shift));
  }

  < Copy a block of words (memmove-style, forward). >
  void copy(int dst, int src, int words) {
    int i = 0;
    while (i < words) { writeWord(dst + i, readWord(src + i)); i = i + 1; }
  }

  int capacity()  { return capacityWords; }
  int readCount() { return reads; }
  int writeCount(){ return writes; }
}