twos complement data

Discussion in 'VHDL' started by redstripe, Apr 21, 2006.

  1. redstripe

    redstripe Guest

    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.
     
    redstripe, Apr 21, 2006
    #1
    1. Advertising

  2. redstripe

    Dave Pollum Guest

    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
     
    Dave Pollum, Apr 21, 2006
    #2
    1. Advertising

  3. 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
     
    Ralf Hildebrandt, Apr 22, 2006
    #3
  4. redstripe

    Andy Guest

    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
     
    Andy, Apr 24, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,612
    Glen Herrmannsfeldt
    Oct 8, 2003
  2. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,197
    Bo Persson
    Aug 2, 2006
  3. sarathy
    Replies:
    22
    Views:
    2,355
    Bo Persson
    Aug 2, 2006
  4. Spiros Bousbouras
    Replies:
    6
    Views:
    424
    Ben Bacarisse
    Mar 18, 2011
  5. Tim Rentsch
    Replies:
    0
    Views:
    470
    Tim Rentsch
    Mar 19, 2011
Loading...

Share This Page