|
| 1 | +/**************************************************************************** |
| 2 | + * LICENSE: MIT (see ../../../../LICENSE) |
| 3 | + * AUTHOR: LogikBench authors |
| 4 | + **************************************************************************** |
| 5 | + * DESCRIPTION: |
| 6 | + * |
| 7 | + * FIR Filter (Direct Form) |
| 8 | + * |
| 9 | + * x[n] --->(+)----->(+)----->(+)-----> ... ---->(+)-----> y[n] |
| 10 | + * ^ ^ ^ ^ |
| 11 | + * [*h0] [*h1] [*h2] [*hN] |
| 12 | + * ^ ^ ^ ^ |
| 13 | + * [z0] [z1] [z2] [zN] |
| 14 | + * |
| 15 | + * Legend: |
| 16 | + * x[n] : input sample |
| 17 | + * h0..hN : FIR coefficients (block inputs) |
| 18 | + * z0..zN : delayed input samples (shift register) |
| 19 | + * y[n] : output sample |
| 20 | + * |
| 21 | + ***************************************************************************/ |
| 22 | +module firprog |
| 23 | + #(parameter DW = 16, // data width |
| 24 | + parameter ACCW = 16, // accumulator width |
| 25 | + parameter N = 8 // number of taps |
| 26 | + ) |
| 27 | + ( |
| 28 | + input clk, |
| 29 | + input clear, |
| 30 | + input valid, |
| 31 | + input signed [N*DW-1:0] h, |
| 32 | + input signed [DW-1:0] x, |
| 33 | + output reg signed [ACCW-1:0] y |
| 34 | + ); |
| 35 | + |
| 36 | + // Shift register to hold input samples samples |
| 37 | + reg signed [DW-1:0] shift_reg [0:N-1]; |
| 38 | + integer i; |
| 39 | + |
| 40 | + // Multiply-Accumulate result |
| 41 | + reg signed [ACCW-1:0] acc; |
| 42 | + |
| 43 | + always @(posedge clk) begin |
| 44 | + if (clear) begin |
| 45 | + for (i = 0; i < N; i = i + 1) |
| 46 | + shift_reg[i] <= 0; |
| 47 | + y <= 0; |
| 48 | + end |
| 49 | + else if (valid) begin |
| 50 | + // shift register |
| 51 | + for (i = N-1; i > 0; i = i - 1) |
| 52 | + shift_reg[i] <= shift_reg[i-1]; |
| 53 | + shift_reg[0] <= x; |
| 54 | + // dot-product |
| 55 | + acc = 0; |
| 56 | + for (i = 0; i < N; i = i + 1) |
| 57 | + acc = acc + shift_reg[i] * h[i*DW+:DW]; |
| 58 | + y <= acc; |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + |
| 63 | +endmodule |
0 commit comments