介绍
对于多路复用器,大家肯定都已经不陌生了。多个输入通过多路复用器后只有一路输出。这里我实现一个四输入的多路复用器。
对于多路复用器,事实上只用逻辑运算操作符就可以实现,我这里给大家提供一种使用when语句实现的方法。
输入输出的对应关系
从上面的图中可以看到,电路有四个输入,1个输出。通过设置sel信号,当sel信号取不同的值时,对应输出a,b,c,d。
设计文件
library ieee;
use ieee.std_logic_1164.all;
entity mux_1 is
port(
a,b,c,d : in std_logic;
sel : in std_logic_vector(1 downto 0);
y : out std_logic);
end mux_1;
architecture mux_1 of mux_1 is
begin
y <= a when sel = "00"else
b when sel = "01"else
c when sel = "10"else
d;
end mux_1;
下面使用另外一种when语句来实现相同的功能。
library ieee;
use ieee.std_logic_1164.all;
entity mux_2 is
port(
a,b,c,d : in std_logic;
sel : in std_logic_vector(1 downto 0);
y : out std_logic);
end mux_2;
architecture mux_2 of mux_2 is
begin
with sel select
y <= a when "00",
b when "01",
c when "10",
d when others;
end mux_2;
测试文件
library ieee;
use ieee.std_logic_1164.all;
entity tb_mux_1 is
end tb_mux_1;
architecture mux_1 of tb_mux_1 is
component mux_1 is
port(
a,b,c,d : in std_logic;
sel : in std_logic_vector(1 downto 0);
y : out std_logic);
end component mux_1;
signal a,b,c,d : std_logic;
signal sel : std_logic_vector(1 downto 0);
signal y : std_logic;
begin
dut : mux_1
port map(
a,b,c,d,sel,y);
process
begin
a <= '0';
b <= '1';
c <= '1';
d <= '0';
sel <= "00";
wait for 20ns;
a <= '1';
c <= '0';
sel <= "01";
wait for 20ns;
b <= '0';
c <= '0';
d <= '1';
sel <= "10";
wait for 20ns;
sel <= "11";
wait for 20ns;
sel <= "01";
wait;
end process;
end mux_1;
仿真结果
结语
有问题的话欢迎指出奥,哈哈哈哈。