首页 文章

如何在VHDL中编写具有输入相关范围的std_logic_vector赋值?

提问于
浏览
1

我试图将std_logic_vector的某些部分复制到另一个位置(索引),具体取决于输入 . 这可以在Vivado中合成,但我想使用另一个工具(SymbiYosys,https://github.com/YosysHQ/SymbiYosys)进行形式验证 . SymbiYosys可以使用Verific作为前端来处理VHDL,但Verific不接受这一点 . 这是一小段代码,可以重现问题 . Verific抱怨"left range bound is not constant" . 那么,是否有一种解决方法可以让Verific接受这样的可变范围分配?

我已经找到了这篇文章VHDL: slice a various part of an array,它建议使用一个循环并为每位分配值,但我宁愿不改变我的代码,因为它适用于Vivado . 此外,我认为这样的循环会损害代码的可读性,也许会影响实现效率 . 因此,我正在寻找一种不同的方法(可能是将此错误转换为警告或不太严格的代码修改的方法) .

library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;

entity test is

port(
    clk         : in std_logic;
    prefix      : in std_logic_vector(  8*8 -1 downto 0);
    msgIn       : in std_logic_vector(128*8 -1 downto 0);
    msgLength   : in integer range 1 to 128;

    test_out    : out std_logic_vector((128+8)*8 -1 downto 0)
);

end test;

architecture behav of test is
begin

process (clk)
begin
    if rising_edge(clk) then

        test_out <= (others => '0');
        test_out((msgLength+8)*8 -1 downto msgLength*8) <= prefix;
        test_out( msgLength   *8 -1 downto           0) <= msgIn(msgLength*8 -1 downto 0);

    end if;
end process;

end behav;

1 回答

  • 0

    应该进行一些转换(如果你的工具支持 srlsll 运算符) . 首先左对齐你的消息(左移),用你的前缀左边填充它,最后右移它:

    process (clk)
        variable tmp1: std_logic_vector(128*8 -1 downto 0);
        variable tmp2: std_logic_vector((128+8)*8 -1 downto 0);
    begin
        if rising_edge(clk) then
            tmp1 := msgIn sll (8 * (128 - msgLength));    -- left-align
            tmp2 := prefix & tmp1;                        -- left-pad
            test_out <= tmp2 srl (8 * (128 - msgLength)); -- right-shift
        end if;
    end process;
    

    备注:

    • 如果您的工具不支持 std_logic_vector 上的 srlsll 运算符,请尝试使用 bit_vector . srlsll 已于1993年在标准中引入 . 示例:
    process (clk)
        variable tmp1: bit_vector(128*8 -1 downto 0);
        variable tmp2: bit_vector((128+8)*8 -1 downto 0);
    begin
        if rising_edge(clk) then
            tmp1 := to_bitvector(msgIn) sll (8 * (128 - msgLength));
            tmp2 := to_bitvector(prefix) & tmp1;
            test_out <= to_stdlogicvector(tmp2 srl (8 * (128 - msgLength)));
        end if;
    end process;
    
    • 合成结果可能是巨大而缓慢的,因为这个1088位桶形移位器具有128种可能的不同移位是一种怪物 .

    • 如果你有时间(我的意思是几个时钟周期),那么可能会有更小更高效的解决方案 .

相关问题