3
\$\begingroup\$

I have written this code in system verilog to generate fifty 12-bit random numbers and write them to a file.

How can I generate the random numbers in octal instead of decimal?

Also, how can I make the numbers display on different lines? Currently, my numbers are being displayed one after the other.

module stimulus_gen();
  class stim_gen;
    rand bit [11:0] addr;
  endclass

  integer file;
  stim_gen obj;

  initial begin
    file = $fopen("input.txt", "w");
    while (!$feof(file))
    begin
     for (int i=0; i<50; i++)
     begin
       obj = new();
       assert(obj.randomize())
       else $fatal(0, "stim_gen::randomize failed");
       //transmit(obj);
       $fwrite(file, obj);
     end
   end
   $fclose(file);
 end
endmodule
\$\endgroup\$
2
  • \$\begingroup\$ You are writing a file, so while (!$feof(file)) will be an infinite loop. Also $fwrite(file, obj); will write the memory address of the object, not the objects addr. \$\endgroup\$ Commented Jun 11, 2014 at 18:09
  • \$\begingroup\$ Please use a regular if statement instead of using an assert. This keeps the assertion coverage reports specific to the verification of your design, not for debugging your testbench. Also, some tools will not execute the expression inside an immediate assertion of you globally disable assertions. Sometime people do that for performance when debugging. \$\endgroup\$ Commented Jun 12, 2014 at 1:39

1 Answer 1

4
\$\begingroup\$

$fwrite(file, obj); will print the memory address of the object. To print the object's addr in oct (followed by a newline), use $fwrite(file, "%o\n", obj.addr); or $fdisplay(file, "%o", obj.addr);

There are also the system display tasks $fwriteo and $fdisplayo which have the default formatting as oct.

This is all well document in IEEE Std 1800-2012 § 21 Input/output system tasks and system functions, as well as previous version of SystemVerilog and all version of Verilog (IEEE Std 1364) in there respected sections on system task/function.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.