#include "Vmicrowatt.h"
#include "verilated.h"
#include "verilated_vcd_c.h"
+#include "uart-verilated.h"
/*
* Current simulation time
main_time++;
}
-// simulated uart tx/rx, assumes a baud rate of 115200 based on CLK_FREQUENCY
-void uart_tx(unsigned char tx);
-unsigned char uart_rx(void);
-
-// true if the uart tx and rx are both idle
-// (saves having to save/restore/sync the uart state)
-bool uart_idle(void);
-
// pretty-print dumped data in ASCII (to help identify strings)
static void ascii_dump(unsigned char *data, int len, FILE *dump)
{
char fname[128];
sprintf(fname, "verilator.save.%ld", time);
VerilatedSave os;
+ struct uart_tx_state *uart = uart_get_state();
+
os.open(fname);
os << main_time; // user code must save the timestamp, etc
+ os << *uart;
os << *topp;
}
char fname[128];
sprintf(fname, "verilator.save.%ld", time);
VerilatedRestore os;
+ struct uart_tx_state uart;
os.open(fname);
os >> main_time;
+ os >> uart;
os >> *topp;
+ uart_restore(&uart);
}
//save bram memory out to a file. use for snapshots
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
+#include "uart-verilator.h"
/* Should we exit simulation on ctrl-c or pass it through? */
#define EXIT_ON_CTRL_C
/* Round to nearest */
#define BITWIDTH ((CLK_FREQUENCY+(BAUD/2))/BAUD)
-enum state {
- IDLE, START_BIT, BITS, STOP_BIT, ERROR
-};
-
-/*
- * Our UART uses 16x oversampling, so at 50 MHz and 115200 baud
- * each sample is: 50000000/(115200*16) = 27 clock cycles. This
- * means each bit is off by 0.47% so for 8 bits plus a start and
- * stop bit the errors add to be 4.7%.
- */
-struct uart_tx_state {
- double error = 0.05;
-
- enum state tx_state = IDLE;
- unsigned long tx_countbits;
- unsigned char tx_bits;
- unsigned char tx_byte;
- unsigned char tx_prev;
-
- enum state rx_state = IDLE;
- unsigned char rx_char;
- unsigned long rx_countbits;
- unsigned char rx_bit;
- unsigned char rx = 1;
-};
-
static struct uart_tx_state uart;
/*
return uart.rx;
}
-// cheating, here: to avoid having to save the uart state, check if it
-// is idle (both tx and rx)
-bool uart_state(void)
+struct uart_tx_state * uart_get_state(void)
{
- return (uart.tx_state == IDLE && uart.rx_state == IDLE);
+ return &uart;
}
+void uart_restore(struct uart_tx_state *new_state)
+{
+ uart = *new_state;
+}
--- /dev/null
+/*
+ * Our UART uses 16x oversampling, so at 50 MHz and 115200 baud
+ * each sample is: 50000000/(115200*16) = 27 clock cycles. This
+ * means each bit is off by 0.47% so for 8 bits plus a start and
+ * stop bit the errors add to be 4.7%.
+ */
+
+enum state {
+ IDLE, START_BIT, BITS, STOP_BIT, ERROR
+};
+
+struct uart_tx_state {
+ double error = 0.05;
+
+ enum state tx_state = IDLE;
+ unsigned long tx_countbits;
+ unsigned char tx_bits;
+ unsigned char tx_byte;
+ unsigned char tx_prev;
+
+ enum state rx_state = IDLE;
+ unsigned char rx_char;
+ unsigned long rx_countbits;
+ unsigned char rx_bit;
+ unsigned char rx = 1;
+};
+
+void uart_tx(unsigned char tx);
+unsigned char uart_rx(void);
+struct uart_tx_state * uart_get_state(void);
+void uart_restore(struct uart_tx_state *);
+