VHDL保护类型的数组

时间:2014-12-18 19:00:31

标签: vhdl

我正在尝试更好地使用VHDL protected类型,因此我将以下测试汇总在一起(当然,仅用于说明 - 我的实际用例相当复杂):

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;

这个编译。但是,以下行不会:

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

Ashenden说(第3版,第589页)&#34;受保护的类型不能用作......复合类型的元素&#34;。这很不幸。我希望能够用身体创建另一个受保护的类型:

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;

并避免重复prot_type1.set()中的代码(在这种情况下这无疑是微不足道的,但在我的实际用例中会更复杂)。但是,似乎我唯一的选择是(1)基本上重写整个prot_type1,除了我的私有变量的数组类型。或者(2),在内部展平数组,如:

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;

这是有效的,但对于小型阵列来说有点不受欢迎,并且对于大型阵列而言非常。还有另一种方式吗?

1 个答案:

答案 0 :(得分:1)

这是基于Morten Zilmer评论的建议。 prot1_type获取整数访问而不是唯一整数。我使用了函数append,remove和get来管理整数值。

以下是代码:

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;