1
0
mirror of https://github.com/pConst/basic_verilog.git synced 2025-01-14 06:42:54 +08:00
basic_verilog/delayed_event.sv

87 lines
2.3 KiB
Systemverilog
Raw Normal View History

2020-10-30 11:40:27 +03:00
//------------------------------------------------------------------------------
// delayed_event.sv
// Konstantin Pavlov, pavlovconst@gmail.com
//------------------------------------------------------------------------------
// INFO ------------------------------------------------------------------------
// Module generates delayed pulse one clock width
// Could be useful for initialization or sequencing some tasks
// Could be easily daisy-chained by connecting "after_event" outputs
// to the subsequent "ena" inputs
//
// |
// |___,___, ,___,___,___,___,___,___,___,___,___,___,___,
// | , |___| , , , , , , , , , , , nrst
// |
// | <---------- DELAY -------->
// | ___
2020-11-03 11:23:37 +03:00
// |___,___,___,___,___,___,___,___,___,___| |___,___,___, on_event
2020-10-30 11:40:27 +03:00
// |
2020-11-03 11:23:37 +03:00
// | ,___,___,___,___,___,___,___,
// |___,___,___| , , , , , , |___,___,___,___, before_event
2020-10-30 11:40:27 +03:00
// |
2020-11-03 11:23:37 +03:00
// |___,___,___ ___,___,___,___,
// | , , |___,___,___,___,___,___,___| , , , , after_event
2020-10-30 11:40:27 +03:00
// |
//
/* --- INSTANTIATION TEMPLATE BEGIN ---
delayed_event #(
.DELAY( 8 )
) de1 (
.clk( clk ),
.nrst( nrst ),
.ena( ),
.on_event( ), // one clock cycle
.before_event( ),
.after_event( )
);
--- INSTANTIATION TEMPLATE END ---*/
2020-11-03 11:23:37 +03:00
module delayed_event #( parameter
2020-10-30 11:40:27 +03:00
DELAY = 32,
2020-11-03 11:23:37 +03:00
CNTR_WIDTH = $clog2(DELAY)
2020-10-30 11:40:27 +03:00
)(
input clk, // system clock
input nrst, // negative reset
input ena, // enable
output on_event, // one clock cycle
output before_event, // event outputs
output after_event // event outputs
);
2020-11-03 11:23:37 +03:00
logic [CNTR_WIDTH-1:0] seq_cntr = CNTR_WIDTH'(DELAY);
2020-10-30 11:40:27 +03:00
logic seq_cntr_is_0;
assign seq_cntr_is_0 = (seq_cntr[CNTR_WIDTH-1:0]=='0);
always_ff @(posedge clk) begin
if( ~nrst) begin
seq_cntr[CNTR_WIDTH-1:0] <= DELAY;
end else begin
if( ena && ~seq_cntr_is_0 ) begin
seq_cntr[CNTR_WIDTH-1:0] <= seq_cntr[CNTR_WIDTH-1:0] - 1'b1;
end
end // nrst
end
edge_detect cntr_edge (
.clk( clk ),
.nrst( 1'b1 ),
.in( seq_cntr_is_0 ),
.rising( on_event )
);
assign before_event = ~seq_cntr_is_0;
assign after_event = seq_cntr_is_0;
endmodule