-
Notifications
You must be signed in to change notification settings - Fork 13
/
huffman_builder.v
244 lines (212 loc) · 6.92 KB
/
huffman_builder.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
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
//--------------------------------------------------------------------------------------------------------
// Module : huffman_builder
// Type : synthesizable, IP's sub module
// Standard: Verilog 2001 (IEEE1364-2001)
//--------------------------------------------------------------------------------------------------------
module huffman_builder #(
parameter NUMCODES = 288,
parameter CODEBITS = 5,
parameter BITLENGTH= 15,
parameter OUTWIDTH = 10
) (
rstn, clk,
istart,
wren, wraddr, wrdata,
run , done,
rdaddr, rddata
);
function integer clogb2;
input integer val;
//function automatic integer clogb2(input integer val);
integer valtmp;
begin
valtmp = val;
for (clogb2=0; valtmp>0; clogb2=clogb2+1)
valtmp = valtmp>>1;
end
endfunction
input rstn;
input clk;
input istart;
input wren;
input [ clogb2(NUMCODES-1)-1:0] wraddr;
input [ CODEBITS -1:0] wrdata;
input run;
output done;
input [clogb2(2*NUMCODES-1)-1:0] rdaddr;
output [ OUTWIDTH-1:0] rddata;
wire rstn;
wire clk;
wire istart;
wire wren;
wire [ clogb2(NUMCODES-1)-1:0] wraddr;
wire [ CODEBITS -1:0] wrdata;
wire run;
wire done;
wire [clogb2(2*NUMCODES-1)-1:0] rdaddr;
reg [ OUTWIDTH-1:0] rddata;
reg [clogb2(NUMCODES)-1:0] blcount [0 : BITLENGTH-1];
reg [ (1<<CODEBITS)-1:0] nextcode [0 : BITLENGTH];
integer i;
initial for(i=0; i< BITLENGTH; i=i+1) blcount[i] = 0;
initial for(i=0; i<=BITLENGTH; i=i+1) nextcode[i] = 0;
reg clear_tree2d = 1'b0;
reg build_tree2d = 1'b0;
reg [clogb2(BITLENGTH)-1:0] idx = 0;
reg [clogb2(2*NUMCODES-1)-1:0] clearidx = 0;
reg [ clogb2(NUMCODES)-1:0] nn=0, nnn, lnn=0;
reg [CODEBITS-1:0] ii=0, lii=0;
reg [CODEBITS-1:0] blenn, blen = 0;
wire [(1<<CODEBITS)-1:0] tree1d = nextcode[blen];
wire islast = (blen==0 || ii==0);
reg [clogb2(2*NUMCODES-1)-1:0] nodefilled = 0;
reg [clogb2(2*NUMCODES-1)-1:0] ntreepos, treepos=0;
wire [clogb2(2*NUMCODES-1)-1:0] ntpos= {ntreepos[clogb2(2*NUMCODES-1)-2:0], tree1d[ii]};
reg [clogb2(2*NUMCODES-1)-1:0] tpos = 0;
reg rdfilled;
reg valid = 1'b0;
wire [OUTWIDTH-1:0] wrtree2d = (lii==0) ? lnn : (nodefilled + NUMCODES);
reg alldone = 1'b0;
assign done = alldone & run;
always @ (posedge clk or negedge rstn)
if(~rstn) begin
valid <= 0;
treepos <= 0;
tpos <= 0;
lii <= 0;
lnn <= 0;
end else begin
if(istart) begin
valid <= 0;
treepos <= 0;
tpos <= 0;
lii <= 0;
lnn <= 0;
end else begin
valid <= build_tree2d & nn<NUMCODES & blen>0;
treepos <= ntreepos;
tpos <= ntpos;
lii <= ii;
lnn <= nn;
end
end
always @ (posedge clk or negedge rstn)
if(~rstn)
blen <= 0;
else begin
if(istart)
blen <= 0;
else if(islast)
blen <= blenn;
end
always @ (posedge clk or negedge rstn)
if(~rstn) begin
for(i=0; i<BITLENGTH; i=i+1)
blcount[i] <= 0;
end else begin
if(istart | done) begin
for(i=0; i<BITLENGTH; i=i+1)
blcount[i] <= 0;
end else begin
if(wren && wrdata<BITLENGTH)
blcount[wrdata] <= blcount[wrdata] + 1;
end
end
always @ (*)
if(build_tree2d)
nnn = (nn<NUMCODES && islast) ? (nn + 1) : nn;
else if (idx<BITLENGTH)
nnn = 64'hFFFF_FFFF_FFFF_FFFF;
else
nnn = 0;
always @ (posedge clk or negedge rstn)
if(~rstn)
nn <= 0;
else begin
if (istart)
nn <= 0;
else
nn <= nnn;
end
always @ (posedge clk or negedge rstn)
if(~rstn) begin
for(i=0; i<=BITLENGTH; i=i+1) nextcode[i] <= 0;
alldone <= 1'b0;
ii <= 0;
idx <= 0;
build_tree2d <= 1'b0;
clearidx <= 0;
clear_tree2d <= 1'b0;
end else begin
nextcode[0] <= 0;
alldone <= 1'b0;
if(istart | ~run) begin
if(istart) for(i=0; i<=BITLENGTH; i=i+1) nextcode[i] <= 0;
ii <= 0;
idx <= 0;
build_tree2d <= 1'b0;
clearidx <= 0;
clear_tree2d <= 1'b0;
end else if(run) begin
if(~clear_tree2d) begin
if ( clearidx >= (2*NUMCODES-1) )
clear_tree2d <= 1'b1;
clearidx <= clearidx + 1;
end else if(build_tree2d) begin
if(nn < NUMCODES) begin
if(islast) begin
ii <= blenn - 1;
if(blen>0)
nextcode[blen] <= tree1d + 1;
end else
ii <= ii - 1;
end else
alldone <= 1'b1;
end else begin
if(idx<BITLENGTH) begin
idx <= idx + 1;
nextcode[idx+1] <= ( ( nextcode[idx] + blcount[idx] ) << 1 );
end else begin
ii <= blen - 1;
build_tree2d <= 1'b1;
end
end
end
end
always @ (*)
if(~run)
ntreepos = 0;
else if(valid) begin
if(~rdfilled)
ntreepos = rddata - NUMCODES;
else if (lii==0)
ntreepos = 0;
else
ntreepos = nodefilled;
end else
ntreepos = treepos;
always @ (posedge clk or negedge rstn)
if(~rstn) begin
nodefilled <= 0;
end else begin
if(istart)
nodefilled <= 0;
else if(~run)
nodefilled <= 1;
else if(valid & rdfilled & lii>0)
nodefilled <= nodefilled + 1;
end
reg [CODEBITS-1:0] mem_huffman_bitlens [0 : NUMCODES-1];
always @ (posedge clk)
if(wren)
mem_huffman_bitlens[wraddr] <= wrdata;
wire [clogb2(NUMCODES-1)-1:0] mem_rdaddr = nnn + 1;
always @ (posedge clk)
blenn <= mem_huffman_bitlens[mem_rdaddr];
reg [OUTWIDTH:0] mem_tree2d [0 : 2*NUMCODES-1];
always @ (posedge clk)
if( ~clear_tree2d | (valid & rdfilled) )
mem_tree2d[ (~clear_tree2d ? clearidx : tpos ) ] <= ~clear_tree2d ? {1'b1, {(OUTWIDTH){1'b0}}} : {1'b0, wrtree2d};
always @ (posedge clk)
{rdfilled, rddata} <= mem_tree2d[ (alldone ? rdaddr : ntpos ) ];
endmodule