Home /Modules/cpu/SLARMCPU.sleela

SLARMCPU.sleela

lib/cpu/arm/SLARMCPU.sleela · 131 line(s) · sleela

<< lib/cpu/arm/SLARMCPU.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLARMCPU — a runnable model of a 32-bit ARM (A32-style):
     16 registers R0..R15 (R13 SP, R14 LR, R15 PC) and the N Z C V flags in a
     CPSR. It extends SLCPURuntime and adds the ARM register set, condition-code
     evaluation, a decoder for a compact data-processing / load-store / branch
     encoding, and the exception / banked-register hooks the skeleton named.
   
   Instruction model (cond in bits 28..31, op in 20..27, rd in 16..19, rn in
   12..15, operand2 in 0..11): MOV / ADD / SUB / CMP / LDR / STR / B / BL /
   SWI(exception+halt) / NOP. All instructions are conditionally executed via
   cond, as on ARM. Compact teaching encoding, enough to run the CPU and host
   further .sleela inputs.
   
   Family: cpu. Extends SLCPURuntime. >>
#sleela 2.5
material;
class SLARMCPU extends SLCPURuntime {
  < ---- 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 = 10424;
  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-10-08";
  < 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 R_SP = 13; static final int R_LR = 14; static final int R_PC = 15;
  static final int R_CPSR = 16;

  < Condition codes. >
  static final int COND_EQ = 0; static final int COND_NE = 1; static final int COND_AL = 14;

  < Operations (bits 20..27 of the model word). >
  static final int MOV = 1; static final int ADD = 2; static final int SUB = 3;
  static final int CMP = 4; static final int LDR = 5; static final int STR = 6;
  static final int B   = 7; static final int BL  = 8; static final int SWI = 9;

  < 0 non-secure, 1 secure (TrustZone hook) >
  int securityState;

  void configure() {
    < ARMv4 (ARM7-class) at ~25 MHz; overclockable >
    setSpecClock(25);
    bringUp("ARMv4 (A32)", 32, 32, 17, 262144);
    securityState = 0;
    writeReg(R_SP, 0x40000 - 4);
  }

  int condOf(int w) { return (w / 268435456) % 16; }
  int opOf(int w)   { return (w / 1048576) % 256; }
  int rdOf(int w)   { return (w / 65536) % 16; }
  int rnOf(int w)   { return (w / 4096) % 16; }
  int op2Of(int w)  { return w % 4096; }

  < Evaluate the ARM condition field against current flags. >
  boolean conditionPasses(int cond) {
    if (cond == COND_AL) { return true; }
    if (cond == COND_EQ) { return zero() == 1; }
    if (cond == COND_NE) { return zero() == 0; }
    return true;
  }

  void decode(int word) {
    int cond = condOf(word);
    < conditional execution >
    if (!conditionPasses(cond)) { return; }

    int opcode = opOf(word);
    int rd = rdOf(word); int rn = rnOf(word); int op2 = op2Of(word);

    if (opcode == MOV) { writeReg(rd, op2); setNZ(op2); return; }
    if (opcode == ADD) { int r = aluOp(readReg(rn), op2, alu.OP_ADD); writeReg(rd, r); return; }
    if (opcode == SUB) { int r = aluOp(readReg(rn), op2, alu.OP_SUB); writeReg(rd, r); return; }
    < flags only >
    if (opcode == CMP) { aluOp(readReg(rn), op2, alu.OP_SUB); return; }
    if (opcode == LDR) { writeReg(rd, readMem(readReg(rn) + op2)); return; }
    if (opcode == STR) { writeMem(readReg(rn) + op2, readReg(rd)); return; }
    if (opcode == B)   { setEntryPoint(programCounter() + signed12(op2)); return; }
    if (opcode == BL)  { writeReg(R_LR, programCounter()); setEntryPoint(programCounter() + signed12(op2)); return; }
    if (opcode == SWI) { exception(2); halted = 1; return; }
  }

  < Exception / banked-register entry (SWI, IRQ, etc.). >
  void exception(int vector) { writeReg(R_CPSR, vector); }
  void interrupt(int vector) { exception(vector); }

  int signed12(int v) { if (v >= 2048) { return v - 4096; } return v; }
  void setNZ(int v) { flagZ = (v == 0) ? 1 : 0; flagN = (v < 0) ? 1 : 0; }

  int encode(int cond, int opcode, int rd, int rn, int op2) {
    return (cond % 16) * 268435456 + (opcode % 256) * 1048576 + (rd % 16) * 65536 + (rn % 16) * 4096 + (op2 % 4096);
  }

  int reg(int i)  { return readReg(i); }
  int cpsr()      { return readReg(R_CPSR); }
  boolean secure(){ return securityState == 1; }
}