-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsram_testbench.sv
More file actions
283 lines (209 loc) · 5.73 KB
/
sram_testbench.sv
File metadata and controls
283 lines (209 loc) · 5.73 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
class transaction;
rand bit write,read;
rand bit [2:0] addres;
rand bit [7:0] data_in;
bit [7:0] data_out;
bit [1:0] cnt;
constraint wr_rd_c { write != read; }
function void post_randomize();
$display("transaction::transaction Generated");
$display("write=%0d-----",write);
$display("read=%0d-----",read);
$display("addres=%0d--------",addres);
$display("data_in=%0d-------",data_in);
$display("data_out=%0d------",data_out);
endfunction
function transaction do_copy();
transaction trans;
trans=new();
trans.write=this.write;
trans.read=this.read;
trans.addres=this.addres;
trans.data_in=this.data_in;
return trans;
endfunction
endclass
/////////////generator block///////////////////
class genrator;
///rand transaction class
rand transaction trans,tr;
////repeat count ,to specfiy
int repeat_count;
mailbox gen2driv; ///Mailbox created
//event
event ended;
function new(mailbox gen2driv,event ended);
this.gen2driv =gen2driv;
this.ended = ended;
trans = new();
endfunction
task main;
repeat(repeat_count)
begin
// trans.randomize();
if(!trans.randomize())
$fatal("Gen:: trans randomization failed");
// $display("transaction error");
tr=trans.do_copy();
gen2driv.put(tr);
$display("genrator :: put into mailbox");
end
-> ended;
endtask
endclass
//module mailbox_ex;
// genrator gen;
// driver dri;
// mailbox gen2driv;
//initial
// begin
// gen2driv=new();
// gen =new (gen2driv);
// dri =new (gen2driv);
// fork
// gen.main();
// dri.run();
// join
//end
//endmodule
///////////interface block////////////////////////////////
interface ram_intf(input logic clk);
logic write,read;
logic[2:0] addres;
logic[7:0] data_in;
logic[7:0] data_out;
clocking ram_cb @(posedge clk);
input write,read;
input addres;
input data_in;
output data_out;
endclocking
// modport drvier_mod( clocking ram_cb,input clk );
modport drvier_mod( input clk,write,read,addres,data_in,output data_out );
endinterface
//`define DRIV_IF ram_vif.ram_cb
///////////////DRIVER block////////////////////////////////
class driver;
//used to count the number of transactions
int no_transactions;
virtual ram_intf ram_vif;
mailbox gen2driv;
function new(virtual ram_intf ram_vif,mailbox gen2driv);
this.ram_vif=ram_vif;
this.gen2driv=gen2driv;
$display("drive is enterd");
endfunction
task drive;
transaction trans;
ram_vif.drvier_mod.write<=0;
ram_vif.drvier_mod.read<=0;
gen2driv.get(trans);
$display("DRIVER-----TRSFER");
@(posedge ram_vif.drvier_mod.clk);
ram_vif.drvier_mod.addres <= trans.addres;
if(trans.write)
begin
ram_vif.drvier_mod.write<=trans.write;
ram_vif.drvier_mod.data_in<=trans.data_in;
$display("ADDRES=%0d,DATAIN=%0d",trans.addres,trans.data_in);
@(posedge ram_vif.drvier_mod.clk);
end
if(trans.read)
begin
ram_vif.drvier_mod.read<=trans.read;
// @(posedge ram_vif.drvier_mod.clk);
// ram_vif.drvier_mod.we<=0;
@(posedge ram_vif.drvier_mod.clk);
trans.data_out=ram_vif.drvier_mod.data_out;
$display("addres=%0d,data_out=%0d",trans.addres,ram_vif.drvier_mod.data_out);
end
$display("-------------------");
no_transactions++;
endtask
task main;
//forever
begin
forever
drive();
end
endtask
endclass
//env is create memory
// // /////////////environment/////////////////
class environment;
genrator gen;
driver driv;
mailbox gen2driv;
//event for synchronization between generator and test
event gen_ended;
virtual ram_intf ram_vif;
function new(virtual ram_intf ram_vif );
this.ram_vif = ram_vif;
gen2driv =new();
//creating generator and driver
gen = new(gen2driv,gen_ended);
driv = new(ram_vif,gen2driv);
endfunction
task test();
fork
gen.main();
driv.main(); // need to check
join_any
endtask
task post_test();
wait(gen_ended.triggered);
wait(gen.repeat_count == driv.no_transactions);
endtask
//run task
task run;
test();
post_test();
$finish;
endtask
endclass
///////////////////TEST/////////////////
program test(ram_intf intf);
class my_trans extends transaction;
bit [1:0] count;
function void pre_randomize();
write.rand_mode(0);
read.rand_mode(0);
addres.rand_mode(0);
if(cnt %2 == 0)
begin
write = 1;
read = 0;
addres = count;
end
else begin
write = 0;
read = 1;
addres = count;
count++;
end
cnt++;
endfunction
endclass
environment env;
my_trans my_tr;
initial
begin
env=new(intf);
my_tr =new();
//setting the repeat count of generator as 4, means to generate 4 packets
env.gen.repeat_count = 6;
env.gen.trans = my_tr;
env.run();
end
endprogram
module tbench_top;
bit clk;
always #5 clk =~clk;
ram_intf intf(clk);
test t1(intf);
ram DUT ( .clk(intf.clk),
.write(intf.write),
.addres(intf.addres),
.data_in(intf.data_in),
.data_out(intf.data_out));
endmodule