mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
641 lines
32 KiB
C++
641 lines
32 KiB
C++
#ifndef EMU_STATE_H
|
|
#define EMU_STATE_H
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <deque>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
/*
|
|
* Emulator State
|
|
*
|
|
* Shared state between CPU emulation, MMIO handlers, and display.
|
|
*/
|
|
|
|
#include <unicorn/unicorn.h>
|
|
|
|
// Bluetooth radio behaviour, selected by `[bluetooth] radio=` in rcm_emu.ini
|
|
// or `--bt-radio` on the command line. The Broadcom CYW4356 model in
|
|
// t210/mmio.cpp reads this every time the payload can observe the part.
|
|
//
|
|
// healthy - the module powers up on the BT_REG_ON edge, holds BT_HOST_WAKE
|
|
// high, drives its RTS_N low once POR completes and answers HCI.
|
|
// faulty - the module is fitted but never comes out of POR: BT_HOST_WAKE
|
|
// reads low, RTS_N never asserts and nothing ever reaches the
|
|
// wire. This is the console that returns 2110-1118 in HOS.
|
|
// absent - no module at all. As faulty, plus nothing drives BT_UART_TXD,
|
|
// so the Tegra's own pull-down on RX reads as a line break.
|
|
enum BtRadioMode : uint8_t {
|
|
BT_RADIO_HEALTHY = 0,
|
|
BT_RADIO_FAULTY = 1,
|
|
BT_RADIO_ABSENT = 2,
|
|
};
|
|
|
|
inline const char *bt_radio_name(uint8_t mode) {
|
|
switch (mode) {
|
|
case BT_RADIO_FAULTY: return "faulty";
|
|
case BT_RADIO_ABSENT: return "absent";
|
|
default: return "healthy";
|
|
}
|
|
}
|
|
|
|
// Accepts the three names above and the raw 0/1/2 the enum uses. Returns
|
|
// false on anything else so the caller can warn instead of silently
|
|
// selecting a mode the user did not ask for -- which is exactly what would
|
|
// happen if this key went through the ini's numeric schema, where
|
|
// strtoull("faulty") is 0 and 0 is "healthy".
|
|
inline bool bt_radio_parse(const char *s, uint8_t *out) {
|
|
if (!s || !*s) return false;
|
|
if (!strcmp(s, "healthy") || !strcmp(s, "0")) { *out = BT_RADIO_HEALTHY; return true; }
|
|
if (!strcmp(s, "faulty") || !strcmp(s, "1")) { *out = BT_RADIO_FAULTY; return true; }
|
|
if (!strcmp(s, "absent") || !strcmp(s, "2")) { *out = BT_RADIO_ABSENT; return true; }
|
|
return false;
|
|
}
|
|
|
|
// WLAN half of the same CYW4356 package, selected by `[wifi] radio=` in
|
|
// rcm_emu.ini or `--wifi-radio` on the command line. It is a PCI Express
|
|
// endpoint on root port 1, so the failure modes are different from the
|
|
// Bluetooth side's and worth separating:
|
|
//
|
|
// healthy - the link trains, the endpoint enumerates as 14E4:43EC, and
|
|
// its BAR0 backplane window answers with ChipCommon ChipID
|
|
// 0x4356. A working radio.
|
|
// faulty - the PCIe front-end is alive: the link trains and config space
|
|
// reads back correctly, but the WLAN die behind it is not, so
|
|
// every backplane read returns all ones. This is the console
|
|
// that enumerates and still has no Wi-Fi.
|
|
// absent - no module, or one whose PCIe side never comes up: the link
|
|
// never leaves detect and no config access is ever answered.
|
|
enum WifiRadioMode : uint8_t {
|
|
WIFI_RADIO_HEALTHY = 0,
|
|
WIFI_RADIO_FAULTY = 1,
|
|
WIFI_RADIO_ABSENT = 2,
|
|
};
|
|
|
|
inline const char *wifi_radio_name(uint8_t mode) {
|
|
switch (mode) {
|
|
case WIFI_RADIO_FAULTY: return "faulty";
|
|
case WIFI_RADIO_ABSENT: return "absent";
|
|
default: return "healthy";
|
|
}
|
|
}
|
|
|
|
inline bool wifi_radio_parse(const char *s, uint8_t *out) {
|
|
if (!s || !*s) return false;
|
|
if (!strcmp(s, "healthy") || !strcmp(s, "0")) { *out = WIFI_RADIO_HEALTHY; return true; }
|
|
if (!strcmp(s, "faulty") || !strcmp(s, "1")) { *out = WIFI_RADIO_FAULTY; return true; }
|
|
if (!strcmp(s, "absent") || !strcmp(s, "2")) { *out = WIFI_RADIO_ABSENT; return true; }
|
|
return false;
|
|
}
|
|
|
|
// ---- Fault injection ------------------------------------------------------
|
|
//
|
|
// Named hardware faults, set by `[faults]` in rcm_emu.ini, `--fault NAME` on
|
|
// the command line or the config window's Faults section. They are hardware,
|
|
// so a payload reboot keeps them. Two kinds:
|
|
//
|
|
// chip The part is dead. It NAKs its I2C address, takes no writes, and
|
|
// every rail it supplies goes down with it.
|
|
// rail The regulator answers, takes its enable and reads back as on, but
|
|
// its output never comes up: an open inductor, a shorted output cap.
|
|
// Nothing visible over I2C; only what the rail feeds notices. This is
|
|
// the hard case, and the one a probe that only reads regulator
|
|
// registers back cannot catch.
|
|
//
|
|
// Coherence is the rule: a part is listed only once everything it feeds that
|
|
// the emulator models reacts to it. A name with no modelled consequence would
|
|
// let a payload "pass" against a fault that cannot fail it, so it is left out
|
|
// rather than accepted. DESIGN.md "Fault injection" has what each one does.
|
|
//
|
|
// Deliberately absent:
|
|
// MAX77620 the main PMIC: with it dead nothing would run to test.
|
|
// ALC5639 the codec: audio leaves the emulator through I2S whether or not
|
|
// the codec answers, so "dead" would be half-modelled.
|
|
// GPU / DRAM rails (MAX77621 0x1C output, MAX77812 M1/M3): no consumer is
|
|
// modelled. The chips' own death is offered, since all it does
|
|
// is NAK.
|
|
enum FaultId : uint32_t {
|
|
FAULT_MAX77621_CPU = 1u << 0, // Erista CPU buck, I2C5 0x1B; feeds vdd_cpu
|
|
FAULT_MAX77621_GPU = 1u << 1, // Erista GPU buck, I2C5 0x1C
|
|
FAULT_MAX77812 = 1u << 2, // Mariko CPU/GPU/DRAM buck, I2C5 0x33; feeds vdd_cpu
|
|
FAULT_BQ24193 = 1u << 3, // charger, I2C1 0x6B
|
|
FAULT_MAX17050 = 1u << 4, // fuel gauge, I2C1 0x36
|
|
FAULT_TMP451 = 1u << 5, // thermal sensor, I2C1 0x4C
|
|
FAULT_BM92T36 = 1u << 6, // USB-PD controller, I2C1 0x18
|
|
FAULT_VDD_CPU = 1u << 7, // the CPU rail's output, whichever buck drives it
|
|
};
|
|
|
|
struct FaultDef {
|
|
uint32_t id;
|
|
const char *name; // ini key and --fault value
|
|
const char *kind; // "chip" or "rail"
|
|
const char *what; // one line, for --fault list and the config window
|
|
};
|
|
|
|
inline const FaultDef kFaults[] = {
|
|
{FAULT_MAX77621_CPU, "max77621_cpu", "chip",
|
|
"Erista CPU buck (I2C5 0x1B): NAKs; vdd_cpu dead with it"},
|
|
{FAULT_MAX77621_GPU, "max77621_gpu", "chip",
|
|
"Erista GPU buck (I2C5 0x1C): NAKs"},
|
|
{FAULT_MAX77812, "max77812", "chip",
|
|
"Mariko CPU/GPU/DRAM buck (I2C5 0x33): NAKs; vdd_cpu dead with it"},
|
|
{FAULT_BQ24193, "bq24193", "chip", "charger (I2C1 0x6B): NAKs"},
|
|
{FAULT_MAX17050, "max17050", "chip", "fuel gauge (I2C1 0x36): NAKs"},
|
|
{FAULT_TMP451, "tmp451", "chip", "thermal sensor (I2C1 0x4C): NAKs"},
|
|
{FAULT_BM92T36, "bm92t36", "chip", "USB-PD (I2C1 0x18): NAKs"},
|
|
{FAULT_VDD_CPU, "vdd_cpu", "rail",
|
|
"CPU rail output dead, buck still answers: CPU RAM repair never "
|
|
"completes, CPU0 cannot run"},
|
|
};
|
|
|
|
inline const FaultDef *fault_find(const char *name) {
|
|
if (!name) return nullptr;
|
|
for (const FaultDef &f : kFaults)
|
|
if (!strcmp(name, f.name)) return &f;
|
|
return nullptr;
|
|
}
|
|
|
|
inline const char *fault_name(uint32_t id) {
|
|
for (const FaultDef &f : kFaults)
|
|
if (f.id == id) return f.name;
|
|
return "?";
|
|
}
|
|
|
|
// Fault value in the ini: dead/1 sets it, ok/0 clears it. Anything else is
|
|
// refused so a typo cannot silently mean "healthy".
|
|
inline bool fault_value_parse(const char *s, bool *dead) {
|
|
if (!s) return false;
|
|
if (!strcmp(s, "dead") || !strcmp(s, "1")) { *dead = true; return true; }
|
|
if (!strcmp(s, "ok") || !strcmp(s, "0")) { *dead = false; return true; }
|
|
return false;
|
|
}
|
|
|
|
// Tegra DC window registers (TRM 24.10-24.11) as slots in a window's
|
|
// register file: the indirect window page 0x700-0x7FF is slots 0x000-0x0FF,
|
|
// the WINBUF page 0x800-0x83F slots 0x100-0x13F.
|
|
namespace dcwin {
|
|
constexpr uint32_t kRegs = 0x140;
|
|
constexpr uint32_t OPTIONS = 0x000; // DC_WIN_WIN_OPTIONS
|
|
constexpr uint32_t COLOR_DEPTH = 0x003; // DC_WIN_COLOR_DEPTH
|
|
constexpr uint32_t POSITION = 0x004; // DC_WIN_POSITION
|
|
constexpr uint32_t SIZE = 0x005; // DC_WIN_SIZE (post-scaling)
|
|
constexpr uint32_t PRESCALED = 0x006; // DC_WIN_PRESCALED_SIZE (bytes x lines)
|
|
constexpr uint32_t LINE_STRIDE = 0x00A; // DC_WIN_LINE_STRIDE (bytes)
|
|
constexpr uint32_t BLEND_LAYER = 0x016; // DC_WINBUF_BLEND_LAYER_CONTROL
|
|
constexpr uint32_t BLEND_MATCH = 0x017; // DC_WINBUF_BLEND_MATCH_SELECT
|
|
constexpr uint32_t START_ADDR = 0x100; // DC_WINBUF_START_ADDR
|
|
constexpr uint32_t ADDR_H_OFF = 0x106; // DC_WINBUF_ADDR_H_OFFSET (bytes)
|
|
constexpr uint32_t ADDR_V_OFF = 0x108; // DC_WINBUF_ADDR_V_OFFSET (lines)
|
|
constexpr uint32_t SURFACE_KIND = 0x10B; // DC_WINBUF_SURFACE_KIND
|
|
|
|
// WIN_OPTIONS bits.
|
|
constexpr uint32_t H_DIRECTION = 1u << 0;
|
|
constexpr uint32_t V_DIRECTION = 1u << 2;
|
|
constexpr uint32_t SCAN_COLUMN = 1u << 4;
|
|
constexpr uint32_t WIN_ENABLE = 1u << 30;
|
|
|
|
// BLEND_LAYER_CONTROL resets to BLEND_BYPASS (TRM 24.10.12).
|
|
constexpr uint32_t BLEND_LAYER_RESET = 0x01000000;
|
|
|
|
// A picture turn, as flip-then-transpose: the source is mirrored left-right
|
|
// (XF_FLIP_X) and/or top-bottom (XF_FLIP_Y), then rows become columns
|
|
// (XF_TRANSPOSE). DC WIN_OPTIONS (H_DIRECTION, V_DIRECTION, SCAN_COLUMN) and
|
|
// VIC's OutputFlipX/FlipY/Transpose both follow this convention.
|
|
constexpr uint32_t XF_FLIP_X = 1u << 0;
|
|
constexpr uint32_t XF_FLIP_Y = 1u << 1;
|
|
constexpr uint32_t XF_TRANSPOSE = 1u << 2;
|
|
} // namespace dcwin
|
|
|
|
struct EmuState {
|
|
uc_engine *uc = nullptr;
|
|
// Button state (updated by SDL keyboard events).
|
|
std::atomic<bool> btn_vol_up{false};
|
|
std::atomic<bool> btn_vol_down{false};
|
|
std::atomic<bool> btn_power{false};
|
|
// A keyboard VOL press ends at this guest time (0: none running). The
|
|
// arrow keys press for a fixed guest duration: guest time outruns the
|
|
// wall clock while the guest sleeps, so a held key's wall time means
|
|
// little to the guest.
|
|
std::atomic<uint64_t> kbd_vol_up_until{0};
|
|
std::atomic<uint64_t> kbd_vol_down_until{0};
|
|
|
|
// 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.
|
|
//
|
|
// Events queue up rather than overwrite each other: a click shorter than
|
|
// the payload's poll interval used to reach it as a bare LEAVE, the
|
|
// ENTER lost under it. Single producer (SDL), single consumer (I2C3).
|
|
struct TouchEvent { uint8_t op; uint16_t x, y; }; // op 3/4/5 = ENTER/LEAVE/MOTION
|
|
static constexpr uint32_t TC_QUEUE = 32;
|
|
TouchEvent tc_queue[TC_QUEUE] = {};
|
|
std::atomic<uint32_t> tc_q_head{0}; // next slot the producer fills
|
|
std::atomic<uint32_t> tc_q_tail{0}; // next slot the consumer takes
|
|
std::atomic<bool> tc_pressed{false};
|
|
uint8_t tc_finger_id = 0; // raw FTS4 ID; bdk reports it + 1
|
|
|
|
// Queue a touch event. MOTION is dropped when the queue is nearly full,
|
|
// so an ENTER or LEAVE always finds room.
|
|
void touch_post(uint8_t op, uint16_t x, uint16_t y) {
|
|
uint32_t head = tc_q_head.load(), tail = tc_q_tail.load();
|
|
uint32_t used = head - tail;
|
|
if (used >= TC_QUEUE || (op == 0x05 && used >= TC_QUEUE - 4))
|
|
return;
|
|
tc_queue[head % TC_QUEUE] = {op, x, y};
|
|
tc_q_head.store(head + 1);
|
|
}
|
|
bool touch_take(TouchEvent *ev) {
|
|
uint32_t tail = tc_q_tail.load();
|
|
if (tail == tc_q_head.load())
|
|
return false;
|
|
*ev = tc_queue[tail % TC_QUEUE];
|
|
tc_q_tail.store(tail + 1);
|
|
return true;
|
|
}
|
|
|
|
// Framebuffer snapshots asked for by the input script (SNAP), written by
|
|
// the display thread from the next frame it shows. CPU thread produces,
|
|
// display thread consumes. A spin lock, not std::mutex: the Windows
|
|
// build's MinGW uses the win32 thread model, which has none.
|
|
std::atomic<bool> snap_busy{false};
|
|
std::vector<std::string> snap_paths;
|
|
void snap_request(const std::string &path) {
|
|
while (snap_busy.exchange(true, std::memory_order_acquire)) {}
|
|
snap_paths.push_back(path);
|
|
snap_busy.store(false, std::memory_order_release);
|
|
}
|
|
bool snap_take(std::string *path) {
|
|
while (snap_busy.exchange(true, std::memory_order_acquire)) {}
|
|
bool got = !snap_paths.empty();
|
|
if (got) {
|
|
*path = snap_paths.front();
|
|
snap_paths.erase(snap_paths.begin());
|
|
}
|
|
snap_busy.store(false, std::memory_order_release);
|
|
return got;
|
|
}
|
|
// 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<uint32_t> last_rot{0};
|
|
std::atomic<uint32_t> last_auto_rot{0}; // what auto-detect picked, before any override
|
|
std::atomic<uint32_t> last_out_w{1280};
|
|
std::atomic<uint32_t> last_out_h{720};
|
|
|
|
// Display controller windows A-D (t210/mmio.cpp, Display). Each window
|
|
// has a register file indexed by dcwin:: slot. The CPU writes the
|
|
// assembly copy; DC_CMD_STATE_CONTROL's WIN_x_ACT_REQ copies it into the
|
|
// active copy, which is what the scan-out (display/sdl_display.cpp)
|
|
// decodes.
|
|
uint32_t dc_win[4][dcwin::kRegs] = {};
|
|
uint32_t dc_win_active[4][dcwin::kRegs] = {};
|
|
uint32_t dc_window_header = 0; // DC_CMD_DISPLAY_WINDOW_HEADER
|
|
// Display head B (DC1, 0x54240000): the head that feeds SOR1 and the
|
|
// dock. Same window model as head A, scanned out to the TV window.
|
|
uint32_t dcb_win[4][dcwin::kRegs] = {};
|
|
uint32_t dcb_win_active[4][dcwin::kRegs] = {};
|
|
uint32_t dcb_window_header = 0;
|
|
bool dcb_programmed = false;
|
|
// False from reset until the payload first activates a window. Until
|
|
// then the scan-out shows the emulator's own framebuffer (FB_BASE).
|
|
bool dc_programmed = false;
|
|
// Where the last VIC compose wrote, and how it turned the picture on the
|
|
// way (dcwin::XF_* bits). The scan-out undoes that turn for display.
|
|
uint64_t vic_out_addr = 0;
|
|
uint32_t vic_out_xform = 0;
|
|
std::atomic<bool> display_dirty{false};
|
|
std::atomic<bool> 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<bool> running{true};
|
|
std::atomic<bool> paused{false};
|
|
std::atomic<bool> reboot_requested{false};
|
|
// The reboot is a power cycle (PMIC software reset, the UI's Reboot):
|
|
// the regulators lose their state too. Otherwise it is a SoC reset
|
|
// (PMC MAIN_RST), which the PMIC, and its rails, sit out.
|
|
std::atomic<bool> reboot_cold{false};
|
|
// The BPMP parked itself for good (bpmp_halt(): WAITEVENT with no timer)
|
|
// while CPU0 still runs - the handoff fusee and hekate's L4T launch do.
|
|
// Time keeps passing for CPU0; the BPMP executes nothing until reset.
|
|
bool bpmp_halted = 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<int> 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<int> 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<uint16_t> bat_soc_pct{80}; // RepSOC: percent (0-100)
|
|
std::atomic<uint16_t> bat_vcell_mv{3950}; // VCELL: mV
|
|
std::atomic<int16_t> bat_temp_c10{250}; // TEMP: °C * 10 (UI unit), e.g. 250 -> 25.0°C
|
|
std::atomic<int16_t> bat_current_ma{0}; // Current: signed mA
|
|
std::atomic<uint16_t> bat_capacity_mah{3500}; // RepCap: mAh (current charge)
|
|
std::atomic<uint16_t> bat_full_cap_mah{4000}; // FullCAP: mAh (learned full capacity)
|
|
std::atomic<uint16_t> bat_design_cap_mah{4310}; // DesignCap: mAh (Switch nominal)
|
|
std::atomic<uint16_t> bat_ocv_mv{3960}; // OCVInternal: mV (open-circuit)
|
|
std::atomic<uint16_t> bat_v_empty_mv{3300}; // V_empty: mV
|
|
std::atomic<uint16_t> bat_min_volt_mv{3500}; // MinMaxVolt low byte * 20
|
|
std::atomic<uint16_t> bat_max_volt_mv{4200}; // MinMaxVolt high byte * 20
|
|
std::atomic<uint8_t> bat_age_pct{100}; // Age: percent (Age MSB)
|
|
std::atomic<uint16_t> bat_cycles{0}; // Cycles count
|
|
|
|
// Main PMIC OEM (MAX77620 CID4): 0x35 = Erista, 0x53 = Mariko, other = Unknown
|
|
std::atomic<uint8_t> 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<bool> 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<uint8_t> pmic_silicon_rev{0x5B}; // MAX77620 CID3
|
|
std::atomic<uint8_t> pmic_es_rev{0x81}; // MAX77620 CID5
|
|
std::atomic<uint8_t> cpu_pmic_version{0x12}; // MAX77621 CHIPID1 (measured, Erista)
|
|
|
|
// MAX77620 CNFG1_32K (reg 0x03), the LPO that clocks the combo radio.
|
|
// 32K_OK (bit 7) and 32K_OUT0_EN (bit 2) are set on every healthy
|
|
// console, but the rest of the byte varies per UNIT, not per SoC
|
|
// generation: 0xFC and 0xDC are both measured on Erista and on Mariko.
|
|
// Exposed so a capture from a specific console can be reproduced exactly.
|
|
std::atomic<uint8_t> pmic_cnfg1_32k{0xFC};
|
|
|
|
// SD card insertion (GPIO Port Z bit 1 = 0 means inserted).
|
|
std::atomic<bool> sd_inserted{true};
|
|
|
|
// A PC on the USB-C port (--usb-host): it enumerates whatever gadget the
|
|
// payload brings up (t210/usb.cpp). Off, nothing is on the cable.
|
|
std::atomic<bool> usb_host{false};
|
|
// --usb-host nbd[:port]: instead of reading a mass storage disk back and
|
|
// ejecting it, the host serves it over NBD on 127.0.0.1:port until the
|
|
// client disconnects. 0: off.
|
|
std::atomic<uint16_t> usb_nbd_port{0};
|
|
|
|
// 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<uint8_t> sd_cid_manfid{0x03}; // 0x03 = SanDisk
|
|
std::atomic<uint16_t> sd_cid_oemid{0x5344}; // "SD"
|
|
std::atomic<uint64_t> sd_cid_prod_name{0x3155445355ULL}; // "USDU1" (low byte first)
|
|
std::atomic<uint8_t> sd_cid_hwrev{1}; // 0-15
|
|
std::atomic<uint8_t> sd_cid_fwrev{0}; // 0-15
|
|
std::atomic<uint32_t> sd_cid_serial{0xC0FFEE42};
|
|
std::atomic<uint8_t> sd_cid_month{10}; // 1-12
|
|
std::atomic<uint16_t> 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<uint8_t> emmc_cid_manfid{0x90}; // 0x90 = SK Hynix
|
|
std::atomic<uint8_t> emmc_cid_oemid{0x01};
|
|
std::atomic<uint64_t> emmc_cid_prod_name{0x326134474248ULL}; // "HBG4a2" (low byte first)
|
|
std::atomic<uint8_t> emmc_cid_prv{0x07}; // displayed as "0.7" (low.high)
|
|
std::atomic<uint32_t> emmc_cid_serial{0x12345678};
|
|
std::atomic<uint8_t> emmc_cid_month{6}; // 1-12
|
|
std::atomic<uint16_t> 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<uint8_t> als_part_id{0x71};
|
|
std::atomic<uint16_t> als_visible{128}; // DATA0 counts
|
|
std::atomic<uint16_t> als_ir{21}; // DATA1 counts
|
|
|
|
// Bluetooth radio (Broadcom CYW4356, HCI/H4 on UART-D plus five control
|
|
// lines on GPIO port H). See BtRadioMode above for what each mode models.
|
|
std::atomic<uint8_t> bt_radio{BT_RADIO_HEALTHY};
|
|
|
|
// WLAN radio (the other half of the same CYW4356, a PCIe endpoint on
|
|
// root port 1). See WifiRadioMode above for what each mode models.
|
|
std::atomic<uint8_t> wifi_radio{WIFI_RADIO_HEALTHY};
|
|
|
|
// Dead chips and rails, a FaultId bitmask. See FaultId above.
|
|
std::atomic<uint32_t> faults{0};
|
|
|
|
// SoC thermal sensor (TMP451, I2C5 @ 0x4C).
|
|
std::atomic<int16_t> soc_temp_c10{420}; // remote channel (SoC die): °C * 10
|
|
std::atomic<int16_t> pcb_temp_c10{350}; // local channel (PCB): °C * 10
|
|
|
|
// Charger (BQ24193, I2C1 @ 0x6B).
|
|
std::atomic<uint8_t> chg_vbus_stat{0}; // 0=none, 1=USB-SDP, 2=adapter, 3=OTG
|
|
std::atomic<uint8_t> chg_chrg_stat{0}; // 0=not charging, 1=pre, 2=fast, 3=done
|
|
std::atomic<bool> 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<uint16_t> chg_input_current_ma{2000}; // table: 100/150/500/900/1200/1500/2000/3000
|
|
std::atomic<uint16_t> chg_input_voltage_mv{4280}; // 3880-5080 mV, 80 mV step
|
|
std::atomic<uint16_t> chg_system_min_mv{3500}; // 3000-3700 mV, 100 mV step
|
|
std::atomic<uint16_t> chg_fast_current_ma{1856}; // 512-4544 mA, 64 mA step
|
|
std::atomic<uint16_t> chg_charge_voltage_mv{4208}; // 3504-4512 mV, 16 mV step
|
|
std::atomic<uint8_t> chg_thermal_c{60}; // 60 / 80 / 100 / 120 °C
|
|
|
|
// USB-PD controller (BM92T36, I2C1 @ 0x18). One synthesized fixed PDO.
|
|
std::atomic<bool> usb_pd_inserted{false};
|
|
// The dock on the USB-C port (t210/dock.h DockKind) and whether a TV is
|
|
// on its HDMI output. [dock] kind=0 none, 1 Nintendo dock, 2 third-party
|
|
// adapter, 3 Nintendo charger; tv=0/1. --dock overrides.
|
|
std::atomic<uint8_t> dock_kind{0};
|
|
std::atomic<bool> dock_tv{true};
|
|
std::atomic<uint16_t> usb_pd_voltage_mv{5000};
|
|
std::atomic<uint16_t> 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<std::atomic<uint32_t>, FUSE_WORDS> fuse_word{};
|
|
std::atomic<uint32_t>& fuse_at(uint32_t offset) { return fuse_word[(offset & 0x3FC) / 4]; }
|
|
|
|
void init_fuse_defaults() {
|
|
for (auto &w : fuse_word)
|
|
w.store(0);
|
|
// 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<uint8_t> dram_vendor{1}; // MR5 MAN_ID (1=Samsung, 6=Hynix, 255=Micron)
|
|
std::atomic<uint8_t> dram_rev_id1{2}; // MR6 REV_ID1
|
|
std::atomic<uint8_t> dram_rev_id2{0}; // MR7 REV_ID2
|
|
std::atomic<uint8_t> 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<uint32_t> 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<uint8_t> touch_panel_idx{0}; // 0..4 -> known panels, 5 -> GiS VA 6.2"
|
|
std::atomic<uint32_t> touch_fw_id{0x32000001}; // displayed as 4CD60D/<n>
|
|
std::atomic<uint16_t> touch_ftb_ver{0x0123};
|
|
std::atomic<uint16_t> 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). Their register blocks are NOT
|
|
// evenly spaced -- bdk soc/uart.c carries the offset table
|
|
// { 0, 0x40, 0x200, 0x300, 0x400 } -- so the mmio dispatcher resolves the
|
|
// port index against that same table (uart_bases[] in t210/mmio.cpp), not
|
|
// by dividing the offset. Index order is therefore A=0..E=4.
|
|
//
|
|
// - 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 waiting to be consumed by the next UART
|
|
// read (RBR via offset 0x00, with bit 0 of LSR at offset 0x14 set when
|
|
// non-empty). Filled by the console window, the input script, 16550
|
|
// internal loopback, and emulated devices such as the CYW4356 on
|
|
// UART_D.
|
|
//
|
|
// 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<uint8_t> uart_rx_fifo[N_UARTS];
|
|
};
|
|
|
|
#endif // EMU_STATE_H
|