From 495bffdc7c11c94843e8b58e73d6dae46910ed80 Mon Sep 17 00:00:00 2001 From: Rob Taylor Date: Thu, 18 Dec 2025 23:50:32 +0000 Subject: [PATCH] feat: Write UART output to uart.log file for VS Code extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add open_uart_log() and close_uart_log() functions to write UART output to a dedicated file at PROJECT_ROOT/uart.log. The file is flushed after each character for real-time monitoring by the ChipFlow VS Code extension. This enables the extension's UART Console panel to display simulation output in real-time by watching the log file. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- chipflow/common/sim/main.cc.jinja | 2 ++ chipflow/common/sim/models.cc | 28 ++++++++++++++++++++++++++-- chipflow/common/sim/models.h | 4 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/chipflow/common/sim/main.cc.jinja b/chipflow/common/sim/main.cc.jinja index cd72b861..b7097ad5 100644 --- a/chipflow/common/sim/main.cc.jinja +++ b/chipflow/common/sim/main.cc.jinja @@ -26,6 +26,7 @@ int main(int argc, char **argv) { std::cerr << "Waiting for debugger on " << agent.start_debugging() << std::endl; open_event_log(BUILD_DIR "/sim/events.json"); + open_uart_log(PROJECT_ROOT "/uart.log"); open_input_commands(PROJECT_ROOT "/design/tests/input.json"); unsigned timestamp = 0; @@ -74,5 +75,6 @@ int main(int argc, char **argv) { tick(); close_event_log(); + close_uart_log(); return 0; } diff --git a/chipflow/common/sim/models.cc b/chipflow/common/sim/models.cc index 0aabda4f..b4863aca 100644 --- a/chipflow/common/sim/models.cc +++ b/chipflow/common/sim/models.cc @@ -86,6 +86,9 @@ void open_input_commands(const std::string &filename) { static std::ofstream event_log; +// UART log file for VS Code extension +static FILE *uart_log = nullptr; + void open_event_log(const std::string &filename) { event_log.open(filename); if (!event_log) { @@ -135,6 +138,22 @@ void close_event_log() { } } +void open_uart_log(const std::string &filename) { + uart_log = fopen(filename.c_str(), "w"); + if (!uart_log) { + fprintf(stderr, "WARNING: failed to open UART log file: %s\n", filename.c_str()); + } else { + fprintf(stderr, "Logging UART to file: %s\n", filename.c_str()); + } +} + +void close_uart_log() { + if (uart_log) { + fclose(uart_log); + uart_log = nullptr; + } +} + namespace models { // SPI flash @@ -246,10 +265,15 @@ void uart::step(unsigned timestamp) { s.rx_sr = (tx ? 0x80U : 0x00U) | (s.rx_sr >> 1U); } if (bit == 8) { - // print to console + // print to console and log file log_event(timestamp, name, "tx", json(s.rx_sr)); - if (name == "uart_0") + if (name == "uart_0") { fprintf(stderr, "%c", char(s.rx_sr)); + if (uart_log) { + fprintf(uart_log, "%c", char(s.rx_sr)); + fflush(uart_log); // Flush for real-time watching + } + } } if (bit == 9) { // end diff --git a/chipflow/common/sim/models.h b/chipflow/common/sim/models.h index 8147bddc..69806a8c 100644 --- a/chipflow/common/sim/models.h +++ b/chipflow/common/sim/models.h @@ -30,6 +30,10 @@ void log_event(unsigned timestamp, const std::string &peripheral, const std::str std::vector get_pending_actions(const std::string &peripheral); void close_event_log(); +// UART log file for VS Code extension real-time monitoring +void open_uart_log(const std::string &filename); +void close_uart_log(); + namespace models {