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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
//
// Copyright 2020 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Module : axi4s_remove_bytes
//
// Description:
// Remove bytes from a packet. 1 removal can happen per
// packet. The removal is made by delaying the output
// by a clock, and then combining the new and old word
// and providing a combination of shifted words.
// This implementation requires that the user field
// holds the number of valid bytes in the word, and the MSB of the user field
// indicates if the MAC had an error.
//
// The block will hold off the input if it goes to the BONUS State.
//
// This block is intended to remove data from the beginning or middle
// of a packet. You can truncate a packet by setting REM_END to -1.
//
// LIMITATIONS
// The block will set the error bit if you put in a packet between
// REM_END and REM_START length, and it is unable to cleanly signal
// and end to the packet. (there is no way to send a zero byte valid
// packet using tuser protocol.
// Packets must be terminated with tlast.
//
// Parameters:
// REM_START - First byte to remove (0 means start)
// REM_END - Last byte to remove (-1 means truncate from REM START)
//
module axi4s_remove_bytes #(
REM_START=0,
REM_END=8
)(
interface.slave i, // AxiStreamIf or AxiStreamPacketIf
interface.master o // AxiStreamIf or AxiStreamPacketIf
);
localparam BYTES_PER_WORD = i.DATA_WIDTH/8;
// tUSER - always {error,numbytes}
localparam UWIDTH = $clog2(BYTES_PER_WORD+1);
localparam ERROR = UWIDTH-1; // MSB is the error bit.
localparam TRUNCATE = REM_END < 0;
// END is inclusive so +1
localparam BYTES_REMOVED = TRUNCATE ? 1 :
REM_END-REM_START+1;
// how many bytes into the word for start and end point
localparam START_BYTE = REM_START% BYTES_PER_WORD;
localparam START_WORD = REM_START/ BYTES_PER_WORD;
localparam END_BYTE = TRUNCATE ? BYTES_PER_WORD-1 :
REM_END % BYTES_PER_WORD;
localparam END_WORD = TRUNCATE ? 65535 : // max word counter value
REM_END / BYTES_PER_WORD;
localparam FIRST_BYTE_AFTER = (END_BYTE+1) % BYTES_PER_WORD;
localparam BYTE_SHIFT = BYTES_REMOVED % BYTES_PER_WORD;
localparam BYTE_CARRY = BYTES_PER_WORD - BYTE_SHIFT;
// CASE differentiators
localparam SINGLE = BYTES_REMOVED <= BYTES_PER_WORD;
localparam START_AT_LSB = START_BYTE == 0;
localparam END_AT_MSB = END_BYTE == BYTES_PER_WORD-1;
localparam EXACT = START_AT_LSB && END_AT_MSB;
localparam MIDDLE = END_BYTE >= START_BYTE;
`include "axi4s.vh"
// Parameter Checks
initial begin
assert (i.DATA_WIDTH == o.DATA_WIDTH) else
$fatal(1, "DATA_WIDTH mismatch");
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);
// implement specialized cases
if (REM_START == 0 && !EXACT) begin : start_not_exact
// START at zero but still shifted
axi4s_remove_bytes_start #(
.REM_END(REM_END)
) axi4s_remove_bytes_start_i (
.i(i), .o(o)
);
end else begin : general
// move from AxiStreamIfc to AxiStreamPacketIf
always_comb begin
`AXI4S_ASSIGN(s0,i)
end
typedef enum {MS_EXACT, MS_START_AT_LSB, MS_END_AT_MSB,
SINGLE_MIDDLE,MULTI_MIDDLE, MS_WRAP} case_t;
case_t MCASE;
logic reached_start;
logic reached_end;
logic reached_end_plus;
// memory for holding old values
logic [s0.DATA_WIDTH-1:0] last_tdata;
logic [s0.DATA_WIDTH-1:0] first_tdata;
logic [UWIDTH-1:0] first_tuser;
// various flavors of data shifting
logic [s0.DATA_WIDTH-1:0] trunc_data;
logic [s0.DATA_WIDTH-1:0] remaining_shift_data;
logic [s0.DATA_WIDTH-1:0] prefirst_shifted_data;
logic [s0.DATA_WIDTH-1:0] first_shifted_data;
logic [s0.DATA_WIDTH-1:0] one_word_data;
logic [s0.DATA_WIDTH-1:0] bonus_data;
logic [15:0] word_count; // Oversized to 65536 words
logic error_bit, error_bit_old;
logic [UWIDTH-1:0] in_byte_count;
//---------------------------------------
// remove state machine
//---------------------------------------
typedef enum {ST_PRE_REMOVE, ST_TRUNCATE, ST_REMOVING,
ST_POST_REMOVE, ST_BONUS} remove_state_t;
remove_state_t remove_state = ST_PRE_REMOVE;
remove_state_t next_remove_state = ST_PRE_REMOVE;
always_comb in_byte_count = get_bytes(s0.tuser);
// Cache a couple of words from the bus
always_ff @(posedge s0.clk) begin
if (s0.rst) begin
last_tdata = 0;
first_tdata = 0;
first_tuser = 0;
end else
if (s0.tvalid && s0.tready &&
(MCASE == MULTI_MIDDLE || MCASE==MS_START_AT_LSB))
last_tdata = s0.tdata;
if (s0.tvalid && s0.tready &&
(reached_start || next_remove_state==ST_POST_REMOVE ||
(remove_state!=ST_REMOVING && remove_state!= ST_TRUNCATE))) begin
first_tdata = s0.tdata;
first_tuser = s0.tuser;
end
end
//***************** DATA SHIFTING CASES ***********************/
//-----------------------------------------------------------------------
// 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 logic [START_BYTE*8-1:0] start_part([s0.DATA_WIDTH-1:0] data);
begin
// workaround :: modelsim optimizer can fail if there is a possibility of a 0+:0
localparam MY_START_BYTE = START_BYTE ? START_BYTE : 1;
return data[0+:MY_START_BYTE*8];
end
endfunction
function automatic logic [s0.DATA_WIDTH-1-FIRST_BYTE_AFTER*8:0] end_part([s0.DATA_WIDTH-1:0] data);
begin
return data[s0.DATA_WIDTH-1:FIRST_BYTE_AFTER*8];
end
endfunction
function automatic logic [s0.DATA_WIDTH-1-BYTE_SHIFT*8:0] bs_part([s0.DATA_WIDTH-1:0] data);
begin
return data[s0.DATA_WIDTH-1:BYTE_SHIFT*8];
end
endfunction
// Examples
//
// ENDING CASE 1
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// D0 C0 B0 A0 <- word 0
// D1 XX XX XX <- R(6:4)) D0 C0 B0 A0
// D2 C2 B2 A2 C2 B2 A2 D1
// D0 C0 B0 A0 <- next packet D2
// D0 C0 B0 A0
//
// ENDING CASE2
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// D0 C0 B0 A0 <- word 0
// D1 XX XX XX <- R(6:4)) D0 C0 B0 A0
// C2 B2 A2 C2 B2 A2 D1
// D0 C0 B0 A0 <- next packet
// D0 C0 B0 A0
// Middle of Word case
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// D0 C0 B0 A0 <- word 0
// D1 XX XX A1 <- R(7:6) D0 C0 B0 A0
// D2 C2 B2 A2 B2 A2 D1 A1
// D3 C3 B3 A3 B3 A3 D2 C2
// D0 C0 B0 A0 <- next packet D3 C3
//
// Easy Truncation (can handle dynamically)
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// D0 C0 B0 A0 <- word 0
// D1 C1 B1 A1 D0 C0 B0 A0
// XX XX XX <- R(11:8)) D1 C1 B1 A1 <- TLAST HERE
// D0 C0 B0 A0 <- next packet
// D0 C0 B0 A0
// Truncation case requiring REM_END=-1
// because last word is to far away to see tlast.
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// D0 C0 B0 A0 <- word 0
// XX XX XX XX <- R(-1:4)) D0 C0 B0 A0 <- TLAST HERE
// XX XX XX XX
// XX XX XX XX
// D0 C0 B0 A0 <- next packet
// D0 C0 B0 A0
// Remove from Front
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// XX XX XX XX <- R(0:7)
// XX XX XX XX <-
// C2 B2 A2
// D0 C0 B0 A0 <- next packet C2 B2 A2
// D0 C0 B0 A0
//
// Remove 1 byte on back to back 1 word packets
// Incoming packet outgoing packet
// ///////////////////////////////////////////////
// D0 C0 XX A0 <- R(1:1)
// D0 C0 XX A0 <- R(1:1) D0 C0 A0
// D0 C0 XX A0 <- R(1:1) D0 C0 A0
// D0 C0 A0
//
//
// Note these should all be static shifts. We don't want to infer a barrel shifter.
if (EXACT) begin // Remove whole words
always_comb begin
MCASE = MS_EXACT;
first_shifted_data = s0.tdata;
remaining_shift_data = s0.tdata;
one_word_data = s0.tdata;
trunc_data = first_tdata;
bonus_data = 'bX;
end
end else if (START_AT_LSB) begin // Remove start of word shift case
// EXAMPLE XX XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0 with START BYTE = 0(A0) END_BYTE = 2(C0) BYTE_SHIFT=3
// 1st word would be C1 B1 A1/H0 G0 F0 E0 D0
// [23:0] C1 B1 A1 / [63:24] H0 G0 F0 E0 D0
// same as remaining_shift_data above
// EXAMPLE XX XX XX XX XX XX XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0
// XX XX XX
// H1 G1 F1 E1 D1 C1 B1 A1 with START BYTE = 0(A0) END_BYTE = 2(10)(C1) BYTE_SHIFT=3
// 1st word would be C2 B2 A2/H1 G1 F1 E1 D1
// [23:0] C2 B2 A2 / [63:24] H1 G1 F1 E1 D1
// same as remaining_shift_data above
// NOTE: Entire words are thrown away at start, so no caching required
always_comb begin
MCASE = MS_START_AT_LSB;
first_shifted_data = {s0.tdata,bs_part(last_tdata)};
if (BYTE_SHIFT==0)
remaining_shift_data = s0.tdata;
else
remaining_shift_data = {s0.tdata,bs_part(last_tdata)};
bonus_data = 'bX;
one_word_data = end_part(s0.tdata);
trunc_data = first_tdata;
bonus_data = 'bX;
bonus_data = bs_part(s0.tdata);
end
end else if (END_AT_MSB) begin // Remove end of word shift case
// EXAMPLE XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0 with START BYTE = 6(G0) END_BYTE = 7(H0) BYTE_SHIFT=2
// 1st word would be B1 A1/F0 E0 D0 C0 B0 A0
// [15:0] B1 A1 / [47:0] F0 E0 D0 C0 B0 A0
// EXAMPLE XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0
// XX XX XX XX XX XX XX XX
// H1 G1 F1 E1 D1 C1 B1 A1 with START BYTE = 6(G0) END_BYTE = 7(15)(H0) BYTE_SHIFT=2
// 1st word would be B2 A2/F0 E0 D0 C0 B0 A0
// NOTE: Uses 1st Data (from when we reach the first word
// [15:0] B2 A2 / [47:0] F0 E0 D0 C0 B0 A0
always_comb begin
MCASE = MS_END_AT_MSB;
first_shifted_data = {s0.tdata,start_part(first_tdata)};
if (BYTE_SHIFT==0)
remaining_shift_data = s0.tdata;
else
remaining_shift_data = {s0.tdata,bs_part(first_tdata)};
one_word_data = s0.tdata;
trunc_data = first_tdata;
bonus_data = 'bX;
bonus_data = bs_part(s0.tdata);
end
end else if(MIDDLE) begin // Remove middle of word shift case
// EXAMPLE XX XX XX XX XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0 with START BYTE = 1(B0) END_BYTE = 6(G0) BYTE_SHIFT=6
// 1st word would be F1 E1 D1 C1 B1 A1/H0/A0
// [47:0] F1 E1 D1 C1 B1 A1 [63:56] H0 [7:0] A0
// EXAMPLE XX XX XX XX XX XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0
// XX XX XX XX XX XX XX
// H1 G1 F1 E1 D1 C1 B1 A1 with START BYTE = 1(B0) END_BYTE = 6(14)(G0) BYTE_SHIFT=6
// 1st word would be F2 E2 D2 C2 B2 A2/H1/A0
// NOTE: Uses first Data from when we reach the first word.
// Also, must advance one clock beyond end for this case.
// [47:0] F2 E2 D2 C2 B2 A2 [63:56] H1 [7:0] A0
// remaining words F2 E2 D2 C2 B2 A2/H1 G1
// [47:0] F2 E2 D2 C2 B2 A2 [63:48] H1 G1
// same as remaining_shift_data above
always_comb begin
if (SINGLE) begin
MCASE = SINGLE_MIDDLE;
first_shifted_data = {s0.tdata,end_part(first_tdata),start_part(first_tdata)};
end else begin
MCASE = MULTI_MIDDLE;
prefirst_shifted_data = {end_part(s0.tdata),start_part(first_tdata)};
first_shifted_data = {s0.tdata,end_part(last_tdata),start_part(first_tdata)};
end
if (BYTE_SHIFT==0)
remaining_shift_data = s0.tdata;
else
remaining_shift_data = {s0.tdata,bs_part(first_tdata)};
one_word_data = {end_part(s0.tdata),start_part(s0.tdata)};
trunc_data = first_tdata;
bonus_data = 'bX;
bonus_data = bs_part(s0.tdata);
end
end else begin //wrapped case
// EXAMPLE XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0 with START BYTE = 6(G0) END_BYTE = 2(10)(C1) BYTE_SHIFT=5
// XX XX XX
// H1 G1 F1 E1 D1 C1 B1 A1
//
// 1st word would be E1 D1/F0 E0 D0 C0 B0 A0
// [39:24] E1 D1 / [47:0] F0 E0 D0 C0 B0 A0
// remaining words E2 D2 C2 B2 A2/H1 G1 F1
// [39:0] E2 D2 C2 B2 A2 / H1 G1 F1 [63:40]
// same as remaining_shift_data_above // EXAMPLE XX XX
// for 8 byte word H0 G0 F0 E0 D0 C0 B0 A0
// XX XX XX XX XX XX XX XX
// H1 G1 F1 E1 D1 C1 B1 A1
// XX XX XX
// H2 G2 F2 E2 D2 C2 B2 A2 with START BYTE = 6(G0) END_BYTE = 2(10)(C1) BYTE_SHIFT=5
//
// 1st word would be E2 D2/F0 E0 D0 C0 B0 A0
// NOTE: Uses 1st Data (from when we reach the first word ;
// [39:24] E2 D2 / [47:0] F0 E0 D0 C0 B0 A0
always_comb begin
MCASE = MS_WRAP;
first_shifted_data = {end_part(s0.tdata),start_part(first_tdata)};
if (BYTE_SHIFT==0)
remaining_shift_data = s0.tdata;
else
remaining_shift_data = {s0.tdata,bs_part(first_tdata)};
one_word_data = s0.tdata;
trunc_data = first_tdata;
bonus_data = 'bX;
bonus_data = bs_part(s0.tdata);
end
end
typedef enum {PASS_THRU,BONUS,REM_SHIFT_DATA,FIRST_SHIFT_DATA,
PREFIRST_SHIFT_DATA,TRUNCATE_DATA,ONE_WORD} data_mux_sel_t;
data_mux_sel_t data_mux_sel = PASS_THRU;
always_comb begin : data_mux
s1.tdata = s0.tdata;
case (data_mux_sel)
PASS_THRU : s1.tdata = s0.tdata;
ONE_WORD : s1.tdata = one_word_data;
FIRST_SHIFT_DATA : s1.tdata = first_shifted_data;
PREFIRST_SHIFT_DATA : if (MCASE==MULTI_MIDDLE)
s1.tdata = prefirst_shifted_data;
else
s1.tdata = first_shifted_data;
REM_SHIFT_DATA : if (!TRUNCATE)
s1.tdata = remaining_shift_data;
TRUNCATE_DATA : if (TRUNCATE)
s1.tdata = trunc_data;
BONUS : if (!TRUNCATE && !EXACT)
s1.tdata = bonus_data;
default : s1.tdata = s0.tdata;
endcase
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[ERROR];
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
// Debug state used to determine which sub-case is taken in simulation
typedef enum {D_IDLE, D_REACHED_START, D_TRUNCATE, D_LAST, D_NOT_LAST,
D_LAST_WO_END, D_LAST_W_END, D_LAST_W_END_BONUS, D_LAST_W_END_PLUS,
D_REACHED_END_PLUS} debug_t;
debug_t debug = D_IDLE;
always_ff @(posedge s0.clk) begin
if (s0.rst) begin
error_bit_old <= 0;
end else begin
// must hold until bonus completes
if (s1.tlast && s1.tvalid && s1.tready && remove_state==ST_BONUS) begin
error_bit_old <= 0;
// or clear if not going to bonus
end else if (s0.tlast && s0.tvalid && s0.tready && next_remove_state!=ST_BONUS) 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
assign error_bit = get_error(s0.tuser) || error_bit_old;
// When truncating we want to hold the last valid word until
// the end so we can accumulate any errors that might of occured
if (TRUNCATE && START_BYTE==0) begin
always_comb reached_start = s0.reached_packet_byte(REM_START-1);
end else begin
always_comb reached_start = s0.reached_packet_byte(REM_START);
end
// the WRAP case leans forward one word since it bridges to
// the next word so it needs to reach end_plus early
always_comb begin : reached_end_comb
if (MCASE==MS_WRAP) begin
reached_end = s0.reached_packet_byte(REM_END);
reached_end_plus = s0.reached_packet_byte(REM_END);
end else begin
reached_end = s0.reached_packet_byte(REM_END);
reached_end_plus = s0.reached_packet_byte(REM_END+BYTES_PER_WORD);
end
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;
// Remove State Machine
always_comb begin : remove_next_state
// default assignment of next_state
next_remove_state = remove_state;
debug = D_IDLE;
data_mux_sel = PASS_THRU;
s1.tuser = s0.tuser;
s1.tlast = s0.tlast;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
case (remove_state)
// *****************************************************
// PRE_REMOVE - wait till we reach REM_START
// *****************************************************
ST_PRE_REMOVE: begin
//defaults
data_mux_sel = PASS_THRU;
s1.tuser = s0.tuser;
s1.tlast = s0.tlast;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
if (reached_start) begin // based only on word count
debug = D_REACHED_START;
// Truncating word end
if (TRUNCATE && s0.tlast) begin
s1.tlast = 1;
data_mux_sel = ONE_WORD;
debug = D_TRUNCATE;
// get number of bytes based on if we had enough to surpass END_BYTE
if (START_BYTE == 0) // Exact case
s1.tuser = uwrite(error_bit,in_byte_count);
else if (in_byte_count < START_BYTE)
s1.tuser = uwrite(error_bit,in_byte_count);
else
s1.tuser = uwrite(error_bit,START_BYTE);
end else if (TRUNCATE && !s0.tlast) begin
s1.tlast = 1;
data_mux_sel = ONE_WORD;
debug = D_TRUNCATE;
if (s1.tready && s0.tvalid) begin
s1.tlast = 0;
s1.tvalid = 0;
next_remove_state = ST_TRUNCATE;
end
// packet ends
end else if (s0.tlast) begin
s1.tlast = 1;
data_mux_sel = ONE_WORD;
debug = D_LAST;
// get number of bytes based on if we had enough to surpass END_BYTE
if (in_byte_count < START_BYTE)
s1.tuser = uwrite(error_bit,in_byte_count);
else if (START_WORD != END_WORD)
s1.tuser = uwrite(error_bit,START_BYTE);
else if (in_byte_count < END_BYTE+1)
s1.tuser = uwrite(error_bit,START_BYTE);
else
s1.tuser = uwrite(error_bit,in_byte_count - BYTES_REMOVED);
// if we are on the first word of the removal and have no way to terminate the packet
// set error.
if ((START_WORD != END_WORD && START_BYTE == 0) || EXACT)
s1.tuser[ERROR] = 1;
// if removal starts at the start of the packet, squelch the packet.
if ( (START_WORD != END_WORD && REM_START == 0) ||
// also if we don't have enough data to publish 1 byte
((in_byte_count <= END_BYTE+1) && REM_START == 0)) begin
s1.tlast = 0;
s1.tvalid = 0;
end
end else begin // not the last word
debug = D_NOT_LAST;
s1.tvalid = 0;
if (s0.tvalid) begin
// we will always need to wait for some more data before
// forming the next word if this was not the start of the packet
next_remove_state = ST_REMOVING;
end
end
end
end //ST_PRE_REMOVE
// *****************************************************
// TRUNCATE - wait for end of packet to put out the
// last word (so we can see if error bit asserts)
// *****************************************************
ST_TRUNCATE: begin
if (TRUNCATE) begin // Simplify synthesis
// get number of bytes based on if we had enough to surpass END_BYTE
if (get_bytes(first_tuser) < START_BYTE)
s1.tuser = uwrite(error_bit,get_bytes(first_tuser));
else
s1.tuser = uwrite(error_bit,START_BYTE);
data_mux_sel = TRUNCATE_DATA;
s1.tlast = s0.tlast;
s1.tvalid = s0.tlast && s0.tvalid;
s0_tready = 1;
if (s1.tready && s0.tvalid && s0.tlast) begin
next_remove_state = ST_PRE_REMOVE;
end
end
end
// *****************************************************
// REMOVING - burn words until we have data to
// start sending again
// *****************************************************
ST_REMOVING: begin
//defaults
data_mux_sel = FIRST_SHIFT_DATA;
s1.tuser = 0;
s1.tlast = 0;
s1.tvalid = 0;
s0_tready = 1;
// if we don't reach the end of the removal
// it is an error case because we don't
// have any valid data to send with the tlast.
if (s0.tlast && !reached_end && !reached_end_plus) begin
debug = D_LAST_WO_END;
s1.tuser = uwrite(1,0);
s1.tlast = 1;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
// started from zero we have pushed
// zero data so we can just squelch the packet
if (REM_START==0)
s1.tvalid = 0;
if (s1.tready && s0.tvalid) begin
next_remove_state = ST_PRE_REMOVE;
end
// end of packet and we have some data to send
// but we didn't buffer the extra word of end data yet
end else if (s0.tlast && reached_end && !reached_end_plus) begin
debug = D_LAST_W_END;
if (MCASE==MULTI_MIDDLE)
data_mux_sel = PREFIRST_SHIFT_DATA;
else if (MCASE==MS_START_AT_LSB)
data_mux_sel = ONE_WORD;
s1.tlast = 1;
// if we are exact and started from zero we have pushed
// zero data so we can just squelch the packet
if (EXACT && REM_START==0)
s1.tvalid = 0;
else
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
if (MCASE==MULTI_MIDDLE)
if (in_byte_count <= FIRST_BYTE_AFTER)
s1.tuser = uwrite(1,0); // not enough data to avoid error
else
s1.tuser = uwrite(error_bit,in_byte_count + BYTE_CARRY);
else if (MCASE==MS_END_AT_MSB)
s1.tuser = uwrite(1,0); // not enough data to avoid error
else if (in_byte_count <= FIRST_BYTE_AFTER)
if (REM_START == 0)
s1.tvalid = 0;
else
s1.tuser = uwrite(1,0); // not enough data to avoid error
else
s1.tuser = uwrite(error_bit,in_byte_count - FIRST_BYTE_AFTER);
// if we are exact and have already published some data
// that data is unterminated and we have no way to
// set a packet end.
if (EXACT && REM_START!=0)
s1.tuser[ERROR] = 1;
if (s1.tready && s0.tvalid) begin
next_remove_state = ST_PRE_REMOVE;
end
// end of packet and we have some some data to send
// and we have more data then we can fit in the
// the current word
end else if (s0.tlast && reached_end_plus && in_byte_count > BYTE_SHIFT
&& BYTE_SHIFT != 0) begin
debug = D_LAST_W_END_BONUS;
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = 0; // don't let a advance
if (s0.tvalid && s1.tready) begin
next_remove_state = ST_BONUS;
end
// end of packet and we have some some data to send
// and we were ready to send data anyways
end else if(s0.tlast && reached_end_plus) begin
debug = D_LAST_W_END_PLUS;
s1.tlast = 1;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
if (EXACT)
s1.tuser = uwrite(error_bit,in_byte_count);
else begin
s1.tuser = uwrite(error_bit,in_byte_count + BYTE_CARRY);
end
if (MCASE==MS_WRAP && in_byte_count <= FIRST_BYTE_AFTER)
s1.tuser = uwrite(1,0); // not enough data to avoid error
if (s1.tready && s0.tvalid) begin
next_remove_state = ST_PRE_REMOVE;
end
// we are ready to send the first byte after the shift
end else if(!s0.tlast && reached_end_plus) begin
debug = D_REACHED_END_PLUS;
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
if (s1.tready && s0.tvalid) begin
next_remove_state = ST_POST_REMOVE;
end
end
end
// *****************************************************
// POST_REMOVAL waiting for end
// *****************************************************
ST_POST_REMOVE: begin
//defaults
data_mux_sel = REM_SHIFT_DATA;
s1.tuser = uwrite(error_bit,BYTES_PER_WORD);
s1.tlast = 0;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
// reached the end, but we have extra bytes to send
if (s0.tlast && in_byte_count > BYTE_SHIFT
&& BYTE_SHIFT != 0) begin
s1.tlast = 0;
s0_tready = 0; // don't let a advance
if (s0.tvalid && s1.tready) begin
next_remove_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,in_byte_count + BYTES_PER_WORD-BYTE_SHIFT);
if (s1.tready && s0.tvalid) begin
next_remove_state = ST_PRE_REMOVE;
end
end
end
// *****************************************************
// BONUS write out any overflow words
// *****************************************************
ST_BONUS: begin
//defaults
data_mux_sel = BONUS;
s1.tuser = uwrite(error_bit,in_byte_count-BYTE_SHIFT);
s1.tlast = 1;
s1.tvalid = s0.tvalid;
s0_tready = s1.tready;
if (s1.tready && s0.tvalid) begin
next_remove_state = ST_PRE_REMOVE;
end
end
// We should never get here
default: begin
next_remove_state = ST_PRE_REMOVE;
end
endcase
end
always_ff @(posedge s0.clk) begin
if (s0.rst) begin
remove_state <= ST_PRE_REMOVE;
end else begin
remove_state <= next_remove_state;
end
end
// move from AxiStreamIfc to AxiStreamPacketIf
always_comb begin
`AXI4S_ASSIGN(o,s1)
end
end
endmodule : axi4s_remove_bytes
|