display a real in verilog but bitstoreal returning only 0.000000

Moberg picture Moberg · Oct 31, 2013 · Viewed 7.6k times · Source

I am trying to display a real number during the simulation of my verilog code in modelsim. But I only get 0 as output. I am trying to use the bitstoreal system function. I'm not so good at verilog so it could be a stupid beginner's mistake.

Following is my code:

reg [31:0] y[1:0];
integer    file;
localparam [31:0] test = 32'h3fb0d05d;

task read_data_from_fifo();
   begin
     file = $fopen("/tmp/data.fifo", "r");
     $fread(y, file);
     $display("y0 = %d, %f, %h", $bitstoreal(y[0]), $bitstoreal(test), $bitstoreal(y[0]));
     $display("y1 = %f, %f, %h", y[1], $bitstoreal(32'h5dd0_b03f), y[1]);    
   end
endtask

(the task is called from an initial begin block) Outputs:

# y0 =          0, 0.000000, 00000000
# y1 = 3742779199.000000, 0.000000, df16473f

All help appreciated.

Update

Looks like bitstoreal only supports double precision floats (64-bit). Because

localparam [63:0] test = 64'h_3FF61A0BE5109071;
$display("%f", $bitstoreal(test));

results in

1.381359

Answer

user5743032 picture user5743032 · Jan 4, 2016

use $bitstoshortreal: ... $shortrealtobits converts values from a shortreal type to the 32-bit vector representation of the real number. $bitstoshortreal converts a bit pattern created by $shortrealtobits to a value of the shortreal type. ...