【问题标题】:arrays of VHDL protected typesVHDL 保护类型的数组
【发布时间】:2014-12-18 19:00:31
【问题描述】:

我试图更好地利用 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 页)“受保护的类型不能用作......复合类型的元素”。这是不幸的。我希望能够用 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;

并避免在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;

这可行,但对于小型阵列来说有点不受欢迎,对于大型阵列来说非常不受欢迎。还有其他方法吗?

【问题讨论】:

  • 也许在prot_type 中使用access 类型和integer 数组可能是一种可能性。然后可以创建始终对数组进行操作的prot_type,并使用指定数组大小的init 方法。那么默认大小可以为1,默认idx可以为0,所以prot_type的使用默认为标量运算,但在需要时仍支持数组运算。是否适合您的实际工作场景?
  • 好主意,值得一试。不过,我更关心的是重用现有的受保护类型。如果在某些 IP 包中为我提供了标量保护类型,例如...
  • @Morten,这正是我在记分牌包中所做的。作为一个开源 IP 提供商,我觉得这很烦人。编写一个提供删除某些项目的命令的标记记分牌已经足够具有挑战性,而不会增加我必须在内部创建一组记分牌的额外复杂性。在 VHDL 标准组中,我们提出了解决此问题的建议。确保下载优先事项表并投票。如果您对记分牌包感兴趣,请给我发送电子邮件。
  • @Jim - 您对为什么不允许这样做有任何见解吗?
  • 在 2008 年修订期间没有要求。

标签: vhdl


【解决方案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;

【讨论】:

  • 编译没问题,需要做一些测试以确保指针管理做得好。
  • 这是一个不错的解决方案,但我真的希望该类型表现得更像一个数组,完全随机访问,而不是伪链表。
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 2021-10-31
  • 2020-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多