Skip to content

Commit

Permalink
Add raw output option
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgerhardt committed May 12, 2022
1 parent 04e35a2 commit 5616a10
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
36 changes: 27 additions & 9 deletions simavr/sim/avr_uart.c
Expand Up @@ -35,6 +35,7 @@
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include "avr_uart.h"
#include "sim_hex.h"
#include "sim_time.h"
Expand All @@ -46,6 +47,7 @@
#endif

DEFINE_FIFO(uint16_t, uart_fifo);
static bool uart_raw_mode = false;

static inline void
avr_uart_clear_interrupt(
Expand Down Expand Up @@ -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);)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions simavr/sim/avr_uart.h
Expand Up @@ -26,6 +26,7 @@
extern "C" {
#endif

#include <stdbool.h>
#include "sim_avr.h"

#include "fifo_declare.h"
Expand Down Expand Up @@ -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 = { \
Expand Down
8 changes: 8 additions & 0 deletions simavr/sim/run_avr.c
Expand Up @@ -21,6 +21,7 @@

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <libgen.h>
#include <string.h>
#include <signal.h>
Expand All @@ -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"

Expand All @@ -55,6 +57,7 @@ display_usage(
" [-ti <vector>] Add traces for IRQ vector <vector>\n"
" [--input|-i <file>] A VCD file to use as input signals\n"
" [--output|-o <file>] A VCD file to save the traced signals\n"
" [--raw|-r] Print UART output of firmware without filtering\n"
" [--add-trace|-at <name=kind@addr/mask>]\n"
" Add signal to be included in VCD output\n"
" [-ff <.hex file>] Load next .hex file as flash\n"
Expand Down Expand Up @@ -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]));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5616a10

Please sign in to comment.