2
constant alternate_bits : std_logic_vector(C_BIT_SIZE-1 downto 0) := X;

如果 C_BIT_SIZE 不是偶数,我应该写什么来代替 X 将其设置为交替的位模式,同时保持其通用性并且不会感到不安?

例如,如果 C_BIT_SIZE = 4 它应该产生“1010”,如果 C_BIT_SIZE = 5 它应该产生“01010”。(它应该适用于任何 C_BIT_SIZE >= 1 的值。)

4

2 回答 2

3

可以使用一个函数:

-- Returns std_logic_vector(BIT_SIZE-1 downto 0) with bits on even indexes
-- as '0' and bits on odd indexes as '1', e.g. 5 bit vector as "01010".
function alternate_fun(BIT_SIZE : natural) return std_logic_vector is
  variable res_v : std_logic_vector(BIT_SIZE - 1 downto 0);
begin
  res_v := (others => '0');
  for i in 1 to BIT_SIZE / 2 loop
    res_v(2 * i - 1) := '1';
  end loop;
  return res_v;
end function;
于 2013-08-22T05:29:31.953 回答
0

我写了一个似乎可以解决问题的函数,但我对其他更整洁的答案感兴趣:

subtype data_vector is std_logic_vector(C_BIT_SIZE-1 downto 0);

function make_alternate_bits return data_vector is
    variable bits : data_vector;
begin
    for i in 0 to C_BIT_SIZE-1 loop
        if (i mod 2) = 0 then
            bits(i) := '0';
        else
            bits(i) := '1';
        end if;
    end loop;
    return bits;
end function;

constant alternate_bits : data_vector := make_alternate_bits;
于 2013-08-22T05:46:11.240 回答