uart: Made the stdio buffer non-static
[simavr] / simavr / sim / avr_uart.c
index 594a6f4..8adfe54 100644 (file)
 #include <stdio.h>
 #include <unistd.h>
 #include <stdint.h>
+#include <stdlib.h>
 #include "avr_uart.h"
 #include "sim_hex.h"
 
+//#define TRACE(_w) _w
+#ifndef TRACE
+#define TRACE(_w)
+#endif
+
 DEFINE_FIFO(uint8_t, uart_fifo);
 
 static avr_cycle_count_t avr_uart_txc_raise(struct avr_t * avr, avr_cycle_count_t when, void * param)
@@ -103,7 +109,7 @@ static uint8_t avr_uart_read(struct avr_t * avr, avr_io_addr_t addr, void * para
        }
        uint8_t v = uart_fifo_read(&p->input);
 
-       //printf("UART read %02x %s\n", v, uart_fifo_isempty(&p->input) ? "EMPTY!" : "");
+//     TRACE(printf("UART read %02x %s\n", v, uart_fifo_isempty(&p->input) ? "EMPTY!" : "");)
        avr->data[addr] = v;
        // made to trigger potential watchpoints
        v = avr_core_watch_read(avr, addr);
@@ -131,8 +137,8 @@ static void avr_uart_baud_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t
        int sb = 1 + avr_regbit_get(avr, p->usbs);
        int word_size = 1 /* start */ + db /* data bits */ + 1 /* parity */ + sb /* stops */;
 
-       printf("UART-%c configured to %04x = %d bps, %d data %d stop\n",
-                       p->name, val, baud, db, sb);
+       printf("UART-%c configured to %04x = %d bps (x%d), %d data %d stop\n",
+                       p->name, val, baud, avr_regbit_get(avr, p->u2x)?2:1, db, sb);
        // TODO: Use the divider value and calculate the straight number of cycles
        p->usec_per_byte = 1000000 / (baud / word_size);
        printf("Roughtly %d usec per bytes\n", (int)p->usec_per_byte);
@@ -151,16 +157,17 @@ static void avr_uart_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, vo
                                p->usec_per_byte, avr_uart_txc_raise, p); // should be uart speed dependent
 
                if (p->flags & AVR_UART_FLAG_STDIO) {
-                       static char buf[128];
-                       static int l = 0;
-                       buf[l++] = v < ' ' ? '.' : v;
-                       buf[l] = 0;
-                       if (v == '\n' || l == 127) {
-                               l = 0;
-                               printf( FONT_GREEN "%s\n" FONT_DEFAULT, buf);
+                       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;
+                               printf( FONT_GREEN "%s\n" FONT_DEFAULT, p->stdio_out);
                        }
                }
-               //printf("UDR%c(%02x) = %02x\n", p->name, addr, v);
+               TRACE(printf("UDR%c(%02x) = %02x\n", p->name, addr, v);)
                // tell other modules we are "outputing" a byte
                if (avr_regbit_get(avr, p->txen))
                        avr_raise_irq(p->io.irq + UART_IRQ_OUTPUT, v);
@@ -207,7 +214,7 @@ static void avr_uart_irq_input(struct avr_irq_t * irq, uint32_t value, void * pa
                avr_cycle_timer_register_usec(avr, p->usec_per_byte, avr_uart_rxc_raise, p); // should be uart speed dependent
        uart_fifo_write(&p->input, value); // add to fifo
 
-       // printf("UART IRQ in %02x (%d/%d) %s\n", value, p->input.read, p->input.write, uart_fifo_isfull(&p->input) ? "FULL!!" : "");
+       TRACE(printf("UART IRQ in %02x (%d/%d) %s\n", value, p->input.read, p->input.write, uart_fifo_isfull(&p->input) ? "FULL!!" : "");)
 
        if (uart_fifo_isfull(&p->input))
                avr_raise_irq(p->io.irq + UART_IRQ_OUT_XOFF, 1);