From 5616a101a1d26470d474147039bd306021043ec0 Mon Sep 17 00:00:00 2001 From: Max Gerhardt Date: Fri, 13 May 2022 00:57:40 +0200 Subject: [PATCH] Add raw output option --- simavr/sim/avr_uart.c | 36 +++++++++++++++++++++++++++--------- simavr/sim/avr_uart.h | 2 ++ simavr/sim/run_avr.c | 8 ++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/simavr/sim/avr_uart.c b/simavr/sim/avr_uart.c index 25b6fe652..a337ecc07 100644 --- a/simavr/sim/avr_uart.c +++ b/simavr/sim/avr_uart.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "avr_uart.h" #include "sim_hex.h" #include "sim_time.h" @@ -46,6 +47,7 @@ #endif DEFINE_FIFO(uint16_t, uart_fifo); +static bool uart_raw_mode = false; static inline void avr_uart_clear_interrupt( @@ -269,15 +271,20 @@ avr_uart_udr_write( } if (p->flags & AVR_UART_FLAG_STDIO) { - const int maxsize = 256; - if (!p->stdio_out) - p->stdio_out = malloc(maxsize); - p->stdio_out[p->stdio_len++] = v < ' ' ? '.' : v; - p->stdio_out[p->stdio_len] = 0; - if (v == '\n' || p->stdio_len == maxsize) { - p->stdio_len = 0; - AVR_LOG(avr, LOG_OUTPUT, - FONT_GREEN "%s\n" FONT_DEFAULT, p->stdio_out); + if (uart_raw_mode) { + // no filtering / transformation applied in raw mode + putchar((int) v); + } else { + const int maxsize = 256; + if (!p->stdio_out) + p->stdio_out = malloc(maxsize); + p->stdio_out[p->stdio_len++] = v < ' ' ? '.' : v; + p->stdio_out[p->stdio_len] = 0; + if (v == '\n' || p->stdio_len == maxsize) { + p->stdio_len = 0; + AVR_LOG(avr, LOG_OUTPUT, + FONT_GREEN "%s\n" FONT_DEFAULT, p->stdio_out); + } } } TRACE(printf("UDR%c(%02x) = %02x\n", p->name, addr, v);) @@ -505,6 +512,17 @@ static avr_io_t _io = { .irq_names = irq_names, }; +void +avr_uart_set_raw_output_mode( + bool raw_mode) +{ + uart_raw_mode = raw_mode; + if (uart_raw_mode) { + //disable buffering for firmware output + setvbuf(stdout, NULL, _IONBF, 0); + } +} + void avr_uart_init( avr_t * avr, diff --git a/simavr/sim/avr_uart.h b/simavr/sim/avr_uart.h index 107359e3a..5149664b6 100644 --- a/simavr/sim/avr_uart.h +++ b/simavr/sim/avr_uart.h @@ -26,6 +26,7 @@ extern "C" { #endif +#include #include "sim_avr.h" #include "fifo_declare.h" @@ -135,6 +136,7 @@ typedef struct avr_uart_t { #define AVR_IOCTL_UART_GET_FLAGS(_name) AVR_IOCTL_DEF('u','a','g',(_name)) void avr_uart_init(avr_t * avr, avr_uart_t * port); +void avr_uart_set_raw_output_mode(bool raw_mode); #define AVR_UARTX_DECLARE(_name, _prr, _prusart) \ .uart ## _name = { \ diff --git a/simavr/sim/run_avr.c b/simavr/sim/run_avr.c index a4f79e7bc..bbeb05ef7 100644 --- a/simavr/sim/run_avr.c +++ b/simavr/sim/run_avr.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -30,6 +31,7 @@ #include "sim_gdb.h" #include "sim_hex.h" #include "sim_vcd_file.h" +#include "avr_uart.h" #include "sim_core_decl.h" @@ -55,6 +57,7 @@ display_usage( " [-ti ] Add traces for IRQ vector \n" " [--input|-i ] A VCD file to use as input signals\n" " [--output|-o ] A VCD file to save the traced signals\n" + " [--raw|-r] Print UART output of firmware without filtering\n" " [--add-trace|-at ]\n" " Add signal to be included in VCD output\n" " [-ff <.hex file>] Load next .hex file as flash\n" @@ -108,6 +111,7 @@ main( int trace_vectors[8] = {0}; int trace_vectors_count = 0; const char *vcd_input = NULL; + bool raw_stdio_output = false; if (argc == 1) display_usage(basename(argv[0])); @@ -139,6 +143,9 @@ main( exit(1); } snprintf(f.tracename, sizeof(f.tracename), "%s", argv[++pi]); + } else if (!strcmp(argv[pi], "-r") || + !strcmp(argv[pi], "--raw")) { + raw_stdio_output = true; } else if (!strcmp(argv[pi], "-t") || !strcmp(argv[pi], "--trace")) { #ifdef CONFIG_SIMAVR_TRACE @@ -272,6 +279,7 @@ main( exit(1); } avr_init(avr); + avr_uart_set_raw_output_mode(raw_stdio_output); avr->log = (log > LOG_TRACE ? LOG_TRACE : log); #ifdef CONFIG_SIMAVR_TRACE avr->trace = trace;