今天在做数字逻辑实验的时候,碰到一个需要将四个1bit全加器组合成4bit全加器的需求。经测试发现,实验使用的Quartus II软件并不支持在一个.vhd文件中定义多个实体。
因此需要将1bit全加器实现的代码放在一个文件中,然后再另一个文件中调用其来实现4bit全加器。
故在此记录一下Quartus II中实现上述效果的两种方法。
准备工作
为确保VHDL代码能够正确编译和仿真,首先需要在软件中创建名为"FullAdder4bit"的工程,注意工程名需与VHDL代码中我们希望实现的4bit全加器的entity名保持一致。
方法一
在放置实现1bit全加器的代码文件FullAdder1bit.vhd中,直接定义其entity并设置architecture。
library ieee;
use ieee.std_logic_1164.all;
entity FullAdder1bit is
port(
A, B, Cin: in std_logic;
Sout, Cout: out std_logic
);
end entity;
architecture arch_fullAdder1bit of FullAdder1bit is
begin
Sout <= A xor B xor Cin;
Cout <= (A and B) or (B and Cin) or (A and Cin);
end architecture;
在主文件FullAdder4bit.vhd中,使用entity work.实体名称的语法即可实例化一个1bit全加器的实例。具体代码如下:
library ieee;
use ieee.std_logic_1164.all;
entity FullAdder4bit is
port(
Ain, Bin: in std_logic_vector(3 downto 0);
Cin: in std_logic;
Sout: out std_logic_vector(3 downto 0);
Cout: out std_logic
);
end entity;
architecture arch_fullAdder_test1 of FullAdder4bit is
signal CarryArray: std_logic_vector(2 downto 0);
begin
FA0: entity work.FullAdder1bit port map(
A=>Ain(0),B=>Bin(0),Cin=>Cin,Sout=>Sout(0),Cout=>CarryArray(0)
);
FA1: entity work.FullAdder1bit port map(
A=>Ain(1),B=>Bin(1),Cin=>CarryArray(0),Sout=>Sout(1),Cout=>CarryArray(1)
);
FA2: entity work.FullAdder1bit port map(
A=>Ain(2),B=>Bin(2),Cin=>CarryArray(1),Sout=>Sout(2),Cout=>CarryArray(2)
);
FA3: entity work.FullAdder1bit port map(
A=>Ain(3),B=>Bin(3),Cin=>CarryArray(2),Sout=>Sout(3),Cout=>Cout
);
end architecture;
方法二
在主文件FullAdder4bit.vhd中,亦可以使用component代码块来调用1bit全加器。这种写法要求首先在调用1bit全加器的实体的architecture中首先声明该组件,并确保组件名与我们希望调用的1bit全加器的实体名一致。其余的语法与上面介绍的方法类似。
--前面的代码与之前一致,此处略
architecture arch_fullAdder of FullAdder4bit is
signal CarryArray: std_logic_vector(2 downto 0);
component FullAdder1bit is
port(
A, B, Cin: in std_logic;
Sout, Cout: out std_logic
);
end component;
begin
FA0: FullAdder1bit port map(
A=>Ain(0),B=>Bin(0),Cin=>Cin,Sout=>Sout(0),Cout=>CarryArray(0)
);
FA1: FullAdder1bit port map(
A=>Ain(1),B=>Bin(1),Cin=>CarryArray(0),Sout=>Sout(1),Cout=>CarryArray(1)
);
FA2: FullAdder1bit port map(
A=>Ain(2),B=>Bin(2),Cin=>CarryArray(1),Sout=>Sout(2),Cout=>CarryArray(2)
);
FA3: FullAdder1bit port map(
A=>Ain(3),B=>Bin(3),Cin=>CarryArray(2),Sout=>Sout(3),Cout=>Cout
);
end architecture;