Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Your First Design: The Half Adder

Write your first VHDL design, a half adder: entity, architecture, concurrent assignments and a simple testbench, plus a C truth-table stand-in to check it.

Every VHDL design has two required parts: an entity, which describes the outside of the box (its ports), and an architecture, which describes the inside. In this lesson you build the simplest useful arithmetic circuit, the half adder, and see all the pieces together. Later lessons explain each piece in depth.

A half adder adds two single bits a and b. The result needs two bits: a sum bit and a carry bit. In boolean terms sum = a xor b and carry = a and b.

a b carry sum
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

The design file

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity half_adder is
  port (
    a     : in  std_logic;
    b     : in  std_logic;
    sum   : out std_logic;
    carry : out std_logic
  );
end entity half_adder;

architecture rtl of half_adder is
begin
  sum   <= a xor b;
  carry <= a and b;
end architecture rtl;

Walk through it from the top:

  • library ieee; use ieee.std_logic_1164.all; makes the type std_logic available. Without it, only the built-in types like bit exist.
  • entity half_adder is port (...) end entity; declares the interface. Each port has a name, a direction (in or out) and a type.
  • architecture rtl of half_adder is begin ... end architecture; holds the behavior. The name rtl (register transfer level) is a convention; you could pick any identifier.
  • <= is the signal assignment operator. It is not "less than or equal" here; the meaning depends on context.

The two assignments are concurrent statements. They describe two gates that exist at the same time. Their order inside the architecture is irrelevant.

Anatomy of a design

Library and use Bring in types
Entity Ports: the outside
Architecture Behavior: the inside

One entity can have several architectures.

1 / 2

Checking the logic with a stand-in

You cannot run the VHDL above here, but you can check the boolean logic with C, which models the same two gates:

C Stand-in (C): half adder truth table

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.

A first testbench

A testbench is a VHDL file with no ports. It instantiates your design (called the device under test, DUT), applies inputs and checks the outputs.

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity half_adder_tb is
end entity half_adder_tb;

architecture sim of half_adder_tb is
  signal a, b, sum, carry : std_logic;
begin
  dut : entity work.half_adder
    port map (a => a, b => b, sum => sum, carry => carry);

  stimulus : process
  begin
    a <= '0'; b <= '0'; wait for 10 ns;
    assert sum = '0' and carry = '0' report "0+0 failed" severity error;
    a <= '0'; b <= '1'; wait for 10 ns;
    assert sum = '1' and carry = '0' report "0+1 failed" severity error;
    a <= '1'; b <= '0'; wait for 10 ns;
    assert sum = '1' and carry = '0' report "1+0 failed" severity error;
    a <= '1'; b <= '1'; wait for 10 ns;
    assert sum = '0' and carry = '1' report "1+1 failed" severity error;
    report "test finished";
    wait;
  end process stimulus;
end architecture sim;

New things here: entity work.half_adder creates an instance of the design from the library work (the current project library); port map connects the instance's ports to signals; the process has sequential statements and wait for 10 ns lets simulated time pass; assert ... report ... severity prints a message when a condition is false. The final wait; stops the process forever so the simulation can end.

With GHDL, the flow from the design flow lesson is ghdl -a, ghdl -e half_adder_tb, ghdl -r half_adder_tb. When all assertions pass, you see only the final report line from the simulator. Testbenches get a whole chapter later on.

A table-driven check in C

Testbenches often use a table of expected values. This second stand-in does the same: it checks the model against an expected table and prints the result for each row, like an assert would.

C Stand-in (C): self-checking half 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.

A very common beginner mistake is writing the architecture body without begin, or putting end entity where end architecture belongs. VHDL is picky: read the first error message, fix that, and recompile.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two design units are required for a simple VHDL design?

    The entity declares the ports; the architecture describes the behavior or structure.
  2. 2. In sum <= a xor b;, what is <=?

    In a statement position, <= assigns a value to a signal. In a condition (for example inside if), it is the comparison operator.
  3. 3. Does the order of sum <= a xor b; and carry <= a and b; inside the architecture matter?

    Both statements are always active; the order in the file does not change the circuit.
  4. 4. What does a testbench entity usually have?

    A testbench is self-contained: it creates the stimulus itself, so it needs no ports.

Summary

  • A design is an entity (interface) plus an architecture (contents).
  • library ieee; use ieee.std_logic_1164.all; gives you std_logic.
  • Concurrent signal assignments with <= describe gates working in parallel.
  • A testbench has no ports, instantiates the design with port map, drives inputs and uses assert.

Next: VHDL Syntax Basics