Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

Assignments for vectors inequal in length

let's t and d - unsigned vectors and they have different length, and perhaps reverse range

does assignment t := to_unsigned(to_integer(d), t'length) right? what is regular way?

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    let's t and d - unsigned vectors and they have different length, and perhaps reverse range

    does assignment t := to_unsigned(to_integer(d), t'length) right? what is regular way?

    --- Quote End ---

    Not sure about your syntax or reverse range but generally your concept of conversion is right in theory but problematic in practice.

    You need to make sure that integer will fit... so best is use resize (sign extension) and for unsigned just add zeros to extra MSBs.

    If going down in bits you need to control what to do with remaining bits; do you chop off LSBs or MSBs and what does it imply for your design.

    so a general universal assignment is not realistic.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    integers are limited to 32 bits signed (31 bits unsigned), so this way is not universal.

    You also would never have a reverse range unsigned value. The result of any arithmatic will always be (N downto 0). So it doesnt really matter how you declare it - the left bit is always the MSB.

    it would be much easier just to use the resize function:

    t := resize(d, t'length);
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    good. I pointed it - have proper declarions.

    what will be in case of d'length > t'length ? what will be on truncation of d : a) d(d'left downto d'left-t'length+1) or b) d(t'length+d'right-1 downto d'right) ?

    i will try avoid using assignment with result truncation until it is really needed.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Basically, you drop the MSBs

    
    -- Id: R.1
      function RESIZE (ARG: SIGNED; NEW_SIZE: NATURAL) return SIGNED;
      -- Result subtype: SIGNED(NEW_SIZE-1 downto 0)
      -- Result: Resizes the SIGNED vector ARG to the specified size.
      --         To create a larger vector, the new  bit positions
      --         are filled with the sign bit (ARG'LEFT). When truncating,
      --         the sign bit is retained along with the rightmost part.
      -- Id: R.2
      function RESIZE (ARG: UNSIGNED; NEW_SIZE: NATURAL) return UNSIGNED;
      -- Result subtype: UNSIGNED(NEW_SIZE-1 downto 0)
      -- Result: Resizes the UNSIGNED vector ARG to the specified size.
      --         To create a larger vector, the new  bit positions
      --         are filled with '0'. When truncating, the leftmost bits
      --         are dropped.