-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathverilator_main.cpp
126 lines (105 loc) · 2.17 KB
/
verilator_main.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
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
#include <stdlib.h>
#include "Vqpitest.h"
#include <verilated.h>
#include <verilated_vcd_c.h>
#include "../psram_emu.hpp"
uint64_t ts=0;
uint64_t tracepos=0;
Vqpitest *tb;
Psram_emu *psram;
VerilatedVcdC *trace;
double sc_time_stamp() {
return ts;
}
#define CHECK(x) do { if (!(x)) printf("%s:%d: check failed: %s\n", __FILE__, __LINE__, #x); } while(0)
int doclk() {
int do_abort;
ts++;
tracepos++;
int sin;
do_abort |= psram->eval(tb->spi_clk, tb->spi_ncs, tb->spi_sout, tb->spi_oe, &sin);
tb->clk = 0;
tb->eval();
tb->spi_sin=sin;
trace->dump(tracepos*21);
do_abort |= psram->eval(tb->spi_clk, tb->spi_ncs, tb->spi_sout, tb->spi_oe, &sin);
tb->clk = 1;
tb->eval();
tb->spi_sin = sin;
trace->dump(tracepos*21+10);
return do_abort;
}
void do_write(int addr, int data) {
tb->addr=addr;
tb->wdata=data;
tb->wen=0xf;
do {
doclk();
} while (tb->ready==0);
tb->wen=0;
doclk();
}
void do_flush(int addr, int endaddr) {
tb->addr=addr;
tb->wdata=endaddr;
tb->flush=1;
do {
doclk();
} while (tb->ready==0);
tb->flush=0;
doclk();
}
int do_read(int addr) {
int ret;
tb->addr=addr;
tb->ren=1;
do {
doclk();
} while (tb->ready==0);
tb->ren=0;
ret=tb->rdata;
doclk();
return ret;
}
int main(int argc, char **argv) {
// Initialize Verilators variables
Verilated::commandArgs(argc, argv);
Verilated::traceEverOn(true);
tb = new Vqpitest;
trace = new VerilatedVcdC;
tb->trace(trace, 99);
trace->open("qpitesttrace.vcd");
psram = new Psram_emu(8*1024*1024);
psram->force_qpi();
tb->rst = 1;
doclk();
tb->rst=0;
doclk();
doclk();
doclk();
doclk();
do_read(0x20000); //dummy because ?????
do_write(0x10001, 0xdeadbeef);
do_write(0x10002, 0xcafebabe);
CHECK(do_read(0x10001)==0xdeadbeef);
CHECK(do_read(0x10002)==0xcafebabe);
do_flush(0, 64);
printf("Writing...\n");
srand(0);
for (int i=0; i<1000; i++) {
int a=rand()&0x1fffff;
int d=rand()&0xffffffff;
do_write(a, d);
}
printf("Reading...\n");
srand(0);
for (int i=0; i<1000; i++) {
int a=rand()&0x1fffff;
int d=rand()&0xffffffff;
int td=do_read(a);
if (td!=d) printf("It %d, addr %d: wrote %x read %x\n", i, a, d, td);
}
trace->flush();
trace->close();
exit(EXIT_SUCCESS);
}