/*
$rtoi converts reals to integers w/truncation e.g. 123.45 -> 123
$itor converts integers to reals e.g. 123 -> 123.0
$realtobits converts reals to 64-bit vector
$bitstoreal converts bit pattern to real
Real numbers in these functions conform to IEEE Std 754. Conversion rounds to the nearest valid number.
*/
module real_add(
// Outputs
sum,
// Inputs
a, b
);
input [63:0] a;
input [63:0] b;
output [63:0] sum;
real real_a, real_b;
assign sum = $realtobits(real_a + real_b);
always @(a or b) begin
real_a = $bitstoreal(a);
real_b = $bitstoreal(b);
end
endmodule // real_add
module test();
reg [63:0] a;
reg [63:0] b;
wire [63:0] sum;
real_add real_add(
// Outputs
.sum (sum[63:0]),
// Inputs
.a (a[63:0]),
.b (b[63:0]));
initial begin
$monitor ($time,,"a= %f b=%f sum = %f", $bitstoreal(a), $bitstoreal(b), $bitstoreal(sum));
a = $realtobits(123.456);
b = $realtobits(321.654);
#10;
a = $realtobits(111.111);
b = $realtobits(222.333);
#10;
a = $realtobits(000.111);
b = $realtobits(111.222);
#10 $finish;
end
endmodule // test