-
Notifications
You must be signed in to change notification settings - Fork 18
/
median.v
329 lines (290 loc) · 9.61 KB
/
median.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
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
/* --------------------------------------------------------------------------------
This file is part of FPGA Median Filter.
FPGA Median Filter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FPGA Median Filter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FPGA Median Filter. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------- */
/* +----------------------------------------------------------------------------
Universidade Federal da Bahia
------------------------------------------------------------------------------
PROJECT: FPGA Median Filter
------------------------------------------------------------------------------
FILE NAME : median.v
AUTHOR : João Carlos Bittencourt
AUTHOR'S E-MAIL : [email protected]
-----------------------------------------------------------------------------
RELEASE HISTORY
VERSION DATE AUTHOR DESCRIPTION
1.0 2013-08-13 joao.nunes initial version
-----------------------------------------------------------------------------
KEYWORDS: median, filter, image processing
-----------------------------------------------------------------------------
PURPOSE: Top level entity of the Median Filter algorithm datapath.
----------------------------------------------------------------------------- */
`define DEBUG
module median
#(
parameter MEM_DATA_WIDTH = 32,
parameter LUT_ADDR_WIDTH = 10, // Input LUTs
parameter MEM_ADDR_WIDTH = 10, // Output Memory
parameter PIXEL_DATA_WIDTH = 8,
parameter IMG_WIDTH = 320,
parameter IMG_HEIGHT = 320
)(
input clk, // Clock
input rst,
input rst_n, // Asynchronous reset active low
input [31:0] word0,
input [31:0] word1,
input [31:0] word2,
// Test signals
`ifdef DEBUG
output [PIXEL_DATA_WIDTH-1:0] pixel1,
output [PIXEL_DATA_WIDTH-1:0] pixel2,
output [PIXEL_DATA_WIDTH-1:0] pixel3,
output [PIXEL_DATA_WIDTH-1:0] pixel4,
`else
output [MEM_DATA_WIDTH-1:0] median_word,
`endif
output [LUT_ADDR_WIDTH-1:0] raddr_a,
output [LUT_ADDR_WIDTH-1:0] raddr_b,
output [LUT_ADDR_WIDTH-1:0] raddr_c,
output [MEM_ADDR_WIDTH-1:0] waddr
);
// always @(posedge clk) begin
// $display("pixel1 = %d, pixel2 = %d, pixel3 = %d, pixel4 = %d", pixel1, pixel2, pixel3, pixel4);
// end
wire [PIXEL_DATA_WIDTH-1:0] x2_y1;
wire [PIXEL_DATA_WIDTH-1:0] x2_y0;
wire [PIXEL_DATA_WIDTH-1:0] x2_ym1;
wire [PIXEL_DATA_WIDTH-1:0] x1_y1;
wire [PIXEL_DATA_WIDTH-1:0] x1_y0;
wire [PIXEL_DATA_WIDTH-1:0] x1_ym1;
wire [PIXEL_DATA_WIDTH-1:0] x0_y1;
wire [PIXEL_DATA_WIDTH-1:0] x0_y0;
wire [PIXEL_DATA_WIDTH-1:0] x0_ym1;
wire [PIXEL_DATA_WIDTH-1:0] xm1_y1;
wire [PIXEL_DATA_WIDTH-1:0] xm1_y0;
wire [PIXEL_DATA_WIDTH-1:0] xm1_ym1;
assign x2_y1 = word0[PIXEL_DATA_WIDTH-1:0];
assign x2_y0 = word1[PIXEL_DATA_WIDTH-1:0];
assign x2_ym1 = word2[PIXEL_DATA_WIDTH-1:0];
assign x1_y1 = word0[(PIXEL_DATA_WIDTH*2)-1:PIXEL_DATA_WIDTH];
assign x1_y0 = word1[(PIXEL_DATA_WIDTH*2)-1:PIXEL_DATA_WIDTH];
assign x1_ym1 = word2[(PIXEL_DATA_WIDTH*2)-1:PIXEL_DATA_WIDTH];
assign x0_y1 = word0[(PIXEL_DATA_WIDTH*3)-1:(PIXEL_DATA_WIDTH*2)];
assign x0_y0 = word1[(PIXEL_DATA_WIDTH*3)-1:(PIXEL_DATA_WIDTH*2)];
assign x0_ym1 = word2[(PIXEL_DATA_WIDTH*3)-1:(PIXEL_DATA_WIDTH*2)];
assign xm1_y1 = word0[(PIXEL_DATA_WIDTH*4)-1:(PIXEL_DATA_WIDTH*3)];
assign xm1_y0 = word1[(PIXEL_DATA_WIDTH*4)-1:(PIXEL_DATA_WIDTH*3)];
assign xm1_ym1 = word2[(PIXEL_DATA_WIDTH*4)-1:(PIXEL_DATA_WIDTH*3)];
// wire [PIXEL_DATA_WIDTH-1:0] pixel1_sig;
// wire [PIXEL_DATA_WIDTH-1:0] pixel2_sig;
// wire [PIXEL_DATA_WIDTH-1:0] pixel3_sig;
// wire [PIXEL_DATA_WIDTH-1:0] pixel4_sig;
`ifndef DEBUG
assign median_word = {pixel1,pixel2,pixel3,pixel4};
`endif
// Common network output signals
wire [PIXEL_DATA_WIDTH-1:0] c3l;
wire [PIXEL_DATA_WIDTH-1:0] c3h;
wire [PIXEL_DATA_WIDTH-1:0] c3m;
wire [PIXEL_DATA_WIDTH-1:0] c3l_reg;
wire [PIXEL_DATA_WIDTH-1:0] c3h_reg;
wire [PIXEL_DATA_WIDTH-1:0] c3m_reg;
wire [PIXEL_DATA_WIDTH-1:0] c2l;
wire [PIXEL_DATA_WIDTH-1:0] c2h;
wire [PIXEL_DATA_WIDTH-1:0] c2m;
wire [PIXEL_DATA_WIDTH-1:0] c2l_reg;
wire [PIXEL_DATA_WIDTH-1:0] c2h_reg;
wire [PIXEL_DATA_WIDTH-1:0] c2m_reg;
wire [PIXEL_DATA_WIDTH-1:0] c1l;
wire [PIXEL_DATA_WIDTH-1:0] c1h;
wire [PIXEL_DATA_WIDTH-1:0] c1m;
wire [PIXEL_DATA_WIDTH-1:0] c0h;
wire [PIXEL_DATA_WIDTH-1:0] c0m;
wire [PIXEL_DATA_WIDTH-1:0] c0l;
// Delay signals to be placed over the output registers
wire [PIXEL_DATA_WIDTH-1:0] p1_sig;
wire [PIXEL_DATA_WIDTH-1:0] p2_sig;
wire [PIXEL_DATA_WIDTH-1:0] p3_sig;
//------------------------------------------------------------
// Windowing Memory Address Controller
//------------------------------------------------------------
state_machine
#(
.LUT_ADDR_WIDTH(LUT_ADDR_WIDTH),
.IMG_WIDTH(IMG_WIDTH),
.IMG_HEIGHT(IMG_HEIGHT)
) window_contol (
.clk(clk), // Clock
.rst_n(rst_n), // Asynchronous reset active low
.raddr_a(raddr_a),
.raddr_b(raddr_b),
.raddr_c(raddr_c),
.waddr(waddr)
);
//------------------------------------------------------------
// Pixel registers
//------------------------------------------------------------
// always @(posedge clk or negedge rst_n)
// begin : pixel_reg
// if(~rst_n) begin
// pixel1 <= {PIXEL_DATA_WIDTH{1'b0}};
// pixel2 <= {PIXEL_DATA_WIDTH{1'b0}};
// pixel3 <= {PIXEL_DATA_WIDTH{1'b0}};
// pixel4 <= {PIXEL_DATA_WIDTH{1'b0}};
// end else begin
// pixel1 <= pixel1_sig;
// pixel2 <= pixel2_sig;
// pixel3 <= pixel3_sig;
// //pixel4 <= pixel4_sig;
// end
// end
//------------------------------------------------------------
// Input datapath common network
//------------------------------------------------------------
common_network
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) common_network_u0 (
.x2_y1(x2_y1),
.x2_y0(x2_y0),
.x2_ym1(x2_ym1),
.x1_y1(x1_y1),
.x1_y0(x1_y0),
.x1_ym1(x1_ym1),
.x0_y1(x0_y1),
.x0_y0(x0_y0),
.x0_ym1(x0_ym1),
.xm1_y1(xm1_y1),
.xm1_y0(xm1_y0),
.xm1_ym1(xm1_ym1),
.c3l(c3l),
.c3h(c3h),
.c3m(c3m),
.c2l(c2l),
.c2h(c2h),
.c2m(c2m),
.c1l(c1l),
.c1h(c1h),
.c1m(c1m),
.c0h(c0h),
.c0m(c0m),
.c0l(c0l)
);
//------------------------------------------------------------
// Pipeline Registers
//------------------------------------------------------------
dff_3_pipe
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) dff_c3_pipe (
.clk(clk),
.rst_n(rst_n),
.d0(c3h),
.d1(c3m),
.d2(c3l),
.q0(c3h_reg),
.q1(c3m_reg),
.q2(c3l_reg)
);
dff_3_pipe
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) dff_c2_pipe (
.clk(clk),
.rst_n(rst_n),
.d0(c2h),
.d1(c2m),
.d2(c2l),
.q0(c2h_reg),
.q1(c2m_reg),
.q2(c2l_reg)
);
// Output pieline registers (P1, P2, P3)
dff_3_pipe
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) dff_out_pipe (
.clk(clk),
.rst_n(rst_n),
.d0(p1_sig),
.d1(p2_sig),
.d2(p3_sig),
.q0(pixel1),
.q1(pixel2),
.q2(pixel3)
);
//------------------------------------------------------------
// Median Filter Pixel Network
//------------------------------------------------------------
// Pixel 1
pixel_network
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) pixel_network_u0 (
.c3h(c1h),
.c3m(c1m),
.c3l(c1l),
.c2h(c0h),
.c2m(c0m),
.c2l(c0l),
.c1h(c3h_reg),
.c1m(c3m_reg),
.c1l(c3l_reg),
.median(p1_sig)
);
pixel_network
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) pixel_network_u1 (
.c3h(c2h),
.c3m(c2m),
.c3l(c2l),
.c2h(c1h),
.c2m(c1m),
.c2l(c1l),
.c1h(c0h),
.c1m(c0m),
.c1l(c0l),
.median(p2_sig)
);
pixel_network
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) pixel_network_u2 (
.c3h(c3h),
.c3m(c3m),
.c3l(c3l),
.c2h(c2h),
.c2m(c2m),
.c2l(c2l),
.c1h(c1h),
.c1m(c1m),
.c1l(c1l),
.median(p3_sig)
);
pixel_network
#(
.DATA_WIDTH(PIXEL_DATA_WIDTH)
) pixel_network_u3 (
.c3h(c0h),
.c3m(c0m),
.c3l(c0l),
.c2h(c3h_reg),
.c2m(c3m_reg),
.c2l(c3l_reg),
.c1h(c2h_reg),
.c1m(c2m_reg),
.c1l(c2l_reg),
.median(pixel4)
);
endmodule