Attention

This documentation is a work in progress. Expect to see errors and unfinished things.

cic_simple_us Source File

 1`timescale 1ns / 1ns
 2
 3// Simple first-order CIC filter and decimator
 4// Note that this module is configured with unsigned input and output!
 5module cic_simple_us #(
 6     parameter ext_roll=0,  // if set, use roll port instead of internal divider
 7     parameter dw=16,
 8     parameter ex=10  // decimate by 2^ex, up to 2^ex when using ext_roll
 9) (
10     input clk,
11     input [dw-1:0] data_in,
12     input data_in_gate,
13     input roll,  // sometimes unused, see ext_roll parameter
14     output [dw-1:0] data_out,
15     output data_out_gate
16);
17
18reg [dw+ex-1:0] data_int=0, data_int_h=0, diff=0;
19reg [ex-1:0] div=0;
20reg iroll=0, roll_r=0;  // only one of these will be used
21always @(posedge clk) if (data_in_gate) begin
22     data_int <= data_int + data_in;  // unsigned arithmetic, zero-padding for data_in
23     {iroll, div} <= div + 1;
24     roll_r <= roll;  // like data_in, roll is sampled on the cycle marked by data_in_gate
25end
26wire uroll = ext_roll ? roll_r : iroll;
27
28reg data_in_gate2=0, data_out_gate_r=0;
29wire deriv_gate = data_in_gate2 & uroll;
30always @(posedge clk) begin
31     data_in_gate2 <= data_in_gate;
32     data_out_gate_r <= deriv_gate;
33     if (deriv_gate) begin
34             diff <= data_int - data_int_h;
35             data_int_h <= data_int;
36     end
37end
38
39assign data_out_gate = data_out_gate_r;
40assign data_out = diff[dw+ex-1:ex];
41
42endmodule