-
Notifications
You must be signed in to change notification settings - Fork 46
/
util.v
158 lines (146 loc) · 2.7 KB
/
util.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
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
/*
* BCH Encode/Decoder Modules
*
* Copyright 2014 - Russ Dill <[email protected]>
* Distributed under 2-clause BSD license as contained in COPYING file.
*/
`timescale 1ns / 1ps
module counter #(
parameter MAX = 15,
parameter START = 0,
parameter signed INC = 1
) (
input clk,
input reset,
input ce,
output reg [$clog2(MAX+1)-1:0] count = START
);
localparam TCQ = 1;
always @(posedge clk)
if (reset)
count <= #TCQ START;
else if (ce)
count <= #TCQ count + INC;
endmodule
module pipeline_ce_reset #(
parameter STAGES = 0
) (
input clk,
input ce,
input reset,
input i,
output o
);
localparam TCQ = 1;
if (!STAGES)
assign o = i;
else begin
reg [STAGES-1:0] pipeline = 0;
assign o = pipeline[STAGES-1];
always @(posedge clk)
if (reset)
pipeline <= #TCQ pipeline << 1;
else if (ce)
pipeline <= #TCQ (pipeline << 1) | i;
end
endmodule
module pipeline_ce #(
parameter STAGES = 0
) (
input clk,
input ce,
input i,
output o
);
pipeline_ce_reset #(STAGES) u_ce(clk, ce, 1'b0, i, o);
endmodule
module pipeline_reset #(
parameter STAGES = 0
) (
input clk,
input reset,
input i,
output o
);
pipeline_ce_reset #(STAGES) u_ce(clk, 1'b1, reset, i, o);
endmodule
module pipeline #(
parameter STAGES = 0
) (
input clk,
input i,
output o
);
pipeline_ce_reset #(STAGES) u_ce(clk, 1'b1, 1'b0, i, o);
endmodule
module reverse_words #(
parameter M = 4,
parameter WORDS = 1
) (
input [M*WORDS-1:0] in,
output [M*WORDS-1:0] out
);
genvar i;
for (i = 0; i < WORDS; i = i + 1) begin : REV
assign out[i*M+:M] = in[(WORDS-i-1)*M+:M];
end
endmodule
module rotate_right #(
parameter M = 4,
parameter S = 0
) (
input [M-1:0] in,
output [M-1:0] out
);
wire [M*2-1:0] in2 = {in, in};
assign out = in2[S%M+:M];
endmodule
module rotate_left #(
parameter M = 4,
parameter S = 0
) (
input [M-1:0] in,
output [M-1:0] out
);
wire [M*2-1:0] in2 = {in, in};
assign out = in2[M-(S%M)+:M];
endmodule
module mux_one #(
parameter WIDTH = 2,
parameter WIDTH_SZ = $clog2(WIDTH+1)
) (
input [WIDTH-1:0] in,
input [WIDTH_SZ-1:0] sel,
output out
);
assign out = in[sel];
endmodule
module mux_shuffle #(
parameter U = 2,
parameter V = 2
) (
input [U*V-1:0] in,
output [V*U-1:0] out
);
genvar u, v;
generate
for (u = 0; u < U; u = u + 1) begin : _U
for (v = 0; v < V; v = v + 1) begin : _V
assign out[v*U+u] = in[u*V+v];
end
end
endgenerate
endmodule
module mux #(
parameter WIDTH = 2,
parameter BITS = 1,
parameter WIDTH_SZ = $clog2(WIDTH+1)
) (
input [BITS*WIDTH-1:0] in,
input [WIDTH_SZ-1:0] sel,
output [BITS-1:0] out
);
wire [WIDTH*BITS-1:0] shuffled;
mux_shuffle #(WIDTH, BITS) u_mux_shuffle(in, shuffled);
mux_one #(WIDTH) u_mux_one [BITS-1:0] (shuffled, sel, out);
endmodule