xtensa.h
C source for Emulating a round-screen ESP32 board, 57 lines. Raw file
// xtensa.h - Xtensa LX7 core (the ESP32-S3 CPU), emulated from scratch.
//
// Windowed-register ABI: 64 physical address registers (AR); a0..a15 are a window
// AR[(WindowBase*4 + i) & 63]. CALL{4,8,12}/ENTRY/RETW rotate the window and use
// window-overflow/underflow exceptions to spill to the stack - all of which the
// real ESP-IDF relies on, so we model them properly.
#pragma once
#include <stdint.h>
#include <stdbool.h>
#define NAREG 64 // physical address registers (LX7: 64)
typedef struct {
uint32_t ar[NAREG]; // physical register file
uint32_t pc;
uint32_t sar; // shift amount register (0..63)
uint32_t ps; // processor state (INTLEVEL/EXCM/UM/WOE/CALLINC/OWB/RING)
uint32_t windowbase; // 0..15
uint32_t windowstart; // 16-bit live-window bitmap
// special registers we care about for boot
uint32_t vecbase; // relocatable vector base (WSR.VECBASE)
uint32_t epc[8]; // EPC1..EPC7
uint32_t eps[8]; // EPS2..EPS7
uint32_t excsave[8]; // EXCSAVE1..7
uint32_t exccause, excvaddr, depc;
uint32_t intenable, interrupt;
uint32_t litbase;
uint32_t lbeg, lend, lcount; // zero-overhead loops
int cbranch; // set for the step just executed if it was a TAKEN conditional
// branch: a conditional branch landing on LEND is a loop EXIT,
// not a loopback, so it suppresses the zero-overhead loopback.
uint32_t sr[256]; // catch-all for other special registers
uint32_t ur[256]; // user registers (RUR/WUR; THREADPTR etc.)
uint32_t fr[16]; // single-precision FP register file (IEEE-754 bit patterns)
uint32_t br; // 16 boolean registers (FP compare results), bit i = BR[i]
uint64_t cycles; // retired-instruction / cycle counter
int halted;
int core_id; // 0 = PRO CPU, 1 = APP CPU (selects RSR.PRID)
int waiti; // in WAITI (idle until interrupt)
int call_gap; // 1 between an eager CALL rotate and the callee's ENTRY: the
// callee's SP (a1) isn't finalised yet, so defer interrupts.
char stopmsg[128];
} xtensa_t;
// The APP CPU (core 1) is started by core 0 via ets_set_appcpu_boot_addr + the
// SYSTEM_CORE_1_CONTROL reset deassert. xtensa.c/mmio.c set these; main.c launches.
extern uint32_t g_core1_entry; // captured APP-CPU boot address
extern int g_core1_start_req; // 1 = core 0 has released the APP CPU
void xt_reset(xtensa_t *c, uint32_t entry);
// Execute one instruction; returns cycles consumed (>=1), or 0 if halted.
int xt_step(xtensa_t *c);
// a0..a15 window accessors.
uint32_t xt_a(xtensa_t *c, int i);
void xt_set_a(xtensa_t *c, int i, uint32_t v);