Verilog - generate random delay time in testbench

Click For Summary

Discussion Overview

The discussion revolves around generating random delay times and values in a Verilog testbench. Participants explore the implementation of a for-loop to achieve random timing and value injection within specified ranges.

Discussion Character

  • Homework-related, Technical explanation, Exploratory

Main Points Raised

  • One participant shares their attempt to generate random delay times using a for-loop and encounters a syntax error related to using a variable for delay timing.
  • Another participant suggests creating a for-loop that waits for a fixed time of #1ns in each iteration instead of using a variable delay.
  • A participant expresses confusion about the suggestion and clarifies their goal of generating random delays between 0 ns and 300 ns, with specific timing for value injection and recovery.
  • Another participant points out a potential missing semicolon in the original code that could be causing the syntax error.
  • A participant mentions the need for two for-loops to adjust the maximum and minimum values for the random delay, indicating ongoing adjustments to their approach.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to implement the random delay timing, and multiple suggestions are presented without resolution.

Contextual Notes

Participants express uncertainty about the syntax rules in Verilog regarding variable delays and the correct implementation of loops for timing control. There are also indications of potential errors in the original code that remain unresolved.

Who May Find This Useful

This discussion may be useful for individuals working on Verilog testbenches, particularly those interested in generating random delays and values in simulations.

hoheiho
Messages
43
Reaction score
0

Homework Statement


Hi, I would like to generate a random delay time and value in testbench. This is what I did:

Code:
for(i=0;i<300;i=i+30)
  begin
  j = i + {$random} % (300 - i) // MIN + {$random} % (MAX - MIN )
  #j b = {$random} %3;
  #3ns b = 3'b000;
  end

I want to generate random value in random time. int i give me the range of random time. j is the actual random delay timing and b is the random value.
What I want to do here is generate a range -> generate delay time-> use value b in the the generated delay time -> recover value b after 3ns.

But it comes out error with:
**near "#": syntax error, unexpected '#', expecting ';'

Is that mean I can only use a actual value for the delay? Like #30, #20...but not a unknown value #j ? I have google it but cannot find any useful information about my problem..

Thank you very much for the help
Ivan
 
Last edited:
Physics news on Phys.org
Hi hoheiho!

Looks like you have it right.
Suppose you create a for-loop up to j and wait #1 each time?
 
Thank for your reply
Sorry for my poor english, I didnt get what you mean by wait #1ns? I would like to create a for loop and get the random delay timing between 0 ns to 300ns. It will generate a random delay time each 30ns. (i=0;i<300;i=i+30).
For example
0 ns to 30ns:
j = 20ns -> inject value b at time 20ns -> recovery back to b = 3'b000 at time 23ns -> start next for loop cycle ->
30ns to 60ns:
...
..
.
 
Now that I look again, didn't you forget a ';' on the line before your error?


Btw, instead of waiting with #j, you can also wait with:
Code:
for(k=0;k<j;k=k+1)
  begin
  #1ns ;
  end
 
Sorry for the typing error again :(.

When I do the simulation, the code doesn't work what I prefer. I think I need to use two for loop together to change the MAX and MIN value. I am trying to fix it now.

Thanks :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 21 ·
Replies
21
Views
8K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K