Search code examples
vhdl

Not a valid resolution function for type bit_vector


I need to write a resolution function for a bit_vector. I understand that bit and bit_vector are unresolved types in VHDL, and thus whenever you have multiple drivers for one of these signals, a resolution function is required.

I've successfully implemented a resolution function for a bit, however am struggling to implement for a bit_vector. This is the error I receive when performing a syntax check: Line 44: resolve_result is not a valid resolution function for type bit_vector.

Here is the resolution function:

function resolve_result(d: bit_vector) return bit_vector is
    variable combined: bit_vector(15 downto 0) := "0000000000000000";   
    begin 
        combined(7 downto 0) := d(7 downto 0);
        combined(15 downto 8) := d(15 downto 8);
        return combined;
end function resolve_result;

subtype resolve_result_bit_vector is resolve_result bit_vector;
signal rResult: resolve_result_bit_vector;

The error, Line 44, is on the subtype declaration. This is within the architecture definition of a testbench.

I understand that the std_ulogic_vector type will automatically resolve; is changing the type of this signal all that is required? I don't need the functionality of std_ulogic_vector hence why I am only using bit_vector.

Thank you.


Solution

  • As you already wrote a resolution function for type bit you probably know what resolution functions look like, what parameter they take and what value they return. And you probably understand that your function cannot be a resolution function for the unconstrained bit_vector type. For instance it always returns a 16-bits bit_vector and can thus not be used for, let's say, an 8-bits bit_vector. It is not the only reason: for such a function the parameter should be an unconstrained array of bit_vector, that is, an array of arrays.

    Anyway, you don't really need all this. If you already have a resolution function foobar for type bit simply declare:

    subtype resolved_bit_vector is (foobar) bit_vector;
    

    If you really want to define a resolution function for 16-bits bit_vector, let's say an or-like one, you can try something like:

    subtype b16 is bit_vector(15 downto 0);
    type b16_array is array(natural range <>) of b16;
    
    function foobar(v: b16_array) return b16 is
      constant n: natural := v'length;
      constant c: b16_array(0 to n - 1) := v;
    begin
      if n = 0 then
        return (15 downto 0 => '0');
      elsif n = 1 then
        return c(0);
      else
        return foobar(c(0 to n - 2)) or c(n - 1);
      end if;
    end function foobar;
    
    subtype resolved_b16 is foobar b16;