-
Notifications
You must be signed in to change notification settings - Fork 0
/
tb_uart.cpp
85 lines (74 loc) · 1.81 KB
/
tb_uart.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Vcpp_cpu_top.h"
#include "util.hpp"
#include <cstdio>
#include <fstream>
#include <iostream>
#include <limits>
#include <vector>
#include <verilated.h>
#define UART
// Set the clock speed of your processor. 10MHz is the default.
static constexpr std::size_t clock_Hz = 10'000'000;
// UART baudrate
static constexpr std::size_t uart_Hz = 115200;
// The number of CoreMark iterations is depend on clock speed.
// Max: 30 seconds
static constexpr std::size_t max_cycle = 1 * clock_Hz;
std::size_t timer_ps = 0;
const std::size_t second = 1'000'000'000'000; // 1 second = 1,000,000,000,000 ps
int uart_rx(unsigned int u) {
static std::size_t s = 0;
static std::size_t b = 0;
static char c = 0;
if (s == 0 && u == 0) {
s = timer_ps;
b = 0;
c = 0;
} else if (s != 0 &&
s + 1'000'000'000'000 / uart_Hz / 2 * (2 * b + 3) < timer_ps) {
if (b < 8) {
c += u << b;
++b;
} else {
std::putchar(c);
std::fflush(stdout);
s = 0;
return c;
}
}
// return MAX_CHAR+1 to indicate no char received
return std::numeric_limits<char>::max() + 1;
}
int main() {
Vcpp_cpu_top top;
top.clk = 0;
top.eval();
top.rstn = 0;
top.eval();
top.clk = 1;
top.eval();
top.clk = 0;
top.eval();
top.rstn = 1;
top.eval();
std::vector<char> c = {};
for (std::size_t cycle = 0; cycle < max_cycle; ++cycle) {
top.clk = 0;
top.eval();
top.clk = 1;
top.eval();
int tc = uart_rx(top.uart_tx);
if (tc != std::numeric_limits<char>::max() + 1) {
c.push_back(static_cast<char>(tc));
}
timer_ps += 1'000'000'000'000 / clock_Hz;
}
std::printf("\n");
if (c.size() == 1 && c[0] == 'A') {
return 0;
} else {
eprintf("Error: got %lu characters, but expected 1 character 'A'.\n",
c.size());
return 1;
}
}