Chuyển đến nội dung

Bài 8 · Entities and Architectures

Structural Design and Components

Build bigger circuits from smaller ones in VHDL: component declarations, direct entity instantiation, port map styles, hierarchy and a ripple-carry adder.

Large designs are assembled from smaller blocks. VHDL calls this structural modeling: you create instances of existing entities and connect their ports with signals. The result resembles a schematic written as text, and it is how hierarchy works in every real project.

Two ways to instantiate

Direct entity instantiation (preferred, VHDL-93 and later) names the entity directly:

VHDL
u_ha1 : entity work.half_adder
  port map (a => a, b => b, sum => s1, carry => c1);

Component instantiation (older style, still common) first declares a component, a local copy of the entity's interface, then instantiates it:

VHDL
architecture structure of full_adder is
  component half_adder is
    port (a, b : in std_logic; sum, carry : out std_logic);
  end component;
  signal s1, c1, c2 : std_logic;
begin
  u_ha1 : half_adder port map (a => a,  b => b,   sum => s1,  carry => c1);
  u_ha2 : half_adder port map (a => s1, b => cin, sum => sum, carry => c2);
  cout <= c1 or c2;
end architecture structure;

The component declaration must match the entity's ports. The advantage of components is that you can instantiate something that has not been written yet, or bind it differently using a configuration. The advantage of direct instantiation is less typing and no duplicated interface to keep in sync. Use it by default.

Every instance needs a unique label (u_ha1). Labels appear in simulation waveforms and synthesis reports, so give them meaningful names.

Port map styles

Named association matches by port name and is strongly recommended:

VHDL
port map (a => x, b => y, sum => z, carry => open)

Positional association matches by order and breaks silently if somebody reorders the ports:

VHDL
port map (x, y, z, open)

Use open for an output you do not need. Inputs can be tied to constants (b => '0') or, in VHDL-2008, to expressions such as a => x and y.

Hierarchy

Each instance is its own copy of the hardware. The full adder has two half adders plus one OR gate; an 8-bit adder built from eight full adders contains sixteen half adders. Designs form a tree: top level, then submodules, then primitives.

Hierarchy of an adder

adder4 Four full adders
full_adder Two half adders + OR
half_adder XOR and AND gates

Each level is an entity with its own architecture.

1 / 2

A ripple-carry adder

Chain full adders so each carry-out feeds the next carry-in. A for ... generate loop (covered in Generate Statements) saves repeating the instance:

VHDL
entity adder4 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 adder4;

architecture structure of adder4 is
  signal carry : std_logic_vector(4 downto 0);
begin
  carry(0) <= cin;
  gen : for i in 0 to 3 generate
    u_fa : entity work.full_adder
      port map (a => a(i), b => b(i), cin => carry(i),
                sum => sum(i), cout => carry(i + 1));
  end generate gen;
  cout <= carry(4);
end architecture structure;

The internal vector carry has one more bit than the data buses: carry(0) is the incoming carry and carry(4) the final carry-out.

The same structure in C

This stand-in builds the 4-bit adder out of a full_adder function exactly the way the VHDL does: a carry array, one function call per bit, all checked against real addition.

C Stand-in (C): 4-bit ripple-carry adder

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Delay through the chain

In real hardware a ripple-carry adder is slow because the carry must travel through all stages. This second stand-in counts "gate delays" along the carry chain for 4, 8, 16 and 32 bits (assuming each full adder's carry output takes 2 gate delays), to show why wide adders use other structures.

C Stand-in (C): carry chain length

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In practice you rarely instantiate adders by hand: you write a + b and the synthesis tool chooses a fast carry structure for the target FPGA. Structural design matters most for hierarchy and for connecting your own blocks.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which line is a direct entity instantiation?

    Direct instantiation uses the keyword entity and a library-qualified name.
  2. 2. Why is named port association safer than positional?

    Names tie each connection to a specific port, so reordering the ports cannot silently connect the wrong signals.
  3. 3. How do you leave an unused output unconnected?

    open marks a port as intentionally unconnected.
  4. 4. An instance needs a label because...

    Instantiation statements require a label, which names that copy in waveforms and reports.

Summary

  • Structural design instantiates smaller entities and wires them with signals.
  • Prefer entity work.name instantiation; components remain useful for late binding.
  • Use named association; open leaves outputs unconnected.
  • Each instance is a separate copy of the hardware; designs form a hierarchy.
  • A for ... generate loop builds repeated structures such as adder chains.

Next: Libraries, Packages and Use Clauses

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.