xtensa.c

C source for Emulating a round-screen ESP32 board, 932 lines. Raw file

#include <stdlib.h>
// xtensa.c - Xtensa LX7 execute core (from scratch). First execution milestone:
// run the real firmware's call_start_cpu0 sequence and beyond, implementing the
// instruction set on demand and trapping anything not yet handled so it is easy to
// extend. Instruction words are little-endian; op0 = byte0 & 0xf; a 16-bit
// (density) instruction has op0 >= 8, else 24-bit.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "xtensa.h"
#include "mem.h"

// ---- single-precision FP coprocessor register access (IEEE-754 bit patterns) ----
static inline float fr_get(xtensa_t *c, int i) { float f; memcpy(&f, &c->fr[i], 4); return f; }
static inline void  fr_set(xtensa_t *c, int i, float f) { memcpy(&c->fr[i], &f, 4); }
// Xtensa/softfloat float->int conversion: NaN and +overflow -> 0x7FFFFFFF, -overflow
// -> 0x80000000 (the host arm64 cast returns 0 for NaN, so do it explicitly).
static inline uint32_t xt_f2i(float v) {
    if (isnan(v)) return 0x7FFFFFFFu;
    if (v >= 2147483648.0f)  return 0x7FFFFFFFu;   // >= 2^31
    if (v <  -2147483648.0f) return 0x80000000u;   // <  -2^31
    return (uint32_t)(int32_t)v;
}
// float->unsigned: a NEGATIVE (non-NaN) input takes the SIGNED path (so -1.0 ->
// 0xFFFFFFFF), matching the Xtensa FPU; NaN and +overflow -> 0xFFFFFFFF.
static inline uint32_t xt_f2u(float v) {
    if (isnan(v))            return 0xFFFFFFFFu;
    if (v < 0.0f)            return xt_f2i(v);
    if (v >= 4294967296.0f)  return 0xFFFFFFFFu;   // >= 2^32
    return (uint32_t)v;
}

// ---- windowed register access: a0..a15 -> physical AR ----
static inline uint32_t *AR(xtensa_t *c, int i) {
    return &c->ar[(c->windowbase * 4 + i) & (NAREG - 1)];
}
uint32_t xt_a(xtensa_t *c, int i)              { return *AR(c, i); }
void     xt_set_a(xtensa_t *c, int i, uint32_t v){ *AR(c, i) = v; }
#define A(i)     (*AR(c, (i)))

// APP-CPU (core 1) launch, set here and acted on by main.c.
uint32_t g_core1_entry;
int      g_core1_start_req;

// Hardware interrupt bits currently asserted for a core (systimer, etc.), computed
// by the peripheral models in mmio.c and OR'd with software-set INTERRUPT bits.
uint32_t mmio_cpu_int(int core);
extern int g_rtc_slp_armed;   // set by mmio.c on SLEEP_EN; used for the deep-sleep-reset model

void xt_reset(xtensa_t *c, uint32_t entry) {
    memset(c, 0, sizeof *c);
    c->pc = entry;
    c->ps = 0x00040000;        // WOE=1, others 0 (approx reset for windowed boot)
    c->windowstart = 1;        // window 0 live
}

// ---- windowed-register exceptions ----
#define PS_EXCM 0x10u
#define PS_WOE  0x40000u
// Window overflow/underflow exceptions are raised by CALL*/RETW only when window
// exceptions are enabled (PS.WOE=1) and we're not already inside an exception
// (PS.EXCM=0). With WOE=0 — as xthal_window_spill sets before its manual s32e-based
// spill — CALL/RETW just rotate the window and access the register file directly.
#define WIN_EXC(c) (((c)->ps & PS_WOE) && !((c)->ps & PS_EXCM))
static inline uint32_t ps_owb(uint32_t ps) { return (ps >> 8) & 0xf; }
static inline uint32_t ps_set_owb(uint32_t ps, uint32_t wb) { return (ps & ~0xf00u) | (wb << 8); }

// #windows free ahead of WindowBase before the next live (WindowStart) window.
static int win_avail(xtensa_t *c) {
    uint32_t ws = c->windowstart & 0xffff;
    int rot = (c->windowbase + 1) & 15;
    uint32_t r = ((ws >> rot) | (ws << (16 - rot))) & 0xffff;
    return r ? (__builtin_ctz(r) + 1) : 16;
}
// Rotate to the frame to spill/restore and vector to the Overflow/Underflow
// handler; it does s32e/l32e then rfwo/rfwu (retry EPC1).
//
// Overflow: the register file is circular (16 frames / 64 regs). Going FORWARD
// from WindowBase are the OLDEST frames (the file wraps). `d` = distance to the
// nearest live frame ahead (= win_avail). That frame is about to be clobbered by
// the new call's window, so spill it. WindowBase rotates to it; the Overflow4/8/12
// vector is chosen by how many registers THAT frame owns = its own CALLINC, i.e.
// how it was ENTERED, stored in its a0[31:30]. (A call8 frame owns a0..a7, so it
// must go through Overflow8 or a4..a7 are lost — exactly what breaks a mixed call
// chain if you instead use the forward/successor distance.)
// Invariant tracker (env WINV): the SP each window was spilled with, so RFWU can catch a
// window being reloaded with a DIFFERENT/garbage SP (the deep-nest stale-link touch bug).
uint32_t g_spill_sp[16], g_spill_a9[16]; int g_pending[16], g_winv = -1;
static void win_over(xtensa_t *c, uint32_t fpc, int d) {
    if (g_winv < 0) g_winv = getenv("WINV") ? 1 : 0;
    int swb = (c->windowbase + d) & 15;              // the frame to spill (oldest live)
    // Handler size = how swb calls FORWARD (to its callee): OverflowN's base register
    // a(N+1) must be the CALLEE's stack pointer (spill target is [callee_sp-16]).
    //  - If a live window sits within 3 ahead of swb, that IS the callee → use its
    //    distance (the callee's SP is live in swb's a(4N+1)).
    //  - Otherwise the callee was already spilled (swb is the last live frame, e.g. the
    //    final window during SPILL_ALL), so the forward distance isn't recoverable from
    //    WindowStart; fall back to swb's own entry size a0[31:30], which equals the
    //    forward size for the common uniform frame. (a0 CALLINC 0 = task root → assume
    //    the usual call8.)
    uint32_t ws = c->windowstart & 0xffff;
    int rot = (swb + 1) & 15;
    uint32_t rr = ((ws >> rot) | (ws << (16 - rot))) & 0xffff;
    int raw = rr ? (__builtin_ctz(rr) + 1) : 16;
    int inc;
    int a0inc = (c->ar[(swb * 4) & (NAREG - 1)] >> 30) & 3;
    if (raw <= 3) inc = raw;
    else { inc = a0inc; if (inc == 0) inc = 2; }
    if (g_winv > 0) {                                // record the SP + callee-SP base this window spilled with
        g_spill_sp[swb] = c->ar[(swb*4 + 1) & (NAREG - 1)]; g_pending[swb] = 1;
        g_spill_a9[swb] = c->ar[(swb*4 + 4*inc + 1) & (NAREG - 1)];   // callee SP (spill base a(4*inc+1))
    }
    c->ps = ps_set_owb(c->ps, c->windowbase);
    c->windowbase = swb;
    c->epc[1] = fpc; c->ps |= PS_EXCM;
    c->pc = c->vecbase + (inc - 1) * 0x80;
}
static void win_under(xtensa_t *c, uint32_t fpc, int n) {
    int twb = (c->windowbase - n) & 15;
    if (g_winv > 0 && g_pending[twb] && g_spill_a9[twb]) {   // reload base a(4n+1) vs spill's callee SP
        uint32_t rel_a9 = c->ar[(twb*4 + 4*n + 1) & (NAREG - 1)];
        if (rel_a9 != g_spill_a9[twb] && (rel_a9 >> 16) == (g_spill_a9[twb] >> 16))   // same task stack region
            fprintf(stderr, "WINV-A9: win%d reload a9=%08x != spill a9=%08x (myPrevSP=%08x n=%d) fpc=%08x cyc=%llu\n",
                    twb, rel_a9, g_spill_a9[twb], g_spill_sp[twb], n, fpc, (unsigned long long)c->cycles);
    }
    c->ps = ps_set_owb(c->ps, c->windowbase);
    c->windowbase = twb;
    c->epc[1] = fpc; c->ps |= PS_EXCM;
    c->pc = c->vecbase + (n - 1) * 0x80 + 0x40;
}

