+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Newbie level 1
    Points: 1,196, Level: 7

    Join Date
    Apr 2006
    Posts
    1
    Helped
    0 / 0
    Points
    1,196
    Level
    7

    verilog system time

    Hi all,

    i want to generate a random number in verilog with the system time as the seed.
    the normal $random() generates a random value by taking the $time (simulation time) as its seed.
    so, how do i get the "system time" in verilog (or) how do i generate random value with system time as its seed.

    Thanks,
    jala

    •   Alt16th September 2006, 08:17

      advertising

        
       

  2. #2
    Newbie level 2
    Points: 741, Level: 5

    Join Date
    Nov 2008
    Posts
    2
    Helped
    2 / 2
    Points
    741
    Level
    5

    Re: how to get system time in verilog

    I could not find an elegant way, but here's how I did it - a system call to write the current time into a file, then read the file to get the info into verilog. The system call to "date" only works on *NIX - if you are on windows, you'll have to change that. Anyway, here's the code:

    Code:
    integer FP;
    integer fgetsResult;
    integer sscanfResult;
    integer NowInSeconds;
    reg [8*10:1] str;
    
    // call "date" and put out time in seconds since Jan 1, 1970 (when time began, no doubt)
    // and put the results in a file called "now_in_seconds"
    $system("date +%s > now_in_seconds");                                                   
    
    // open the file for reading
    FP = $fopen("now_in_seconds","r");
    
    // get a string from the open file - "fgetsResult" should be a 1 - you can test 
    // that for completeness if you'd like
    fgetsResult = $fgets(str,FP);
    
    // convert the string to an integer - "sscanfResult" should also be a 1, and
    // you can test that, too, 
    sscanfResult = $sscanf(str,"%d",NowInSeconds);
    
    // close the file...
    $fclose(FP);  // closes the file
    
    // use the number as a seed...
    process::self.srandom(NowInSeconds);

    I have run this in "ncverilog" and confirmed it on both Solaris and Linux systems.


    2 members found this post helpful.

+ Post New Thread
Please login