-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapb_test.sv
More file actions
342 lines (303 loc) · 10.5 KB
/
apb_test.sv
File metadata and controls
342 lines (303 loc) · 10.5 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) 2018 ETH Zurich, University of Bologna
//
// Copyright and related rights are licensed under the Solderpad Hardware
// License, Version 0.51 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
// or agreed to in writing, software, hardware and materials distributed under
// this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
// Test infrastructure for APB interfaces
package apb_test;
class apb_request #(
parameter ADDR_WIDTH = 32'd32,
parameter DATA_WIDTH = 32'd32
);
localparam STRB_WIDTH = cf_math_pkg::ceil_div(DATA_WIDTH, 8);
rand logic [ADDR_WIDTH-1:0] paddr = '0;
rand logic [DATA_WIDTH-1:0] pwdata = '0;
rand logic [STRB_WIDTH-1:0] pstrb = '0;
rand logic pwrite = 1'b1;
endclass;
class apb_response #(
parameter DATA_WIDTH = 32'd32
);
rand logic [DATA_WIDTH-1:0] prdata = '0;
rand logic pslverr = 1'b0;
endclass;
// Simple APB driver with thread-safe read and write functions
class apb_driver #(
parameter int unsigned ADDR_WIDTH = 32'd32, // APB4 address width
parameter int unsigned DATA_WIDTH = 32'd32, // APB4 data width
parameter time TA = 0ns, // application time
parameter time TT = 0ns // test time
);
localparam int unsigned STRB_WIDTH = cf_math_pkg::ceil_div(DATA_WIDTH, 8);
typedef logic [ADDR_WIDTH-1:0] addr_t;
typedef logic [DATA_WIDTH-1:0] data_t;
typedef logic [STRB_WIDTH-1:0] strb_t;
typedef apb_request #(.ADDR_WIDTH(ADDR_WIDTH), .DATA_WIDTH(DATA_WIDTH)) apb_request_t;
typedef apb_response #(.DATA_WIDTH(DATA_WIDTH)) apb_response_t;
virtual APB_DV #(
.ADDR_WIDTH(ADDR_WIDTH),
.DATA_WIDTH(DATA_WIDTH)
) apb;
std::semaphore lock;
function new(virtual APB_DV #(.ADDR_WIDTH(ADDR_WIDTH), .DATA_WIDTH(DATA_WIDTH)) apb);
this.apb = apb;
this.lock = new(1);
endfunction
function void reset_master();
apb.paddr <= '0;
apb.pprot <= '0;
apb.psel <= 1'b0;
apb.penable <= 1'b0;
apb.pwrite <= 1'b0;
apb.pwdata <= '0;
apb.pstrb <= '0;
endfunction
function void reset_slave();
apb.pready <= 1'b0;
apb.prdata <= '0;
apb.pslverr <= 1'b0;
endfunction
task cycle_start;
#TT;
endtask
task cycle_end;
@(posedge apb.clk_i);
endtask
// this task reads from an APB4 slave, acts as master
task read(
input addr_t addr,
output data_t data,
output logic err
);
while (!lock.try_get()) begin
cycle_end();
end
apb.paddr <= #TA addr;
apb.pwrite <= #TA 1'b0;
apb.psel <= #TA 1'b1;
cycle_end();
apb.penable <= #TA 1'b1;
cycle_start();
while (!apb.pready) begin
cycle_end();
cycle_start();
end
data = apb.prdata;
err = apb.pslverr;
cycle_end();
apb.paddr <= #TA '0;
apb.psel <= #TA 1'b0;
apb.penable <= #TA 1'b0;
lock.put();
endtask
// this task writes to an APB4 slave, acts as master
task write(
input addr_t addr,
input data_t data,
input strb_t strb,
output logic err
);
while (!lock.try_get()) begin
cycle_end();
end
apb.paddr <= #TA addr;
apb.pwdata <= #TA data;
apb.pstrb <= #TA strb;
apb.pwrite <= #TA 1'b1;
apb.psel <= #TA 1'b1;
cycle_end();
apb.penable <= #TA 1'b1;
cycle_start();
while (!apb.pready) begin
cycle_end();
cycle_start();
end
err = apb.pslverr;
cycle_end();
apb.paddr <= #TA '0;
apb.pwdata <= #TA '0;
apb.pstrb <= #TA '0;
apb.pwrite <= #TA 1'b0;
apb.psel <= #TA 1'b0;
apb.penable <= #TA 1'b0;
lock.put();
endtask
// Wait for incoming apb request
task recv_request (
output apb_request_t request
);
cycle_start();
while (!apb.psel) begin cycle_end(); cycle_start(); end
while (!apb.penable) begin cycle_end(); cycle_start(); end
request = new;
request.paddr = apb.paddr;
request.pwdata = apb.pwdata;
request.pwrite = apb.pwrite;
request.pstrb = apb.pstrb;
cycle_end();
endtask
// Acknowledge incoming APB write request (assert pready).
task send_write_ack();
apb.pready <= #TA 1;
cycle_end();
apb.pready <= #TA 0;
endtask
// Send response for incoming APB read request acknowledging the request (assert pready).
task send_read_response (
input apb_response_t response
);
apb.pready <= #TA 1;
apb.prdata <= #TA response.prdata;
apb.pslverr <= #TA response.pslverr;
cycle_end();
apb.pready <= #TA 0;
apb.prdata <= #TA '0;
apb.pslverr <= #TA 0;
endtask
endclass
class apb_rand_slave #(
// APB interface parmeters
parameter int unsigned ADDR_WIDTH = 32'd32,
parameter int unsigned DATA_WIDTH = 32'd32,
// Stimuli application and test time
parameter time TA = 0ps,
parameter time TT = 0ps,
// Upper and lower bounds on wait states on Response (pready)
parameter int RESP_MIN_WAIT_CYCLES = 0,
parameter int RESP_MAX_WAIT_CYCLES = 20
);
typedef apb_test::apb_driver #(
.ADDR_WIDTH(ADDR_WIDTH), .DATA_WIDTH(DATA_WIDTH), .TA(TA), .TT(TT)
) apb_driver_t;
typedef apb_driver_t::addr_t addr_t;
typedef apb_driver_t::data_t data_t;
typedef apb_driver_t::strb_t strb_t;
typedef apb_driver_t::apb_request_t apb_request_t;
typedef apb_driver_t::apb_response_t apb_response_t;
apb_driver_t drv;
std::mailbox #(apb_request_t) request_queue;
std::mailbox #(apb_response_t) response_queue;
function new(
virtual APB_DV #(
.ADDR_WIDTH(ADDR_WIDTH),
.DATA_WIDTH(DATA_WIDTH)
) apb
);
this.drv = new(apb);
this.request_queue = new;
this.response_queue = new;
this.reset();
endfunction
function reset();
drv.reset_slave();
endfunction
// TODO: The `rand_wait` task exists in `rand_verif_pkg`, but that task cannot be called with
// `this.drv.apb.clk_i` as `clk` argument. What is the syntax getting an assignable reference?
task automatic rand_wait(input int unsigned min, max);
int unsigned rand_success, cycles;
rand_success = std::randomize(cycles) with {
cycles >= min;
cycles <= max;
};
assert (rand_success) else $error("Failed to randomize wait cycles!");
repeat (cycles) @(posedge this.drv.apb.clk_i);
endtask : rand_wait
task handle_requests();
forever begin
automatic apb_request_t request;
automatic apb_response_t read_response = new;
automatic logic rand_success;
drv.recv_request(request);
request_queue.put(request);
if (request.pwrite) begin
rand_wait(RESP_MIN_WAIT_CYCLES, RESP_MAX_WAIT_CYCLES);
drv.send_write_ack();
end else begin
rand_success = read_response.randomize(); assert(rand_success);
response_queue.put(read_response);
rand_wait(RESP_MIN_WAIT_CYCLES, RESP_MAX_WAIT_CYCLES);
drv.send_read_response(read_response);
end
end
endtask : handle_requests // recv_requests
task run();
handle_requests();
endtask
endclass
class apb_rand_master #(
// APB interface parmeters
parameter int unsigned ADDR_WIDTH = 32'd32,
parameter int unsigned DATA_WIDTH = 32'd32,
// Stimuli application and test time
parameter time TA = 0ps,
parameter time TT = 0ps,
// Upper and lower bounds on wait cycles on request channel
parameter int REQ_MIN_WAIT_CYCLES = 0,
parameter int REQ_MAX_WAIT_CYCLES = 20
);
typedef apb_test::apb_driver #(
.ADDR_WIDTH(ADDR_WIDTH), .DATA_WIDTH(DATA_WIDTH), .TA(TA), .TT(TT)
) apb_driver_t;
typedef apb_driver_t::addr_t addr_t;
typedef apb_driver_t::data_t data_t;
typedef apb_driver_t::strb_t strb_t;
typedef apb_driver_t::apb_request_t apb_request_t;
typedef apb_driver_t::apb_response_t apb_response_t;
apb_driver_t drv;
std::mailbox #(apb_request_t) request_queue;
std::mailbox #(apb_response_t) response_queue;
function new(
virtual APB_DV #(
.ADDR_WIDTH(ADDR_WIDTH),
.DATA_WIDTH(DATA_WIDTH)
) apb
);
this.drv = new(apb);
this.request_queue = new;
this.response_queue = new;
this.reset();
endfunction
function reset();
drv.reset_master();
endfunction
// TODO: The `rand_wait` task exists in `rand_verif_pkg`, but that task cannot be called with
// `this.drv.apb.clk_i` as `clk` argument. What is the syntax getting an assignable reference?
task automatic rand_wait(input int unsigned min, max);
int unsigned rand_success, cycles;
rand_success = std::randomize(cycles) with {
cycles >= min;
cycles <= max;
};
assert (rand_success) else $error("Failed to randomize wait cycles!");
repeat (cycles) @(posedge this.drv.apb.clk_i);
endtask
task automatic send_requests(input int unsigned n_requests);
automatic apb_request_t request = new;
automatic apb_response_t response = new;
automatic logic write_err;
automatic logic rand_success;
repeat (n_requests) begin
rand_wait(REQ_MIN_WAIT_CYCLES, REQ_MAX_WAIT_CYCLES);
rand_success = request.randomize(); assert(rand_success);
request_queue.put(request);
if (request.pwrite) begin
$info("Sending write request with addr: %h, pstrb: %b and pwdata: %h", request.paddr, request.pstrb, request.pwdata);
drv.write(request.paddr, request.pwdata, request.pstrb, write_err);
end else begin
$info("Sending read request with addr: %h...", request.paddr);
drv.read(request.paddr, response.prdata, response.pslverr);
response_queue.put(response);
end
end
endtask : send_requests
task automatic run(input int unsigned n_request);
$info("Run apb_rand_master for %0d transactions", n_request);
send_requests(n_request);
endtask
endclass
endpackage