Theo
Theo

Reputation: 41

Convert unsigned int to Time in System-verilog

I have in large part of my System-Verilog code used parameters to define different waiting times such as:

int unsigned    HALF_SPI_CLOCK = ((SYSTEM_CLK_PERIOD/2)*DIVISION_FACTOR); //DEFINES THE TIME

Now since I define a timescale in my files I can directly use these params to introduce waiting cycles:

`timescale 1ns/1ns
initial begin
    #HALF_SPI_CLOCK; 
end

Now I want to have time-specified delays everywhere. Means that the simulation will still respect all the timings even if I change the timescale. I would like to keep the parameters but wherever I have a wait statement, I need to specify the time. Something like

#(HALF_SPI_CLOCK) ns; 

But this is not accepted by Modelsim. Is there a way to cast a parameter or an Unsigned int to a variable of type time in System-Verilog? Is there a way to specify the time unit? I have looked around but could not find any workaround. The reason why I want to have control over the time and make it independent from the timescale is because I intend to change thetimescale later to try to make my simulation faster. Other recommendations or thoughts are very welcome*

Upvotes: 1

Views: 10351

Answers (2)

Victor L
Victor L

Reputation: 10230

It is possible to pass time as a parameter in SystemVerilog, like:

module my_module #(time MY_TIME = 100ns);

  initial begin
    #MY_TIME;
    $display("[%t] End waiting", $time);
  end

endmodule

Or use multiplication to get the right time units, like:

module my_module2 #(longint MY_TIME = 100);

  initial begin
    # (MY_TIME * 1us);
    $display("[%t] End waiting 2", $time);
  end

endmodule

See runnable example on EDA Playground: http://www.edaplayground.com/x/m2

Upvotes: 5

dwikle
dwikle

Reputation: 6978

This will simulate and do what you want. While not the most elegant, it works.

task wait_ns(int num);
  repeat (num) #1ns;
endtask 

...

wait_ns(HALF_SPI_CLOCK);

This could have a negative impact simulation speed depending on how the timescale, clock events, and the unit of delay relate to each other.

Upvotes: 2

Related Questions