forked from pulp-platform/axi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axi_sim_mem.sv
413 lines (395 loc) · 14.4 KB
/
axi_sim_mem.sv
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2020 ETH Zurich and University of Bologna
// SPDX-License-Identifier: SHL-0.51
//
// Authors:
// - Andreas Kurth <[email protected]>
// - Samuel Riedel <[email protected]>
// - Michael Rogenmoser <[email protected]>
// - Thomas Benz <[email protected]>
`include "axi/typedef.svh"
/// Infinite (Simulation-Only) Memory with AXI Slave Port
///
/// The memory array is named `mem`, and it is *not* initialized or reset. This makes it possible to
/// load the memory of this module in simulation with an external `$readmem*` command, e.g.,
/// ```sv
/// axi_sim_mem #( ... ) i_sim_mem ( ... );
/// initial begin
/// $readmemh("file_with_memory_addrs_and_data.mem", i_sim_mem.mem);
/// $readmemh("file_with_memory_addrs_and_read_errors.mem", i_sim_mem.rerr);
/// $readmemh("file_with_memory_addrs_and_write_errors.mem", i_sim_mem.werr);
/// end
/// ```
/// `mem` is addressed (or indexed) byte-wise with `AddrWidth`-wide addresses.
///
/// This module does not support atomic operations (ATOPs).
module axi_sim_mem #(
/// AXI Address Width
parameter int unsigned AddrWidth = 32'd0,
/// AXI Data Width
parameter int unsigned DataWidth = 32'd0,
/// AXI ID Width
parameter int unsigned IdWidth = 32'd0,
/// AXI User Width.
parameter int unsigned UserWidth = 32'd0,
/// AXI4 request struct definition
parameter type axi_req_t = logic,
/// AXI4 response struct definition
parameter type axi_rsp_t = logic,
/// Warn on accesses to uninitialized bytes
parameter bit WarnUninitialized = 1'b0,
/// Clear error on access
parameter bit ClearErrOnAccess = 1'b0,
/// Application delay (measured after rising clock edge)
parameter time ApplDelay = 0ps,
/// Acquisition delay (measured after rising clock edge)
parameter time AcqDelay = 0ps
) (
/// Rising-edge clock
input logic clk_i,
/// Active-low reset
input logic rst_ni,
/// AXI4 request struct
input axi_req_t axi_req_i,
/// AXI4 response struct
output axi_rsp_t axi_rsp_o,
/// Memory monitor write valid. All `mon_w_*` outputs are only valid if this signal is high.
/// A write to the memory is visible on the `mon_w_*` outputs in the clock cycle after it has
/// happened.
output logic mon_w_valid_o,
/// Memory monitor write address
output logic [AddrWidth-1:0] mon_w_addr_o,
/// Memory monitor write data
output logic [DataWidth-1:0] mon_w_data_o,
/// Memory monitor write ID
output logic [IdWidth-1:0] mon_w_id_o,
/// Memory monitor write user
output logic [UserWidth-1:0] mon_w_user_o,
/// Memory monitor write beat count
output axi_pkg::len_t mon_w_beat_count_o,
/// Memory monitor write last
output logic mon_w_last_o,
/// Memory monitor read valid. All `mon_r_*` outputs are only valid if this signal is high.
/// A read from the memory is visible on the `mon_w_*` outputs in the clock cycle after it has
/// happened.
output logic mon_r_valid_o,
/// Memory monitor read address
output logic [AddrWidth-1:0] mon_r_addr_o,
/// Memory monitor read data
output logic [DataWidth-1:0] mon_r_data_o,
/// Memory monitor read ID
output logic [IdWidth-1:0] mon_r_id_o,
/// Memory monitor read user
output logic [UserWidth-1:0] mon_r_user_o,
/// Memory monitor read beat count
output axi_pkg::len_t mon_r_beat_count_o,
/// Memory monitor read last
output logic mon_r_last_o
);
localparam int unsigned StrbWidth = DataWidth / 8;
typedef logic [AddrWidth-1:0] addr_t;
typedef logic [DataWidth-1:0] data_t;
typedef logic [IdWidth-1:0] id_t;
typedef logic [StrbWidth-1:0] strb_t;
typedef logic [UserWidth-1:0] user_t;
`AXI_TYPEDEF_AW_CHAN_T(aw_t, addr_t, id_t, user_t)
`AXI_TYPEDEF_W_CHAN_T(w_t, data_t, strb_t, user_t)
`AXI_TYPEDEF_B_CHAN_T(b_t, id_t, user_t)
`AXI_TYPEDEF_AR_CHAN_T(ar_t, addr_t, id_t, user_t)
`AXI_TYPEDEF_R_CHAN_T(r_t, data_t, id_t, user_t)
typedef struct packed {
logic valid;
logic [AddrWidth-1:0] addr;
logic [DataWidth-1:0] data;
logic [IdWidth-1:0] id;
logic [UserWidth-1:0] user;
axi_pkg::len_t beat_count;
logic last;
} monitor_t;
monitor_t mon_w, mon_r;
logic [7:0] mem[addr_t];
axi_pkg::resp_t rerr[addr_t] = '{default: axi_pkg::RESP_OKAY};
axi_pkg::resp_t werr[addr_t] = '{default: axi_pkg::RESP_OKAY};
// error happened in write burst
axi_pkg::resp_t error_happened = axi_pkg::RESP_OKAY;
initial begin
automatic ar_t ar_queue[$];
automatic aw_t aw_queue[$];
automatic b_t b_queue[$];
automatic shortint unsigned r_cnt = 0, w_cnt = 0;
axi_rsp_o = '0;
// Monitor interface
mon_w = '0;
mon_r = '0;
wait (rst_ni);
fork
// AW
forever begin
@(posedge clk_i);
#(ApplDelay);
axi_rsp_o.aw_ready = 1'b1;
#(AcqDelay - ApplDelay);
if (axi_req_i.aw_valid) begin
automatic aw_t aw = axi_req_i.aw;
aw_queue.push_back(aw);
end
end
// W
forever begin
@(posedge clk_i);
#(ApplDelay);
axi_rsp_o.w_ready = 1'b0;
mon_w = '0;
if (aw_queue.size() != 0) begin
axi_rsp_o.w_ready = 1'b1;
#(AcqDelay - ApplDelay);
if (axi_req_i.w_valid) begin
automatic axi_pkg::burst_t burst = aw_queue[0].burst;
automatic axi_pkg::len_t len = aw_queue[0].len;
automatic axi_pkg::size_t size = aw_queue[0].size;
automatic addr_t addr = axi_pkg::beat_addr(aw_queue[0].addr, size, len, burst,
w_cnt);
mon_w.valid = 1'b1;
mon_w.addr = addr;
mon_w.data = axi_req_i.w.data;
mon_w.id = aw_queue[0].id;
mon_w.user = aw_queue[0].user;
mon_w.beat_count = w_cnt;
for (shortint unsigned
i_byte = axi_pkg::beat_lower_byte(addr, size, len, burst, StrbWidth, w_cnt);
i_byte <= axi_pkg::beat_upper_byte(addr, size, len, burst, StrbWidth, w_cnt);
i_byte++) begin
if (axi_req_i.w.strb[i_byte]) begin
automatic addr_t byte_addr = (addr / StrbWidth) * StrbWidth + i_byte;
mem[byte_addr] = axi_req_i.w.data[i_byte*8+:8];
error_happened = axi_pkg::resp_precedence(werr[byte_addr], error_happened);
if (ClearErrOnAccess)
werr[byte_addr] = axi_pkg::RESP_OKAY;
end
end
if (w_cnt == aw_queue[0].len) begin
automatic b_t b_beat = '0;
assert (axi_req_i.w.last) else $error("Expected last beat of W burst!");
b_beat.id = aw_queue[0].id;
b_beat.resp = error_happened;
b_queue.push_back(b_beat);
w_cnt = 0;
mon_w.last = 1'b1;
error_happened = axi_pkg::RESP_OKAY;
void'(aw_queue.pop_front());
end else begin
assert (!axi_req_i.w.last) else $error("Did not expect last beat of W burst!");
w_cnt++;
end
end
end
end
// B
forever begin
@(posedge clk_i);
#(ApplDelay);
axi_rsp_o.b_valid = 1'b0;
if (b_queue.size() != 0) begin
axi_rsp_o.b = b_queue[0];
axi_rsp_o.b_valid = 1'b1;
#(AcqDelay - ApplDelay);
if (axi_req_i.b_ready) begin
void'(b_queue.pop_front());
end
end
end
// AR
forever begin
@(posedge clk_i);
#(ApplDelay);
axi_rsp_o.ar_ready = 1'b1;
#(AcqDelay - ApplDelay);
if (axi_req_i.ar_valid) begin
automatic ar_t ar = axi_req_i.ar;
ar_queue.push_back(ar);
end
end
// R
forever begin
@(posedge clk_i);
#(ApplDelay);
axi_rsp_o.r_valid = 1'b0;
mon_r = '0;
if (ar_queue.size() != 0) begin
automatic axi_pkg::burst_t burst = ar_queue[0].burst;
automatic axi_pkg::len_t len = ar_queue[0].len;
automatic axi_pkg::size_t size = ar_queue[0].size;
automatic addr_t addr = axi_pkg::beat_addr(ar_queue[0].addr, size, len, burst, r_cnt);
automatic r_t r_beat = '0;
automatic data_t r_data = 'x; // compatibility reasons
r_beat.data = 'x;
r_beat.id = ar_queue[0].id;
r_beat.resp = axi_pkg::RESP_OKAY;
for (shortint unsigned
i_byte = axi_pkg::beat_lower_byte(addr, size, len, burst, StrbWidth, r_cnt);
i_byte <= axi_pkg::beat_upper_byte(addr, size, len, burst, StrbWidth, r_cnt);
i_byte++) begin
automatic addr_t byte_addr = (addr / StrbWidth) * StrbWidth + i_byte;
if (!mem.exists(byte_addr)) begin
if (WarnUninitialized) begin
$warning("Access to non-initialized byte at address 0x%016x by ID 0x%x.", byte_addr,
r_beat.id);
end
r_data[i_byte*8+:8] = 'x;
end else begin
r_data[i_byte*8+:8] = mem[byte_addr];
end
r_beat.resp = axi_pkg::resp_precedence(rerr[byte_addr], r_beat.resp);
if (ClearErrOnAccess & axi_req_i.r_ready) begin
rerr[byte_addr] = axi_pkg::RESP_OKAY;
end
end
r_beat.data = r_data;
if (r_cnt == ar_queue[0].len) begin
r_beat.last = 1'b1;
mon_r.last = 1'b1;
end
axi_rsp_o.r = r_beat;
axi_rsp_o.r_valid = 1'b1;
mon_r.valid = 1'b1;
mon_r.addr = addr;
mon_r.data = r_beat.data;
mon_r.id = r_beat.id;
mon_r.user = ar_queue[0].user;
mon_r.beat_count = r_cnt;
#(AcqDelay - ApplDelay);
while (!axi_req_i.r_ready) begin
@(posedge clk_i);
#(AcqDelay);
mon_r = '0;
end
if (r_beat.last) begin
r_cnt = 0;
void'(ar_queue.pop_front());
end else begin
r_cnt++;
end
end
end
join
end
// Assign the monitor output in the next clock cycle. Rationale: We only know whether we are
// writing until after `AcqDelay`. This means we could only provide the monitoring output for
// writes after `AcqDelay` in the same cycle, which is incompatible with ATI timing. Thus, we
// provide the monitoring output for writes (and for uniformity also for reads) in the next clock
// cycle.
initial begin
mon_w_valid_o = '0;
mon_w_addr_o = '0;
mon_w_data_o = '0;
mon_w_id_o = '0;
mon_w_user_o = '0;
mon_w_beat_count_o = '0;
mon_w_last_o = '0;
mon_r_valid_o = '0;
mon_r_addr_o = '0;
mon_r_data_o = '0;
mon_r_id_o = '0;
mon_r_user_o = '0;
mon_r_beat_count_o = '0;
mon_r_last_o = '0;
wait (rst_ni);
forever begin
@(posedge clk_i);
mon_w_valid_o <= #(ApplDelay) mon_w.valid;
mon_w_addr_o <= #(ApplDelay) mon_w.addr;
mon_w_data_o <= #(ApplDelay) mon_w.data;
mon_w_id_o <= #(ApplDelay) mon_w.id;
mon_w_user_o <= #(ApplDelay) mon_w.user;
mon_w_beat_count_o <= #(ApplDelay) mon_w.beat_count;
mon_w_last_o <= #(ApplDelay) mon_w.last;
mon_r_valid_o <= #(ApplDelay) mon_r.valid;
mon_r_addr_o <= #(ApplDelay) mon_r.addr;
mon_r_data_o <= #(ApplDelay) mon_r.data;
mon_r_id_o <= #(ApplDelay) mon_r.id;
mon_r_user_o <= #(ApplDelay) mon_r.user;
mon_r_beat_count_o <= #(ApplDelay) mon_r.beat_count;
mon_r_last_o <= #(ApplDelay) mon_r.last;
end
end
// Parameter Assertions
initial begin
assert (AddrWidth != 0) else $fatal("AddrWidth must be non-zero!", 1);
assert (DataWidth != 0) else $fatal("DataWidth must be non-zero!", 1);
assert (IdWidth != 0) else $fatal("IdWidth must be non-zero!", 1);
assert (UserWidth != 0) else $fatal("UserWidth must be non-zero!", 1);
end
endmodule
`include "axi/assign.svh"
/// Interface variant of [`axi_sim_mem`](module.axi_sim_mem).
///
/// See the documentation of the main module for the definition of ports and parameters.
module axi_sim_mem_intf #(
parameter int unsigned AXI_ADDR_WIDTH = 32'd0,
parameter int unsigned AXI_DATA_WIDTH = 32'd0,
parameter int unsigned AXI_ID_WIDTH = 32'd0,
parameter int unsigned AXI_USER_WIDTH = 32'd0,
parameter bit WARN_UNINITIALIZED = 1'b0,
parameter bit ClearErrOnAccess = 1'b0,
parameter time APPL_DELAY = 0ps,
parameter time ACQ_DELAY = 0ps
) (
input logic clk_i,
input logic rst_ni,
AXI_BUS.Slave axi_slv,
output logic mon_w_valid_o,
output logic [AXI_ADDR_WIDTH-1:0] mon_w_addr_o,
output logic [AXI_DATA_WIDTH-1:0] mon_w_data_o,
output logic [AXI_ID_WIDTH-1:0] mon_w_id_o,
output logic [AXI_USER_WIDTH-1:0] mon_w_user_o,
output axi_pkg::len_t mon_w_beat_count_o,
output logic mon_w_last_o,
output logic mon_r_valid_o,
output logic [AXI_ADDR_WIDTH-1:0] mon_r_addr_o,
output logic [AXI_DATA_WIDTH-1:0] mon_r_data_o,
output logic [AXI_ID_WIDTH-1:0] mon_r_id_o,
output logic [AXI_USER_WIDTH-1:0] mon_r_user_o,
output axi_pkg::len_t mon_r_beat_count_o,
output logic mon_r_last_o
);
typedef logic [AXI_ADDR_WIDTH-1:0] axi_addr_t;
typedef logic [AXI_DATA_WIDTH-1:0] axi_data_t;
typedef logic [AXI_ID_WIDTH-1:0] axi_id_t;
typedef logic [AXI_DATA_WIDTH/8-1:0] axi_strb_t;
typedef logic [AXI_USER_WIDTH-1:0] axi_user_t;
`AXI_TYPEDEF_ALL(axi, axi_addr_t, axi_id_t, axi_data_t, axi_strb_t, axi_user_t)
axi_req_t axi_req;
axi_resp_t axi_rsp;
`AXI_ASSIGN_TO_REQ(axi_req, axi_slv)
`AXI_ASSIGN_FROM_RESP(axi_slv, axi_rsp)
axi_sim_mem #(
.AddrWidth (AXI_ADDR_WIDTH),
.DataWidth (AXI_DATA_WIDTH),
.IdWidth (AXI_ID_WIDTH),
.UserWidth (AXI_USER_WIDTH),
.axi_req_t (axi_req_t),
.axi_rsp_t (axi_resp_t),
.WarnUninitialized (WARN_UNINITIALIZED),
.ClearErrOnAccess (ClearErrOnAccess),
.ApplDelay (APPL_DELAY),
.AcqDelay (ACQ_DELAY)
) i_sim_mem (
.clk_i,
.rst_ni,
.axi_req_i (axi_req),
.axi_rsp_o (axi_rsp),
.mon_w_valid_o,
.mon_w_addr_o,
.mon_w_data_o,
.mon_w_id_o,
.mon_w_user_o,
.mon_w_beat_count_o,
.mon_w_last_o,
.mon_r_valid_o,
.mon_r_addr_o,
.mon_r_data_o,
.mon_r_id_o,
.mon_r_user_o,
.mon_r_beat_count_o,
.mon_r_last_o
);
endmodule