Arrays of protected VHDL types

I'm trying to make better use of VHDL types protected

, so I put together the following test (for illustration purposes only, of course my actual use case is considerably more complicated):

type prot_type1 is protected
  procedure set (new_data : integer);
  impure function get return integer;
end protected prot_type1;

type prot_type1 is protected body
  variable data : integer := 0;

  procedure set (new_data : integer) is
  begin
    data := new_data;
  end procedure set;

  impure function get return integer is
  begin
    return data;
  end function get;
end protected body prot_type1;

      

This compiles. However, the following line does not contain:

type prot_type1_array is array (natural range <>) of prot_type1;

      

Ashenden says (3rd ed., P. 589) "Protected types cannot be used as members ... of composite types." This is unfortunate. I was hoping I could create another protected type with a body:

type prot_type2 is protected body
  variable data : prot_type1_array(0 to 3);

  procedure set (idx : natural; new_data : integer) is
  begin
    data(idx).set(new_data);
  end procedure set;

  ...
end protected body prot_type2;

      

and avoid code duplication in prot_type1.set()

(which is admittedly trivial in this case, but would be much more difficult in my actual use). My only choice seems to be to (1) completely rewrite everything prot_type1

except the array for my private variable. Or (2), flatten the array inside, like this:

type prot_type2 is protected body
  variable data0 : prot_type1;
  variable data1 : prot_type1;

  procedure set (idx : natural; new_data : integer) is
  begin
    case idx is
      when 0 =>
        data0.set(new_data);
      when 1 =>
        data1.set(new_data);
      when others =>
        -- handle exceptions here
    end case;
  end procedure set;

  ...
end protected body prot_type2;

      

This works, but is mildly undesirable for small arrays and highly undesirable for large arrays. Is there another way?

+3


source to share


1 answer


here is a suggestion based on Morten Silmer's comment. prot1_type

accesses an integer instead of a unique integer. I used append, remove and get control of integer values.

Here is the code:



type array_int is array (natural range <>) of integer;
type a_integer is access array_int;

type prot_type1 is protected
  -- add a new value at the end of the vector
  procedure append (new_data : integer); 
  -- remove a value from the vector, return 0 ik OK, -1 is the item doesn't exist
  impure function remove (index : integer) return integer;
  -- return the integer value of the item
  impure function get(index : integer) return integer;
end protected prot_type1;

type prot_type1 is protected body

  variable data : a_integer;

  procedure append(new_data : integer) is
    variable temp : a_integer;
  begin
    -- create a temporary vector with the new values
    temp := new array_int'(data.all & new_data);
    -- free memory of the real vector
    Deallocate(data);        
    -- reallocate the real vector with the good values
    data := new array_int'(temp.all);
    -- free memory of the temporary vector
    Deallocate(temp);   
  end procedure append;

  impure function remove(index : integer) return integer is
    variable temp : a_integer;
  begin
    if (index > data'length-1 or index < 0) then  -- not sure if the vector is (0 to length -1) or (1 to length). to be tested !!!
      return -1;
    else
      -- create a temporary vector with the new values
      temp := new array_int'(data(0 to index-1) & data(index+1 to data'length-1));
      -- free memory of the real vector
      Deallocate(data);        
      -- reallocate the real vector with the good values
      data := new array_int'(temp.all);
      -- free memory of the temporary vector
      Deallocate(temp);
      return 0;
    end if;
  end function remove;

  impure function get(index : integer) return integer is
  begin
    return   data(index);
  end function get;

end protected body prot_type1;

      

+1


source







All Articles