1
0
mirror of https://github.com/pConst/basic_verilog.git synced 2025-01-28 07:02:55 +08:00
basic_verilog/pulse_stretch.sv

98 lines
2.4 KiB
Systemverilog
Raw Normal View History

2019-11-14 12:22:18 +03:00
//--------------------------------------------------------------------------------
// pulse_stretch.sv
2023-05-23 11:49:24 +03:00
// published as part of https://github.com/pConst/basic_verilog
2019-11-14 12:22:18 +03:00
// Konstantin Pavlov, pavlovconst@gmail.com
//--------------------------------------------------------------------------------
// INFO --------------------------------------------------------------------------------
// Pulse stretcher/extender module
2023-05-23 11:49:24 +03:00
//
// - this implementftion uses a simple delay line or counter to stretch pulses
// - WIDTH parameter sets output pulse width
// - if you need variable output poulse width, see pulse_gen.sv module
2019-11-14 12:22:18 +03:00
/* --- INSTANTIATION TEMPLATE BEGIN ---
pulse_stretch #(
.WIDTH( 8 ),
.USE_CNTR( 0 )
2019-11-14 12:22:18 +03:00
) ps1 (
.clk( clk ),
.nrst( nrst ),
.in( ),
.out( )
);
--- INSTANTIATION TEMPLATE END ---*/
2023-05-23 11:49:24 +03:00
2019-11-14 12:22:18 +03:00
module pulse_stretch #( parameter
2019-11-15 15:32:26 +03:00
WIDTH = 8,
USE_CNTR = 0 // ==0 - stretcher is implemented on delay line
// ==1 - stretcher is implemented on counter
2019-11-14 12:22:18 +03:00
)(
input clk,
input nrst,
input in,
output out
);
2023-05-23 11:49:24 +03:00
localparam CNTR_W = $clog2(WIDTH+1);
2019-11-15 15:32:26 +03:00
2023-05-23 11:49:24 +03:00
generate
//==========================================================================
if( WIDTH == 0 ) begin
assign out = 0;
2019-11-15 15:32:26 +03:00
2023-05-23 11:49:24 +03:00
//==========================================================================
end else if( WIDTH == 1 ) begin
assign out = in;
2019-11-15 15:32:26 +03:00
2023-05-23 11:49:24 +03:00
//==========================================================================
2019-11-15 15:32:26 +03:00
end else begin
2023-05-23 11:49:24 +03:00
if( USE_CNTR == '0 ) begin
// delay line
logic [WIDTH-1:0] shifter = '0;
always_ff @(posedge clk) begin
if( ~nrst ) begin
shifter[WIDTH-1:0] <= '0;
end else begin
// shifting
shifter[WIDTH-1:0] <= {shifter[WIDTH-2:0],in};
end // nrst
end // always
assign out = (shifter[WIDTH-1:0] != '0);
end else begin
// counter
logic [CNTR_W-1:0] cntr = '0;
always_ff @(posedge clk) begin
if( ~nrst ) begin
cntr[CNTR_W-1:0] <= '0;
end else begin
if( in ) begin
// setting counter
cntr[CNTR_W-1:0] <= CNTR_W'(WIDTH);
end else if( out ) begin
// decrementing counter
cntr[CNTR_W-1:0] <= cntr[CNTR_W-1:0] - 1'b1;
end
end // nrst
end // always
assign out = (cntr[CNTR_W-1:0] != '0);
end
end // if WIDTH
endgenerate
2019-11-14 12:22:18 +03:00
endmodule