デザインエンティティの実体化 / for-generateステートメント

pp.254-255

Nビット(ここでは4ビット)のキャリーリップルアダーを作る。for-generateステートメントで全加算器をN個実体化する。
f:id:ti-nspire:20210406143222p:plain:w500

これがトップファイル:

library ieee;
use ieee.std_logic_1164.all;

entity carry_ripple_adder is
    generic(
        NUM_BITS : natural := 4 -- ビット数(全加算器の個数)
    );
    port(
        a, b : in std_logic_vector(NUM_BITS-1 downto 0);
        cin  : in std_logic;
        
        sum  : out std_logic_vector(NUM_BITS-1 downto 0);
        cout : out std_logic
    );
    
end entity;

architecture rtl of carry_ripple_adder is
    signal c : std_logic_vector(0 to NUM_BITS);
begin
    c(0) <= cin;
    gen_adder : for i in 0 to NUM_BITS-1 generate
        adder : entity work.full_adder_unit
           -- 同じ順番でポートを割り当てる。
            port map(a(i), b(i), c(i), sum(i), c(i+1));
    end generate;
    cout <= c(NUM_BITS);
end architecture;

これが全加算器:

library ieee;
use ieee.std_logic_1164.all;

entity full_adder_unit is
    port(
        a, b, cin: in  std_logic;
        sum, cout: out std_logic
    );
end entity full_adder_unit;

architecture rtl of full_adder_unit is
begin
    sum  <= a xor b xor cin;
    cout <= (a and b) or (a and cin) or (b and cin);
end architecture;