vhdl

VHDL Coding .. conversion from integer to bit_vector


I'm facing this problem , i'm asked to implement a function in VHDL that takes an integer and returns a bit_vector , assumed that this integer is represented by 4 bits.

i don't want to use already built in function, i have to code the function.

I have made a function to convert from bit_vector to integer which was kinda of easy, but im stuck here :S

Any ideas how can i do it ?


Solution

  • The VHDL standard packages is good inspiration for home brewed functions, and the numeric_bit package defines the to_unsigned function for conversion of natural type to unsigned type, which is the function VHDL actually uses for conversion to bit_vector. The function is implemented as:

    function TO_UNSIGNED (ARG, SIZE: NATURAL) return UNSIGNED is
      variable RESULT: UNSIGNED(SIZE-1 downto 0);
      variable I_VAL: NATURAL := ARG;
    begin
      if (SIZE < 1) then return NAU;
      end if;
      for I in 0 to RESULT'LEFT loop
        if (I_VAL mod 2) = 0 then
          RESULT(I) := '0';
        else 
          RESULT(I) := '1';
        end if;
        I_VAL := I_VAL/2;
      end loop;
      if not(I_VAL =0) then
        assert NO_WARNING
            report "NUMERIC_BIT.TO_UNSIGNED: vector truncated"
            severity WARNING;
      end if;
      return RESULT;
    end TO_UNSIGNED;
    

    The initial if (SIZE < 1) and final if not(I_VAL =0) checks may be removed, if it is known that the function is never used with values that makes the checks relevant.

    This leaves the for I in 0 to RESULT'LEFT loop that creates one result bit per iteration.

    Based on Brian's answer, the constant LUT can be initialized using the TO_UNSIGNED function, to avoid the hand written literals:

    function to_bv(n, size : natural) return bit_vector is
    
      type bv_arr_t is array (0 to 2 ** size - 1) of bit_vector(size - 1 downto 0);
    
      function bv_arr_init(size : natural) return bv_arr_t is
        variable res_v : bv_arr_t;
      begin
        for i in 0 to 2 ** size - 1 loop
          res_v(i) := bit_vector(TO_UNSIGNED(i, size));
        end loop;
        return res_v;
      end function;
    
      constant LUT : bv_arr_t := bv_arr_init(size);
    
    begin
      return LUT(n);
    end to_bv;