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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
////////////////////////////////////////////////////////////////////////////////
//
// Filename: addrdecode.v
// {{{
// Project: WB2AXIPSP: bus bridges and other odds and ends
//
// Purpose: Supports bus crossbars by answering the question, which slave
// does the current address need to be routed to? Requests are
// pipelined using valid/!stall handshaking. For those familiar with
// AXI, READY=!STALL. The outgoing stream is identical to the incoming
// one, save for the (new) o_decode field. This is a bitmask containing
// one bit for each slave that the request might be routed to, and one
// extra bit to indicate no slaves matched.
//
// The keys to the operation of this module are found in the two
// parameters, SLAVE_ADDR and SLAVE_MASK.
//
// SLAVE_ADDR specifies the address of the slave in question. It's a large
// array, with one address (i.e. one set of AW bits) for each
// potential slave address region.
//
// SLAVE_MASK specifies which of the bits in SLAVE_ADDR need to match in
// order to route a request to a that slave.
//
// It's important to guarantee that no two slaves will ever map to the
// same address, and likewise any given slave may only map to a single
// address region.
//
// Incidentally, the algorithm forces all slaves to have an address
// aligned with their memory size. Hence a 2GB memory must have an
// address (range) of either 0-2GB, 2GB-4GB, 4GB-6GB, etc. However, a
// second slave having only 8kB of memory may be placed at 0-8kB,
// 8-16kB, 16-24kB, etc. For logic minimization purposes, it is often to
// the advantage of the bus compositor to minimize the number of mask
// bits, and hence 8kB slaves may be aliased to many places in memory.
// Bus composition and address assignment, however, are both outside of
// the scope of the operation of this module.
//
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2019-2024, Gisselquist Technology, LLC
// {{{
// This file is part of the WB2AXIP project.
//
// The WB2AXIP project contains free software and gateware, licensed under the
// Apache License, Version 2.0 (the "License"). You may not use this project,
// or this file, except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the 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.
//
////////////////////////////////////////////////////////////////////////////////
//
//
//`default_nettype none
// }}}
module addrdecode #(
// {{{
parameter NS=8,
parameter AW = 32, DW=32+32/8+1+1,
//
// SLAVE_ADDR contains address assignments for each of the
// various slaves we are adjudicating between.
parameter [NS*AW-1:0] SLAVE_ADDR = {
{ 3'b111, {(AW-3){1'b0}} },
{ 3'b110, {(AW-3){1'b0}} },
{ 3'b101, {(AW-3){1'b0}} },
{ 3'b100, {(AW-3){1'b0}} },
{ 3'b011, {(AW-3){1'b0}} },
{ 3'b010, {(AW-3){1'b0}} },
{ 4'b0010, {(AW-4){1'b0}} },
{ 4'b0000, {(AW-4){1'b0}} }},
//
// SLAVE_MASK contains a mask of those address bits in
// SLAVE_ADDR which are relevant. It shall be true that if
// !SLAVE_MASK[k] then !SLAVE_ADDR[k], for any bits of k
parameter [NS*AW-1:0] SLAVE_MASK = (NS <= 1) ? 0
: { {(NS-2){ 3'b111, {(AW-3){1'b0}} }},
{(2){ 4'b1111, {(AW-4){1'b0}} }} },
//
// ACCESS_ALLOWED is a bit-wise mask indicating which slaves
// may get access to the bus. If ACCESS_ALLOWED[slave] is true,
// then a master can connect to the slave via this method. This
// parameter is primarily here to support AXI (or other similar
// buses) which may have separate accesses for both read and
// write. By using this, a read-only slave can be connected,
// which would also naturally create an error on any attempt to
// write to it.
parameter [NS-1:0] ACCESS_ALLOWED = -1,
//
// If OPT_REGISTERED is set, address decoding will take an extra
// clock, and will register the results of the decoding
// operation.
parameter [0:0] OPT_REGISTERED = 0,
//
// If OPT_LOWPOWER is set, then whenever the output is not
// valid, any respective data linse will also be forced to zero
// in an effort to minimize power.
parameter [0:0] OPT_LOWPOWER = 0
// }}}
) (
// {{{
input wire i_clk, i_reset,
//
input wire i_valid,
output reg o_stall,
input wire [AW-1:0] i_addr,
input wire [DW-1:0] i_data,
//
output reg o_valid,
input wire i_stall,
output reg [NS:0] o_decode,
output reg [AW-1:0] o_addr,
output reg [DW-1:0] o_data
// }}}
);
// Local declarations
// {{{
//
// OPT_NONESEL controls whether or not the address lines are fully
// proscribed, or whether or not a "no-slave identified" slave should
// be created. To avoid a "no-slave selected" output, slave zero must
// have no mask bits set (and therefore no address bits set), and it
// must also allow access.
localparam [0:0] OPT_NONESEL = (!ACCESS_ALLOWED[0])
|| (SLAVE_MASK[AW-1:0] != 0);
//
wire [NS:0] request;
reg [NS-1:0] prerequest;
integer iM;
// }}}
// prerequest
// {{{
always @(*)
for(iM=0; iM<NS; iM=iM+1)
prerequest[iM] = (((i_addr ^ SLAVE_ADDR[iM*AW +: AW])
&SLAVE_MASK[iM*AW +: AW])==0)
&&(ACCESS_ALLOWED[iM]);
// }}}
// request
// {{{
generate if (OPT_NONESEL)
begin : NO_DEFAULT_REQUEST
// {{{
reg [NS-1:0] r_request;
// Need to create a slave to describe when nothing is selected
//
always @(*)
begin
for(iM=0; iM<NS; iM=iM+1)
r_request[iM] = i_valid && prerequest[iM];
if (!OPT_NONESEL && (NS > 1 && |prerequest[NS-1:1]))
r_request[0] = 1'b0;
end
assign request[NS-1:0] = r_request;
// }}}
end else if (NS == 1)
begin : SINGLE_SLAVE
// {{{
assign request[0] = i_valid;
// }}}
end else begin : GENERAL_CASE
// {{{
reg [NS-1:0] r_request;
always @(*)
begin
for(iM=0; iM<NS; iM=iM+1)
r_request[iM] = i_valid && prerequest[iM];
if (!OPT_NONESEL && (NS > 1 && |prerequest[NS-1:1]))
r_request[0] = 1'b0;
end
assign request[NS-1:0] = r_request;
// }}}
end endgenerate
// }}}
// request[NS]
// {{{
generate if (OPT_NONESEL)
begin : GENERATE_NONSEL_SLAVE
reg r_request_NS, r_none_sel;
always @(*)
begin
// Let's assume nothing's been selected, and then check
// to prove ourselves wrong.
//
// Note that none_sel will be considered an error
// condition in the follow-on processing. Therefore
// it's important to clear it if no request is pending.
r_none_sel = i_valid && (prerequest == 0);
//
// request[NS] indicates a request for a non-existent
// slave. A request that should (eventually) return a
// bus error
//
r_request_NS = r_none_sel;
end
assign request[NS] = r_request_NS;
end else begin : NO_NONESEL_SLAVE
assign request[NS] = 1'b0;
end endgenerate
// }}}
// o_valid, o_addr, o_data, o_decode, o_stall
// {{{
generate if (OPT_REGISTERED)
begin : GEN_REG_OUTPUTS
// o_valid
// {{{
initial o_valid = 0;
always @(posedge i_clk)
if (i_reset)
o_valid <= 0;
else if (!o_stall)
o_valid <= i_valid;
// }}}
// o_addr, o_data
// {{{
initial o_addr = 0;
initial o_data = 0;
always @(posedge i_clk)
if (i_reset && OPT_LOWPOWER)
begin
o_addr <= 0;
o_data <= 0;
end else if ((!o_valid || !i_stall)
&& (i_valid || !OPT_LOWPOWER))
begin
o_addr <= i_addr;
o_data <= i_data;
end else if (OPT_LOWPOWER && !i_stall)
begin
o_addr <= 0;
o_data <= 0;
end
// }}}
// o_decode
// {{{
initial o_decode = 0;
always @(posedge i_clk)
if (i_reset)
o_decode <= 0;
else if ((!o_valid || !i_stall)
&& (i_valid || !OPT_LOWPOWER))
o_decode <= request;
else if (OPT_LOWPOWER && !i_stall)
o_decode <= 0;
// }}}
// o_stall
// {{{
always @(*)
o_stall = (o_valid && i_stall);
// }}}
end else begin : GEN_COMBINATORIAL_OUTPUTS
always @(*)
begin
o_valid = i_valid;
o_stall = i_stall;
o_addr = i_addr;
o_data = i_data;
o_decode = request;
end
// Make Verilator happy
// {{{
// verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0,
`ifdef VERILATOR
// Can't declare the clock as unused for formal,
// lest it not be recognized as *the* clock
i_clk,
`endif
i_reset };
// verilator lint_on UNUSED
// }}}
end endgenerate
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
reg f_past_valid;
initial f_past_valid = 0;
always @(posedge i_clk)
f_past_valid <= 1;
reg [AW+DW-1:0] f_idata;
always @(*)
f_idata = { i_addr, i_data };
`ifdef ADDRDECODE
always @(posedge i_clk)
if (!f_past_valid)
assume(i_reset);
`else
always @(posedge i_clk)
if (!f_past_valid)
assert(i_reset);
`endif // ADDRDECODE
always @(posedge i_clk)
if (OPT_REGISTERED && (!f_past_valid || $past(i_reset)))
begin
assert(!o_valid);
assert(o_decode == 0);
end else if ($past(o_valid && i_stall) && OPT_REGISTERED)
begin
assert($stable(o_addr));
assert($stable(o_decode));
assert($stable(o_data));
end
// If the output is ever valid, there must be at least one
// decoded output
always @(*)
assert(o_valid == (o_decode != 0));
always @(*)
for(iM=0; iM<NS; iM=iM+1)
if (o_decode[iM])
begin
// The address must match
assert((((o_addr ^ SLAVE_ADDR[iM*AW +: AW])
& SLAVE_MASK[iM*AW +: AW])==0)
&& ACCESS_ALLOWED[iM]);
//
// And nothing else must match
assert(o_decode == (1<<iM));
end
always @(*)
for(iM=0; iM<NS; iM=iM+1)
if (!ACCESS_ALLOWED[iM])
assert(!o_decode[iM]);
// LOWPOWER check
// {{{
generate if (OPT_LOWPOWER && OPT_REGISTERED)
begin
always @(*)
if (!o_valid)
begin
assert(o_addr == 0);
assert(o_decode == 0);
assert(o_data == 0);
end
end endgenerate
// }}}
//
// The output decoded value may only ever have one value high,
// never more--i.e. $onehot0
// {{{
`ifdef VERIFIC
always @(*)
assert($onehot0(request));
`else
reg onehot_request;
always @(*)
begin
onehot_request = 0;
for(iM=0; iM<NS+1; iM=iM+1)
if ((request ^ (1<<iM))==0)
onehot_request = 1;
end
always @(*)
if (request != 0)
assert(onehot_request);
`endif
// }}}
////////////////////////////////////////////////////////////////////////
//
// Cover properties
// {{{
////////////////////////////////////////////////////////////////////////
//
//
//
// Make sure all addresses are reachable
//
reg [NS:0] f_reached;
always @(posedge i_clk)
cover(i_valid);
always @(posedge i_clk)
cover(o_valid);
always @(posedge i_clk)
cover(o_valid && !i_stall);
initial f_reached = 0;
always @(posedge i_clk)
if (i_reset)
f_reached = 0;
else if (o_valid)
f_reached = f_reached | o_decode;
generate if (!OPT_NONESEL && ACCESS_ALLOWED[0]
&& SLAVE_MASK == 0 && NS == 1)
begin
always @(*)
cover(f_reached[0]);
always @(posedge i_clk)
if (f_past_valid && $stable(o_valid))
assert($stable(o_decode));
end else begin
always @(*)
cover(&f_reached);
always @(posedge i_clk)
if (f_past_valid && $stable(o_valid))
cover($changed(o_decode));
end endgenerate
// }}}
`endif // FORMAL
// }}}
endmodule
`ifndef YOSYS
//`default_nettype wire
`endif
|