Below is a VHDL code of a even/odd parity generator for a bus of given width.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
entity paritygen is
generic( WIDTH : positive := 8 ); -- Bus width
port ( abus : in std_logic_vector( WIDTH-1 downto 0 ); -- n-bit Bus
even : out std_logic; -- Even parity bit
odd : out std_logic ); -- Odd parity bit
end paritygen;
architecture behavioral of paritygen is
begin
process( abus )
variable sum : unsigned( natural( ceil( log2( real( WIDTH + 1 ) ) ) ) - 1 downto 0 );
variable remdr: unsigned( natural( ceil( log2( real( WIDTH + 1 ) ) ) ) - 1 downto 0 );
begin
sum := (others => '0');
remdr := (others => '0');
for i in abus'range loop
sum := sum + unsigned( abus(i) );
end loop;
remdr := sum MOD 2;
if ( remdr = 0 ) then
even <= '1';
else
even <= '0';
end if;
odd <= not even;
end process;
end behavioral;
When I compile the code in Synopsys Synplify Pro I get an error on the following line:
sum := sum + unsigned( abus(i) );
error: @CD715: Cast of incompatible types
In this line, I am calculating a running sum (of unsigned type) of all bit-lines of the bus. As bus is of type std_logic_vector, I am casting it to unsigned type before using add operator to calculate sum.
The error vanishes if I remove the explicit casting. I am confused as to why this is incompatible casting because as I know std_logic_vector should be casted explicitly to unsigned type using unsigned keyword. Can anyone tell me what is wrong in my vhdl code ?