1
\$\begingroup\$

Considering variable a and b as STD_LOGIC_VECTOR (31 DOWNTO 0) we have a + b as 33 bit result;

How can we get 32 bits out of this?

Does VHDL have something like (a+b)(31 downto 0) or we should store c:= a+b and then get c(31 downto 0)?

\$\endgroup\$
  • 1
    \$\begingroup\$ Are a and b representing signed or unsigned numbers? This is why I don't use slv for arithmetic... \$\endgroup\$ – Martin Thompson Jan 21 '15 at 16:53
  • \$\begingroup\$ they seem to be unsigned, since i did not use signed library. \$\endgroup\$ – VSB Jan 21 '15 at 17:23
  • \$\begingroup\$ It's not recommended to perform arithmetic calculations on STD_LOGIC types. Please use ieee.numeric_std and cast all your operands to SIGNED or UNSIGNED signals with the appropriate length. VHDL does not increase vector sizes to capture potential overflows. If you need \$C_{out}\$ then expand a and b to 33 bits by concatenation or by using the resize function. \$\endgroup\$ – Paebbels Jan 21 '15 at 18:15
  • \$\begingroup\$ You don't specify which package."+" you use to produce a result with length 33. \$\endgroup\$ – user8352 Jan 21 '15 at 23:21
  • \$\begingroup\$ @VSB - if you don't use a library they are not anything. They are just a bag of bits. VHDL is strongly typed. If you mean something to represnt a number, please use the perfectly good types available for that. Either integer int he basic language or the unsigned and signed vectors from ieee.numeric_std. parallelpoints.com/numbers-in-vhdl \$\endgroup\$ – Martin Thompson Jan 22 '15 at 8:31
2
\$\begingroup\$

If a, b, c are of type std_logic_vector(31 downto 0),

then, c := a + b;
will give the 32 bit answer in c (without carry) as you required.

If you want 33 bit answer in c (where c is std_logic_vector(32 downto 0))

Then c := ('0' & a) + ('0' & b) will give the 33 bit answer.

But you will need ieee.std_logic_unsigned package for adding std_logic_vector using + operator.

\$\endgroup\$
  • \$\begingroup\$ Interesting. Personally I'd like to hear why and how this works :) \$\endgroup\$ – Dzarda Jan 21 '15 at 14:59
  • \$\begingroup\$ I think a misunderstanding happend. A(32)+B(32) --> OUT(33) this is due to carry out of add operation, and I want OUT[31..0]. \$\endgroup\$ – VSB Jan 21 '15 at 15:11
  • \$\begingroup\$ @VSB Sorry about the misunderstanding. :) \$\endgroup\$ – nidhin Jan 21 '15 at 15:54
0
\$\begingroup\$

declare

signal temp : std_logic_vector(32 downto 0);

and in code

temp <= ('0' & a) + ('0' & b);

c <= temp(31 downto 0);

From what I understood from your question this answer will help you. Else let me know.

\$\endgroup\$
  • \$\begingroup\$ I want to know is it possible to this without temp variable, somehow? \$\endgroup\$ – VSB Jan 22 '15 at 17:34
  • 2
    \$\begingroup\$ With any conventional package, temp <= a + b won't compile. Neither the operators in the non-standard std_logic_unsigned package nor those in the standard numeric_std_unsigned produce an output that is automatically 1 bit larger. \$\endgroup\$ – fru1tbat Jan 23 '15 at 13:21
  • \$\begingroup\$ @fru1tbat yes you are right. Made changes. Now it is correct right? \$\endgroup\$ – tollin jose Jan 23 '15 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.