并行如果VHDL中的语句
问题描述:
我正在编写用于将信号同时与多个信号进行比较的代码。并行如果VHDL中的语句
这里是例如:
process (CLK, reset)
if reset = '0' then
data <= (others => '0');
elsif rising_edge (CLK) then
if A = B then
data <= data OR "0001";
else data <= data AND "1110";
end if;
if A = C then
data <= data OR "0010";
else data <= data AND "1101";
end if;
if A = D then
data <= data OR "0100";
else data <= data AND "1011";
end if;
if A = E then
data <= data OR "1000";
else data <= data AND "0111";
end if;
end if;
end process;
只想到A比较B,C,d和E的信号,然后打开相关的位数据和关闭。我上面写的代码不起作用,因为综合工具将优化B,C和D if语句并且只留下E if语句。我也考虑过使用case - when语句,但它没有关闭相关单个位的机制。其他人只能关闭所有4位。什么是有效的方法来做到这一点?谢谢!
顺便说一句,所有这4个if语句是否同时运行?或者他们在不同的周期运行?我想他们会一个接一个跑,否则会造成粉丝进入。
答
您正在尝试使用不需要的语言编写C语言! 在C语言中,你不能访问一个位,只能访问字节和较大的单元,所以C程序员不得不求助于AND/OR,即&,|设置或清除位。
在VHDL你可以解决一个字的各个位,并写
if A = B then
data(0) <= '1';
else
data(0) <= '0';
end if;
简单得多。是的,他们都在同一时间运行,每个时钟周期。
我宁愿数据boolean数组声明,
signal data : array(3 downto 0) of boolean;
然后,我可以写
process (CLK, reset)
begin
if reset = '0' then
data <= (others => false);
elsif rising_edge (CLK) then
data <= (A = E) & (A = D) & (A = C) & (A = B);
end if;
end process;
如果我不得不使用std_logic_vector为data
,这种形式的方便性(几乎)足以让我重载A的类型的“=”运算符,其中一个返回std_logic。然后,为了编写一个小函数的代价,我可以保留这段代码。
编辑: 为了解决原来的方法不起作用的原因,有必要了解信号分配的语义,如解释for example here。
因此,数据的第一个赋值(对于A = B)将在进程挂起后进行存储。然后第二个任务在它发生之前将其替换掉,所以第一个这样的任务永远不会发生。
您需要的原始方法的工作,是变量因为变量赋值立即发生。
process (CLK, reset)
variable data_int : whatever; -- same type as data
begin
if reset = '0' then
data <= (others => '0');
elsif rising_edge (CLK) then
data_int := data;
if A = B then
data_int := data_int OR "0001";
else data_int := data_int AND "1110";
end if;
...
if A = E then
data_int := data_int OR "1000";
else data_int := data_int AND "0111";
end if;
data <= data_int;
end if;
end process;
现在对data
的单个赋值将包含所有单独的修改。然而,它可能会综合到比最佳解决方案大得多的东西。
你的方式有效!非常感谢。为了我的好奇心,是否因为我的方法正在导致扇入,以便优化低3位? – nigong 2013-04-30 23:42:55
不...我会编辑答案。 – 2013-05-01 08:25:25