|
| 1 | +//-------------------------------------------------------------------------------- |
| 2 | +// dynamic_delay.sv |
| 3 | +// published as part of https://github.com/pConst/basic_verilog |
| 4 | +// Konstantin Pavlov, pavlovconst@gmail.com |
| 5 | +//-------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +// INFO -------------------------------------------------------------------------------- |
| 8 | +// Dynamic delay for arbitrary signal. |
| 9 | +// |
| 10 | +// Incoming data elements have WIDTH bits each. Module does serialization of |
| 11 | +// input data and outputs flattened bits, based on provided selector value. |
| 12 | +// You can perform delays bit-wize, not just element-wize. |
| 13 | +// |
| 14 | +// CAUTION: Be careful selecting last, most-delayed "WIDTH" number of bits. |
| 15 | +// The module intentionally does NOT implement "out of range" |
| 16 | +// checks. Please handle them externally. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +/* --- INSTANTIATION TEMPLATE BEGIN --- |
| 21 | +
|
| 22 | +dynamic_delay #( |
| 23 | + .LENGTH( 3 ), |
| 24 | + .WIDTH( 4 ) |
| 25 | +) M ( |
| 26 | + .clk( clk ), |
| 27 | + .nrst( nrst ), |
| 28 | + .ena( 1'b1 ), |
| 29 | + .in( in_data[3:0] ), |
| 30 | + .sel( sel[3:0] ), |
| 31 | + .out( out_data[3:0] ) |
| 32 | +); |
| 33 | +
|
| 34 | +--- INSTANTIATION TEMPLATE END ---*/ |
| 35 | + |
| 36 | + |
| 37 | +module dynamic_delay #( parameter |
| 38 | + LENGTH = 63, // maximum delay chain length |
| 39 | + WIDTH = 4, // data width |
| 40 | + |
| 41 | + SEL_W = $clog2( (LENGTH+1)*WIDTH ) // output selector width |
| 42 | + // plus one is for zero delay element |
| 43 | +)( |
| 44 | + input clk, |
| 45 | + input nrst, |
| 46 | + input ena, |
| 47 | + input [WIDTH-1:0] in, // input data |
| 48 | + // bit in[0] is the "oldest" one |
| 49 | + // bit in[WIDTH] is considered the most recent |
| 50 | + input [SEL_W-1:0] sel, // output selector |
| 51 | + output logic [WIDTH-1:0] out // output data |
| 52 | +); |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +logic [(LENGTH+1)-1:0][WIDTH-1:0] data = '0; |
| 57 | + |
| 58 | +// packed vector includes extra bits |
| 59 | +logic [(LENGTH+1)*WIDTH-1:0] pack_data; |
| 60 | +assign pack_data[(LENGTH+1)*WIDTH-1:0] = data; |
| 61 | + |
| 62 | +integer i; |
| 63 | +always_ff @(posedge clk) begin |
| 64 | + if( ~nrst ) begin |
| 65 | + // reset all data except zero element |
| 66 | + for( i=1; i<(LENGTH+1); i=i+1 ) begin |
| 67 | + data[i][WIDTH-1:0] <= '0; |
| 68 | + end |
| 69 | + end else if (ena) begin |
| 70 | + for( i=1; i<(LENGTH+1); i=i+1 ) begin |
| 71 | + data[i][WIDTH-1:0] <= data[i-1][WIDTH-1:0]; |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +integer j; |
| 77 | +always_comb begin |
| 78 | + // zero element assignment |
| 79 | + data[0][WIDTH-1:0] <= in[WIDTH-1:0]; |
| 80 | + |
| 81 | + // output selector, sel==0 gives non-delayed output |
| 82 | + for( j=0; j<WIDTH; j=j+1 ) begin |
| 83 | + out[j] <= pack_data[sel[SEL_W-1:0]+j]; |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +endmodule |
0 commit comments