<< lib/cpu/6502/SL6502CPU.sleela SLeeLa Standard Library Definition. Definition: Defines SL6502CPU — a runnable model of the MOS 6502: an 8-bit accumulator machine with X/Y index registers, a stack pointer, and the N V - B D I Z C status flags. It extends SLCPURuntime, so register storage, memory, the ALU, and the fetch-decode-execute loop are inherited; this file supplies the 6502's registers and an instruction decoder. Instruction model (one word per instruction: op in bits 8..15, operand in bits 0..7) — a compact teaching encoding, not the byte-exact 6502 opcode map, sufficient to run the CPU as a CPU and as a model for further .sleela inputs: LDA#imm, LDX#imm, LDY#imm, STA abs, LDA abs, ADC#imm, SBC#imm, INX, INY, TAX, TAY, JMP abs, BEQ rel, BNE rel, BRK(halt), NOP. decode() routes arithmetic through the inherited ALU (so flags are real) and can also lower the stream onto the secondary VM via the inherited emit*(). Family: cpu. Extends SLCPURuntime. >> #sleela 2.5 material; class SL6502CPU 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 = 10416; 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 | | 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; } < Register file indices (into the inherited SLRAM register store). > < accumulator > static final int R_A = 0; < X index > static final int R_X = 1; < Y index > static final int R_Y = 2; < stack pointer > static final int R_SP = 3; < processor status > static final int R_P = 4; < Opcodes in the compact model encoding (high byte of the instruction word). > static final int LDA_IMM = 1; static final int LDX_IMM = 2; static final int LDY_IMM = 3; static final int STA_ABS = 4; static final int LDA_ABS = 5; static final int ADC_IMM = 6; static final int SBC_IMM = 7; static final int INX = 8; static final int INY = 9; static final int TAX = 10; static final int TAY = 11; static final int JMP_ABS = 12; static final int BEQ_REL = 13; static final int BNE_REL = 14; static final int BRK = 15; void configure() { < 8-bit data, 16-bit address, 5 architectural registers, 64 KiB words. > < MOS 6502 ran at ~1 MHz; overclockable via the clock policy > setSpecClock(1); bringUp("MOS 6502", 8, 16, 5, 65536); writeReg(R_SP, 0xFF); } int opOf(int word) { return (word / 256) % 256; } int operandOf(int word) { return word % 256; } void decode(int word) { int opcode = opOf(word); int arg = operandOf(word); if (opcode == LDA_IMM) { writeReg(R_A, arg); setZN(arg); return; } if (opcode == LDX_IMM) { writeReg(R_X, arg); setZN(arg); return; } if (opcode == LDY_IMM) { writeReg(R_Y, arg); setZN(arg); return; } if (opcode == STA_ABS) { writeMem(arg, readReg(R_A)); return; } if (opcode == LDA_ABS) { int v = readMem(arg); writeReg(R_A, v); setZN(v); return; } if (opcode == ADC_IMM) { int r = aluOp(readReg(R_A), arg, alu.OP_ADD); writeReg(R_A, r); return; } if (opcode == SBC_IMM) { int r = aluOp(readReg(R_A), arg, alu.OP_SUB); writeReg(R_A, r); return; } if (opcode == INX) { int r = aluOp(readReg(R_X), 1, alu.OP_ADD); writeReg(R_X, r); return; } if (opcode == INY) { int r = aluOp(readReg(R_Y), 1, alu.OP_ADD); writeReg(R_Y, r); return; } if (opcode == TAX) { int v = readReg(R_A); writeReg(R_X, v); setZN(v); return; } if (opcode == TAY) { int v = readReg(R_A); writeReg(R_Y, v); setZN(v); return; } if (opcode == JMP_ABS) { setEntryPoint(arg); return; } if (opcode == BEQ_REL) { if (zero() == 1) { setEntryPoint(programCounter() + signed(arg)); } return; } if (opcode == BNE_REL) { if (zero() == 0) { setEntryPoint(programCounter() + signed(arg)); } return; } if (opcode == BRK) { halted = 1; return; } < NOP / unknown > } < Treat the low byte as a signed relative offset for branches. > int signed(int b) { if (b >= 128) { return b - 256; } return b; } void setZN(int v) { flagZ = (v % 256 == 0) ? 1 : 0; flagN = ((v / 128) % 2 == 1) ? 1 : 0; } < Assembler helper: pack an instruction word for loadProgramWord(). > int encode(int opcode, int operand) { return (opcode % 256) * 256 + (operand % 256); } int accumulator() { return readReg(R_A); } int indexX() { return readReg(R_X); } int indexY() { return readReg(R_Y); } < The 6502 leaves a computation's result in the accumulator, so the opcode > < path's observable (used by the inherited runs-by-both basis check) is A. > int opcodeObservable() { return readReg(R_A); } }