-
Notifications
You must be signed in to change notification settings - Fork 0
/
tb.cc
90 lines (78 loc) · 2.06 KB
/
tb.cc
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
#include "tb.h"
void tb::source() {
//Generation of input sequence
sc_int<16> tmp;
//Initialize handshake
inp_vld.write(0);
//Reset
inp.write( 0 );
rst.write( 1 );
wait();
rst.write( 0 );
wait();
//Send stimulus to FIR
for(int i=0; i<64; i++) //A simple pulse input
{
if (i>23 && i <29)
tmp = 256;
else
tmp = 0;
inp_vld.write(1);
inp.write( tmp );
//wait for input readiness
do{
wait();
} while( !inp_rdy.read() );
//Reset
inp_vld.write(0);
//Measurment for diagnosis and reporting
start_time[i] = sc_time_stamp();
}
// Hanging simulation guard condition
wait(10000);
printf("Hanging simulation stopped by TB source thread. Please check DUT module.\n");
sc_stop();
}
void tb::sink() {
sc_int<16> indata;
//Extract clock period
sc_clock *clk_p = dynamic_cast<sc_clock *>(clk.get_interface());
clock_period = clk_p->period();
//Open simulation output results file
char output_file[256];
sprintf( output_file, "./output.dat");
outfp = fopen(output_file, "wb");
if (outfp == NULL)
{
printf("Couldn't open output.dat for writing");
exit(0);
}
// Initialize port
outp_rdy.write(0);
double total_cycles = 0; //Cumulative latencies for all inputs
//Read output coming from DUT
for(int i=0; i<64; i++)
{
outp_rdy.write(1);
do {
wait();
}while( !outp_vld.read());
indata = outp.read();
//Measurment for diagnosis and reporting
end_time[i] = sc_time_stamp();
//Calculate and append latency
total_cycles += (end_time[i] - start_time[i])/ clock_period;
//Reset
outp_rdy.write(0);
//Write to file
fprintf( outfp, "%d\n", (int)indata);
cout << i << ":\t" << indata.to_int() << endl;
}
// Print latency and throughput
double total_throughput = (start_time[63] - start_time[0])/clock_period;
printf("Average latency is %g cycles.\n", (double)(total_cycles/64));
printf("Average throughput is %g cycles per input.\n", (double)(total_throughput/63));
// End simulation
fclose(outfp);
sc_stop();
}