Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - What's wrong in this VHDL subtraction?

 
Thread Tools Search this Thread
Old 01-18-2006, 08:27 AM   #1
Default What's wrong in this VHDL subtraction?


hi,

I have this code (pseudo code, all inputs and signal should be unsigned
integers):

input first_input, second_input: std_logic_vector (4 downto 0);

Library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;

signal total : std_logic_vector (5 downto 0);
get_total : process (my_inputs)
begin -- process
total <= '0' & (("11110" - first_input) + ("11110" -
second_input));
end process;

that is, total = (30 - first input) + (30 -second_input).

When first_input and second_input are both zero total comes equal to 28
decimal, not 60,
with modelsim. What am i doing wrong? Bad libraries, bad signal types,
what? I have experience
with Verilog, but not vhdl....

Thanks,
Mon



mweb@inwind.it
  Reply With Quote
Old 01-18-2006, 10:28 AM   #2
David R Brooks
 
Posts: n/a
Default Re: What's wrong in this VHDL subtraction?
wrote:
> hi,
>
> I have this code (pseudo code, all inputs and signal should be unsigned
> integers):
>
> input first_input, second_input: std_logic_vector (4 downto 0);
>
> Library IEEE;
> use IEEE.std_logic_1164.all;
> use IEEE.std_logic_arith.all;
> use IEEE.std_logic_unsigned.all;
>
> signal total : std_logic_vector (5 downto 0);
> get_total : process (my_inputs)
> begin -- process
> total <= '0' & (("11110" - first_input) + ("11110" -
> second_input));
> end process;
>
> that is, total = (30 - first input) + (30 -second_input).
>
> When first_input and second_input are both zero total comes equal to 28
> decimal, not 60,
> with modelsim. What am i doing wrong? Bad libraries, bad signal types,
> what? I have experience
> with Verilog, but not vhdl....
>
> Thanks,
> Mon
>

Consider, 28 = 60 - 32
You have lost the top bit. Reason: your intermediate results are still
of type std_logic_vector(4 downto 0), ie 5 bits. So 60 (111100 binary)
gets truncated to fit.

Try this:
input first_input, second_input: std_logic_vector (4 downto 0);

Library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;

signal total : std_logic_vector (5 downto 0);
get_total : process (my_inputs)
variable a, b : std_logic_vector (5 downto 0);
begin -- process
a := '0' & first_input; -- prepend zero to each
b := '0' & second_input;
-- again, prepending zero to the consts.
total <= ("011110" - a) + ("011110" - b);
end process;


David R Brooks
  Reply With Quote
Old 01-18-2006, 12:33 PM   #3
mweb@inwind.it
 
Posts: n/a
Default Re: What's wrong in this VHDL subtraction?
David,
you're right of course. Everything works now.

I had already realized that 28 = 60 -32, with all it implies, by
myself, ten minutes
after posting. I'm embarassed I missed something so elementary. Ah,
well, probably
useful as a proof to my boss (who knows I *can* code HDL circuits)
that he's stressing
me too much

Mon



mweb@inwind.it
  Reply With Quote
Old 01-18-2006, 09:42 PM   #4
Eric Smith
 
Posts: n/a
Default Re: What's wrong in this VHDL subtraction?
writes:
> Library IEEE;
> use IEEE.std_logic_1164.all;
> use IEEE.std_logic_arith.all;
> use IEEE.std_logic_unsigned.all;


Someone else pointed out the problem with the subtraction.

I'd suggest using IEEE.numeric_std rather than std_logic_arith or
std_logic_unsigned, as neither of the latter are actually IEEE standard
packages (the library naming notwithstanding).


Eric Smith
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an external software from VHDL? And how to interface VHDL with JAVA? becool_nikks Software 0 03-06-2009 07:08 PM
Help on auto conversion from Matlab to vhdl on filter design hardheart Hardware 0 12-07-2007 09:19 AM
VHDL RAM help!:) lastval Hardware 0 11-09-2007 01:40 PM
ARRAY(n DOWNTO 0) OF STD_LOGIC_VECTOR(m DOWNTO 0) - VHDL freitass Hardware 0 11-01-2007 03:44 PM
netflix claims that wrong dvd was returned Tonerhead DVD Video 0 06-24-2004 06:11 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46