Assigning to the float32 type produces the error below in questa 2024.3 on eda playground.
** Error: testbench.vhd(9): Real literal 0 is not of type ieee.float_pkg.float32.
** Error: testbench.vhd(14): Real literal 0.5 is not of type ieee.float_pkg.float32.
** Error: testbench.vhd(15): Real literal 0.25 is not of type ieee.float_pkg.float32.
What are suitable values for assignment to float32 type?
Example code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.FLOAT_PKG.ALL;
entity FloatExample is
end FloatExample;
architecture Behavioral of FloatExample is
signal a, b, c : float32 := 0.0;
begin
process
begin
-- Assign values to float32 variables
a <= 0.5;
b <= 0.25;
-- Perform floating-point addition
c <= a + b;
wait;
end process;
end Behavioral;
The type float32 is defined as a subtype of float, as an array of std_logic:
type float is array (integer range <>) of std_logic; type float is array (integer range <>) of std_logic
subtype float32 is float( 8 downto -23); subtype float32 is float( 8 downto -23)
The bit values of the float32 have specific bit-mapped meaning such as sign bit, exponent and fraction, each defined on defined bits in the number, and thre are rules for interpreting and encoding a number onto the array of std_logic (the float32).
The rules and some examples are given here, vhdl_fixedfloat_lewis_bishop_date_2007.pdf, in the section 'floating point types'.
I modified the post based on the example in the link, and added printing, a nice type conversion, and some delays for the non-blocking operator to update signals.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.FLOAT_PKG.ALL;
entity FloatExample is
end FloatExample;
architecture Behavioral of FloatExample is
signal a, b, c : float32;
begin
process
begin
-- Assign values to float32 variables
a <= "01000000110100000000000000000000"; -- 6.5
b <= to_float(1.5, c);
wait for 1 ns;
-- Perform floating-point addition
c <= a + b;
wait for 1 ns;
report "The value of a is: " & real'image(to_real(a));
report "The value of b is: " & real'image(to_real(b));
report "The value of c is: " & real'image(to_real(c));
wait;
end process;
end Behavioral;
produces
# ** Note: The value of a is: 6.500000e+00
# Time: 2 ns Iteration: 0 Instance: /floatexample
# ** Note: The value of b is: 1.500000e+00
# Time: 2 ns Iteration: 0 Instance: /floatexample
# ** Note: The value of c is: 8.000000e+00
# Time: 2 ns Iteration: 0 Instance: /floatexample