To count the number of pulses in a certain time period of 1sec in System Verilog

Hello,

I want to count the number of pulses in a certain period of time, for eg. 1 Sec. If it s 1 pulse , I want to do certain actions and if it is 2 pulses then do another action. If is more than 2 do nothing.
Can you help with System Verilog code for this ?

Thankyou

In reply to nijo:

reg [31:0] pulse_counter;
reg [31:0] pulse_time;

always @(posedge clk) begin
    if (rst_n == 0) begin
        pulse_counter <= 0;
        pulse_time <= 0;
    end else begin
        if (pulse_time < 1000000000) begin // 1 sec
            pulse_time <= pulse_time + 1;
            if (pulse == 1) pulse_counter <= pulse_counter + 1;
        end else begin
            pulse_time <= 0;
            if (pulse_counter == 1) begin
                // do certain actions if it's 1 pulse
            end else if (pulse_counter == 2) begin
                // do another action if it's 2 pulses
            end else if (pulse_counter > 2) begin
                // do nothing if it's more than 2 pulses
            end
            pulse_counter <= 0;
        end
end
end

generate helper signals called window1, toggling every 1 second.

and try something like below code:



always@(posedge pulse) begin
  if(window!=prev_window) pulse_counter=1; //reset counter to 1 in new window
  else pulse_counter++;
  prev_window=window;
end


https://letuslearnsv.com