Skip to content

Commit 7c9acbf

Browse files
committed
Added pulse_stretch.sv and testbench
1 parent 99eaebc commit 7c9acbf

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

pulse_stretch.sv

+49-19
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
// INFO --------------------------------------------------------------------------------
77
// Pulse stretcher/extender module
8-
// this implementftion uses a simple delay line
9-
// suits when LENGTH of desired output pulse is low
10-
// when you need wide output pulses - counter implementation will make sense
8+
// this implementftion uses a simple delay line or counter to stretch pulses
9+
// WIDTH parameter sets output pulse width
10+
// if you need variable output poulse width, see pulse_gen.sv module
1111

1212

1313
/* --- INSTANTIATION TEMPLATE BEGIN ---
1414
1515
pulse_stretch #(
16-
.LENGTH( 8 )
16+
.WIDTH( 8 )
17+
.USE_COUNTER(0)
1718
) ps1 (
1819
.clk( clk ),
1920
.nrst( nrst ),
@@ -24,7 +25,9 @@ pulse_stretch #(
2425
--- INSTANTIATION TEMPLATE END ---*/
2526

2627
module pulse_stretch #( parameter
27-
LENGTH = 8
28+
WIDTH = 8,
29+
USE_CNTR = 0 // ==0 - stretcher is implemented on delay line
30+
// ==1 - stretcher is implemented on counter
2831
)(
2932
input clk,
3033
input nrst,
@@ -34,27 +37,54 @@ module pulse_stretch #( parameter
3437
);
3538

3639

40+
localparam CNTR_WIDTH = $clog2(WIDTH) + 1;
41+
3742
generate
3843

39-
if ( LENGTH == 0 ) begin
44+
if ( WIDTH == 0 ) begin
4045
assign out = 0;
4146

42-
end else if( LENGTH == 1 ) begin
47+
end else if( WIDTH == 1 ) begin
4348
assign out = in;
4449

4550
end else begin
46-
logic [LENGTH-1:0] shifter = '0;
47-
always_ff @(posedge clk) begin
48-
if( ~nrst ) begin
49-
shifter[LENGTH-1:0] <= '0;
50-
end else begin
51-
shifter[LENGTH-1:0] <= {shifter[LENGTH-2:0],in};
52-
end // nrst
53-
end // always
54-
55-
assign out = |shifter[LENGTH-1:0];
56-
57-
end // if LENGTH
51+
if( USE_CNTR == '0 ) begin
52+
// delay line
53+
54+
logic [WIDTH-1:0] shifter = '0;
55+
always_ff @(posedge clk) begin
56+
if( ~nrst ) begin
57+
shifter[WIDTH-1:0] <= '0;
58+
end else begin
59+
// shifting
60+
shifter[WIDTH-1:0] <= {shifter[WIDTH-2:0],in};
61+
end // nrst
62+
end // always
63+
64+
assign out = (shifter[WIDTH-1:0] != '0);
65+
66+
end else begin
67+
// counter
68+
69+
logic [CNTR_WIDTH-1:0] cntr = '0;
70+
always_ff @(posedge clk) begin
71+
if( ~nrst ) begin
72+
cntr[CNTR_WIDTH-1:0] <= '0;
73+
end else begin
74+
if( in ) begin
75+
// setting counter
76+
cntr[CNTR_WIDTH-1:0] <= WIDTH;
77+
end else if( out ) begin
78+
// decrementing counter
79+
cntr[CNTR_WIDTH-1:0] <= cntr[CNTR_WIDTH-1:0] - 1'b1;
80+
end
81+
end // nrst
82+
end // always
83+
84+
assign out = (cntr[CNTR_WIDTH-1:0] != '0);
85+
86+
end
87+
end // if WIDTH
5888
endgenerate
5989

6090

pulse_stretch_tb.sv

+11-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,23 @@ end
7777
// Modules under test ==========================================================
7878

7979
pulse_stretch #(
80-
.LENGTH( 8 )
80+
.WIDTH( 8 ),
81+
.USE_CNTR( 0 )
8182
) ps1 (
8283
.clk( clk200 ),
8384
.nrst( nrst ),
8485
.in( start ),
8586
.out( )
8687
);
8788

89+
pulse_stretch #(
90+
.WIDTH( 8 ),
91+
.USE_CNTR( 1 )
92+
) ps2 (
93+
.clk( clk200 ),
94+
.nrst( nrst ),
95+
.in( start ),
96+
.out( )
97+
);
8898

8999
endmodule

0 commit comments

Comments
 (0)