Ripple-carry Adder

入出力

名前 入力・出力 ビット幅 説明
ain 入力 N
bin 入力 N
cin 入力 1 下位からの桁上がり
cout 出力 1 桁上がり
sout 出力 N 合計

コード

  • VerilogHDL

ripple_carry_adder.v

module ripple_carry_adder(ain, bin, cin, cout, sout);
  parameter N = 8;
 
  input [N-1:0] ain, bin;
  input cin;
  output reg cout;
  output reg [N-1:0] sout;
 
  reg [N:0] c;
 
  integer i;
  always@(ain or bin or cin)
  begin
    c[0] = cin;
    for(i=1;i<=N;i=i+1)
      c[i] = (ain[i-1] | bin[i-1]) & (bin[i-1] | c[i-1]) & (c[i-1] | ain[i-1]);
 
    cout = c[N];
    sout = ain ^ bin ^ c[N-1:0];
  end
endmodule
  • VHDL

ripple_carry_adder.vhd

library	IEEE;
use IEEE.std_logic_1164.all;
 
entity ripple_carry_adder is
  generic(N : integer := 8);
  port (ain, bin : in std_logic_vector(N-1 downto 0);
        cin : in std_logic;
        cout : out std_logic;
        sout : out std_logic_vector(N-1 downto 0) );
end ripple_carry_adder;
 
architecture rtl of ripple_carry_adder is
begin
 
  process(ain, bin, cin)
    variable c : std_logic_vector(N downto 0);
    variable i : integer;
  begin
    c(0) := cin;
    for i in 1 to N loop
      c(i) := (ain(i-1) or bin(i-1))
          and (bin(i-1) or c(i-1))
          and (c(i-1) or ain(i-1));
    end loop;
    cout <= c(N);
    sout <= ain xor bin xor c(N-1 downto 0);
  end process;
 
end rtl;

シミュレーション

  • テストベンチ(VerilogHDL)

ripple_carry_adder_tb.v

`timescale 1ns / 1ps
module ripple_carry_adder_tb;
  parameter N=8;
 
  reg [N-1:0] ain, bin;
  reg cin;
  wire cout;
  wire [N-1:0] sout;
 
  ripple_carry_adder ripple_carry_adder_d(
    .ain(ain), .bin(bin), .cin(cin),
    .cout(cout), .sout(sout));
 
  initial begin
    ain = 0; bin = 0; cin = 0;
    #100 ain = 12;  bin = 46; cin = 0;
    #100 ain = 12;  bin = 6;  cin = 1;
    #100 ain = 0;   bin = 4;  cin = 1;
    #100 ain = 112; bin = 9;  cin = 0;
    #100 ain = 12;  bin = 46; cin = 1;
    #100 ain = 255; bin = 0;  cin = 0;
    #100 ain = 255; bin = 0;  cin = 1;
    #100 ain = 0;   bin = 0;  cin = 0;
  end
 
endmodule
  • テストベンチ(VHDL)

ripple_carry_adder_tb.vhd

library ieee;
use ieee.std_logic_1164.all;
 
entity ripple_carry_adder_tb is
end ripple_carry_adder_tb;
 
architecture rtl of ripple_carry_adder_tb is
 
  component ripple_carry_adder
    generic(N : integer := 8);
    port (ain, bin : in std_logic_vector(N-1 downto 0);
          cin : in std_logic;
          cout : out std_logic;
          sout : out std_logic_vector(N-1 downto 0) );
   end component;
 
  constant N : integer := 8;
 
  signal ain, bin, sout : std_logic_vector(N-1 downto 0);
  signal cin, cout : std_logic;
begin
  ripple_carry_adder_d: ripple_carry_adder
    generic map(N => N)
    port map(ain => ain, bin => bin, cin => cin,
             cout => cout, sout => sout);
  process
  begin
    ain <= X"00"; bin <= X"00"; cin <= '0';
    wait for 100 ns; ain <= X"0C"; bin <= X"01"; cin <= '0';
    wait for 100 ns; ain <= X"0C"; bin <= X"2E"; cin <= '1';
    wait for 100 ns; ain <= X"00"; bin <= X"06"; cin <= '1';
    wait for 100 ns; ain <= X"70"; bin <= X"09"; cin <= '0';
    wait for 100 ns; ain <= X"0C"; bin <= X"2E"; cin <= '1';
    wait for 100 ns; ain <= X"FF"; bin <= X"00"; cin <= '0';
    wait for 100 ns; ain <= X"FF"; bin <= X"00"; cin <= '1';
    wait for 100 ns; ain <= X"00"; bin <= X"00"; cin <= '0';
    wait;
  end process;
 
end;
  • シミュレーション波形

関連項目