Electrical – VHDL Compile Error saying “Type of VARIABLE is incompatible with

vhdl

I am pretty new to the world of VHDL programming.

In a simple code, I am trying to make a simple BCD adder.

I assigned/declared variable as following

Port ( dipSW : in  unsigned (7 downto 0); ......


signal n1 : unsigned(3 downto 0); 
signal n2 : unsigned(3 downto 0); 
signal sum : integer range 0 to 31; 

Inside the Archetecutre(not in Process), I tried to compile following code

n1 <= 9 when num1 > 9 else num1; 
n2 <= 9 when num2 > 9 else num2; 
sum <= n1 + n2;

However, I get following error

  1. Type of n1 is incompatible with type of 9.
  2. Type of n2 is incompatible with type of 9
  3. type of sum is incompatible with type of +

Can someone tell me what I am doing wrong?

Best Answer

Welcome to how very strongly typed VHDL is. Unlike Verilog, you can't just use numbers together and have it work. Specifically, the VHDL types UNSIGNED and INTEGER are not compatible with the operations you have specified. Check out this set of lecture slides on VHDL arithmetic and look up the operations provided by NUMERIC_STD.

A fix may be to only use the UNSIGNED type and wrap constants in calls to TO_UNSIGNED(constant, width), ensuring matching types.