5
5
6
6
// INFO --------------------------------------------------------------------------------
7
7
// 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
11
11
12
12
13
13
/* --- INSTANTIATION TEMPLATE BEGIN ---
14
14
15
15
pulse_stretch #(
16
- .LENGTH( 8 )
16
+ .WIDTH( 8 )
17
+ .USE_COUNTER(0)
17
18
) ps1 (
18
19
.clk( clk ),
19
20
.nrst( nrst ),
@@ -24,7 +25,9 @@ pulse_stretch #(
24
25
--- INSTANTIATION TEMPLATE END ---*/
25
26
26
27
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
28
31
)(
29
32
input clk,
30
33
input nrst,
@@ -34,27 +37,54 @@ module pulse_stretch #( parameter
34
37
);
35
38
36
39
40
+ localparam CNTR_WIDTH = $clog2 (WIDTH ) + 1 ;
41
+
37
42
generate
38
43
39
- if ( LENGTH == 0 ) begin
44
+ if ( WIDTH == 0 ) begin
40
45
assign out = 0 ;
41
46
42
- end else if ( LENGTH == 1 ) begin
47
+ end else if ( WIDTH == 1 ) begin
43
48
assign out = in;
44
49
45
50
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
58
88
endgenerate
59
89
60
90
0 commit comments