-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis_width_converter.v
114 lines (102 loc) · 3.39 KB
/
axis_width_converter.v
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
`timescale 1ns/1ps
`default_nettype none
// axi_stream_width_converter #(
// .INPUT_WIDTH (),
// .OUTPUT_WIDTH ()
// ) eight_to_one_bit_converter_inst (
// .clk (),
// .reset (),
// .s_valid_i (),
// .s_data_i (),
// .s_ready_o (),
// .m_ready_i (),
// .m_valid_o (),
// .m_data_o ()
// );
module axi_stream_width_converter #(
parameter INPUT_WIDTH = 8,
parameter OUTPUT_WIDTH = 1,
parameter MSB_FIRST = 1 // endianness, which direction to shift out the data
) (
input wire clk,
input wire reset,
input wire s_valid_i,
input wire [INPUT_WIDTH-1:0] s_data_i,
output wire s_ready_o,
input wire m_ready_i,
output wire m_valid_o,
output wire [OUTPUT_WIDTH-1:0] m_data_o
);
// only supports the following case:
// INPUT_WIDTH > OUTPUT_WIDTH and INPUT_WIDTH % OUTPUT_WIDTH == 0
localparam COUNT_HIGH = INPUT_WIDTH / OUTPUT_WIDTH - 1;
wire [$clog2(COUNT_HIGH)-1:0] count;
wire rollover;
counter #(
.HIGH (COUNT_HIGH)
) counter_inst (
.clk (clk),
.clock_enable (1'b1),
.sync_reset (1'b0),
.enable (m_valid_o && m_ready_i),
.count (count),
.tc (rollover)
);
reg valid_next;
always @(*) begin : valid_next_proc
if (m_valid_o) begin
if (rollover && m_ready_i) begin
// last beat of the packet is being sent
valid_next = 0;
end else begin
// either a beat is not being sent or there is another beat to follow it
valid_next = 1;
end
end else begin
if (s_valid_i && s_ready_o) begin
// loading some new data
valid_next = 1;
end else begin
// ready for new data but there isn't any
valid_next = 0;
end
end
end
wire shift_register_loaded;
register #(
.RESET_VALUE (1'b0),
.WIDTH (1)
) load_register_inst (
.clk (clk),
.reset (reset),
.write_enable (1'b1),
.data_i (valid_next),
.data_o (shift_register_loaded)
);
assign s_ready_o = ~shift_register_loaded;
assign m_valid_o = shift_register_loaded;
wire [INPUT_WIDTH-1:0] data;
wire [INPUT_WIDTH-1:0] next_data;
wire [INPUT_WIDTH-1:0] shifted_data;
assign next_data = (s_valid_i && s_ready_o) ? s_data_i : shifted_data;
register #(
.RESET_VALUE ('b0),
.WIDTH (INPUT_WIDTH)
) shift_register_inst (
.clk (clk),
.reset (reset),
.write_enable ((s_valid_i && s_ready_o) || (m_valid_o && m_ready_i)),
.data_i (next_data),
.data_o (data)
);
generate
if (MSB_FIRST) begin
// not tested
assign shifted_data = { data[INPUT_WIDTH-OUTPUT_WIDTH-1:0], {OUTPUT_WIDTH{1'bz}} };
assign m_data_o = data[INPUT_WIDTH-1:INPUT_WIDTH-OUTPUT_WIDTH];
end else begin
assign shifted_data = { {OUTPUT_WIDTH{1'bz}}, data[INPUT_WIDTH-1:OUTPUT_WIDTH] };
assign m_data_o = data[0+:OUTPUT_WIDTH];
end
endgenerate
endmodule