Files
RCM-Emulator/t210/regcache.h
T
Claude f19a52b779 Run CCPLEX CPU0 so hwtest's PCIe probe completes, and speed up the core
hwtest-rcm's Wi-Fi probe hands PCIe to an AArch64 stub on CPU0, because
PCIe and MSELECT answer a CPU-complex master only. The emulator had no
CCPLEX, so the stub never reported; and the sweep never got that far
anyway, because it hung in the fan test.

CPU0 (t210/ccplex.cpp)
- A second Unicorn engine, AArch64, Cortex-A57, sharing IRAM, DRAM, TZRAM
  and every peripheral model with the BPMP. It starts when
  RST_CPUG_CMPLX_CLR releases CPU0 with every precondition of bdk's
  ccplex_boot_cpu0() met: CPU rail (MAX77621 + MAX77620 GPIO5 on Erista,
  MAX77812 EN_M4 on Mariko), CRAIL/C0NC/CE0, CPUG clock, PLLX, MSELECT,
  RAM repair, an AArch64 vector in SB_AA64_RESET_LOW/HIGH. A release that
  misses one is refused and the reason printed.
- Entered at EL3, MMU off, like silicon. Unicorn builds its ARM64 core at
  EL1 and neither a PSTATE write (stale hflags) nor an exception (never
  delivered) moves it, so a one-instruction ERET trampoline does.
- Its own clock (1 ns/instruction, 250 ns/bus access), run in 250 us
  lockstep slices behind the BPMP; WFE/WFI park it to the slice end.
- PCIe is master-aware: the BPMP still reads all ones, CPU0 is served.
  MSELECT, AFI_PCIE_CONFIG, LNKSTA, the endpoint class/BAR, the PCIE
  partition, PEX_L1 pinmux and PMC_GLB_AMAP_CFG take their measured values.

hwtest now runs its whole sweep and reports "WLAN core enumerated on the
PCIe bus" / "Wireless : pass", matching the healthy-console capture in
its README.

Performance
- No per-instruction hooks. Three UC_HOOK_CODE callbacks (one registered
  twice, one doing a uc_mem_read per instruction) are replaced by a block
  hook that counts each block's instructions once (cached), credited when
  it has run - the same totals, so the clock is unchanged.
- Peripherals are uc_mmio_map() windows instead of memory hooks, which
  had forced every guest load/store onto Unicorn's slow path; the register
  cache is a paged array instead of a std::map.
- The display reads the framebuffer straight from host DRAM, reuses its
  buffers, skips unchanged frames, and no renderer uses PRESENTVSYNC (a
  vsync'd present could stall the emulated CPU for a whole frame).
Reaching hwtest's fan test: 2.7 s -> 0.8 s; whole sweep ~3 s.

Reliability
- TIMERUS poll pacing: bdk's fan_get_speed() waits for TIMERUS to EQUAL a
  deadline (int - u32 is unsigned, so `> 0` is `!= 0`), and the dilated
  clock stepped past it, so the loop spun until TIMERUS wrapped. Polls at
  most 200 instructions apart now advance the clock by at most 1 us.
- MAX77621/MAX77812 are register files: bdk's hw_init() disabling the
  Erista CPU/GPU bucks used to be ignored and read back as enabled.
- Mariko reads FUSE_RESERVED_ODM28_B01 bit 0 set, so bdk addresses the
  retail MAX77812 at 0x33 instead of the NAK'd 0x31.
- Windows: disk images are opened O_BINARY (MinGW defaults to text mode,
  corrupting sectors) and rawnand chunks are sized with _fstati64.
- Soft reboot resets CPU0 and the PCIe root complex; the payload is
  loaded once, size-checked; an empty/unreadable payload is rejected; the
  NOP-slide check no longer trips on a counter that never reset; the M/C
  windows fall back to a software renderer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
2026-09-25 15:55:48 +00:00

87 lines
2.5 KiB
C++

#ifndef T210_REGCACHE_H
#define T210_REGCACHE_H
#include <cstdint>
#include <cstring>
#include <memory>
#include <unordered_map>
// Last-written value of every MMIO address, for the registers whose model is
// "read back what the payload wrote" (PINMUX, PWM, UART LCR, CLK_SOURCE_*...).
//
// This sits on the hottest path in the emulator - every MMIO write stores
// into it and a good share of reads consult it - and it used to be a
// std::map<uint64_t, uint32_t>: a red-black tree walk plus, for a new key, a
// heap allocation per access. Profiling a hwtest sweep put the tree lookups
// on par with the peripheral models themselves.
//
// Registers cluster into a few dozen 4 KiB pages, so this keeps one flat page
// per touched page with a presence bitmap, and caches the last page looked
// up. The interface is the subset of std::map the call sites use (count() and
// operator[]), so they did not have to change. Keys stay byte-granular: a
// byte store at 0x70006001 is its own entry, exactly as it was in the map.
class RegCache {
struct Page {
uint32_t val[4096];
uint64_t present[4096 / 64];
};
std::unordered_map<uint64_t, std::unique_ptr<Page>> pages_;
mutable uint64_t last_key_ = ~0ULL;
mutable Page *last_ = nullptr;
Page *find(uint64_t key) const {
if (key == last_key_)
return last_;
auto it = pages_.find(key);
if (it == pages_.end())
return nullptr;
last_key_ = key;
last_ = it->second.get();
return last_;
}
public:
size_t count(uint64_t addr) const {
const Page *p = find(addr >> 12);
if (!p)
return 0;
uint32_t o = (uint32_t)(addr & 0xFFF);
return (p->present[o >> 6] >> (o & 63)) & 1;
}
uint32_t &operator[](uint64_t addr) {
uint64_t key = addr >> 12;
Page *p = find(key);
if (!p) {
auto page = std::make_unique<Page>();
memset(page.get(), 0, sizeof(Page));
p = page.get();
pages_.emplace(key, std::move(page));
last_key_ = key;
last_ = p;
}
uint32_t o = (uint32_t)(addr & 0xFFF);
p->present[o >> 6] |= 1ULL << (o & 63);
return p->val[o];
}
// Value if ever written, else `dflt`. One lookup instead of the
// count()-then-operator[] pair.
uint32_t get(uint64_t addr, uint32_t dflt = 0) const {
const Page *p = find(addr >> 12);
if (!p)
return dflt;
uint32_t o = (uint32_t)(addr & 0xFFF);
return ((p->present[o >> 6] >> (o & 63)) & 1) ? p->val[o] : dflt;
}
void clear() {
pages_.clear();
last_key_ = ~0ULL;
last_ = nullptr;
}
};
#endif // T210_REGCACHE_H