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
|
//
// Copyright 2020 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Module: axi4s_add_bytes
//
// Description:
//
// Add zero filled bytes to a packet.
// tUser = {error,trailing bytes};
//
// LIMITATIONS
// The block only adds bytes to the beginning of a word.
//
// Parameters:
// ADD_START - Add bytes before this point (0 means start)
// 0 is the only supported value right now
// ADD_BYTES - Number of bytes to add
// SYNC - When 1 we wait for the start word to be
// valid before we start shifting.
// When 0 we aggressively pad 0 early, but
// it means the extra space may be added before
// we setup the values we want to overwrite onto
// that space.
module axi4s_add_bytes #(
int ADD_START = 0,
int ADD_BYTES = 6,
bit SYNC = 1
) (
interface i, // AxiStreamIf or AxiStreamPacketIf
interface o // AxiStreamIf or AxiStreamPacketIf
);
localparam BYTES_PER_WORD = i.DATA_WIDTH/8;
// tUSER - always {error,numbytes}
localparam UWIDTH = $clog2(BYTES_PER_WORD+1);
//packet position in bytes of the last removed byte.
localparam ADD_END = ADD_START + ADD_BYTES-1;
//packet position in bytes of the 1st byte after removal.
localparam ADD_RESTART = ADD_END+1;
////////////// Byte offsets in a word /////////////////
localparam START_BYTE = ADD_START % BYTES_PER_WORD;
localparam END_BYTE = ADD_END % BYTES_PER_WORD;
localparam RESTART_BYTE = ADD_RESTART % BYTES_PER_WORD;
// An Important shift offset
localparam BYTE_SHIFT = (BYTES_PER_WORD - RESTART_BYTE)%BYTES_PER_WORD;
// Subcase Recognition
// EXACT case - the removal expression is removing an entire word
localparam EXACT = BYTE_SHIFT == 0;
`include "axi4s.vh"
// Parameter Checks
initial begin
assert (i.DATA_WIDTH == o.DATA_WIDTH) else
$fatal("DATA_WIDTH mismatch");
assert (i.USER_WIDTH == o.USER_WIDTH) else
$fatal("USER_WIDTH mismatch");
assert (i.USER_WIDTH >= UWIDTH) else
$fatal("i.USER_WIDTH is to small");
assert (o.USER_WIDTH >= UWIDTH) else
$fatal("o.USER_WIDTH is to small");
assert (ADD_START == 0) else
$fatal("Only tested for ADD_START = 0");
end
AxiStreamPacketIf #(.DATA_WIDTH(i.DATA_WIDTH),.USER_WIDTH(i.USER_WIDTH),
.TKEEP(0),.MAX_PACKET_BYTES(i.MAX_PACKET_BYTES))
s0(i.clk,i.rst);
AxiStreamPacketIf #(.DATA_WIDTH(i.DATA_WIDTH),.USER_WIDTH(i.USER_WIDTH),
.TKEEP(0),.MAX_PACKET_BYTES(i.MAX_PACKET_BYTES))
s1(i.clk,i.rst);
// move from AxiStreamIfc to AxiStreamPacketIf
always_comb begin
`AXI4S_ASSIGN(s0,i)
end
logic reached_start;
logic reached_end;
logic byte_overflow;
logic [s0.DATA_WIDTH-1:0] zero_data;
logic [s0.DATA_WIDTH-1:0] last_tdata;
logic [s0.DATA_WIDTH-1:0] remaining_shift_data;
logic [s0.DATA_WIDTH-1:0] last_shift_data;
logic [s0.DATA_WIDTH-1:0] first_shifted_data;
logic error_bit, error_bit_old;
// Cache a couple of words from the bus
always_ff @(posedge s0.clk) begin
if (s0.rst) begin
last_tdata <= 0;
end else if (s0.tvalid && s0.tready) begin
last_tdata <= s0.tdata;
end
end
if (EXACT) begin
always_comb begin
// If END_BYTE=3
zero_data = 'b0;
first_shifted_data = s0.tdata;
remaining_shift_data = s0.tdata;
last_shift_data = s0.tdata;
end
end else begin
always_comb begin
zero_data = 'b0;
// If END_BYTE=2 [7:0] [23:0]
// If END_BYTE=1 [15:0] [15:0]
// If END_BYTE=0 [23:0] [7:0]
first_shifted_data = {s0.tdata[BYTE_SHIFT*8-1:0],zero_data[END_BYTE*8+7:0]};
// If END_BYTE=0 [23:0] [31:24]
remaining_shift_data = {s0.tdata[BYTE_SHIFT*8-1:0],last_tdata[s0.DATA_WIDTH-1:BYTE_SHIFT*8]};
// If END_BYTE=0 [23:0] [31:24]
last_shift_data = {zero_data[BYTE_SHIFT*8-1:0],s0.tdata[s0.DATA_WIDTH-1:BYTE_SHIFT*8]};
end
end
//-----------------------------------------------------------------------
// user write function
// this module ASSUMES user includes error in the MSB and the rest is the
// number of bytes in the word
//-----------------------------------------------------------------------
function automatic [UWIDTH-1:0] uwrite(error=0,[UWIDTH-2:0] bytes=0);
begin
return {error,bytes};
end
endfunction
//-----------------------------------------------------------------------
// get_error -extract error from tuser
//-----------------------------------------------------------------------
function automatic get_error([UWIDTH-1:0] tuser);
begin
return tuser[UWIDTH-1];
end
endfunction
//-----------------------------------------------------------------------
// get_bytes -extract num_bytes from tuser
//-----------------------------------------------------------------------
function automatic [UWIDTH-1:0] get_bytes([UWIDTH-1:0] tuser);
logic [UWIDTH-1:0] bytes;
begin
if (tuser[UWIDTH-2:0] == 0) bytes = BYTES_PER_WORD;
else bytes = tuser[UWIDTH-2:0];
return bytes;
end
endfunction
//---------------------------------------
// remove state machine
//---------------------------------------
typedef enum {ST_PRE_ADD, ST_ADDING, ST_POST_ADD,ST_BONUS} add_state_t;
add_state_t add_state = ST_PRE_ADD;
add_state_t next_add_state = ST_PRE_ADD;
always_ff @(posedge s0.clk) begin
if (s0.rst) begin
error_bit_old <= 0;
end else begin
// must hold until output completes
if (s1.tlast && s1.tvalid && s1.tready) begin
error_bit_old <= 0;
// but they set based on the input
end else if (s0.tvalid && s0.tready) begin
error_bit_old <= error_bit;
end
end
end
// Find the landmark bytes
always_comb error_bit = get_error(s0.tuser) || error_bit_old;
always_comb begin
reached_start = s1.reached_packet_byte(ADD_START);
reached_end = s1.reached_packet_byte(ADD_START+ADD_BYTES);
end
if (EXACT) begin
always_comb byte_overflow = 0;
end else begin
always_comb byte_overflow = get_bytes(s0.tuser) > BYTE_SHIFT;
end
// because s0.tready feeds back and generates a
// change event for the entire interface,
// it can trigger an infinite loop of assignment
// even when nothing is changing. This breaks
// the feedback loop.
logic s0_tready;
always_comb s0.tready = s0_tready;
// ADD state machine
always_comb begin
// default assignment of next_state
next_add_state = add_state;
s1.tuser = s0.tuser;
s1.tlast = s0.tlast;
s1.tvalid = s0.tvalid;
s1.tdata = first_shifted_data;
s0_tready = s1.tready;
case (add_state)
// *****************************************************
// PRE_ADD - wait till we reach ADD_START
// *****************************************************
ST_PRE_ADD: begin
if (!SYNC || s0.tvalid) begin
// reached start and end in same clock and end of word
if (reached_start && reached_end && s0.tlast) begin
// if final word has more bytes than we can fit.
if (byte_overflow) begin
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = 0; // don't advance
s1.tdata = first_shifted_data;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
if (s0.tvalid && s1.tready) begin
next_add_state = ST_BONUS;
end
// we can finish this clock because final word
// didn't overflow into an additional word.
end else begin
s1.tlast = 1;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
s1.tdata = first_shifted_data;
s1.tuser = uwrite(error_bit,get_bytes(s0.tuser) + RESTART_BYTE);
// NO state advance
end
// reached start and end, and not the end of the packet
end else if (reached_start && reached_end && !s0.tlast) begin
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
s1.tdata = first_shifted_data;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
if (s0.tvalid && s1.tready) begin
next_add_state = ST_POST_ADD;
end
// reached start but not the end of byte insertion
end else if (reached_start && !reached_end) begin
s1.tlast = 0;
s1.tvalid = 1;
s0_tready = 0; // don't advance
s1.tdata = zero_data;
s1.tuser = uwrite(0,BYTES_PER_WORD);
if (s1.tready) begin
next_add_state = ST_ADDING;
end
end
end
end //ST_PRE_REMOVE
// *****************************************************
// REMOVING - burn words until we have data to
// start sending again
// *****************************************************
ST_ADDING: begin
//defaults
s1.tlast = 0;
s1.tvalid = 1;
s0_tready = 0; // don't advance
s1.tdata = zero_data;
s1.tuser = uwrite(0,BYTES_PER_WORD);
// reached the end of incoming packet and data insertion
if (reached_end && s0.tlast) begin
// if final word has more bytes than we can fit.
if (byte_overflow) begin
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = 0; // don't advance
s1.tdata = first_shifted_data;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
if (s0.tvalid && s1.tready) begin
next_add_state = ST_BONUS;
end
end else begin
// we can finish this clock because final word
// didn't overflow into an additional word.
s1.tlast = 1;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
s1.tdata = first_shifted_data;
s1.tuser = uwrite(error_bit,get_bytes(s0.tuser) + RESTART_BYTE);
if (s0.tvalid && s1.tready) begin
next_add_state = ST_PRE_ADD;
end
end
// reached the end of data insertion - not end of packet
end else if (reached_end && !s0.tlast) begin
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
s1.tdata = first_shifted_data;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
if (s0.tvalid && s1.tready) begin
next_add_state = ST_POST_ADD;
end
end
end
// *****************************************************
// POST_ADD waiting for end
// *****************************************************
ST_POST_ADD: begin
//defaults
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
s1.tdata = remaining_shift_data;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
// reached the end, but we have extra bytes to send
if (s0.tlast && byte_overflow) begin
s1.tlast = 0;
s0_tready = 0; // don't let a advance
if (s0.tvalid && s1.tready) begin
next_add_state = ST_BONUS;
end
// reached the end, and don't need the bonus state
end else if (s0.tlast) begin
s1.tlast = 1;
s1.tuser = uwrite(error_bit,get_bytes(s0.tuser) + RESTART_BYTE);
if (s1.tready && s0.tvalid) begin
next_add_state = ST_PRE_ADD;
end
end
end
// *****************************************************
// BONUS write out any overflow words
// *****************************************************
ST_BONUS: begin
//defaults
s1.tdata = last_shift_data;
s1.tuser = uwrite(error_bit,get_bytes(s0.tuser)+ RESTART_BYTE);
s1.tlast = 1;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
if (s1.tready && s0.tvalid) begin
next_add_state = ST_PRE_ADD;
end
end
// We should never get here
default: begin
next_add_state = ST_PRE_ADD;
end
endcase
end
always_ff @(posedge s0.clk) begin
if (s0.rst) begin
add_state <= ST_PRE_ADD;
end else begin
add_state <= next_add_state;
end
end
always_comb begin
`AXI4S_ASSIGN(o,s1)
end
endmodule : axi4s_add_bytes
|