我试图这样做
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity mux_8to1 is
Port ( Y : in STD_LOGIC_VECTOR (0 to 7);
S : in STD_LOGIC_VECTOR (2 downto 0);
F : out STD_LOGIC);
end mux_8to1;
architecture Behavioral of mux_8to1 is
begin
run: for i in 0 to 7 generate
F <= Y(i) when S = i else
'0';
end generate run;
end Behavioral;
但Xilinx报告错误
ERROR:Xst:528 - Multi-source in Unit <mux_8to1> on signal <F>
索引是否不能在输入或某处使用?
答案 0 :(得分:3)
综合工具将展开生成循环,结果是:
F <= Y(0) when S = 0 else '0';
F <= Y(1) when S = 1 else '0';
...
F <= Y(7) when S = 7 else '0';
因此你可以看到F
有多个驱动程序,这正是
Xilinx综合抱怨。
使用非标准VHDL包std_logic_unsigned
制作多路复用器的方法,
是用{:1>替换run: for ...
F <= Y(conv_integer(S));
只是为了说明如何使用循环,代码是:
process (Y, S) is
begin
F <= 'X'; -- Default to avoid latch if no resulting driver in loop
for i in 0 to 7 loop
if S = i then
F <= Y(i);
end if;
end loop;
end process;
根据Jim Lewis评论的灵感,该代码使用标准VHDL-2002包
numeric_std
with:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
是简短形式:
F <= Y(to_integer(unsigned(S)));
使用循环:
process (Y, S) is
begin
F <= 'X'; -- Default to avoid latch if no resulting driver in loop
for i in 0 to 7 loop
if unsigned(S) = i then
F <= Y(i);
end if;
end loop;
end process;