在VHDL中将位模式扩展为通用向量大小

时间:2013-08-22 03:38:08

标签: vhdl

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的任何值。)

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;
相关问题