Actually, I need to add something to this, as I have been bitten by the following before, and I was just writing some similar code. This is a code issue rather than synthesis/optimisation issue.
Lets say you have the following code:
Code VHDL - [expand] |
1
2
3
4
5
6
7
| signal A,B : unsigned(3 downto 0);
signal C : unsigned(7 downto 0);
signal OP : unsigned(7 downto 0);
....
OP <= A + B + C; |
This is perfectly legal code. And probably looks fine. But will give incorrect results in certain situations.
eg. A=B=8.
Here, OP = C, which would be wrong, because A+B gives a 4 bit result, and hence the carry is the 5th bit is lost, and results in 0. This is because the implied sum is OP <= (A+B) +C;
A slight change would give correct results:
OP <= A + (B+C);
This is because (B+C) is now done with both B and C at 8 bits, because in VHDL addition operations always expand all operands to the longest before doing addition. And it will also expand A to 8 bits.
In my current code, I was doing:
You could also resize one or both of A or B to get correct results:
Code:
OP <= resize(A, OP'length) + B + x"08";
But dont get caught out (like I have in the past) with the following:
Code:
OP <= resize(A+B, 8) + x"08";