VHDL question 2 - how to get codes together in one code

Status
Not open for further replies.

lahrach

Full Member level 3
Joined
Feb 6, 2009
Messages
170
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,285
VHDL question 2

hi;

i want to encode a 16 bits adder so:

first i did code for 1 bit adder then 4 bits adder , 16 bits adder

can i get all this codes to gether in one code ( 1 entity)?

thanks
 

yes.. You can build a 4 bit adder using a 4 single bit adders. and similarly you can use 4 4bit adders to generate 16 bit adder.
 

Re: VHDL question 2

Hi,

Yes you can code your design in one entity, but it is better that you defragment it.
 

Re: VHDL question 2

Thank you FIRAS for your answer, what is your level in VHDL coding!

regards
 

I presume you have an entity for a single bit adder. Say something like:
Code:
entity Adder_1bit is
  port
  (
    a : in std_logic;
    b : in std_logic;
    cin : in std_logic;
    y : out std_logic;
    cout : out std_logic
  );
end entity Adder_1bit;
You can then create a 4-bit adder by doing something like this:
Code:
entity Adder_4bit is
  port
  (
    a : in std_logic_vector(3 downto 0);
    b : in std_logic_vector(3 downto 0);
    cin : in std_logic;
    y : out std_logic_vector(3 downto 0);
    cout : out std_logic
  );
end entity Adder_4bit;

architecture structural of Adder_4bit is
  component Adder_1bit
    port
    (
      a : in std_logic;
      b : in std_logic;
      cin : in std_logic;
      y : out std_logic;
      cout : out std_logic
    );
  end component Addr_1bit;

  signal carries : std_logic_vector(2 downto 0);
begin
  a0 : Adder_1bit port map (a => a(0), b => b(0), y => y(0), cin => cin,        cout => carries(0) );
  a1 : Adder_1bit port map (a => a(1), b => b(1), y => y(1), cin => carries(0), cout => carries(1) );
  a2 : Adder_1bit port map (a => a(2), b => b(2), y => y(2), cin => carries(1), cout => carries(2) );
  a3 : Adder_1bit port map (a => a(3), b => b(3), y => y(3), cin => carries(2), cout => cout );
end architecture structural;
Of course, you could create a generic width adder (using, you guessed it, generics) and generate statements to make it much cleaner.

Or do it the way it is done in the real world:
Code:
entity adder_16bit is
  port
  (
    a : in std_logic_vector(15 downto 0);
    b : in std_logic_vector(15 downto 0);
    y : in std_logic_vector(15 downto 0);
  );

architecture rtl of some_entity is
  signal a_l : signed(a'range);
  signal b_l : signed(b'range);
  signal y_l : signed(y'range);
begin
  a_l <= signed(a);
  b_l <= signed(b);

  y_l <= a_l + b_l;

  y <= std_logic_vector(y_l);
end architecture rtl;
(If you want a carry-in/carry-out in the above example, it is a bit more work.)
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…