// Highest a-register index (0..15) this instruction reads/writes, or -1 if it uses
// no a-registers or only ones handled by the window-control path (CALL/RET/ENTRY,
// which do their own overflow/underflow). Used for the window-overflow-on-access
// check: an instruction touching a[i] where i >= 4*win_avail would clobber a live
// older window, so the hardware raises WindowOverflow to spill it first. This is
// what makes the SPILL_ALL_WINDOWS idiom (rotw + `and a12,a12,a12`) actually spill.
static int insn_maxreg(uint32_t insn) {
    int op0 = insn & 0xf, t = (insn>>4)&0xf, s = (insn>>8)&0xf, r = (insn>>12)&0xf;
    int op1 = (insn>>16)&0xf, op2 = (insn>>20)&0xf;
    #define MX2(a,b) ((a)>(b)?(a):(b))
    #define MX3(a,b,cc) MX2(MX2(a,b),cc)
    switch (op0) {
    case 0x0: // QRST
        if (op1 == 4 || op1 == 5) return MX2(r,t);          // EXTUI (s is part of sa imm)
        switch (op1) {
        case 0x0: // RST0
            switch (op2) {
            case 0x0: // ST0 (control/special): CALLX/RET/RETW/JX/RFx/WAITI own the window path
                if (r == 1) return MX2(s,t);                // MOVSP
                if (r == 6) return t;                       // RSIL
                return -1;
            case 0x1: case 0x2: case 0x3: return MX3(r,s,t); // AND/OR/XOR  (SPILL_ALL's `and a12`)
            case 0x4:                                        // ST1
                if (r == 0xe || r == 0xf) return MX2(s,t);   // NSA/NSAU
                if (r == 8) return -1;                       // ROTW (t is imm)
                if (r == 4) return -1;                       // SSAI (s,t imm)
                return s;                                    // SSR/SSL/SSA8L/SSA8B
            case 0x5: case 0x7: return MX2(s,t);             // TLB ops
            case 0x6: return MX2(r,t);                       // NEG/ABS
            default: return MX3(r,s,t);                      // ADD/ADDX/SUB/SUBX
            }
        case 0x1: // RST1
            switch (op2) {
            case 0x0: case 0x1: return MX2(r,s);             // SLLI (t imm)
            case 0x2: case 0x3: case 0x4: return MX2(r,t);   // SRAI/SRLI (s imm)
            case 0x6: return t;                              // XSR (r,s = SR index)
            default: return MX3(r,s,t);                      // SRC/SRL/SLL/SRA/MUL16
            }
        case 0x2: return MX3(r,s,t);                         // RST2 MUL/DIV/REM/SALT
        case 0x3: // RST3
            if (op2 == 0x2) return MX2(r,s);                 // SEXT (t imm)
            return t;                                        // RSR/WSR/XSR (t reg)
        default: return MX3(r,s,t);
        }
    case 0x1: return t;                                      // L32R
    case 0x2: return MX2(t,s);                               // LSAI loads/stores (t data, s base)
    case 0x3: return s;                                      // cache/store-ish (base)
    case 0x4: return MX3(r,s,t);                             // MAC16 (conservative)
    case 0x5: return -1;                                     // CALLn (own window path)
    case 0x6: return -1;                                     // SI: J/BZ/BI0/ENTRY/LOOP (own path/imm)
    case 0x7: return MX2(s,t);                               // B: register-compare branches
    case 0x8: return MX2(t,s);                               // L32I.N
    case 0x9: return MX2(t,s);                               // S32I.N
    case 0xa: return MX3(r,s,t);                             // ADD.N
    case 0xb: return MX2(r,s);                               // ADDI.N (t imm)
    case 0xc: return s;                                      // MOVI.N/BEQZ.N (s)
    case 0xd: return (r == 0) ? MX2(s,t) : -1;               // MOV.N ; RET.N/RETW.N own path
    }
    #undef MX2
    #undef MX3
    return -1;
}

static void trap(xtensa_t *c, uint32_t insn, const char *why) {
    c->halted = 1;
    snprintf(c->stopmsg, sizeof c->stopmsg,
             "%s: insn=%06x at pc=%08x (cycle %llu)", why, insn, c->pc,
             (unsigned long long)c->cycles);
}

// sign-extend helpers
static inline int32_t sext(uint32_t v, int bits) {
    // Sign-extend a `bits`-wide field. MUST mask the upper bits of v first: HW SEXT
    // ignores bits above the sign position, and callers (e.g. `sext a,a,7`) often pass
    // a register still holding higher bits. Without the mask, (v^m)-m leaks them.
    if (bits < 32) v &= (1u << bits) - 1;
    uint32_t m = 1u << (bits - 1); return (int32_t)((v ^ m) - m);
}

// --- ROM leaf-function intercepts (faithful window model) ---
// A ROM API function is intercepted AT ITS ENTRY pc (before its own ENTRY runs), so in
// the faithful model the CALL has set PS.CALLINC and written the return address into the
// callee's a0 slot but has NOT rotated the window. The callee's a2 (its 1st arg / result
// register) therefore lives at slot (WindowBase+CALLINC)*4+2, and the return address at
// (WindowBase+CALLINC)*4 — NOT the current window's a0/a2. For a call0 entry (CALLINC 0)
// these collapse to the current window. Returning emulates the skipped ENTRY+body+RETW,
// whose net window rotation is zero, so WindowBase is left unchanged.
static uint32_t *rom_leaf_a2(xtensa_t *c) {
    int ci = (c->ps >> 16) & 3;
    return &c->ar[((c->windowbase + ci) * 4 + 2) & (NAREG - 1)];
}
static void rom_leaf_ret(xtensa_t *c, uint32_t pc) {
    int ci = (c->ps >> 16) & 3;
    if (ci) {   // windowed call: return through the callee's a0 slot, no rotation
        uint32_t ret = c->ar[((c->windowbase + ci) * 4) & (NAREG - 1)];
        c->ps &= ~(3u << 16);
        c->pc = (pc & 0xc0000000) | (ret & 0x3fffffff);
    } else {    // call0 entry: plain return through a0
        c->pc = c->ar[(c->windowbase * 4) & (NAREG - 1)];
    }
}

xtensa_t *g_cur_cpu;   // the core currently stepping (for per-core MMIO)
int cur_core(void) { return g_cur_cpu ? g_cur_cpu->core_id : 0; }

