VHDL:是否可以同时移位数组元素?

时间:2015-11-26 19:37:54

标签: arrays vhdl insertion-sort

我想为了学习目的而创建插入排序的实现。 正如您现在所做的那样,其中一个步骤是将数组元素向右移动1.主要的困难是此操作的范围必须是动态的。

所以如果sort_reg是从0到array_length的数组,我需要实现sort_reg(n)< = sort_reg(n-1),sort_reg(n-1)< = sort_reg(n-2)... sort_reg (NI + 1) - ; = sort_reg(NI); (n-m)> = i> = 1,其中m是该范围的起始数组索引,它将从范围(m到n-1)右移1到(m + 1到n)。

问题是,是否有可能一步到位,然后如何实现?

2 个答案:

答案 0 :(得分:2)

是的,可以一步完成。必须将元素存储在寄存器中,然后在同一个上升沿为所有数组元素分配新值。

让我们用两个类型为std_logic的信号a和b做一个简单的例子。然后,此过程将在clock的上升沿交换两个元素:

process(clock)
begin
  if rising_edge(clock) then
    a <= b;
    b <= a;
  end if;
end process;

这是有效的,因为信号在过程完成后获得新值。因此,在b的赋值中,a的旧值(在时钟上升沿之前)被分配。

让我们继续你的例子。 你没有指定一个特定的数组,所以我选择了这个:

type array_type is array(0 to SIZE-1) of std_logic_vector(7 downto 0);
signal sort_reg : array_type;

然后可以使用for循环编写该过程。 编辑:在每个迭代步骤中,if语句可用于检查元素是否应实际移位。信号nm应为unsigned(首选)类型,或integer,范围为0到SIZE-1。 编辑2 :示例已更改为轮播,如注释中所示。

-- value to insert in case of rotation
value_to_insert <= sort_reg(to_integer(n)); -- to_integer required if type of 'n' is unsigned

process(clock)
begin
  if rising_edge(clock) then
    -- This loop is unrolled by the synthesis tool.
    for i in SIZE-1 downto 1 loop
      -- shift elements [n-1:m] to [n:m+1]
      if (i <= n) and (i >= m+1) then
        sort_reg(i) <= sort_reg(i-1);
      end if;

      -- insert new value
      if i = m then
        sort_reg(i) <= value_to_insert;
      end if;
    end loop;

    -- insert into position zero
    if m = 0 then
      sort_reg(0) <= value_to_insert;
    end if;
  end if;
end process;

答案 1 :(得分:0)

这个怎么样;

sort_reg <= sort_reg(1 to sort_reg'high) & sort_reg(0);

我假设sort_reg是一个定义为的信号;

signal sort_reg : some_array_type(0 to N);

在这种情况下,sort_reg'high是一个等于N的属性。

在vhdl &中用作连接运算符。它将两个矢量/数组连接在一起形成一个矢量/数组。

以上示例仅移动1项。如果你想换M,你可以使用这样的东西;

sort_reg <= sort_reg(M to sort_reg'high) & sort_reg(0 to M-1);

请注意,如果要移位信号(不将其分配给不同的信号),则应在Martin所述的过程中执行此操作。

相关问题