From e527872acb133f4ff56f10e3efc1e5f6d5c36c13 Mon Sep 17 00:00:00 2001 From: Baptiste MOINE Date: Sun, 3 May 2026 08:57:46 +0200 Subject: [PATCH] Track per-port UART TX log and host-injected RX FIFO --- emu_state.h | 21 +++++++++++++++++ t210/mmio.cpp | 63 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/emu_state.h b/emu_state.h index b23f8cb..6cfbbe9 100644 --- a/emu_state.h +++ b/emu_state.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -289,6 +290,26 @@ struct EmuState { 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 diff --git a/t210/mmio.cpp b/t210/mmio.cpp index e056a12..923608d 100644 --- a/t210/mmio.cpp +++ b/t210/mmio.cpp @@ -554,9 +554,26 @@ uint32_t misc_read(EmuState *state, uint64_t addr) { // UART if (addr >= 0x70006000 && addr < 0x70006500) { + uint32_t port = (addr - 0x70006000) / 0x40; uint32_t offset = (addr - 0x70006000) % 0x40; - if (offset == 0x14) - return 0x60; // LSR: THRE | TMTY always ready + if (offset == 0x14) { + // LSR: THRE | TMTY (0x60) always ready, plus RDR (bit 0) when our + // host-side RX FIFO has bytes queued by the console window. + uint32_t lsr = 0x60; + if (port < EmuState::N_UARTS && !state->uart_rx_fifo[port].empty()) + lsr |= 0x01; + return lsr; + } + if (offset == 0x00) { + // RBR (read alias of THR/DLL when DLAB=0). Pop the next host-injected + // byte; if the FIFO is empty return 0 (the payload should have + // checked LSR.RDR first). + if (port < EmuState::N_UARTS && !state->uart_rx_fifo[port].empty()) { + uint8_t b = state->uart_rx_fifo[port].front(); + state->uart_rx_fifo[port].pop_front(); + return b; + } + } return 0; } // DSI @@ -1673,30 +1690,38 @@ static void hook_mmio_write(uc_engine *uc, uc_mem_type type, uint64_t address, } else if (address >= SDMMC4_BASE && address < SDMMC4_BASE + 0x200) { misc_write(uc, state, address, value, size); } else if (address >= 0x70006000 && address < 0x70006500) { + uint32_t port = (address - 0x70006000) / 0x40; uint32_t offset = (address - 0x70006000) % 0x40; - if (offset == 0) { - // UART_THR. Hekate's UART carries Joy-Con HID protocol bytes (binary) - // in addition to occasional debug text. We buffer printable ASCII into - // a per-line buffer and flush on \n / \r / overflow so that a - // gfx_putc-mirrored println shows up as a single [uart] line. - static char uart_line[512]; - static size_t uart_len = 0; - auto flush_uart_line = []() { - if (uart_len > 0) { - uart_line[uart_len] = 0; - printf("[uart] %s\n", uart_line); + if (offset == 0 && port < EmuState::N_UARTS) { + uint8_t b = (uint8_t)val; + + // Append every byte to the per-port TX log — the console window + // renders this as scrolling text. Trim from the front if it grows + // past 64 KB so ImGui rendering stays responsive. + std::string &log = state->uart_tx_log[port]; + if (b == '\n' || (b >= 0x20 && b < 0x7F)) + log.push_back((char)b); + if (log.size() > 64 * 1024) + log.erase(0, log.size() - 48 * 1024); + + // Mirror to host stdout in line-buffered form so existing + // `grep '[uart]'` workflows still work. + static char uart_line[5][512]; + static size_t uart_len[5] = {0}; + auto flush_uart_line = [&](uint32_t p) { + if (uart_len[p] > 0) { + uart_line[p][uart_len[p]] = 0; + printf("[uart%c] %s\n", 'A' + (char)p, uart_line[p]); fflush(stdout); - uart_len = 0; + uart_len[p] = 0; } }; - uint8_t b = (uint8_t)val; if (b == '\n' || b == '\r') { - flush_uart_line(); + flush_uart_line(port); } else if (b >= 0x20 && b < 0x7F) { - if (uart_len + 1 >= sizeof(uart_line)) flush_uart_line(); - uart_line[uart_len++] = (char)b; + if (uart_len[port] + 1 >= sizeof(uart_line[port])) flush_uart_line(port); + uart_line[port][uart_len[port]++] = (char)b; } - // Non-printables (Joy-Con HID etc.) are dropped silently. } } else if (address >= VIC_BASE && address < VIC_BASE + VIC_SIZE) { vic_write(state, address, val);