Velocity Reviews > VHDL > twos complement data

twos complement data

redstripe
Guest
Posts: n/a

 04-21-2006
I am working on a GRAPHICAL design in QuartusII where I have data
represented with the twos complement number system. This is not a
problem as most of the time the data is the same bit width (usually 12)
and I preserve the sign and magnitude.

The problem I am having is in a "simple" VHDL section I am trying to
write. There are times when I want to take the 12 bit twos complement
data and limit it to some smaller value (lets say 6bits). So the input
would be 12 bits and the output would be 6 bits. If the 12 bit number
is within the 6 bits it passes through, otherwise it is limited. I am
very new to VHDL and I assumed this task would be simple.

First, is there any easy way to set a 6bit output equal to a 12bit
input? assuming the 12bit data is within the 6 bits? Every time I try
I run into some sort of error.

The next part would be just checking if the 12bit input is out of the 6
bit range and if it is setting the output to the max/min value.

Any help would be greatly appreciated.

Dave Pollum
Guest
Posts: n/a

 04-21-2006
redstripe wrote:
> I am working on a GRAPHICAL design in QuartusII where I have data
> represented with the twos complement number system. This is not a
> problem as most of the time the data is the same bit width (usually 12)
> and I preserve the sign and magnitude.
>
> The problem I am having is in a "simple" VHDL section I am trying to
> write. There are times when I want to take the 12 bit twos complement
> data and limit it to some smaller value (lets say 6bits). So the input
> would be 12 bits and the output would be 6 bits. If the 12 bit number
> is within the 6 bits it passes through, otherwise it is limited. I am
> very new to VHDL and I assumed this task would be simple.
>
> First, is there any easy way to set a 6bit output equal to a 12bit
> input? assuming the 12bit data is within the 6 bits? Every time I try
> I run into some sort of error.
>
> The next part would be just checking if the 12bit input is out of the 6
> bit range and if it is setting the output to the max/min value.
>
> Any help would be greatly appreciated.

assuming that "input" and "output" are std_logic_vectors:
input std_logic_vector(11 downto 0)
output std_logic_vector(5 downto 0);
then:
output <= input(5 downto 0); -- should work.

HTH
-Dave Pollum

Ralf Hildebrandt
Guest
Posts: n/a

 04-22-2006
Dave Pollum wrote:

> output <= input(5 downto 0); -- should work.

And if you need to take care about overflow, you have to monitor the
discarded bits and the sign bit of the result.

overflow<='0' when (input(11 downto 5)="0000000" OR
input(11 downto 5)="1111111") else '1';

Ralf

Andy
Guest
Posts: n/a

 04-24-2006
Or, for a little more flexible code, assuming output is smaller than
input and both have descending ranges ending at the same bit (not
necessarily 0):

output <= input(output'range);

This way, as long as the above assumptions are true, you can change the
sizes of input and output, and it still works.

Or do like I do, and use integers; they are much quicker to simulate
too.

Andy

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Tim Rentsch C Programming 0 03-19-2011 04:48 PM Spiros Bousbouras C Programming 6 03-18-2011 03:14 PM sarathy C++ 22 08-02-2006 05:53 PM sarathy C Programming 20 08-02-2006 05:53 PM Mantorok Redgormor C Programming 8 10-07-2003 11:52 PM

Advertisments