1
1
// --------------------------------------------------------------------------------
2
2
// pulse_stretch.sv
3
+ // published as part of https://github.com/pConst/basic_verilog
3
4
// Konstantin Pavlov, [email protected]
4
5
// --------------------------------------------------------------------------------
5
6
6
7
// INFO --------------------------------------------------------------------------------
7
8
// Pulse stretcher/extender module
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
9
+ //
10
+ // - this implementftion uses a simple delay line or counter to stretch pulses
11
+ // - WIDTH parameter sets output pulse width
12
+ // - if you need variable output poulse width, see pulse_gen.sv module
11
13
12
14
13
15
/* --- INSTANTIATION TEMPLATE BEGIN ---
@@ -24,6 +26,7 @@ pulse_stretch #(
24
26
25
27
--- INSTANTIATION TEMPLATE END ---*/
26
28
29
+
27
30
module pulse_stretch # ( parameter
28
31
WIDTH = 8 ,
29
32
USE_CNTR = 0 // ==0 - stretcher is implemented on delay line
@@ -37,55 +40,57 @@ module pulse_stretch #( parameter
37
40
);
38
41
39
42
40
- localparam CNTR_WIDTH = $clog2 (WIDTH ) + 1 ;
41
-
42
- generate
43
-
44
- if ( WIDTH == 0 ) begin
45
- assign out = 0 ;
46
-
47
- end else if ( WIDTH == 1 ) begin
48
- assign out = in;
49
-
50
- end else begin
51
- if ( USE_CNTR == '0 ) begin
52
- // delay line
43
+ localparam CNTR_W = $clog2 (WIDTH + 1 );
53
44
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
45
+ generate
46
+ // ==========================================================================
47
+ if ( WIDTH == 0 ) begin
48
+ assign out = 0 ;
63
49
64
- assign out = (shifter[WIDTH - 1 : 0 ] != '0 );
50
+ // ==========================================================================
51
+ end else if ( WIDTH == 1 ) begin
52
+ assign out = in;
65
53
54
+ // ==========================================================================
66
55
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 ] <= CNTR_WIDTH ' (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
88
- endgenerate
56
+ if ( USE_CNTR == '0 ) begin
57
+ // delay line
58
+
59
+ logic [WIDTH - 1 : 0 ] shifter = '0 ;
60
+ always_ff @ (posedge clk) begin
61
+ if ( ~ nrst ) begin
62
+ shifter[WIDTH - 1 : 0 ] <= '0 ;
63
+ end else begin
64
+ // shifting
65
+ shifter[WIDTH - 1 : 0 ] <= { shifter[WIDTH - 2 : 0 ],in} ;
66
+ end // nrst
67
+ end // always
68
+
69
+ assign out = (shifter[WIDTH - 1 : 0 ] != '0 );
70
+
71
+ end else begin
72
+ // counter
73
+
74
+ logic [CNTR_W - 1 : 0 ] cntr = '0 ;
75
+ always_ff @ (posedge clk) begin
76
+ if ( ~ nrst ) begin
77
+ cntr[CNTR_W - 1 : 0 ] <= '0 ;
78
+ end else begin
79
+ if ( in ) begin
80
+ // setting counter
81
+ cntr[CNTR_W - 1 : 0 ] <= CNTR_W ' (WIDTH );
82
+ end else if ( out ) begin
83
+ // decrementing counter
84
+ cntr[CNTR_W - 1 : 0 ] <= cntr[CNTR_W - 1 : 0 ] - 1'b1 ;
85
+ end
86
+ end // nrst
87
+ end // always
88
+
89
+ assign out = (cntr[CNTR_W - 1 : 0 ] != '0 );
90
+
91
+ end
92
+ end // if WIDTH
93
+ endgenerate
89
94
90
95
91
96
endmodule
0 commit comments