32-bit Ripple Carry Adder이다.
rca.vhd
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port ( d : IN std_logic;
clk : IN std_logic;
set, rst : IN std_logic;
q : OUT std_logic );
end dff;
architecture arch_dff of dff is
begin
process_dff : process(clk) is
begin
if ( rst = '1' ) then
q <= '0';
elsif ( set = '1' ) then
q <= '1';
elsif ( clk = '1' and clk'event ) then
q <= d;
end if;
end process;
end arch_dff;
rca_tb.vhd
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
entity rca_tb is
end rca_tb;
architecture arch_rca_tb of rca_tb is
component rca is
port ( a, b : IN std_logic_vector ( 31 downto 0 );
cin : IN std_logic;
s : OUT std_logic_vector (31 downto 0);
cout : OUT std_logic );
end component;
signal input_a : std_logic_vector (31 downto 0) := "00000000000000000000000000000000";
signal input_b : std_logic_vector (31 downto 0) := "11111111111111111111111111111111";
signal input_cin : std_logic;
signal output_s : std_logic_vector (31 downto 0);
signal output_cout : std_logic;
begin
rca_port_map : rca port map ( input_a, input_b, input_cin, output_s, output_cout );
process_a : process begin
input_a <= unsigned(input_a) + 1;
wait for 10 ns;
end process;
process_b : process begin
input_b <= unsigned(input_b) - 1;
wait for 10 ns;
end process;
process_cin: process begin
input_cin <= '0';
wait for 100 ns;
input_cin <= '1';
wait for 100 ns;
end process;
end arch_rca_tb;
10 ns의 간격을 두고 input_a는 1씩 더하며 input_b는 1씩 빼주기 때문에 더한 값은 언제나 "11111111111111111111111111111111"이 된다.
여기서 변수는 당연하게도 맨 처음 올라오는 캐리 비트인 input_cin이며, 이 값이 1일 경우 오버플로우가 일어나 output_s는 모든 비트가 0이 되고, output_cout이 1이 된다.
![](https://t1.daumcdn.net/cfile/tistory/263F253C56BCAA0112)
위는 이 글의 소스 그대로 컴파일하여 시뮬레이션을 돌려 본 결과이다.
5 ns 마다 input_cin이 0, 1을 왔다갔다 하기 때문에 output_s와 output_cout 도 마찬가지로 각각 0, 1을 왔다갔다 한다.
![](https://t1.daumcdn.net/cfile/tistory/274CCA3C56BCAA020B)
이것은 input_cin의 딜레이를 100 ns로 늘려보았을 때의 결과이다.
보다시피 input_cin이 0인 구간에서는 input_a + input_b 는 항상 "11111111111111111111111111111111"로 고정인 것을 알 수 있다.
input_cin이 1이 되면 자연스럽게 output_s는 모든 비트가 0이 되고 output_cout은 1이 된다.