int xt_step(xtensa_t *c) {
    if (c->halted) return 0;
    // Null-PC guard: nothing legitimately executes in the first page (code lives at
    // 0x4000_0400+ / 0x4200_0000+). A jump to ~0 is always a wild/corrupted control
    // transfer; halt cleanly with the address instead of spinning forever on the
    // zero-word (ILL) it finds there, so the crash is diagnosable.
    if (c->pc < 0x1000u) { trap(c, 0, "NULL-PC (wild jump)"); return 0; }
    // Light-sleep wake: once execution returns to the app in flash (0x42xxxxxx) after a sleep
    // arm, the CPU resumed in place (light sleep), so disarm — only a deep-sleep `j .` in IRAM,
    // reached before returning to the app, models a reset. (Cheap: g_rtc_slp_armed is ~always 0.)
    if (g_rtc_slp_armed && c->pc >= 0x42000000u) g_rtc_slp_armed = 0;
    g_cur_cpu = c;
    // Interrupt delivery. The ESP32-S3 (Xtensa LX7) fixes each interrupt number to a
    // priority level (1..7); an interrupt is taken when it is enabled + pending and its
    // level exceeds the core's current level. PS.INTLEVEL sets the current level, and
    // PS.EXCM additionally masks everything up to EXCM_LEVEL (3) — so a level-4 source
    // (the FROM_CPU cross-core reschedule) can preempt even a level-3 spinlock section.
    // Level 1 vectors through the windowed user/kernel exception vector with
    // EXCCAUSE=LEVEL1INTERRUPT (and needs a valid stack); levels 2..6 have their own
    // dedicated high-priority vector and save PC/PS into EPC[level]/EPS[level].
    {
        static const uint8_t ilvl[32] = {   // XCHAL_INTn_LEVEL for esp32s3
            1,1,1,1,1,1,1,1, 1,1,1,3,1,1,7,3, 5,1,1,2,2,2,3,3, 4,4,5,3,4,3,4,5 };
        static const uint16_t vecofs[8] = { 0,0,0x180,0x1c0,0x200,0x240,0x280,0x280 };
        // Short-circuit when no interrupts are enabled (critical sections, early boot):
        // pend is 0 regardless, so skip the per-step mmio_cpu_int() matrix scan entirely.
        uint32_t pend = c->intenable ? ((c->interrupt | mmio_cpu_int(c->core_id)) & c->intenable) : 0;
        int cur = c->ps & 0xf;
        if ((c->ps & PS_EXCM) && cur < 3) cur = 3;      // EXCM masks up to EXCM_LEVEL
        if (pend) {
            int best = 0;
            for (int i = 0; i < 32; i++)
                if ((pend >> i) & 1) { int L = ilvl[i]; if (L > best) best = L; }
            if (best > cur && best <= 6) {
                if (best == 1) {
                    if (mem_is_ram(*AR(c,1))) {          // level-1 handler runs on the stack
                        c->waiti = 0; c->epc[1] = c->pc; c->exccause = 4; c->ps |= PS_EXCM;
                        c->pc = c->vecbase + ((c->ps & 0x20) ? 0x340 : 0x300);
                        c->cycles++; return 1;
                    }
                } else {                                 // high-priority interrupt (2..6)
                    c->waiti = 0; c->epc[best] = c->pc; c->eps[best] = c->ps;
                    c->ps = (c->ps & ~0xfu) | (uint32_t)best | PS_EXCM;
                    c->pc = c->vecbase + vecofs[best];
                    c->cycles++; return 1;
                }
            }
        }
    }
    uint32_t pc = c->pc;
    // Zero-overhead loop: reaching LEND with LCOUNT>0 branches back to LBEG — but ONLY
    // when execution FALLS THROUGH (or returns) to LEND. If the previous step was a taken
    // conditional branch that landed on LEND, that is the loop's EXIT test firing (e.g. the
    // ROM strlen loop ends with `bnone` whose branch target == fall-through == LEND); real
    // HW executes LEND then, it does not loop back. cbranch carries that one-step signal.
    int cb = c->cbranch; c->cbranch = 0;
    if (pc == c->lend && c->lcount != 0 && !cb) { c->lcount--; pc = c->lbeg; c->pc = pc; }
    // ROM analog-bus (regi2c) leaf intercepts: these ROM API functions dispatch
    // through the PHY-ROM data table at _dram_start_phyrom, which only the ROM's
    // own reset boot fills in. We enter at app entry, so that table is null and a
    // real call would jump through 0. The regi2c bus tunes the analog PLL/RF; it
    // exposes no digital state the firmware polls here, so we model the four public
    // leaves functionally (writes = no-op, reads = 0) and return like the ENTRY/RETW
    // pair would. This keeps the *binary* unchanged; we supply the missing HW behaviour.
    if (pc == 0x400357f8 || pc == 0x40035880 ||       // rom_i2c_readReg / writeReg
        pc == 0x4003589c || pc == 0x400358d8) {       // rom_i2c_readReg_Mask / writeReg_Mask
        *rom_leaf_a2(c) = 0;                          // read value / write returns 0
        rom_leaf_ret(c, pc);
        c->cycles++; return 1;
    }
    // ets_set_appcpu_boot_addr(entry): core 0 sets the APP CPU's boot address then
    // releases it from reset. We capture the entry (a2) and flag the launch; main.c
    // brings core 1 online at that address. (Thunk at 0x40000720 -> real func.)
    if (pc == 0x40000720 || pc == 0x40043664) {
        g_core1_entry = *rom_leaf_a2(c); g_core1_start_req = 1;   // entry arg in a2
        rom_leaf_ret(c, pc);
        c->cycles++; return 1;
    }
    // ets_delay_us busy-waits on CCOUNT for real-world microseconds. Emulated time
    // isn't wall-clock, so hardware-settling delays would burn millions of steps for
    // nothing; the delay has no observable digital effect. Return immediately.
    if (pc == 0x40041a68) {
        rom_leaf_ret(c, pc);
        c->cycles++; return 1;
    }
    // __divsf3 (ROM 0x40056124): the ESP32-S3 FPU has no hardware divide, so the ROM
    // computes a2/a3 with a Newton-Raphson reciprocal built from the FP division-support
    // micro-ops (div0.s/nexp01.s/maddn.s/divn.s/mkdadj.s...). Rather than bit-exactly
    // emulate that fragile micro-op algorithm, intercept the leaf and compute the IEEE-754
    // quotient on the host (args a2=dividend, a3=divisor bit patterns; result in a2).
    if (pc == 0x40056124) {
        int ci = (c->ps >> 16) & 3, b = (c->windowbase + ci) * 4;
        uint32_t ua = c->ar[(b+2) & (NAREG-1)], ub = c->ar[(b+3) & (NAREG-1)];
        float fa, fb, fq; memcpy(&fa,&ua,4); memcpy(&fb,&ub,4); fq = fa / fb;
        memcpy(&c->ar[(b+2) & (NAREG-1)], &fq, 4);
        rom_leaf_ret(c, pc);
        c->cycles++; return 1;
    }
    // Instruction fetch. Fast path: when the whole 2-3 byte insn lies inside one 64 KB
    // MMU page (so the backing bytes are contiguous), resolve the host pointer ONCE and
    // read the bytes directly — avoids 2-3 full mem_host range-check + MMU-translate calls
    // per instruction. Falls back to byte-wise mem_r8 at a page boundary (rare).
    uint8_t *ip = ((pc & 0xffffu) <= 0xfffdu) ? mem_host(pc) : NULL;
    uint8_t  b0 = ip ? ip[0] : mem_r8(pc);
    int      len = (b0 & 0x8) ? 2 : 3;
    uint32_t insn = ip ? (b0 | ((uint32_t)ip[1] << 8) | (len == 3 ? ((uint32_t)ip[2] << 16) : 0))
                       : (b0 | (mem_r8(pc + 1) << 8) | (len == 3 ? (mem_r8(pc + 2) << 16) : 0));
    uint32_t npc = pc + len;

    int op0 = insn & 0xf;
    int t   = (insn >> 4)  & 0xf;
    int s   = (insn >> 8)  & 0xf;
    int r   = (insn >> 12) & 0xf;
    int op1 = (insn >> 16) & 0xf;
    int op2 = (insn >> 20) & 0xf;
    uint32_t imm8  = (insn >> 16) & 0xff;
    uint32_t imm12 = (insn >> 12) & 0xfff;
    uint32_t imm16 = (insn >> 8)  & 0xffff;

    // Window overflow on register access: touching a[i] where i >= 4*win_avail would
    // clobber a live older window (they overlap in the circular AR file), so spill it
    // first — faithful to HW and the mechanism behind SPILL_ALL_WINDOWS. Gated on WOE
    // (window exceptions enabled) and skipped for control ops (maxreg<0), which manage
    // the window themselves.
    if (WIN_EXC(c)) {
        int mr = insn_maxreg(insn);
        if (mr >= 4 * win_avail(c)) { win_over(c, pc, win_avail(c)); c->cycles++; return 1; }
    }

    switch (op0) {
    case 0x0: // QRST
        if (op1 == 4 || op1 == 5) {   // EXTUI at(=r), as(=t), shiftimm, maskimm
            int sa = ((op1 & 1) << 4) | s;
            int mw = op2 + 1;
            uint32_t mask = (mw >= 32) ? 0xffffffffu : ((1u << mw) - 1);
            A(r) = (A(t) >> sa) & mask;   // dest=r, source=t (was swapped; hidden while r==t)
            c->pc = npc; c->cycles++; return 1;
        }
        switch (op1) {
        case 0x0: // RST0
            switch (op2) {
            case 0x0: // ST0
                if (r == 0) {              // SNM0: CALLX / RET / JX
                    int m = (t >> 2) & 3, n = t & 3;
                    if (m == 3) {          // CALLX0/4/8/12
                        uint32_t target = A(s);
                        if (n == 0) { A(0) = npc; c->pc = target; c->cycles++; return 1; } // CALLX0
                        // Faithful HW: DO NOT rotate. Record the return address in the
                        // callee window's a0 slot and set PS.CALLINC; the callee's ENTRY
                        // performs the rotation and sets the SP. Guard the a0-slot write
                        // against clobbering a still-live older window (spill first).
                        if (WIN_EXC(c) && win_avail(c) <= n + 3) { win_over(c, pc, win_avail(c)); c->cycles++; return 1; }
                        c->ar[((c->windowbase + n) * 4) & (NAREG - 1)] =
                            (npc & 0x3fffffff) | ((uint32_t)n << 30);
                        c->ps = (c->ps & ~(3u << 16)) | ((uint32_t)n << 16);   // PS.CALLINC = n
                        c->pc = target; c->cycles++; return 1;
                    } else if (m == 2) {   // RET (n=0) / RETW (n=1) / JX (n=2)
                        if (n == 2) { c->pc = A(s); c->cycles++; return 1; }   // JX
                        if (n == 0) { c->pc = A(0); c->cycles++; return 1; }   // RET
                        uint32_t a0 = A(0); int dec = (a0 >> 30) & 3;          // RETW
                        int twb = (c->windowbase - dec) & 15;
                        if (WIN_EXC(c) && !(c->windowstart & (1u << twb))) { win_under(c, pc, dec); c->cycles++; return 1; }
                        c->windowstart &= ~(1u << c->windowbase);
                        c->windowbase = twb;
                        c->pc = (pc & 0xc0000000) | (a0 & 0x3fffffff);
                        c->cycles++; return 1;
                    }
                    if (insn == 0) {   // ILL (0x000000): raise IllegalInstructionCause (0)
                        // through the windowed user/kernel exception vector — exactly as HW,
                        // so e.g. panic_abort()'s deliberate `ill` runs the real panic handler
                        // instead of silently halting the core. EPC1 = the faulting pc.
                        c->epc[1] = c->pc; c->exccause = 0; c->ps |= PS_EXCM;
                        c->pc = c->vecbase + ((c->ps & 0x20) ? 0x340 : 0x300);
                        c->cycles++; return 1;
                    }
                    trap(c, insn, "SNM0?"); return 0;
                }
                if (r == 1) { /* MOVSP at, as: move the stack pointer, register-window aware */
                    // Moving SP must CARRY the current frame's 16-byte base save area (the
                    // caller's spilled a0..a3, at [SP-16..SP-4]) so it follows the new SP.
                    // HW raises the Alloca exception to do this when the caller window is
                    // spilled; we just perform the copy unconditionally (observably identical:
                    // if the caller is live the copied bytes are stale but get overwritten by
                    // its own later overflow, which now targets the new [SP-16]). Without this
                    // a mid-function alloca (`sub aX,a1,size; movsp a1,aX`, as in the I2C
                    // driver's VLA) left the caller's saved SP behind, so its later underflow
                    // reloaded a stale/garbage slot -> SP=1 -> NULL-PC in the touch task.
                    uint32_t oldsp = A(t), newsp = A(s);
                    uint32_t w0=mem_r32(oldsp-16), w1=mem_r32(oldsp-12), w2=mem_r32(oldsp-8), w3=mem_r32(oldsp-4);
                    mem_w32(newsp-16,w0); mem_w32(newsp-12,w1); mem_w32(newsp-8,w2); mem_w32(newsp-4,w3);
                    A(t) = newsp; c->pc = npc; c->cycles++; return 1; }
                if (r == 2) { /* SYNC group: ISYNC/RSYNC/ESYNC/DSYNC/MEMW/EXTW/NOP */
                    c->pc = npc; c->cycles++; return 1; }
                if (r == 3) { // RFI (t=1) / RFE/RFUE/RFDE (s=0..2) / RFWO (s=4) / RFWU (s=5)
                    if (t == 1) { uint32_t lvl = s & 7;                          // RFI level=s
                                  c->ps = c->eps[lvl]; c->pc = c->epc[lvl];      // restore from EPS/EPC[level]
                                  c->cycles++; return 1; }
                    if (s == 4) { c->windowstart &= ~(1u << c->windowbase);      // RFWO
                                  c->windowbase = ps_owb(c->ps); c->ps &= ~PS_EXCM;
                                  c->pc = c->epc[1]; c->cycles++; return 1; }
                    if (s == 5) { c->windowstart |= (1u << c->windowbase);       // RFWU
                                  if (g_winv > 0) { int rw = c->windowbase;
                                      uint32_t rsp = c->ar[(rw*4+1)&(NAREG-1)];
                                      int garbage = (rsp < 0x3fc80000u || rsp >= 0x3fd00000u); // not a valid DRAM SP
                                      if (g_pending[rw] && g_spill_sp[rw] && rsp != g_spill_sp[rw] && garbage)
                                          fprintf(stderr, "WINV-BADSP: win%d reloaded SP=%08x != spilled SP=%08x (a0=%08x) ws=%04x cyc=%llu\n",
                                                  rw, rsp, g_spill_sp[rw], c->ar[(rw*4)&(NAREG-1)], c->windowstart & 0xffff,
                                                  (unsigned long long)c->cycles);
                                      g_pending[rw] = 0; }
                                  c->windowbase = ps_owb(c->ps); c->ps &= ~PS_EXCM;
                                  c->pc = c->epc[1]; c->cycles++; return 1; }
                    c->ps &= ~PS_EXCM; c->pc = c->epc[1]; c->cycles++; return 1; // RFE
                }
                if (r == 6) { A(t) = c->ps; c->ps = (c->ps & ~0xfu) | s; c->pc = npc; c->cycles++; return 1; } // RSIL
                if (r == 7) { c->ps = (c->ps & ~0xfu) | s; c->waiti = 1; c->pc = npc; c->cycles++; return 1; } // WAITI s: set INTLEVEL, idle until an interrupt above it preempts
                // Boolean reductions: BR[t] = AND/OR of a 4- or 8-bool group based at s.
                if (r == 8)  { int b=s&~3; int v=((c->br>>b)&0xF)!=0;    c->br=(c->br&~(1u<<t))|((uint32_t)v<<t); c->pc=npc; c->cycles++; return 1; } // ANY4
                if (r == 9)  { int b=s&~3; int v=((c->br>>b)&0xF)==0xF;  c->br=(c->br&~(1u<<t))|((uint32_t)v<<t); c->pc=npc; c->cycles++; return 1; } // ALL4
                if (r == 10) { int b=s&~7; int v=((c->br>>b)&0xFF)!=0;   c->br=(c->br&~(1u<<t))|((uint32_t)v<<t); c->pc=npc; c->cycles++; return 1; } // ANY8
                if (r == 11) { int b=s&~7; int v=((c->br>>b)&0xFF)==0xFF;c->br=(c->br&~(1u<<t))|((uint32_t)v<<t); c->pc=npc; c->cycles++; return 1; } // ALL8
                trap(c, insn, "ST0?"); return 0;
            case 0x1: A(r) = A(s) & A(t); c->pc = npc; c->cycles++; return 1; // AND
            case 0x2: A(r) = A(s) | A(t); c->pc = npc; c->cycles++; return 1; // OR
            case 0x3: A(r) = A(s) ^ A(t); c->pc = npc; c->cycles++; return 1; // XOR
            case 0x4: // ST1: SAR setters
                switch (r) {
                case 0x0: c->sar = A(s) & 0x1f; break;            // SSR
                case 0x1: c->sar = 32 - (A(s) & 0x1f); break;     // SSL
                case 0x2: c->sar = (A(s) & 3) << 3; break;        // SSA8L
                case 0x3: c->sar = 32 - ((A(s) & 3) << 3); break; // SSA8B
                case 0x4: c->sar = ((s & 0xf) | ((t & 1) << 4)) & 0x1f; break; // SSAI sa: sa[3:0]=s, sa[4]=t
                case 0x8: { int imm = (t & 8) ? (t - 16) : t;     // ROTW imm4 (privileged): rotate the
                    c->windowbase = (c->windowbase + imm) & 15; } break; // register window by imm4 (leaves WindowStart)
                case 0xe: { int32_t v=A(s); A(t) = (v==0||v==-1)?31:((v<0?__builtin_clz(~(uint32_t)v):__builtin_clz(v))-1); break; } // NSA
                case 0xf: A(t) = A(s) ? (uint32_t)__builtin_clz(A(s)) : 32; break; // NSAU
                default: break;
                }
                c->pc = npc; c->cycles++; return 1;
            case 0x5: case 0x7: // TLB group: [RWIP]ITLB / [RWIP]DTLB
                // We map memory directly (no MMU/TLB), so writes/invalidates are
                // no-ops; probes/reads (RITLB/RDTLB/PITLB/PDTLB, odd r) return an
                // identity translation of the virtual address so lookups "hit".
                if (r == 3 || r == 5 || r == 7 || r == 0xb || r == 0xd || r == 0xf) A(t) = A(s);
                c->pc = npc; c->cycles++; return 1;
            case 0x6: // RT0: NEG / ABS
                if (s == 0) A(r) = (uint32_t)(-(int32_t)A(t));
                else        A(r) = (int32_t)A(t) < 0 ? -(int32_t)A(t) : A(t);
                c->pc = npc; c->cycles++; return 1;
            case 0x8: A(r) = A(s) + A(t); c->pc = npc; c->cycles++; return 1; // ADD
            case 0x9: A(r) = (A(s) << 1) + A(t); c->pc = npc; c->cycles++; return 1; // ADDX2
            case 0xa: A(r) = (A(s) << 2) + A(t); c->pc = npc; c->cycles++; return 1; // ADDX4
            case 0xb: A(r) = (A(s) << 3) + A(t); c->pc = npc; c->cycles++; return 1; // ADDX8
            case 0xc: A(r) = A(s) - A(t); c->pc = npc; c->cycles++; return 1; // SUB
            case 0xd: A(r) = (A(s) << 1) - A(t); c->pc = npc; c->cycles++; return 1; // SUBX2
            case 0xe: A(r) = (A(s) << 2) - A(t); c->pc = npc; c->cycles++; return 1; // SUBX4
            case 0xf: A(r) = (A(s) << 3) - A(t); c->pc = npc; c->cycles++; return 1; // SUBX8
            }
            trap(c, insn, "RST0?"); return 0;
        case 0x1: // RST1: immediate + SAR shifts
            switch (op2) {
            case 0x0: case 0x1: { int sa=((op2&1)<<4)|t, sh=32-sa;   // SLLI
                A(r) = (sh>=32)?0:(A(s)<<sh); c->pc=npc; c->cycles++; return 1; }
            case 0x2: case 0x3: { int sa=((op2&1)<<4)|s;             // SRAI: dest=r, src=t, sa=(op2.0<<4)|s
                A(r) = (int32_t)A(t) >> (sa&31); c->pc=npc; c->cycles++; return 1; }
            case 0x4: { int sa=s; A(r) = A(t) >> (sa&31); c->pc=npc; c->cycles++; return 1; } // SRLI
            case 0x6: { // XSR.sr at : exchange A(t) with special register sr=(r<<4)|s
                int sr = (r<<4)|s;
                uint32_t old = (sr==0xea)?(uint32_t)c->cycles : (sr==0xeb)?(c->core_id?0xABABu:0xCDCDu)
                             : (sr==0xe6)?c->ps
                             : (sr==0xe4)?c->intenable                      // INTENABLE (xt_ints_off/on)
                             : (sr==0xe2)?(c->interrupt | mmio_cpu_int(c->core_id))  // INTERRUPT
                             : (sr==0xe8)?c->exccause : (sr==0xee)?c->excvaddr
                             : (sr==0x03)?c->sar : (sr==0xe7)?c->vecbase
                             : (sr==0x48)?c->windowbase : (sr==0x49)?c->windowstart
                             : (sr>=0xb1&&sr<=0xb7)?c->epc[sr-0xb0] : (sr>=0xc2&&sr<=0xc7)?c->eps[sr-0xc0]
                             : (sr>=0xd1&&sr<=0xd7)?c->excsave[sr-0xd0] : c->sr[sr];
                uint32_t nv = A(t); c->sr[sr]=nv;
                if(sr==0x48)c->windowbase=nv&15; else if(sr==0x49)c->windowstart=nv&0xffff;
                else if(sr==0xe6)c->ps=nv; else if(sr==0xe7)c->vecbase=nv; else if(sr==0x03)c->sar=nv&0x3f;
                else if(sr==0xe4)c->intenable=nv;                          // INTENABLE
                else if(sr==0xe8)c->exccause=nv; else if(sr==0xee)c->excvaddr=nv;
                else if(sr==0x00)c->lbeg=nv; else if(sr==0x01)c->lend=nv; else if(sr==0x02)c->lcount=nv;
                else if(sr>=0xb1&&sr<=0xb7)c->epc[sr-0xb0]=nv; else if(sr>=0xc2&&sr<=0xc7)c->eps[sr-0xc0]=nv;
                else if(sr>=0xd1&&sr<=0xd7)c->excsave[sr-0xd0]=nv;
                A(t)=old; c->pc=npc; c->cycles++; return 1; }
            case 0x8: { uint64_t v=((uint64_t)A(s)<<32)|A(t);        // SRC (funnel)
                A(r) = (uint32_t)(v >> (c->sar & 0x3f)); c->pc=npc; c->cycles++; return 1; }
            case 0x9: A(r) = A(t) >> (c->sar & 0x1f); c->pc=npc; c->cycles++; return 1; // SRL
            case 0xa: { int sh=32-(int)(c->sar&0x3f);                // SLL
                A(r) = (sh<=0)?A(s):(sh>=32)?0:(A(s)<<sh); c->pc=npc; c->cycles++; return 1; }
            case 0xb: A(r) = (int32_t)A(t) >> (c->sar & 0x1f); c->pc=npc; c->cycles++; return 1; // SRA
            case 0xc: A(r) = (A(s) & 0xffff) * (A(t) & 0xffff); c->pc=npc; c->cycles++; return 1; // MUL16U
            case 0xd: A(r) = (uint32_t)((int32_t)(int16_t)A(s) * (int32_t)(int16_t)A(t)); c->pc=npc; c->cycles++; return 1; // MUL16S
            }
            trap(c, insn, "RST1?"); return 0;
        case 0x2: // RST2: boolean ops / SALT / MUL / DIV / REM
            switch (op2) {
            // Boolean-register logic (BR[r] = BR[s] OP BR[t]); bs/bt are single bits.
            #define BRBIT(x) ((c->br>>(x))&1)
            #define BRSET(v) do{ c->br=(c->br&~(1u<<r))|((uint32_t)((v)&1)<<r); }while(0)
            case 0x0: BRSET(BRBIT(s) & BRBIT(t));      c->pc=npc; c->cycles++; return 1; // ANDB
            case 0x1: BRSET(BRBIT(s) & (BRBIT(t)^1));  c->pc=npc; c->cycles++; return 1; // ANDBC
            case 0x2: BRSET(BRBIT(s) | BRBIT(t));      c->pc=npc; c->cycles++; return 1; // ORB
            case 0x3: BRSET(BRBIT(s) | (BRBIT(t)^1));  c->pc=npc; c->cycles++; return 1; // ORBC
            case 0x4: BRSET(BRBIT(s) ^ BRBIT(t));      c->pc=npc; c->cycles++; return 1; // XORB
            case 0x6: A(r) = (A(s) < A(t)) ? 1 : 0; c->pc=npc; c->cycles++; return 1; // SALTU
            case 0x7: A(r) = ((int32_t)A(s) < (int32_t)A(t)) ? 1 : 0; c->pc=npc; c->cycles++; return 1; // SALT
            case 0x8: A(r) = A(s) * A(t); c->pc=npc; c->cycles++; return 1; // MULL
            case 0xa: A(r) = (uint32_t)(((uint64_t)A(s) * (uint64_t)A(t)) >> 32); c->pc=npc; c->cycles++; return 1; // MULUH
            case 0xb: A(r) = (uint32_t)(((int64_t)(int32_t)A(s) * (int64_t)(int32_t)A(t)) >> 32); c->pc=npc; c->cycles++; return 1; // MULSH
            case 0xc: A(r) = A(t) ? A(s) / A(t) : 0; c->pc=npc; c->cycles++; return 1; // QUOU
            case 0xd: A(r) = A(t) ? (uint32_t)((int32_t)A(s) / (int32_t)A(t)) : 0; c->pc=npc; c->cycles++; return 1; // QUOS
            case 0xe: A(r) = A(t) ? A(s) % A(t) : 0; c->pc=npc; c->cycles++; return 1; // REMU
            case 0xf: A(r) = A(t) ? (uint32_t)((int32_t)A(s) % (int32_t)A(t)) : 0; c->pc=npc; c->cycles++; return 1; // REMS
            }
            trap(c, insn, "RST2?"); return 0;
        case 0x3: { // RST3
            int sr = (r << 4) | s;
            switch (op2) {
            case 0x0: // RSR: special regs share dedicated storage with the exception logic
                A(t) = (sr==0xea) ? (uint32_t)c->cycles
                     : (sr==0xeb) ? (c->core_id ? 0xABABu : 0xCDCDu)
                     : (sr==0xe6) ? c->ps                            // PS
                     : (sr==0xe4) ? c->intenable                     // INTENABLE
                     : (sr==0xe2) ? (c->interrupt | mmio_cpu_int(c->core_id))  // INTERRUPT (sw|hw)
                     : (sr==0xe8) ? c->exccause : (sr==0xee) ? c->excvaddr
                     : (sr==0x03) ? c->sar : (sr==0x04) ? c->br : (sr==0xe7) ? c->vecbase
                     : (sr==0x00) ? c->lbeg : (sr==0x01) ? c->lend : (sr==0x02) ? c->lcount
                     : (sr>=0xb1&&sr<=0xb7) ? c->epc[sr-0xb0]        // EPC1..7
                     : (sr>=0xc2&&sr<=0xc7) ? c->eps[sr-0xc0]        // EPS2..7
                     : (sr>=0xd1&&sr<=0xd7) ? c->excsave[sr-0xd0]    // EXCSAVE1..7
                     : (sr==0x48) ? c->windowbase                    // WINDOWBASE
                     : (sr==0x49) ? c->windowstart                   // WINDOWSTART
                     : c->sr[sr];
                c->pc=npc; c->cycles++; return 1;
            case 0x1: c->sr[sr]=A(t); if(sr==0xe7)c->vecbase=A(t); if(sr==0x03)c->sar=A(t)&0x3f;
                      if(sr==0x04)c->br=A(t);                          // BR (boolean regfile)
                      if(sr==0xe6)c->ps=A(t);                           // PS
                      if(sr==0xe4)c->intenable=A(t);                    // INTENABLE
                      if(sr==0xe2)c->interrupt|=A(t);                   // INTSET
                      if(sr==0xe3)c->interrupt&=~A(t);                  // INTCLEAR
                      if(sr==0xe8)c->exccause=A(t);
                      if(sr==0x00)c->lbeg=A(t); if(sr==0x01)c->lend=A(t); if(sr==0x02)c->lcount=A(t);
                      if(sr>=0xb1&&sr<=0xb7)c->epc[sr-0xb0]=A(t);       // EPC1..7
                      if(sr>=0xc2&&sr<=0xc7)c->eps[sr-0xc0]=A(t);       // EPS2..7
                      if(sr>=0xd1&&sr<=0xd7)c->excsave[sr-0xd0]=A(t);   // EXCSAVE1..7
                      if(sr==0x48)c->windowbase=A(t)&15;                 // WINDOWBASE
                      if(sr==0x49)c->windowstart=A(t)&0xffff;            // WINDOWSTART
                      c->pc=npc; c->cycles++; return 1; // WSR
            case 0x2: { int b = t + 7; A(r) = (uint32_t)sext(A(s), b + 1); c->pc=npc; c->cycles++; return 1; } // SEXT (dest=r, src=s, imm=t)
            case 0x3: { int b = t + 7; int32_t v=(int32_t)A(s), hi=(int32_t)((1u<<b)-1), lo=-(int32_t)(1u<<b); // CLAMPS
                        A(r) = (uint32_t)(v>hi?hi : v<lo?lo : v); c->pc=npc; c->cycles++; return 1; }
            case 0x4: A(r) = (int32_t)A(s) < (int32_t)A(t) ? A(s):A(t); c->pc=npc; c->cycles++; return 1; // MIN
            case 0x5: A(r) = (int32_t)A(s) > (int32_t)A(t) ? A(s):A(t); c->pc=npc; c->cycles++; return 1; // MAX
            case 0x6: A(r) = A(s) < A(t) ? A(s):A(t); c->pc=npc; c->cycles++; return 1; // MINU
            case 0x7: A(r) = A(s) > A(t) ? A(s):A(t); c->pc=npc; c->cycles++; return 1; // MAXU
            case 0x8: if (A(t)==0) A(r)=A(s); c->pc=npc; c->cycles++; return 1; // MOVEQZ
            case 0x9: if (A(t)!=0) A(r)=A(s); c->pc=npc; c->cycles++; return 1; // MOVNEZ
            case 0xa: if ((int32_t)A(t)<0)  A(r)=A(s); c->pc=npc; c->cycles++; return 1; // MOVLTZ
            case 0xb: if ((int32_t)A(t)>=0) A(r)=A(s); c->pc=npc; c->cycles++; return 1; // MOVGEZ
            case 0xc: if (!((c->br>>t)&1)) A(r)=A(s); c->pc=npc; c->cycles++; return 1; // MOVF  (BR[t]==0)
            case 0xd: if (  (c->br>>t)&1 ) A(r)=A(s); c->pc=npc; c->cycles++; return 1; // MOVT  (BR[t]==1)
            case 0xe: A(r) = c->ur[(s << 4) | t]; c->pc=npc; c->cycles++; return 1;       // RUR (THREADPTR etc.)
            case 0xf: c->ur[(r << 4) | s] = A(t); c->pc=npc; c->cycles++; return 1;       // WUR
            }
            trap(c, insn, "RST3?"); return 0;
        }
        case 0x9: { // L32E (op2=0) / S32E (op2=4)  offset = (r<<2)-64
            int32_t off = ((int32_t)r << 2) - 64;
            uint32_t ea = A(s) + off;
            if (op2 == 0) A(t) = mem_r32(ea);
            else if (op2 == 4) mem_w32(ea, A(t));
            else { trap(c, insn, "op1=9?"); return 0; }
            c->pc = npc; c->cycles++; return 1;
        }
        case 0x8: { // LSCX: indexed single-precision FP load/store. frr(float), ars(base), art(index)
            uint32_t ea = A(s) + A(t);                            // base=s, index=t (NOT r)
            switch (op2) {
            case 0x0: c->fr[r] = mem_r32(ea);            break;   // LSX  (float reg = r)
            case 0x4: mem_w32(ea, c->fr[r]);             break;   // SSX
            case 0x8: c->fr[r] = mem_r32(ea); A(s) = ea; break;   // LSXU (update base)
            case 0xc: mem_w32(ea, c->fr[r]);  A(s) = ea; break;   // SSXU
            default: trap(c, insn, "LSCX?"); return 0;
            }
            c->pc = npc; c->cycles++; return 1;
        }
        case 0xa: { // FP0: single-precision arithmetic + int<->float conversions
            switch (op2) {
            case 0x0: fr_set(c,r, fr_get(c,s) + fr_get(c,t)); break;                 // ADD.S
            case 0x1: fr_set(c,r, fr_get(c,s) - fr_get(c,t)); break;                 // SUB.S
            case 0x2: fr_set(c,r, fr_get(c,s) * fr_get(c,t)); break;                 // MUL.S
            case 0x4: fr_set(c,r, fr_get(c,r) + fr_get(c,s) * fr_get(c,t)); break;   // MADD.S
            case 0x5: fr_set(c,r, fr_get(c,r) - fr_get(c,s) * fr_get(c,t)); break;   // MSUB.S
            case 0x6: break;   // MADDN.S - div/sqrt-sequence helper (see FP1OP note); NOP
            case 0x7: break;   // DIVN.S  - div/sqrt-sequence helper; NOP
            case 0x8: A(r) = xt_f2i(rintf(fr_get(c,s)  * (float)(1u<<t))); break;   // ROUND.S (nearest-even)
            case 0x9: A(r) = xt_f2i(truncf(fr_get(c,s) * (float)(1u<<t))); break;   // TRUNC.S (toward zero)
            case 0xa: A(r) = xt_f2i(floorf(fr_get(c,s) * (float)(1u<<t))); break;   // FLOOR.S
            case 0xb: A(r) = xt_f2i(ceilf(fr_get(c,s)  * (float)(1u<<t))); break;   // CEIL.S
            case 0xc: fr_set(c,r, (float)(int32_t)A(s) / (float)(1u<<t)); break;     // FLOAT.S
            case 0xd: fr_set(c,r, (float)(uint32_t)A(s) / (float)(1u<<t)); break;    // UFLOAT.S
            case 0xe: A(r) = xt_f2u(truncf(fr_get(c,s) * (float)(1u<<t))); break;   // UTRUNC.S
            case 0xf:                                                               // FP1OP (by t)
                switch (t) {
                case 0x0: c->fr[r] = c->fr[s]; break;                               // MOV.S
                case 0x1: fr_set(c,r, fabsf(fr_get(c,s))); break;                   // ABS.S
                case 0x4: A(r) = c->fr[s]; break;                                   // RFR (FR->AR bits)
                case 0x5: c->fr[r] = A(s); break;                                   // WFR (AR->FR bits)
                case 0x6: fr_set(c,r, -fr_get(c,s)); break;                         // NEG.S
                // FP div/sqrt acceleration family. The ESP32-S3 FPU has no direct
                // SQRT.S/DIV.S; newlib's __ieee754_sqrtf runs a fixed ~26-insn
                // reciprocal-sqrt sequence built from these helpers. Following the
                // QEMU model: MKSADJ does the real sqrt, ADDEXPM moves it to the
                // result, CONST loads a small table, and SQRT0/DIV0/NEXP01/ADDEXP
                // (+ MADDN/DIVN above) are NOPs - the fixed sequence resolves
                // correctly because the true value flows MKSADJ->ADDEXPM->result.
                case 0x3: { static const uint32_t cv[4]={0x00000000,0x3f800000,     // CONST.S
                            0x40000000,0x3f000000}; c->fr[r]=cv[s&3]; } break;
                case 0x9: break;                                                    // SQRT0.S  (NOP)
                case 0xb: break;                                                    // NEXP01.S (NOP)
                case 0xc: fr_set(c,r, sqrtf(fr_get(c,s))); break;                   // MKSADJ.S = sqrt
                case 0xe: break;                                                    // ADDEXP.S (NOP)
                case 0xf: c->fr[r] = c->fr[s]; break;                               // ADDEXPM.S = MOV
                default: trap(c, insn, "FP1OP?"); return 0;
                }
                break;
            default: trap(c, insn, "FP0?"); return 0;
            }
            c->pc = npc; c->cycles++; return 1;
        }
        case 0xb: { // FP1: compares (set BR[r]) + conditional moves
            float fs = fr_get(c,s), ft = fr_get(c,t);
            int un = isnan(fs) || isnan(ft), bit;
            switch (op2) {
            case 0x1: bit = un;                    goto setbr;   // UN.S
            case 0x2: bit = !un && fs == ft;       goto setbr;   // OEQ.S
            case 0x3: bit =  un || fs == ft;       goto setbr;   // UEQ.S
            case 0x4: bit = !un && fs <  ft;       goto setbr;   // OLT.S
            case 0x5: bit =  un || fs <  ft;       goto setbr;   // ULT.S
            case 0x6: bit = !un && fs <= ft;       goto setbr;   // OLE.S
            case 0x7: bit =  un || fs <= ft;       goto setbr;   // ULE.S
            setbr:    c->br = (c->br & ~(1u<<r)) | ((uint32_t)(bit&1) << r); break;
            case 0x8: if (A(t) == 0)          c->fr[r] = c->fr[s]; break;   // MOVEQZ.S
            case 0x9: if (A(t) != 0)          c->fr[r] = c->fr[s]; break;   // MOVNEZ.S
            case 0xa: if ((int32_t)A(t) < 0)  c->fr[r] = c->fr[s]; break;   // MOVLTZ.S
            case 0xb: if ((int32_t)A(t) >= 0) c->fr[r] = c->fr[s]; break;   // MOVGEZ.S
            case 0xc: if (!((c->br>>t)&1))    c->fr[r] = c->fr[s]; break;   // MOVF.S
            case 0xd: if (  (c->br>>t)&1 )    c->fr[r] = c->fr[s]; break;   // MOVT.S
            default: trap(c, insn, "FP1?"); return 0;
            }
            c->pc = npc; c->cycles++; return 1;
        }
        }
        trap(c, insn, "QRST?"); return 0;

    case 0x1: { // L32R  (a[t] = mem[ (pc & ~3) + (imm16<<2) - 0x40000 ]) negative
        int32_t off = ((int32_t)imm16 - 0x10000) << 2;   // 16-bit signed, word
        uint32_t ea = ((pc + 3) & ~3u) + off;
        A(t) = mem_r32(ea);
        c->pc = npc; c->cycles++; return 1;
    }
    case 0x2: { // LSAI: loads/stores with imm8 (scaled)
        uint32_t base = A(s);
        switch (r) {
        case 0x0: A(t) = mem_r8 (base + imm8);        c->pc=npc; c->cycles++; return 1; // L8UI
        case 0x1: A(t) = mem_r16(base + (imm8<<1));   c->pc=npc; c->cycles++; return 1; // L16UI
        case 0x2: A(t) = mem_r32(base + (imm8<<2));   c->pc=npc; c->cycles++; return 1; // L32I
        case 0x4: mem_w8 (base + imm8,       A(t));   c->pc=npc; c->cycles++; return 1; // S8I
        case 0x5: mem_w16(base + (imm8<<1),  A(t));   c->pc=npc; c->cycles++; return 1; // S16I
        case 0x6: mem_w32(base + (imm8<<2),  A(t));   c->pc=npc; c->cycles++; return 1; // S32I
        case 0x9: A(t) = (int32_t)(int16_t)mem_r16(base+(imm8<<1)); c->pc=npc; c->cycles++; return 1; // L16SI
        case 0xa: { // MOVI a[t]=sext(imm12); imm12 = (s<<8)|imm8, s = bits[11:8] = insn&0xf00
            A(t) = (uint32_t)sext((insn & 0xf00) | imm8, 12); c->pc=npc; c->cycles++; return 1; }
        case 0xb: A(t) = A(s) + (uint32_t)sext((insn & 0xf00)|imm8, 12); c->pc=npc; c->cycles++; return 1; // (reserved)
        case 0xc: A(t) = A(s) + (uint32_t)(int32_t)(int8_t)imm8;  c->pc=npc; c->cycles++; return 1; // ADDI.N? (ADDI)
        case 0xd: A(t) = A(s) + ((uint32_t)(int32_t)(int8_t)imm8 << 8); c->pc=npc; c->cycles++; return 1; // ADDMI
        case 0xe: { // S32C1I  atomic compare-and-swap (vs SCOMPARE1 = SR 0x0c)
            uint32_t addr = base + (imm8 << 2), old = mem_r32(addr);
            if (old == c->sr[0x0c]) mem_w32(addr, A(t));
            A(t) = old; c->pc=npc; c->cycles++; return 1;
        }
        case 0xf: c->pc=npc; c->cycles++; return 1; // S32RI/cache ops - treat as nop-ish
        }
        trap(c, insn, "LSAI?"); return 0;
    }
    case 0x3: { // LSCI: single-precision FP load/store, imm8 scaled. frt, ars(base), imm8<<2
        uint32_t ea = A(s) + (imm8 << 2);
        switch (r) {
        case 0x0: c->fr[t] = mem_r32(ea);            break;   // LSI
        case 0x4: mem_w32(ea, c->fr[t]);             break;   // SSI
        case 0x8: c->fr[t] = mem_r32(ea); A(s) = ea; break;   // LSIU (update base)
        case 0xc: mem_w32(ea, c->fr[t]);  A(s) = ea; break;   // SSIU
        default: trap(c, insn, "LSCI?"); return 0;
        }
        c->pc = npc; c->cycles++; return 1;
    }
    case 0x4: { // MAC16: 16x16->40-bit multiply/accumulate into ACC (SR16/17), MR m0-m3 (SR32-35)
        // op2 picks the operand pattern & optional load; op1[3:2]=UMUL/MUL/MULA/MULS,
        // op1[0]=half of x, op1[1]=half of y. MR x in {m0,m1}=bit14, MR y in {m2,m3}=bit6.
        #define MR(i) c->sr[0x20 + ((i) & 3)]
        int half_x = op1 & 1, half_y = (op1 >> 1) & 1, oper = (op1 >> 2) & 3;
        int mx = (insn >> 14) & 1;          // x MR index (m0/m1)
        int my = 2 | ((insn >> 6) & 1);     // y MR index (m2/m3)
        int is_load = 0, load_dec = 0, has_mul = 1;
        uint32_t xv = 0, yv = 0;
        switch (op2) {
        case 0x0: is_load=1;             xv=MR(mx); yv=MR(my); break;      // MULA.DD.*.LDINC
        case 0x1: is_load=1; load_dec=1; xv=MR(mx); yv=MR(my); break;      // MULA.DD.*.LDDEC
        case 0x2:                        xv=MR(mx); yv=MR(my); break;      // MUL/MULA/MULS.DD
        case 0x3:                        xv=A(s);   yv=MR(my); break;      // .AD  (x=AR[s], y=MR)
        case 0x4: is_load=1;             xv=MR(mx); yv=A(t);   break;      // MULA.DA.*.LDINC
        case 0x5: is_load=1; load_dec=1; xv=MR(mx); yv=A(t);   break;      // MULA.DA.*.LDDEC
        case 0x6:                        xv=MR(mx); yv=A(t);   break;      // .DA  (x=MR, y=AR[t])
        case 0x7:                        xv=A(s);   yv=A(t);   break;      // .AA  (both AR)
        case 0x8: has_mul=0; is_load=1;             break;                 // LDINC
        case 0x9: has_mul=0; is_load=1; load_dec=1; break;                 // LDDEC
        default: trap(c, insn, "MAC16?"); return 0;
        }
        if (has_mul) {
            int32_t xh = half_x ? (int16_t)(xv >> 16) : (int16_t)(xv & 0xffff);
            int32_t yh = half_y ? (int16_t)(yv >> 16) : (int16_t)(yv & 0xffff);
            int64_t prod;
            if (oper == 0) prod = (int64_t)(uint32_t)((uint16_t)(half_x?xv>>16:xv) * (uint32_t)(uint16_t)(half_y?yv>>16:yv)); // UMUL (unsigned)
            else           prod = (int64_t)xh * (int64_t)yh;                                                                // signed
            int64_t acc = ((int64_t)(int8_t)c->sr[0x11] << 32) | (uint32_t)c->sr[0x10];   // 40-bit signed ACC
            if      (oper == 1 || oper == 0) acc = prod;   // MUL / UMUL: load ACC with the product
            else if (oper == 2)              acc += prod;  // MULA: accumulate
            else                             acc -= prod;  // MULS: subtract
            c->sr[0x10] = (uint32_t)acc;
            c->sr[0x11] = (uint32_t)(int32_t)(int8_t)(acc >> 32);   // ACCHI: 8-bit, sign-extended for RSR
        }
        if (is_load) {                       // LDINC/LDDEC: MR[r&3] = mem[AR[s]]; AR[s] += / -= 4
            MR(r & 3) = mem_r32(A(s));
            A(s) += load_dec ? (uint32_t)-4 : 4u;
        }
        #undef MR
        c->pc = npc; c->cycles++; return 1;
    }
    case 0x5: { // CALL0/CALL4/CALL8/CALL12
        int inc = (insn >> 4) & 0x3;
        int32_t off = sext(insn >> 6, 18);
        uint32_t target = ((pc + 4) & ~3u) + (off << 2);
        if (inc == 0) { A(0) = npc; c->pc = target; c->cycles++; return 1; } // CALL0
        // Faithful HW: no rotation (see CALLX). Record the return address in the callee
        // window's a0 slot and set PS.CALLINC; the callee's ENTRY rotates + sets the SP.
        if (WIN_EXC(c) && win_avail(c) <= inc + 3) { win_over(c, pc, win_avail(c)); c->cycles++; return 1; }
        c->ar[((c->windowbase + inc) * 4) & (NAREG - 1)] =
            (npc & 0x3fffffff) | ((uint32_t)inc << 30);
        c->ps = (c->ps & ~(3u << 16)) | ((uint32_t)inc << 16);   // PS.CALLINC = inc
        c->pc = target; c->cycles++; return 1;
    }
    case 0x6: { // SI
        int n = (insn >> 4) & 0x3;
        int m = (insn >> 6) & 0x3;
        if (n == 0) {                                             // J
            uint32_t tgt = pc + 4 + sext(insn >> 6, 18);
            // Deep-sleep power-off: esp_deep_sleep_start's terminal `j .` (jump-to-self) is
            // where the chip powers down after SLEEP_EN. On real HW the digital core dies and
            // stays off until an external wake (touch/timer) resets it; the e-ink holds its
            // last frame. Headless there is no wake, so halt cleanly (frame preserved) rather
            // than spin — distinct from a crash, and NOT a STUCK/hang. (Light sleep never
            // reaches here: it returns to app flash, which disarms above.)
            if (tgt == pc && g_rtc_slp_armed) {
                c->halted = 1;
                snprintf(c->stopmsg, sizeof c->stopmsg, "deep sleep: chip powered down, frame held (pc=%08x)", pc);
                return 0;
            }
            c->pc = tgt; c->cycles++; return 1;
        }
        if (n == 1) { // BZ: BEQZ/BNEZ/BLTZ/BGEZ  (12-bit offset)
            int32_t off = sext((insn >> 12) & 0xfff, 12);
            int32_t v = (int32_t)A(s); int take;
            if (m==0) take=(v==0); else if(m==1) take=(v!=0);
            else if(m==2) take=(v<0); else take=(v>=0);
            c->pc = take ? (pc + 4 + off) : npc; c->cbranch = take; c->cycles++; return 1;
        }
        if (n == 2) { // BI0: BEQI/BNEI/BLTI/BGEI  (imm from B4CONST[r])
            static const int32_t b4c[16] = {-1,1,2,3,4,5,6,7,8,10,12,16,32,64,128,256};
            int32_t imm = b4c[r], off = sext(imm8, 8), v = (int32_t)A(s); int take;
            if (m==0) take=(v==imm); else if(m==1) take=(v!=imm);
            else if(m==2) take=(v<imm); else take=(v>=imm);
            c->pc = take ? (pc + 4 + off) : npc; c->cbranch = take; c->cycles++; return 1;
        }
        // n == 3
        if (m == 0) { // ENTRY as, imm  (imm scaled by 8)
            // ENTRY completes a windowed CALL: it rotates WindowBase by PS.CALLINC (set
            // by the CALL, and preserved across ANY intervening instructions/interrupts)
            // and allocates the frame. This is the SINGLE rotation point for a call —
            // faithful to HW, independent of the compiler's CALL/ENTRY adjacency.
            // First, if establishing the new window would overlap a still-live older
            // window, raise WindowOverflow to spill it (then retry ENTRY). Then read the
            // caller's SP from the CURRENT window, subtract the frame, rotate, and write
            // the new SP into the NEW window's a[s]; subtracting after the rotate would
            // read the rotated window's a[s] and corrupt the stack pointer.
            int callinc = (c->ps >> 16) & 3;
            if (callinc && WIN_EXC(c) && win_avail(c) <= callinc + 3) {
                win_over(c, pc, win_avail(c)); c->cycles++; return 1;   // spill, retry ENTRY
            }
            uint32_t newsp = A(s) - (((insn >> 12) & 0xfff) << 3);
            if (callinc) { c->windowbase = (c->windowbase + callinc) & 15; c->ps &= ~(3u << 16); }
            A(s) = newsp;
            c->windowstart |= (1u << c->windowbase);
            c->pc = npc; c->cycles++; return 1;
        }
        if (m == 1) { // r sub-decodes: BF(0)/BT(1) boolean branch, LOOP(8)/LOOPNEZ(9)/LOOPGTZ(10)
            if (r == 0 || r == 1) {   // BF (branch if BR[s]==0) / BT (branch if BR[s]==1)
                int take = (int)((c->br >> s) & 1) == r;   // r=1: branch if true; r=0: if false
                int32_t off = sext(imm8, 8);
                c->pc = take ? (pc + 4 + off) : npc; c->cbranch = take; c->cycles++; return 1;
            }
            uint32_t lend = pc + 4 + imm8;
            int32_t v = (int32_t)A(s);
            // HW writes LBEG/LEND/LCOUNT on EVERY LOOP, including the LOOPNEZ/LOOPGTZ
            // skip (count==0) path. Must set LCOUNT=0 here too, else a stale nonzero
            // LCOUNT from a prior loop with the same LEND spuriously loops back when
            // execution lands on LEND (e.g. an ISR memcpy len<16 inheriting an
            // interrupted large memcpy's loop state -> runaway copy).
            c->lbeg = npc; c->lend = lend;
            if ((r == 9 && v == 0) || (r == 10 && v <= 0)) { c->lcount = 0; c->pc = lend; c->cycles++; return 1; }
            c->lcount = (uint32_t)(v - 1);
            c->pc = npc; c->cycles++; return 1;
        }
        if (m == 2 || m == 3) { // BLTUI (m=2) / BGEUI (m=3), imm from B4CONSTU[r]
            static const uint32_t b4cu[16] = {0x8000,0x10000,2,3,4,5,6,7,8,10,12,16,32,64,128,256};
            uint32_t imm = b4cu[r], v = A(s); int32_t off = sext(imm8, 8);
            int take = (m == 2) ? (v < imm) : (v >= imm);
            c->pc = take ? (pc+4+off) : npc; c->cbranch = take; c->cycles++; return 1;
        }
        trap(c, insn, "SI.n3?"); return 0;
    }
    case 0x7: { // B: register-compare + bit-test branches (r selects), imm8
        int32_t off = sext(imm8, 8);
        uint32_t sa = A(s), ta = A(t); int take = 0;
        switch (r) {
        case 0x0: take = ((sa & ta) == 0); break;               // BNONE
        case 0x1: take = (sa == ta); break;                     // BEQ
        case 0x2: take = ((int32_t)sa <  (int32_t)ta); break;   // BLT
        case 0x3: take = (sa <  ta); break;                     // BLTU
        case 0x4: take = ((sa & ta) == ta); break;              // BALL
        case 0x5: take = ((sa & (1u << (ta & 31))) == 0); break;// BBC
        case 0x6: case 0x7: { int b=((r&1)<<4)|t; take = ((sa & (1u<<(b&31)))==0); break; } // BBCI
        case 0x8: take = ((sa & ta) != 0); break;               // BANY
        case 0x9: take = (sa != ta); break;                     // BNE
        case 0xa: take = ((int32_t)sa >= (int32_t)ta); break;   // BGE
        case 0xb: take = (sa >= ta); break;                     // BGEU
        case 0xc: take = ((sa & ta) != ta); break;              // BNALL
        case 0xd: take = ((sa & (1u << (ta & 31))) != 0); break;// BBS
        case 0xe: case 0xf: { int b=((r&1)<<4)|t; take = ((sa & (1u<<(b&31)))!=0); break; } // BBSI
        }
        c->pc = take ? (pc + 4 + off) : npc; c->cbranch = take; c->cycles++; return 1;
    }

    // ---- density (16-bit) ----
    case 0x8: { // L32I.N / S32I.N
        uint32_t base = A(s), ea = base + (r << 2);
        // t is dest/src; distinguish by... op0=8 is L32I.N, op0=9 is S32I.N
        A(t) = mem_r32(ea); c->pc = npc; c->cycles++; return 1;
    }
    case 0x9: { uint32_t ea = A(s) + (r << 2); mem_w32(ea, A(t)); c->pc=npc; c->cycles++; return 1; } // S32I.N
    case 0xa: { A(r) = A(s) + A(t); c->pc = npc; c->cycles++; return 1; } // ADD.N
    case 0xb: { int32_t im = (t==0)? -1 : t; A(r) = A(s) + im; c->pc=npc; c->cycles++; return 1; } // ADDI.N
    case 0xc: { // ST2: MOVI.N (t[3]=0) / BEQZ.N,BNEZ.N (t[3]=1)
        if ((t & 0x8) == 0) {                       // MOVI.N as, imm7
            int v = (t << 4) | r; if (v >= 96) v -= 128;   // range -32..95
            A(s) = (uint32_t)v; c->pc = npc; c->cycles++; return 1;
        } else {                                    // BEQZ.N / BNEZ.N as, imm6 (fwd)
            uint32_t imm = r | ((t & 3) << 4);
            int take = (t & 4) ? (A(s) != 0) : (A(s) == 0);
            c->pc = take ? (pc + 4 + imm) : npc; c->cbranch = take; c->cycles++; return 1;
        }
    }
    case 0xd: { // ST3: MOV.N (r=0) / RET.N,RETW.N,NOP.N,BREAK.N (r=0xf)
        if (r == 0) { A(t) = A(s); c->pc = npc; c->cycles++; return 1; }   // MOV.N
        if (r == 0xf) {
            if (t == 0) { c->pc = A(0); c->cycles++; return 1; }           // RET.N
            if (t == 1) {                                                  // RETW.N
                uint32_t a0 = A(0); int dec = (a0 >> 30) & 3;
                int twb = (c->windowbase - dec) & 15;
                if (WIN_EXC(c) && !(c->windowstart & (1u << twb))) { win_under(c, pc, dec); c->cycles++; return 1; }
                c->windowstart &= ~(1u << c->windowbase);
                c->windowbase = twb;
                c->pc = (pc & 0xc0000000) | (a0 & 0x3fffffff);
                c->cycles++; return 1;
            }
            c->pc = npc; c->cycles++; return 1;                           // NOP.N / BREAK.N
        }
        c->pc = npc; c->cycles++; return 1;
    }
    }
    trap(c, insn, "op0?");
    return 0;
}

uint32_t cur_pc_dbg(void){ return g_cur_cpu ? g_cur_cpu->pc : 0; }
uint64_t cur_cyc_dbg(void){ return g_cur_cpu ? g_cur_cpu->cycles : 0; }
int      cur_core_dbg(void){ return g_cur_cpu ? g_cur_cpu->core_id : 0; }