mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
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
288 lines
9.2 KiB
C++
288 lines
9.2 KiB
C++
// In-emulator UART console.
|
|
//
|
|
// A second SDL window backed by its own ImGui context. The main window
|
|
// toggles it via the C key. The window:
|
|
//
|
|
// 1. Lets you pick which Tegra UART port to inspect (UART_A..UART_E).
|
|
// 2. Scrolls the selected port's TX history (every byte the payload
|
|
// writes to that port's THR is appended to EmuState::uart_tx_log[idx]
|
|
// by the mmio dispatcher).
|
|
// 3. Lets you type bytes that get pushed into EmuState::uart_rx_fifo[idx]
|
|
// so the next CPU-side `uart_recv()` returns them. There's a "Send"
|
|
// button plus shortcut buttons for the keys hwtest's pager listens
|
|
// for (n / p / r / s / q).
|
|
//
|
|
// All TX append / RX pop is single-threaded — both the CPU emulation and
|
|
// ImGui rendering run on the main thread between SDL polls — so no
|
|
// locking. The TX log is trimmed if it grows past ~64 KB to keep imgui
|
|
// rendering snappy.
|
|
|
|
#include "console_window.h"
|
|
#include "emu_state.h"
|
|
|
|
#include "imgui.h"
|
|
#include "backends/imgui_impl_sdl2.h"
|
|
#include "backends/imgui_impl_sdlrenderer2.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
namespace {
|
|
|
|
SDL_Window *g_window = nullptr;
|
|
SDL_Renderer *g_renderer = nullptr;
|
|
ImGuiContext *g_imgui = nullptr;
|
|
bool g_visible = false;
|
|
Uint32 g_window_id = 0;
|
|
bool g_sdl2_inited = false;
|
|
bool g_renderer_inited = false;
|
|
|
|
// Currently-displayed UART index (0..4).
|
|
int g_port = 1; // UART_B by default — that's hwtest's debug port.
|
|
|
|
// Send-line buffer. Cleared on submit.
|
|
char g_send_buf[256] = {0};
|
|
|
|
const char *kPortNames[] = {"UART_A", "UART_B", "UART_C", "UART_D", "UART_E"};
|
|
|
|
void inject_bytes(EmuState *state, const uint8_t *data, size_t n)
|
|
{
|
|
if (g_port < 0 || g_port >= (int)EmuState::N_UARTS) return;
|
|
auto &fifo = state->uart_rx_fifo[g_port];
|
|
for (size_t i = 0; i < n; i++)
|
|
fifo.push_back(data[i]);
|
|
}
|
|
|
|
void inject_str(EmuState *state, const char *s)
|
|
{
|
|
inject_bytes(state, (const uint8_t *)s, strlen(s));
|
|
}
|
|
|
|
void build_ui(EmuState *state)
|
|
{
|
|
int win_w = 0, win_h = 0;
|
|
SDL_GetWindowSize(g_window, &win_w, &win_h);
|
|
ImGui::SetNextWindowPos(ImVec2(0, 0));
|
|
ImGui::SetNextWindowSize(ImVec2((float)win_w, (float)win_h));
|
|
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
|
|
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
|
|
ImGuiWindowFlags_NoBringToFrontOnFocus;
|
|
ImGui::Begin("##console", nullptr, flags);
|
|
|
|
// --- Top row: port selector + clear button ---
|
|
ImGui::SetNextItemWidth(140);
|
|
ImGui::Combo("Port", &g_port, kPortNames, IM_ARRAYSIZE(kPortNames));
|
|
ImGui::SameLine();
|
|
if (ImGui::Button("Clear")) {
|
|
state->uart_tx_log[g_port].clear();
|
|
}
|
|
ImGui::SameLine();
|
|
ImGui::Text("rx fifo: %d byte(s)", (int)state->uart_rx_fifo[g_port].size());
|
|
|
|
// --- Scrolling output area ---
|
|
ImGui::Separator();
|
|
{
|
|
const float input_h = ImGui::GetFrameHeightWithSpacing() * 3 + 4;
|
|
ImGui::BeginChild("scroll", ImVec2(0, -input_h), true,
|
|
ImGuiWindowFlags_HorizontalScrollbar);
|
|
const std::string &log = state->uart_tx_log[g_port];
|
|
if (!log.empty()) {
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0xCC, 0xCC, 0xCC, 0xFF));
|
|
ImGui::TextUnformatted(log.data(), log.data() + log.size());
|
|
ImGui::PopStyleColor();
|
|
}
|
|
// Auto-scroll to bottom unless the user has scrolled up manually.
|
|
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY() - 1.0f)
|
|
ImGui::SetScrollHereY(1.0f);
|
|
ImGui::EndChild();
|
|
}
|
|
|
|
// --- Send line + shortcuts ---
|
|
ImGui::Separator();
|
|
bool focus_input = false;
|
|
if (ImGui::InputText("##send", g_send_buf, sizeof(g_send_buf),
|
|
ImGuiInputTextFlags_EnterReturnsTrue)) {
|
|
if (g_send_buf[0]) {
|
|
inject_str(state, g_send_buf);
|
|
g_send_buf[0] = 0;
|
|
}
|
|
focus_input = true;
|
|
}
|
|
ImGui::SameLine();
|
|
if (ImGui::Button("Send")) {
|
|
if (g_send_buf[0]) {
|
|
inject_str(state, g_send_buf);
|
|
g_send_buf[0] = 0;
|
|
}
|
|
focus_input = true;
|
|
}
|
|
ImGui::SameLine();
|
|
if (ImGui::Button("+ LF")) {
|
|
if (g_send_buf[0]) {
|
|
inject_str(state, g_send_buf);
|
|
g_send_buf[0] = 0;
|
|
}
|
|
const uint8_t lf = '\n';
|
|
inject_bytes(state, &lf, 1);
|
|
focus_input = true;
|
|
}
|
|
|
|
// Shortcut row — hwtest's pager keys.
|
|
ImGui::TextDisabled("Shortcuts:");
|
|
ImGui::SameLine();
|
|
static const struct { const char *label; char key; } kShortcuts[] = {
|
|
{"n (next)", 'n'},
|
|
{"p (prev)", 'p'},
|
|
{"r (refresh)", 'r'},
|
|
{"s (save)", 's'},
|
|
{"q (off)", 'q'},
|
|
{"CR", '\r'},
|
|
{"LF", '\n'},
|
|
};
|
|
for (size_t i = 0; i < sizeof(kShortcuts) / sizeof(kShortcuts[0]); i++) {
|
|
if (i > 0) ImGui::SameLine();
|
|
if (ImGui::Button(kShortcuts[i].label)) {
|
|
uint8_t b = (uint8_t)kShortcuts[i].key;
|
|
inject_bytes(state, &b, 1);
|
|
}
|
|
}
|
|
|
|
if (focus_input)
|
|
ImGui::SetKeyboardFocusHere(-1);
|
|
|
|
ImGui::End();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool console_window_init()
|
|
{
|
|
g_window = SDL_CreateWindow("rcm_emu - UART Console",
|
|
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
|
720, 540,
|
|
SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
|
|
if (!g_window) {
|
|
fprintf(stderr, "[console_window] SDL_CreateWindow failed: %s\n", SDL_GetError());
|
|
return false;
|
|
}
|
|
// No PRESENTVSYNC: this window renders on the emulation thread, and
|
|
// vsync would stall the emulated CPU for up to a frame per present.
|
|
g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED);
|
|
// Same fallback as the main window: no GPU renderer (a headless run, a
|
|
// VM without GL) should not cost the window altogether.
|
|
if (!g_renderer)
|
|
g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_SOFTWARE);
|
|
if (!g_renderer) {
|
|
fprintf(stderr, "[console_window] SDL_CreateRenderer failed: %s\n", SDL_GetError());
|
|
SDL_DestroyWindow(g_window);
|
|
g_window = nullptr;
|
|
return false;
|
|
}
|
|
|
|
IMGUI_CHECKVERSION();
|
|
g_imgui = ImGui::CreateContext();
|
|
ImGui::SetCurrentContext(g_imgui);
|
|
ImGui::StyleColorsDark();
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
io.IniFilename = nullptr; // don't write a sibling imgui.ini
|
|
|
|
if (!ImGui_ImplSDL2_InitForSDLRenderer(g_window, g_renderer)) {
|
|
fprintf(stderr, "[console_window] ImGui SDL2 backend init failed\n");
|
|
ImGui::DestroyContext(g_imgui);
|
|
g_imgui = nullptr;
|
|
return false;
|
|
}
|
|
g_sdl2_inited = true;
|
|
if (!ImGui_ImplSDLRenderer2_Init(g_renderer)) {
|
|
fprintf(stderr, "[console_window] ImGui SDLRenderer2 backend init failed\n");
|
|
ImGui_ImplSDL2_Shutdown();
|
|
g_sdl2_inited = false;
|
|
ImGui::DestroyContext(g_imgui);
|
|
g_imgui = nullptr;
|
|
return false;
|
|
}
|
|
g_renderer_inited = true;
|
|
|
|
g_window_id = SDL_GetWindowID(g_window);
|
|
return true;
|
|
}
|
|
|
|
void console_window_shutdown()
|
|
{
|
|
if (g_imgui) {
|
|
ImGui::SetCurrentContext(g_imgui);
|
|
// Backend Shutdown asserts on `bd != nullptr`. Check the per-context
|
|
// backend pointers directly so we never call Shutdown on a context
|
|
// whose backend data was somehow already cleared (defensive — this
|
|
// handles the case where another ImGui context-switch elsewhere
|
|
// perturbed the global state).
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
if (io.BackendRendererUserData) {
|
|
ImGui_ImplSDLRenderer2_Shutdown();
|
|
}
|
|
if (io.BackendPlatformUserData) {
|
|
ImGui_ImplSDL2_Shutdown();
|
|
}
|
|
g_renderer_inited = false;
|
|
g_sdl2_inited = false;
|
|
ImGui::DestroyContext(g_imgui);
|
|
g_imgui = nullptr;
|
|
}
|
|
if (g_renderer) {
|
|
SDL_DestroyRenderer(g_renderer);
|
|
g_renderer = nullptr;
|
|
}
|
|
if (g_window) {
|
|
SDL_DestroyWindow(g_window);
|
|
g_window = nullptr;
|
|
}
|
|
g_window_id = 0;
|
|
}
|
|
|
|
bool console_window_handle_event(const SDL_Event &ev)
|
|
{
|
|
if (!g_imgui) return false;
|
|
ImGui::SetCurrentContext(g_imgui);
|
|
ImGui_ImplSDL2_ProcessEvent(&ev);
|
|
|
|
// Hide on close.
|
|
if (ev.type == SDL_WINDOWEVENT &&
|
|
ev.window.windowID == g_window_id &&
|
|
ev.window.event == SDL_WINDOWEVENT_CLOSE) {
|
|
SDL_HideWindow(g_window);
|
|
g_visible = false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void console_window_toggle()
|
|
{
|
|
if (!g_window) return;
|
|
g_visible = !g_visible;
|
|
if (g_visible) SDL_ShowWindow(g_window);
|
|
else SDL_HideWindow(g_window);
|
|
}
|
|
|
|
bool console_window_is_visible() { return g_visible; }
|
|
|
|
void console_window_render(EmuState *state)
|
|
{
|
|
if (!g_visible || !g_imgui) return;
|
|
|
|
ImGui::SetCurrentContext(g_imgui);
|
|
SDL_SetRenderDrawColor(g_renderer, 0x10, 0x10, 0x14, 0xFF);
|
|
SDL_RenderClear(g_renderer);
|
|
|
|
ImGui_ImplSDLRenderer2_NewFrame();
|
|
ImGui_ImplSDL2_NewFrame();
|
|
ImGui::NewFrame();
|
|
|
|
build_ui(state);
|
|
|
|
ImGui::Render();
|
|
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), g_renderer);
|
|
SDL_RenderPresent(g_renderer);
|
|
}
|
|
|
|
Uint32 console_window_id() { return g_window_id; }
|