-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtop.cpp
More file actions
205 lines (180 loc) · 4.46 KB
/
top.cpp
File metadata and controls
205 lines (180 loc) · 4.46 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <verilated_vcd_c.h>
#include "Vtop.h"
#include "verilated.h"
#include "fifo.h"
VerilatedVcdC *pTrace;
Vtop *pCore;
uint64_t tickcount;
enum UART_CONSTANTS {
CLK = 25000000,
BAUDRATE = 115200,
TICK = CLK / BAUDRATE,
HALF_TICK = TICK / 2
};
char fifo_buf[256];
FIFO fifo_uart_rx = { .first = 0, .count = 0, .len = sizeof(fifo_buf), .buf = fifo_buf};
void debug() {
pCore->debug_wire = !pCore->debug_wire;
}
void opentrace(const char *vcdname) {
if (!pTrace) {
pTrace = new VerilatedVcdC;
pCore->trace(pTrace, 99);
pTrace->open(vcdname);
}
}
char rx() { return pCore->uart_tx; }
// uart_handle() needs to be called every clock cycle
void uart_handle(void) {
static enum state_t { IDLE = 0, STARTBIT = 1, BYTE = 2, STOPBIT = 10 } state = IDLE;
static uint32_t counter = 0;
static unsigned char recbyte = 0;
counter++;
switch( state ) {
case IDLE:
if( rx() == 0 ) {
counter = 0;
state = STARTBIT;
}
break;
case STARTBIT:
if( counter >= HALF_TICK ) {
debug();
if( rx() == 0 ) {
counter = 0;
recbyte = 0;
state = BYTE;
} else {
// error
state = IDLE;
}
}
break;
case STOPBIT:
if( counter >= TICK ) {
debug();
if( rx() == 1 ) {
fifo_push(&fifo_uart_rx, recbyte);
//printf("rec: %c\n", recbyte);
}
state = IDLE;
}
break;
default:
if( counter >= TICK ) {
debug();
recbyte = (recbyte >> 1) | (rx() << 7);
counter = 0;
state = (state_t)((int)state + 1);
}
break;
}
}
void tick() {
pCore->i_clk = 0;
pCore->eval();
if(pTrace) {
pTrace->dump(static_cast<vluint64_t>(tickcount));
}
tickcount++;
pCore->i_clk = 1;
pCore->eval();
if(pTrace) {
pTrace->dump(static_cast<vluint64_t>(tickcount));
}
tickcount++;
uart_handle();
}
void clkcycles(int count = 1) {
while( count-- ) {
tick();
}
pTrace->flush();
}
void reset() {
pCore->i_reset = 1;
pCore->i_clk = 0;
tick();
pCore->i_reset = 0;
pCore->uart_rx = 1;
tick();
}
void uart_halftick() {
uint32_t counter = 0;
while(counter++ < CLK / BAUDRATE / 2) {
tick();
}
}
void uart_tick() {
uart_halftick();
uart_halftick();
}
void uart_send( char c ) {
// start bit
pCore->uart_rx = 0;
uart_tick();
for( int i = 0; i < 8; i++ ) {
pCore->uart_rx = (c >> i)&1;
uart_tick();
}
// stop bit
pCore->uart_rx = 1;
uart_tick();
}
void uart_sendstr( const char *s ) {
while(*s) {
uart_send(*s++);
}
}
void uart_receivestr( int len, char *str ) {
// adds null termination. str[] must have length > len
while( fifo_uart_rx.count < len ) {
clkcycles();
}
while( len-- ) {
fifo_pop( &fifo_uart_rx, str++ );
}
*str = 0;
}
#define nibble(val,idx) ((val >> 4*idx) & 0xf)
#define reverse(val) ((nibble(val,0) << 28) | (nibble(val,1) << 24) | (nibble(val,2) << 20) | (nibble(val,3) << 16) | \
(nibble(val,4) << 12) | (nibble(val,5) << 8) | (nibble(val,6) << 4) | (nibble(val,7) << 0) )
void write(uint32_t addr, uint32_t data) {
char s[20];
sprintf(s, "a%08Xd%08Xw", reverse(addr), reverse(data));
//printf("sending: %s\n", s);
uart_sendstr(s);
assert( pCore->top__DOT__ram[addr] == data);
}
uint32_t read(uint32_t addr) {
char s[12];//,s2[20];
sprintf(s, "a%08Xr", reverse(addr));
//printf("sending: %s\n", s);
uart_sendstr(s);
uart_receivestr( 8, s );
//printf("read: %s\n", s);
uint32_t data;
sscanf(s, "%8X", &data);
return data;
}
int main(int argc, char *argv[]) {
Verilated::traceEverOn(true);
pCore = new Vtop();
opentrace("trace.vcd");
reset();
clkcycles();
for( uint32_t i = 0; i < 16; i++ ) {
write(i, i * 0x1234567);
}
for( uint32_t i = 0; i < 16; i++ ) {
assert(read(i) == i * 0x1234567);
}
if (pTrace) {
pTrace->close();
pTrace = NULL;
}
return 0;
}