#ifndef EMU_STATE_H #define EMU_STATE_H #include #include #include #include #include #include /* * Emulator State * * Shared state between CPU emulation, MMIO handlers, and display. */ #include struct EmuState { uc_engine *uc = nullptr; // Button state (updated by SDL keyboard events). std::atomic btn_vol_up{false}; std::atomic btn_vol_down{false}; std::atomic btn_power{false}; // Touchscreen state (FTS4/STMFTS stub on I2C3 @ slave 0x49). // Updated by SDL mouse events; consumed by i2c3_*/STMFTS code on CPU thread. // Coordinates are in panel-raw space (X long axis 0..1264, Y short 0..704) // matching what touch.c expects before its rescaling. std::atomic tc_x{0}; std::atomic tc_y{0}; std::atomic tc_pressed{false}; std::atomic tc_event_pending{false}; std::atomic tc_event_op{0}; // 0x03=ENTER, 0x04=LEAVE, 0x05=MOTION uint8_t tc_finger_id = 1; // FTS4 finger IDs are 1-indexed // last_rot mirrors the rotation last applied by sdl_display_update. // Read from the SDL event handler to invert the display→window transform. std::atomic last_rot{0}; std::atomic last_out_w{1280}; std::atomic last_out_h{720}; // Display state. uint64_t fb_addr = 0, pre_addr = 0; uint32_t fb_width = 720, pre_w = 720; uint32_t fb_height = 1280, pre_h = 1280; uint32_t fb_stride = 2880, pre_stride = 2880; uint32_t fb_swizzle = 0, pre_sw = 0; uint32_t fb_rotation = 0, pre_rot = 0; uint32_t pre_bh = 0; // block height in GOBs from DC surface-kind (0 = unset) uint32_t fb_sw_gobs = 80; uint32_t fb_bh = 0; // 0 = use display code default until DC surface-kind latches // DC window selection: tracks DC_CMD_DISPLAY_WINDOW_HEADER. // Bit 4 = Window A, Bit 5 = Window B, Bit 6 = Window C, Bit 7 = Window D. uint32_t dc_window_sel = 0x10; // Default: Window A // Saved Window A parameters (primary display surface). uint64_t winA_addr = 0; uint32_t winA_w = 720, winA_h = 1280, winA_stride = 2880; uint32_t winA_sw = 0, winA_rot = 0, winA_bh = 0; std::atomic display_dirty{false}; std::atomic display_initialized{false}; int32_t manual_offset = 0; int32_t rotation_override = -1; // -1 = Auto, 0=0, 1=90, 2=180, 3=270 // Emulation control. std::atomic running{true}; std::atomic paused{false}; std::atomic reboot_requested{false}; // Payload kept around for soft reboot (re-write to IRAM and reset PC). uint8_t *payload_ptr = nullptr; size_t payload_len = 0; // Backlight brightness (0-255). uint32_t backlight = 100; // Deterministic timer. uint64_t emu_usec = 0; uint64_t insn_count = 0; // Total time the BPMP has spent parked in a FLOW_CTLR timed halt // (bpmp_usleep / bpmp_msleep). The ACTMON model derives BPMP load from // this: time not slept during a sample window counts as active. uint64_t bpmp_slept_us = 0; // DRAM pointer (host memory backing the emulated DRAM). // DRAM is one contiguous host allocation covering 0x80000000..0x100000000. // dram_low_ptr owns it; dram_ptr is a view at the 0xC0000000 offset kept // for the existing high-DRAM/framebuffer call sites. Free only dram_low_ptr. uint8_t *dram_ptr = nullptr; // view: high DRAM (0xC0000000+) uint8_t *dram_low_ptr = nullptr; // owner: base of the whole DRAM window uint8_t *iram_ptr = nullptr; uint8_t *fb_ptr = nullptr; // SDMMC1 State uint32_t sdmmc_arg = 0; uint32_t sdmmc_rsp[4] = {0}; uint32_t sdmmc_norintsts = 0; uint32_t sdmmc_errintsts = 0; uint32_t sdmmc_sysad = 0; uint8_t sdmmc_hostctl = 0; uint16_t sdmmc_blksize = 0; uint16_t sdmmc_blkcnt = 0; uint16_t sdmmc_trnmod = 0; uint64_t sdmmc_adma_addr = 0; // SDMMC4 State uint32_t sdmmc4_arg = 0; uint32_t sdmmc4_rsp[4] = {0}; uint32_t sdmmc4_norintsts = 0; uint32_t sdmmc4_errintsts = 0; uint32_t sdmmc4_sysad = 0; uint8_t sdmmc4_hostctl = 0; uint16_t sdmmc4_blksize = 0; uint16_t sdmmc4_blkcnt = 0; uint16_t sdmmc4_trnmod = 0; uint64_t sdmmc4_adma_addr = 0; // File-backed storage int sd_fd = -1; int emmc_boot0_fd = -1; int emmc_boot1_fd = -1; std::vector emmc_gpp_fds; uint32_t emmc_partition = 0; // 0=GPP, 1=BOOT0, 2=BOOT1 // Command state tracking bool last_cmd_was_55 = false; bool last_cmd4_was_55 = false; // Touch injection state (STMFTS FTS5 controller, I2C addr 0x49 on I2C1) // touch_phase: 0=idle, 1=send_down, 2=send_up, 3=done std::atomic touch_phase{0}; uint16_t touch_x = 360; // Portrait X coordinate (0-719) uint16_t touch_y = 640; // Portrait Y coordinate (0-1279) // ==================== Tweakable hardware values (config window) ==================== // Battery fuel gauge (MAX17050, I2C_1 @ 0x36). // UI-friendly units; the I2C handler converts to MAX17050's raw register // encoding (cf. Hekate's max17050_get_property formulas). std::atomic bat_soc_pct{80}; // RepSOC: percent (0-100) std::atomic bat_vcell_mv{3950}; // VCELL: mV std::atomic bat_temp_c10{250}; // TEMP: °C * 10 (UI unit), e.g. 250 -> 25.0°C std::atomic bat_current_ma{0}; // Current: signed mA std::atomic bat_capacity_mah{3500}; // RepCap: mAh (current charge) std::atomic bat_full_cap_mah{4000}; // FullCAP: mAh (learned full capacity) std::atomic bat_design_cap_mah{4310}; // DesignCap: mAh (Switch nominal) std::atomic bat_ocv_mv{3960}; // OCVInternal: mV (open-circuit) std::atomic bat_v_empty_mv{3300}; // V_empty: mV std::atomic bat_min_volt_mv{3500}; // MinMaxVolt low byte * 20 std::atomic bat_max_volt_mv{4200}; // MinMaxVolt high byte * 20 std::atomic bat_age_pct{100}; // Age: percent (Age MSB) std::atomic bat_cycles{0}; // Cycles count // Main PMIC OEM (MAX77620 CID4): 0x35 = Erista, 0x53 = Mariko, other = Unknown std::atomic pmic_otp{0x35}; // Tegra X1 generation (drives APB_MISC_GP_HIDREV major nibble: 1 = T210 // Erista, 2 = T210B01 Mariko). Hekate's h_cfg.t210b01 derives from this, // which controls the pkg1 OEM-header skip among other things. std::atomic is_mariko{false}; // Silicon revisions, as read off a real console (Mariko, HAC-001(-01)): // CID3 = 0x5B -> payloads print the low nibble, "max77620 v11" // CID5 = 0x81 -> ES version (DIDM 8 / DIDO 1) // These are whole bytes, not nibbles: masking CID3 to 4 bits (the old // behaviour) made a genuine 0x5B read back as 0x0B. CID3/CID5 describe the // silicon and are expected to be identical on Erista - only CID4 (the OTP // version) differs by generation - but only Mariko is measured so far. std::atomic pmic_silicon_rev{0x5B}; // MAX77620 CID3 std::atomic pmic_es_rev{0x81}; // MAX77620 CID5 std::atomic cpu_pmic_version{0x12}; // MAX77621 CHIPID1 (measured, Erista) // SD card insertion (GPIO Port Z bit 1 = 0 means inserted). std::atomic sd_inserted{true}; // SD card identity (returned for SDMMC1 CMD2 ALL_SEND_CID). // Hekate parses these out of the R2 response per bdk/storage/sdmmc.c // _sd_storage_parse_cid; the I2C handler builds the 16-byte CID payload // from these atomics each time CMD2 fires. std::atomic sd_cid_manfid{0x03}; // 0x03 = SanDisk std::atomic sd_cid_oemid{0x5344}; // "SD" std::atomic sd_cid_prod_name{0x3155445355ULL}; // "USDU1" (low byte first) std::atomic sd_cid_hwrev{1}; // 0-15 std::atomic sd_cid_fwrev{0}; // 0-15 std::atomic sd_cid_serial{0xC0FFEE42}; std::atomic sd_cid_month{10}; // 1-12 std::atomic sd_cid_year{2024}; // 2000-2255 // eMMC identity (SDMMC4 CMD2). Layout per _mmc_storage_parse_cid (MMC v4): // 8-bit manfid, 8-bit oemid, 6-byte prod_name, 8-bit prv (BCD nibble.nibble), // 32-bit serial, 4-bit month, 4-bit year offset (year base = 2013 when // ext_csd.rev >= 5, which is what our EXT_CSD reports). std::atomic emmc_cid_manfid{0x90}; // 0x90 = SK Hynix std::atomic emmc_cid_oemid{0x01}; std::atomic emmc_cid_prod_name{0x326134474248ULL}; // "HBG4a2" (low byte first) std::atomic emmc_cid_prv{0x07}; // displayed as "0.7" (low.high) std::atomic emmc_cid_serial{0x12345678}; std::atomic emmc_cid_month{6}; // 1-12 std::atomic emmc_cid_year{2017}; // 2013-2025 (Hekate caps at 2025: see config_window.cpp) // Ambient light sensor (Rohm BH1730, I2C2 @ 0x29). Defaults are what a // real Mariko reported in a lit room; part id 0x71 is the measured value // a payload checks to decide the sensor is present at all. std::atomic als_part_id{0x71}; std::atomic als_visible{128}; // DATA0 counts std::atomic als_ir{21}; // DATA1 counts // SoC thermal sensor (TMP451, I2C5 @ 0x4C). std::atomic soc_temp_c10{420}; // remote channel (SoC die): °C * 10 std::atomic pcb_temp_c10{350}; // local channel (PCB): °C * 10 // Charger (BQ24193, I2C1 @ 0x6B). std::atomic chg_vbus_stat{0}; // 0=none, 1=USB-SDP, 2=adapter, 3=OTG std::atomic chg_chrg_stat{0}; // 0=not charging, 1=pre, 2=fast, 3=done std::atomic chg_power_good{false}; // BQ24193 charger limits (encoded into regs 0x00/0x01/0x02/0x04/0x06). // mmio.cpp does the reverse-encoding from these decoded values; the user // sees real units, the chip sees the closest legal register value. std::atomic chg_input_current_ma{2000}; // table: 100/150/500/900/1200/1500/2000/3000 std::atomic chg_input_voltage_mv{4280}; // 3880-5080 mV, 80 mV step std::atomic chg_system_min_mv{3500}; // 3000-3700 mV, 100 mV step std::atomic chg_fast_current_ma{1856}; // 512-4544 mA, 64 mA step std::atomic chg_charge_voltage_mv{4208}; // 3504-4512 mV, 16 mV step std::atomic chg_thermal_c{60}; // 60 / 80 / 100 / 120 °C // USB-PD controller (BM92T36, I2C1 @ 0x18). One synthesized fixed PDO. std::atomic usb_pd_inserted{false}; std::atomic usb_pd_voltage_mv{5000}; std::atomic usb_pd_amperage_ma{1500}; // Fuse driver (FUSE @ 0x7000F800). One u32 per offset, indexed by // offset/4. The HW & Fuses Info screen reads several dozen offsets // (speedos, IDDQ, SBK, public key, lot/wafer, etc.); seeding plausible // defaults here keeps the screen rendering meaningful values without a // dedicated atomic per field. Defaults populated by init_fuse_defaults(). static constexpr size_t FUSE_WORDS = 0x100; std::array, FUSE_WORDS> fuse_word{}; std::atomic& fuse_at(uint32_t offset) { return fuse_word[(offset & 0x3FC) / 4]; } void init_fuse_defaults() { // Names below mirror Hekate's bdk/soc/fuse.h. Values picked from a // typical Erista golden-sample dump (any retail Switch is similar). fuse_at(0x100).store(1); // FUSE_PRODUCTION_MODE fuse_at(0x110).store(0x83); // FUSE_SKU_INFO (SKU_ODIN) fuse_at(0x114).store(2289); // FUSE_CPU_SPEEDO_0_CALIB fuse_at(0x118).store(1786); // FUSE_CPU_IDDQ_CALIB (x4 -> 7144) fuse_at(0x128).store(0x82); // FUSE_OPT_FT_REV (4.02) fuse_at(0x12C).store(2330); // FUSE_CPU_SPEEDO_1_CALIB fuse_at(0x130).store(2342); // FUSE_CPU_SPEEDO_2_CALIB fuse_at(0x134).store(1801); // FUSE_SOC_SPEEDO_0_CALIB fuse_at(0x138).store(0x80); // FUSE_SOC_SPEEDO_1_CALIB (BROM rev) fuse_at(0x13C).store(1817); // FUSE_SOC_SPEEDO_2_CALIB fuse_at(0x140).store(1500); // FUSE_SOC_IDDQ_CALIB (x4 -> 6000) fuse_at(0x148).store(0x83000001); // FUSE_FA // Public key (8 words). Hekate prints them byte-swapped, so storage // is the byte-reverse of the desired displayed hex string. Two rows // of "5F1B7A4D8E2C6F09B3A815C7D4E0F26B" / "A91F4C8B7D2E0653F1A8B4C7E0D962F4". fuse_at(0x164).store(0x4D7A1B5F); fuse_at(0x168).store(0x096F2C8E); fuse_at(0x16C).store(0xC715A8B3); fuse_at(0x170).store(0x6BF2E0D4); fuse_at(0x174).store(0x8B4C1FA9); fuse_at(0x178).store(0x53062E7D); fuse_at(0x17C).store(0xC7B4A8F1); fuse_at(0x180).store(0xF462D9E0); fuse_at(0x190).store(0x100); // FUSE_OPT_CP_REV (8.00) fuse_at(0x1A0).store(0x06); // FUSE_SECURITY_MODE // SBK + DK placeholders. Same byte-swap convention as the public key. // SBK displays as "C8E5719FA34D6B208E14F7B965A028D3", DK as "47A1F39E". // --prod-keys overrides these via the SE keytable at runtime. fuse_at(0x1A4).store(0x9F71E5C8); fuse_at(0x1A8).store(0x206B4DA3); fuse_at(0x1AC).store(0xB9F7148E); fuse_at(0x1B0).store(0xD328A065); fuse_at(0x1B4).store(0x9EF3A147); // RESERVED_SW bit 7 selects USB controller string in Hekate. Off = "USB2". fuse_at(0x1C0).store(0x00); fuse_at(0x1D8).store(0x20); // FUSE_RESERVED_ODM4 (DRAM ID 4) // FUSE_RESERVED_ODM7: burnt anti-downgrade fuses. A payload compares // the popcount here against the HOS version of the pkg1 in BOOT0 and // flags a mismatch as "pkg1 too new/old for this console". Left at 0 // it always looked like a downgraded console. 16 bits matches the // HOS 13.2.1 pkg1 in the bundled BOOT0 dump; change it alongside the // dump (real consoles measured: 23 burnt on HOS 22.0.0). fuse_at(0x1E4).store(0x0000FFFF); // FUSE_RESERVED_ODM7 (16 burnt) fuse_at(0x200).store(0x06); // FUSE_OPT_VENDOR_CODE fuse_at(0x204).store(0x12); // FUSE_OPT_FAB_CODE ('I' = base36[18]) // LOT_CODE_0 holds five base36 chars in 6-bit fields at bits 29:24, // 23:18, 17:12, 11:6, 5:0. Each digit must stay <= 35 or Hekate // indexes past the base36[] table and prints garbage. Encoded "K1SHQ". fuse_at(0x208).store(0x1405C45A); fuse_at(0x210).store(1); // FUSE_OPT_WAFER_ID // Wafer X/Y must land inside the Erista lithography map // (X in [-9, 15], Y in [0, 24]) or _hw_info_wafer marks "Error". fuse_at(0x214).store(5); // FUSE_OPT_X_COORDINATE fuse_at(0x218).store(12); // FUSE_OPT_Y_COORDINATE fuse_at(0x228).store(1500); // FUSE_GPU_IDDQ_CALIB (x5 -> 7500) } // DRAM mode register responses. Hekate reads these via the EMC controller // (sdram_read_mrx) to populate the "Vendor / Rev / Density" columns on // the HW & Fuses Info screen. Defaults match a Samsung K4F6E304HB-MGCH // 4 GB module on both channels. std::atomic dram_vendor{1}; // MR5 MAN_ID (1=Samsung, 6=Hynix, 255=Micron) std::atomic dram_rev_id1{2}; // MR6 REV_ID1 std::atomic dram_rev_id2{0}; // MR7 REV_ID2 std::atomic dram_density{0x18}; // MR8 DENSITY (bits 5:2 = density code) // Display panel ID (DSI). Hekate reads MIPI_DCS_GET_DISPLAY_ID (0x04) over // DSI and stores 3 raw bytes; the screen prints them as "ID: b0 b1 b2" // and decodes them as `((raw >> 8) & 0xFF00) | (raw & 0xFF)`. Default // 0x099310 -> JDI LAM062M109A rev 0x93 (a common Erista launch panel). std::atomic panel_id_raw{0x099310}; // Touch panel + FW info (FTS4 controller, I2C3 @ 0x49). Idx maps to the // _panels[] table in Hekate's bdk/input/touch.c (0=NISSHA NFT-K12D pairs // with fw_id 4CD60D/1, etc.). Defaults render as "NISSHA NFT-K12D" with // ID "32.00.00.01 (Paired)". std::atomic touch_panel_idx{0}; // 0..4 -> known panels, 5 -> GiS VA 6.2" std::atomic touch_fw_id{0x32000001}; // displayed as 4CD60D/ std::atomic touch_ftb_ver{0x0123}; std::atomic touch_fw_rev{0x4567}; // Hekate byte-swaps before printing // ============================================================ // Per-UART scratch state used by the in-emulator UART console. // ============================================================ // Tegra X1 has UART_A..UART_E (5 ports). The mmio dispatcher for the // 0x70006000 region uses (addr & 0x1FF) / 0x40 to derive the port idx. // // - uart_tx_log[idx] mirrors every byte the payload writes to that // port's TX register, so the console window can render scrolling // history without having to re-parse stdout. Capped at ~64 KB by a // simple "drop the front when too big" trim. // - uart_rx_fifo[idx] is bytes the host has typed in the console // window awaiting consumption by the next UART read (RBR via // offset 0x00, with bit 0 of LSR at offset 0x14 set when non-empty). // // The CPU-emulation hooks and the ImGui console both run on the main // thread between SDL polls, so no locking is required. static constexpr size_t N_UARTS = 5; std::string uart_tx_log[N_UARTS]; std::deque uart_rx_fifo[N_UARTS]; }; #endif // EMU_STATE_H