lundi 18 juillet 2016

Why do I need to run this function twice to get the expected output?

In system-verilog i have a random number generator like this:

task set_rand_value(output bit [7:0] rand_frms, output bit [13:0] rand_bcnt);
   begin

     bit [7:0]  rand_num;
     // Set the random value between 2 and 254
     rand_num=$urandom_range(254,2);
     rand_bcnt=$urandom_range(2047,1);

     // Check to ensure number is even
     else if ((rand_num/2)*2 != rand_num) begin
        rand_num=rand_num+1;
        $display("Generated %d frames. 8'h%h", rand_num, rand_num);
        $display("Packets generated with 0x%4h bytes.", rand_bcnt);
     end

     // Set the random frame number
     else begin
        rand_frms = rand_num;
        $display("Generated %d frames. 8'h%h", rand_num, rand_num);
        $display("Packets generated with 0x%4h bytes.", rand_bcnt);

     end
   end
endtask // set_rand_value

For some reason the first time I run this (and the third and fifth... etc) it returns a value of zero even though a number is properly generated.

I am calling it like this:

  // Generate random values
  set_rand_value(num_frms, size_val); // Run twice to correct initial write error
  $display("Error correction for packets sent: 0x%h", num_frms);
  set_rand_value(num_frms, size_val);
  $display("The number of packets being sent: 0x%h", num_frms);

Which gives me this output:

Generated 214 frames. 8'hd6
Packets generated with 0x05b2 bytes.
Error correction for packets sent: 0x00

Generated 252 frames. 8'hfc
Packets generated with 0x011f bytes.
The number of packets being sent: 0xfc
0x80fc011f // Expected number

I have tried quite a few things to correct it but for some reason the only way i can get it to behave the way i expect it to is to just run it twice every time i need to use it.




Aucun commentaire:

Enregistrer un commentaire