Calculation of simulation time in verilog

I want to calculate the simulation time of calculating a single prime. This means that there is no clock cycle to calculate one prime number. Because we also know that a large prime number gets more clock cycle than a small prime number to calculate. I used $ time in verilog whenever a sample is computed and fixed in the time_s register. And I calculated the difference of the calculations after another prime number. Here is mine, where you can see that time_s1 captured the time of calculating the count. Time_s2 - time to calculate the difference.

module prime_number_count(
  input clk
);

//for count 1
parameter N =100;          // size of array
parameter N_bits = 32;
reg     [N_bits-1:0] prime_number[0:N-1]; // memory array for prime_number 
reg     [N_bits-1:0] prime_aftr50 [0:49]; // memory array to get       
integer     k;               // counter variable   
integer     k1;               // counter variable   
integer     count;  
integer     test;
integer     time_s1;
integer     time_s2;
integer      check; //Counts 1 to k
localparam S_INC   = 2'b01;
localparam S_CHECK = 2'b10;
reg [1:0] state;

initial begin
 prime_number[0] = 'd1;
 prime_number[1] = 'd2;
  //prime_aftr50[0] = 'd0;
  state           = S_CHECK; //Check set count first
  count           = 'd3;
  k               = 'd2; //0,1 preloaded
  check           = 'd1;
  test            = 'd1;
   time_s1            = 'd0;
   time_s2            = 'd0;
     k1               = 'd0;
end

always @(posedge clk ) 
 begin

         $display ("time of clock   %d ", $time );
        if(state == S_INC)
        begin  // if state is 1
          //$display("State: Incrementing Number to check %d", count+1);
          count <= count+1 ;
          state <= S_CHECK ;           // chang the state to 2
          check <= 'd1; // Do not check against [0] value 1
          test  <= 'd1; // Safe default
        end     

        else if (state == S_CHECK) begin
             if (test == 0) begin
             // Failed Prime test (exact divisor found)
              $display("Reject        %3d", count);
              state           <= S_INC ;
             end
             else 
            if (time_s2>30000)begin
             prime_number[k]=prime_number[k-1];
             time_s1         <=$realtime ;
         state           <= S_INC ;     
             k               <=  k + 1;              
             $display("Found %1d th Prime_1 %1d", k, count);
             $display("display of simulation time" , time_s2);
             end // end of simulation time
             else 

                    if (check == k) begin
                      //Passed Prime check
                      time_s1         <=$time ;
                      prime_number[k] <=  count;
                      k               <=  k + 1;
                      state           <= S_INC ; 
                      $display("Found %1d th Prime_1 %1d", k, count);
                      $display("display of simulation time" , time_s2);
                      end

             else begin
             //$display("Check");
             test  <= count % prime_number[check] ;
             check <= check + 1;
             //$display("Checking %1d against %1d prime %1d : %1d", count, check, prime_number[check], count % prime_number[check]); 
             end
      end
end     
 ////////////////////////////////////////////////////////////////// 
always @(posedge clk )
begin 
     if(check==k-1)
     begin
     time_s2 <=$realtime-time_s1;
    // $display("display of simulation time" , time_s2) ;
     end
 end

always @ (posedge clk) begin
  if ( k==51+(50*k1)) begin
    prime_aftr50[k1] <= count;
    k1               <= k1+1;   
  end               
end

endmodule
+4
source share
1 answer

Time background

time integer, - . , time_unit *. realtime, .

%t %d %f . $timeformat.

realtime capture = 0.0;
//To change the way (below) is displayed
initial begin
  #80.1ns;
  capture = $realtime;
  $display("%t", capture);
end

%t:

//$timeformat(unit#, prec#, "unit", minwidth);
$timeformat(-3, 2, " ms", 10);    // -3 and " ms" give useful display msg

unit      is the base that time is to be displayed in, from 0 to -15
precision is the number of decimal points to display.
"unit"    is a string appended to the time, such as " ns".
minwidth  is the minimum number of characters that will be displayed.

unit:  recommended "unit" text
  0 =   1 sec
 -1 = 100 ms
 -2 =  10 ms
 -3 =   1 ms 
 -4 = 100 us
 -5 =  10 us
 -6 =   1 us 
 -7 = 100 ns
 -8 =  10 ns
 -9 =   1 ns 
-10 = 100 ps
-11 =  10 ps
-12 =   1 ps 
-13 = 100 fs
-14 =  10 fs
-15 =   1 fs 

: realtime , $realtime %t .

, :

intial begin:

$timeformat(-9, 2, " ns", 10); 

, , :

          //Passed Prime check
          time_s2         = time_s1; //Last Prime
          time_s1         = $realtime ;
          $display("Found %1d th Prime_1 %1d", k, count);
          $display("Found at time : %t", time_s1);
          $display("Time Diff      : %t", time_s1 - time_s2);

EDA Playground.

*: verilog , time_unit , time integer .

`timescale <time_unit>/ <time_precision>

. 22.7 IEEE 1800-1012.

+7

All Articles