Problem with simple 4-bit adder vhdl code

Status
Not open for further replies.

darshkamal

Junior Member level 1
Joined
Nov 7, 2011
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,443
Hello,
What's wrong with this code:
Code:
library ieee;
use ieee.numeric_std.all;

entity adder is
  port(a,b: in signed(3 downto 0);
       c: out signed(4 downto 0));
end entity adder;

architecture adder1 of adder is
begin
  c <= ('0'&a) + ('0'&b);
end architecture adder1;

Error given by modelsim compiler is:
** Error: C:/Modeltech_pe_edu_10.2a/examples/adder/test.vhd(11): No feasible entries for infix operator "&".
** Error: C:/Modeltech_pe_edu_10.2a/examples/adder/test.vhd(11): No feasible entries for infix operator "&".
** Error: C:/Modeltech_pe_edu_10.2a/examples/adder/test.vhd(11): Bad expression in left operand of infix expression "+".
** Error: C:/Modeltech_pe_edu_10.2a/examples/adder/test.vhd(11): Bad expression in right operand of infix expression "+".
** Error: C:/Modeltech_pe_edu_10.2a/examples/adder/test.vhd(11): Type error resolving infix expression "+" as type ieee.NUMERIC_STD.SIGNED.
** Error: C:/Modeltech_pe_edu_10.2a/examples/adder/test.vhd(12): VHDL Compiler exiting


Thank you in advance

- - - Updated - - -

Does the concatenation operator not work with signed vectors?
 

You need to import std_logic_1164 to make it work.

The concatenation is effectively changing the input numbers to unsigned, it this what you want? For regular signed addition, you'll use resize() instead. It's sufficient to resize one operand, the other will be resized automatically.
 

First, importing std_logic_1164 has solved the problem.
Second, you are right. I was changing, by mistake, the input numbers to unsigned.
It should be as follows:
Code:
c <= ('a(3)'&a) + ('b(3)'&b);--to make sign extension.
I have a question:
What I'm doing is because VHDL imposes that the LHS to the equal sign of addition is the same size as the RHS' operands.
Will this concatenated bit be removed during synthesis
 

Code:
c <= ('a(3)'&a) + ('b(3)'&b);--to make sign extension.
You can also write
Code:
c <= resize(a,5) + resize(b,5);
or even shorter
Code:
c <= resize(a,5) + b;

What I'm doing is because VHDL imposes that the LHS to the equal sign of addition is the same size as the RHS' operands.
Will this concatenated bit be removed during synthesis
I guess, you don't want the sign extension before addition to be removed. Otherwise could write
Code:
c <= resize(a+b,5);
Which restricts the result to 4 bit, possibly causing an overflow, e.g. 5 + 6 = - 5
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…