使用VHDL语言实现一个4位全加器电路设计,并进行仿真验证。

FPGA开发岗位面试题

在FPGA开发岗位中,使用VHDL语言实现一个4位全加器电路设计并进行仿真验证是一个常见的问题。下面是示例答案:

-- 4位全加器
library ieee;
use ieee.std_logic_1164.all;

entity full_adder_4bit is
    port(
        A, B: in std_logic_vector(3 downto 0);
        Cin: in std_logic;
        Sum: out std_logic_vector(3 downto 0);
        Cout: out std_logic
    );
end entity full_adder_4bit;

architecture behavioral of full_adder_4bit is
    signal c: std_logic_vector(3 downto 0);
begin
    c(0) <= '0' & (A(0) xor B(0) xor Cin);
    c(1) <= (A(1) and B(1)) or (A(1) and Cin) or (B(1) and Cin);
    c(2) <= (A(2) and B(2)) or (A(2) and c(1)) or (B(2) and c(1));
    c(3) <= (A(3) and B(3)) or (A(3) and c(2)) or (B(3) and c(2));
    Sum <= A xor B xor Cin;
    Cout <= c(3);
end architecture behavioral;
-- 4位全加器仿真
library ieee;
use ieee.std_logic_1164.all;

entity test_full_adder_4bit is
end entity test_full_adder_4bit;

architecture behavior of test_full_adder_4bit is
    component full_adder_4bit
    port(
        A, B: in std_logic_vector(3 downto 0);
        Cin: in std_logic;
        Sum: out std_logic_vector(3 downto 0);
        Cout: out std_logic
    );
    end component;

    signal A, B: std_logic_vector(3 downto 0);
    signal Cin: std_logic;
    signal Sum: std_logic_vector(3 downto 0);
    signal Cout: std_logic;
begin
    UUT: full_adder_4bit port map(A, B, Cin, Sum, Cout);

    process
    begin
        A <= "0001";
        B <= "0011";
        Cin <= '0';
        wait for 10 ns;
        assert Sum = "0100" report "Sum错误" severity error;
        assert Cout = '0' report "Cout错误" severity error;
        wait;
    end process;
end architecture behavior;