# Fundamentals of VHDL for Digital Design

**Estimated Reading Time:** 30–45 minutes

## Learning Objectives

After completing this chapter, you will be able to:

1. Explain the fundamental difference between **sequential software programming languages** (such as Python, C, Java, and Rust) and **concurrent hardware description** (VHDL).

2. Define and relate the four core concepts of VHDL: **Entity**, **Architecture**, **Component**, and **Named Instance**.

3. Write and interpret valid **Entity** declarations using IEEE 1164 data types (`std_logic` and `std_logic_vector`), port modes, legal identifiers, and ESEC naming conventions.

4. Implement and interpret combinational circuits using all three VHDL architecture styles: **Dataflow**, **Behavioral**, and **Structural** (with explicit component declarations and named port mapping).

5. Apply the principles of **Abstraction** and **Hierarchy** to explain how complex digital systems are constructed from basic logic gates.

## Before You Begin

In the last few lessons, you were introduced to the building blocks of digital systems: binary numbers, logic gates (e.g. AND, OR, XOR, XNOR, etc.), truth tables, and multi-bit buses. You also gained experience writing software programs in previous courses (e.g. CompSci 110). This chapter bridges those two worlds—showing you how to describe digital circuits using text, and helping you shift your mindset from *software programmer* to *hardware designer*.

---

## 1. Describing Hardware Instead of Writing Software

In traditional software programming languages—such as **Python, C, Java, or Rust**—you solve problems by writing **algorithms**: step-by-step recipes that a processor (CPU) executes one line at a time. For example, in software you might write:

```python
a = 1
b = a + 1
```

The CPU executes line 1, finishes it, and *then* executes line 2. This is called **sequential execution**, and it is how virtually all general-purpose software languages operate.

**VHDL** (which stands for *VHSIC Hardware Description Language*) looks a little like a software language on the screen, **but it is not a programming language.** You are not writing instructions for a CPU to execute. Instead, you are writing a blueprint of a **physical electronic circuit**.

In this course, specialized software will read your VHDL blueprint and configure an **FPGA (Field-Programmable Gate Array)**—a reconfigurable chip filled with programmable logic blocks and wires—so the chip physically *becomes* the circuit you described.

### Concurrency: Everything Happens at Once

Imagine wiring two separate light switches to two separate light bulbs on a circuit board. When you flip both switches, the bulbs do not wait in line for each other—electricity flows through both circuits simultaneously.

Digital hardware works the same way. In VHDL, statements outside of special blocks represent physical hardware circuits that all operate **concurrently** (at the exact same time). Changing the order of two concurrent statements in a VHDL file does not change the circuit at all—just like drawing Gate A on the top of a schematic and Gate B on the bottom produces the exact same circuit as drawing Gate B on top and Gate A on the bottom.

> **A Note on "Concurrency": Software vs. Hardware**
> If you have encountered the term *concurrency* in a software course, be careful—it means something different there. In **software**, concurrency typically refers to **interleaving**: a CPU rapidly switches back and forth between multiple tasks so they *appear* to run together, even though a single core still executes only one instruction at a time. In **hardware**, concurrency means **true parallelism**: every logic gate and wire you describe physically exists on the FPGA at the same time, so all parts of your circuit operate simultaneously.

| Concept | Software Languages (e.g., Python, C, Java, Rust) | VHDL (Hardware Description) |
| :--- | :--- | :--- |
| **Primary Goal** | Tell a CPU what steps to run | Describe physical logic gates and wires |
| **Execution Model** | Sequential (one line after another) | Concurrent (all hardware runs simultaneously) |
| **Meaning of a name** | A variable stored in RAM | A physical wire (signal) carrying voltage (`0` or `1`) |
| **Hardware Target** | Runs on a general-purpose CPU | Synthesized into custom circuitry on an FPGA |

### Checkpoint 1
*(Check your work against the Checkpoint Answers at the end of the chapter.)*

1. Why is it inaccurate to say that an FPGA "runs VHDL code line by line" the way a computer runs a software program?
2. If you swap the order of two independent Boolean equations in a VHDL dataflow design, does the resulting physical circuit change? Why or why not?

---

## 2. The Four Pillars of VHDL Design

When electrical engineers build a circuit on a physical breadboard, they pick integrated circuit (IC) chips from a parts bin, place them on the board, and connect their metal pins with wires. VHDL uses the exact same mental model, organized around **four core concepts**:

1. **Entity:** The *external interface* of a design—just like the physical pins sticking out of a black plastic IC chip. An entity tells the rest of the system what inputs go in and what outputs come out, without revealing what is inside.
2. **Architecture:** The *internal behavior or structure inside that black plastic IC chip* (the entity)—describing what actually happens between the entity's pins.
3. **Component:** A reusable "chip" or "building block" that can be placed inside a larger system. Declaring a component in VHDL is like listing a chip in your **parts inventory**—you cannot place a chip onto your circuit board unless it is in your inventory!
4. **Named Instance:** A specific, uniquely labeled copy of a component placed into your circuit. If you need four 2-input XNOR gates in a design, you declare the `xnor2` component *once* in your inventory, and then create four **named instances** (e.g., `u0`, `u1`, `u2`, `u3`) in your circuit.

Every standalone VHDL module consists of an **Entity/Architecture Pair**. An architecture cannot exist on its own—it must always be paired with an entity.

---

## 3. The Entity Declaration: Designing the Chip's Pins

### Standard Libraries and Signal Types (IEEE 1164)

Out of the box, core VHDL does not include the standard electrical logic types used in modern digital design. To access them, we include two lines at the top of every VHDL file:

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
```

The `std_logic_1164` package defines a 9-valued logic system (capable of representing `'0'`, `'1'`, and seven other hardware states such as uninitialized `'U'`, unknown `'X'`, or high-impedance `'Z'`). As a beginner, you will work almost exclusively with `'0'` (logic low) and `'1'` (logic high), using two data types:

* **`std_logic`**: Represents a **single wire** carrying 1 bit of information. Single-bit values are written in **single quotes**: `'0'` or `'1'`.
* **`std_logic_vector`**: Represents a **multi-bit bus**—a group of parallel wires treated as a single bundle.

> **Digital Logic Refresher — Multi-Bit Buses (`DOWNTO` vs. `TO`) & Bit Indexing:**
> Suppose you want to send an 8-bit binary number (like `10110010`) into a circuit. Instead of creating eight separate ports named `data0`, `data1`, ..., `data7`, digital designers bundle those 8 wires into a single **bus**, often drawn on schematic diagrams as `data[7:0]`.
>
> In VHDL, you declare this bus as `std_logic_vector(7 DOWNTO 0)`. Why `DOWNTO`? Because in standard binary notation, the leftmost bit is the Most Significant Bit (MSB, worth $2^7 = 128$) and the rightmost bit is the Least Significant Bit (LSB, worth $2^0 = 1$). Using `DOWNTO` keeps each bit's index aligned with its power of 2.
>
> Note that VHDL also allows ascending ranges using **`TO`**, such as `std_logic_vector(0 TO 3)`, where bit 0 is on the left. While we primarily use **`DOWNTO`** in this course, knowing that `TO` exists is helpful if you consult external textbooks or online VHDL references.
>
> **Quotes & Single-Wire Indexing:** When assigning a value to an *entire* multi-bit bus, use **double quotes**, such as `o_data_out <= "10110010";`. However, you can still read or assign a value to a **single wire** inside a multi-bit bus using **parentheses** for indexing—for example, `o_data_out(0) <= '1';` (the VHDL equivalent of `data[0]` on a schematic). Because `o_data_out(0)` refers to just *one* wire (`std_logic`), you must use **single quotes** (`'0'` or `'1'`).

### Legal VHDL Identifiers

Every entity, port, signal, and architecture needs a name (an **identifier**). VHDL enforces four strict rules:

1. Only **letters** (`A–Z`, `a–z`), **digits** (`0–9`), and **underscores** (`_`) are allowed.
2. The **first character must be a letter**.
3. The **last character cannot be an underscore**, and **two underscores in a row (`__`) are not allowed**.
4. **Reserved words** (keywords like `entity`, `port`, `in`, `out`, `case`, `end`) cannot be used as names.

Additionally, VHDL is **case-insensitive** (`i_Clk`, `I_CLK`, and `i_clk` all refer to the exact same identifier), though staying consistent makes your code much easier to read.

| Legal Identifiers | Illegal Identifiers | Reason Illegal |
| :--- | :--- | :--- |
| `i_tx_clk` | `_tx_clk` | Starts with an underscore |
| `i_Three_State_Enable` | `8B10B` | Starts with a digit |
| `o_select_7D` | `large#num` | Contains an invalid character (`#`) |
| `w_hit_count` | `case` | Reserved VHDL keyword |
| `c_data_out` | `data__out` | Two consecutive underscores |
| `o_clk` | `clk_` | Ends with an underscore |

> **Best Practice — Choose Descriptive Identifiers:** Just because an identifier is *legal* in VHDL does not mean it is a *good* name. Always choose descriptive names that make a signal's function immediately obvious to a human reader—for example, `i_data_in`, `o_carry_out`, and `o_sum` instead of cryptic abbreviations like `i_x`, `o_c`, or `o_s`. Short, single-letter names are acceptable only when they are universally recognized standard notation for a specific component (such as `i_d` and `o_q` on a D flip-flop, or `i_a` and `i_b` on a half adder).

### ESEC Naming Conventions

To keep complex hardware designs readable and easy to debug, our department uses the **ESEC Naming Conventions**—short prefixes (and one suffix) attached to signal and port names to immediately reveal their role. You will see this reference block in the header comments of every starter `.vhd` file throughout this course.

#### Used Right Away (Combinational Circuits & Interfaces)

| Prefix / Suffix | Meaning | Example |
| :--- | :--- | :--- |
| `i_` | On-chip **input** port | `i_clk`, `i_data_in` |
| `o_` | On-chip **output** port | `o_sum`, `o_equals` |
| `b_` | On-chip **bidirectional** port | `b_data_bus` |
| `c_` | Internal **combinational** signal | `c_bit_match` |
| `w_` | Internal / top-level **wiring** signal (connecting components) | `w_carry_1`, `w_bit_match` |
| `_n` (suffix) | **Active-low** signal (`'0'` means active/on) | `o_seg_n`, `w_7SD_EN_n` |

#### Preview: Used Later This Semester (Testbenches, Sequential Circuits & FSMs)

As you progress through the semester's in-class exercises and labs, starter code templates will introduce a few additional ESEC prefixes—keep this table handy as a reference:

| Prefix | Meaning | Example (from later labs/ICEs) |
| :--- | :--- | :--- |
| `f_` | **Synchronous** / flip-flop signal (updates on a clock edge) | `f_Q`, `f_Q_next`, `f_sel_n` |
| `k_` | **Constant** (fixed value, such as clock period or bus width) | `k_clk_period`, `k_DIV`, `k_width` |
| `sm_` | **State machine** enumerated type definition | `sm_floor` |
| `s_` | **State** name inside a state machine | `s_floor1`, `s_floor2` |

> **A Note on Industry Practice:** The ESEC naming convention is a style guide used in our course and department—it is **not** a universal rule of the VHDL language. When you read external textbooks, datasheets, or industry codebases, you will frequently see VHDL code written without prefixes (e.g., `clk`, `data_in`, `equals`) or using a different company's naming convention. In this course, however, we will use ESEC prefixes consistently in all examples and labs.

### Port Modes

Every port in an entity must declare a **mode**, which defines the direction data travels across the boundary of the entity:

* **`IN`**: Data enters the entity only. Inside the architecture, you can *read* an `IN` port, but you can never assign a value to it.
* **`OUT`**: Data leaves the entity only. Inside the architecture, you can *assign* a value to an `OUT` port, but you cannot read its value back internally.
* **`INOUT`**: Data is **bidirectional**—it can flow both into and out of the entity (used for shared buses, such as memory data pins). Inside the architecture, you can both *read from* and *assign a value to* an `INOUT` port.
* **`BUFFER`**: Data flows out of the entity *and* is fed back internally so the architecture can both assign a value to it and read its own output back internally.

> **Best Practice Note on `BUFFER`:** Although VHDL includes the `BUFFER` mode, professional FPGA designers generally **avoid** using `BUFFER` ports. In hierarchical designs, connecting a `BUFFER` port to other components can cause strict type/mode mismatch errors. The recommended best practice whenever you need to both output a signal *and* read it internally is to declare an internal `SIGNAL`, read and drive that internal signal inside the architecture, and then assign it to a standard `OUT` port (e.g., `o_flag <= c_flag;`).

### Anatomy of an Entity Declaration

Here is the general syntax for an entity:

```vhdl
ENTITY entity_name IS
    PORT (
        name      : mode type;
        ...
        last_name : mode type
    );
END entity_name;
```

Let's examine a concrete example: a module named `black_box` with single-bit inputs `i_clk` and `i_reset`, an 8-bit input bus `i_data_in`, an 8-bit output bus `o_data_out`, and a single-bit carry-out output `o_carry_out`.

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY black_box IS
    PORT (
        i_clk, i_reset : IN  std_logic;
        i_data_in      : IN  std_logic_vector(7 DOWNTO 0);
        o_data_out     : OUT std_logic_vector(7 DOWNTO 0);
        o_carry_out    : OUT std_logic
    );
END black_box;
```

**Watch out for these common beginner syntax pitfalls:**

1. **Comma-separated lists:** `i_clk` and `i_reset` share the exact same mode (`IN`) and type (`std_logic`), so they can be declared on the same line separated by a comma.
2. **The missing last semicolon:** Notice that `o_carry_out : OUT std_logic` does **not** end with a semicolon! In VHDL, semicolons separate port declarations inside `PORT (...)`, and the final semicolon goes *outside* the closing parenthesis: `);`. Putting a semicolon on the last port line is the #1 syntax error for new VHDL students.
3. **Matching names:** The name after `END` (`black_box`) must match the name after `ENTITY`.

### Worked Example 1: Interpreting an Entity Declaration

**Prompt:** Examine the following VHDL entity declaration. How many total physical input wires and output wires does this chip have? Describe each port in plain English.

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY mux2to1 IS
    PORT (
        i_data_0, i_data_1 : IN  std_logic_vector(3 DOWNTO 0);
        i_select           : IN  std_logic;
        o_mux_out          : OUT std_logic_vector(3 DOWNTO 0)
    );
END mux2to1;
```

#### Solution Walkthrough

1. **Identify the entity name:** `mux2to1` (a 2-to-1 multiplexer).
2. **Analyze the `IN` ports:**
   * `i_data_0` is a 4-bit input bus (`3 DOWNTO 0` = bits 3, 2, 1, 0 $\rightarrow$ 4 wires).
   * `i_data_1` is also a 4-bit input bus (4 wires).
   * `i_select` is a 1-bit input (`std_logic` $\rightarrow$ 1 wire) that selects which input bus passes through to the output.
   * **Total input wires:** $4 + 4 + 1 = 9$ input wires.
3. **Analyze the `OUT` ports:**
   * `o_mux_out` is a 4-bit output bus (`3 DOWNTO 0` $\rightarrow$ 4 wires).
   * **Total output wires:** $4$ output wires.

### Worked Example 2: Writing an Entity Declaration from a Specification

**Prompt:** Write a complete VHDL entity declaration for a **Half Adder** named `half_adder`. A Half Adder takes two 1-bit binary inputs (`a` and `b`) and produces two 1-bit outputs: the binary `sum` and the `carry_out`. Follow ESEC naming conventions.

#### Solution Walkthrough

1. **Start with the IEEE library declarations** so `std_logic` is available.
2. **Choose ESEC names:** Conventional half-adder inputs `a` and `b` become `i_a` and `i_b`. Outputs become `o_sum` and `o_carry_out`.
3. **Assign modes and types:** All four ports are single-bit wires (`std_logic`). `i_a` and `i_b` are `IN`; `o_sum` and `o_carry_out` are `OUT`.
4. **Check punctuation:** No semicolon on the last port line inside `PORT (...)`.

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY half_adder IS
    PORT (
        i_a, i_b    : IN  std_logic;
        o_sum       : OUT std_logic;
        o_carry_out : OUT std_logic
    );
END half_adder;
```

### Checkpoint 2
*(Check your work against the Checkpoint Answers at the end of the chapter.)*

1. Identify which of the following VHDL identifiers are **illegal**, and state the rule each one violates:
   `i_reset_n`, `4_bit_adder`, `o_sum_`, `w_bus__a`, `out`
2. Suppose `o_leds` is declared as `OUT std_logic_vector(3 DOWNTO 0)`. Write a single VHDL statement that sets only bit 2 of `o_leds` to logic high (`1`). Should you use single quotes or double quotes?
3. You are designing a circuit where an output port `o_flag` needs to be sent to an external LED *and* checked internally by another logic gate inside the same architecture. Which port mode technically allows this, and what is the recommended **best practice** alternative?
4. Spot the **two** syntax errors in the following entity declaration:

   ```vhdl
   ENTITY my_gate IS
       PORT (
           i_in_a, i_in_b : IN  std_logic;
           o_result       : OUT std_logic;
       );
   END gate;
   ```

---

## 4. The Architecture Body: What Happens Inside the Black Box

While the entity defines the pins on the outside of the IC chip, the **architecture** defines what happens inside it. Here is the general template:

```vhdl
ARCHITECTURE arch_name OF entity_name IS
    -- Declarative region: optional local signals and component declarations
BEGIN
    -- Architecture body: VHDL statements describing the circuit's function
END arch_name;
```

Notice the **declarative region** between `IS` and `BEGIN`. This is where you declare anything *internal* to the chip—such as local wires (`SIGNAL`) or sub-chips (`COMPONENT`)—that does not connect directly to the outside world.

### Digital Logic Refresher: A 4-Bit Equality Comparator

To see how the exact same hardware can be described in three different VHDL styles, let's build a **4-bit equality comparator** named `compare`. It has two 4-bit input buses (`i_bus_a` and `i_bus_b`) and a single-bit output (`o_equals`). It outputs `'1'` if `i_bus_a` and `i_bus_b` are identical, and `'0'` otherwise.

How do we build this out of basic logic gates?

1. First, compare each pair of corresponding bits (`i_bus_a(0)` with `i_bus_b(0)`, `i_bus_a(1)` with `i_bus_b(1)`, etc.) using a 2-input **XNOR** gate. Remember the truth table for an XNOR gate—it outputs `1` whenever both inputs are *equal*:

| `i_in1` | `i_in2` | `i_in1 XNOR i_in2` |
| :---: | :---: | :---: |
| `0` | `0` | **`1`** |
| `0` | `1` | `0` |
| `1` | `0` | `0` |
| `1` | `1` | **`1`** |

2. Next, feed all four XNOR outputs into a 4-input **AND** gate. An AND gate outputs `1` *only* when **all** of its inputs are `1`. Therefore, `o_equals` will be `1` if and only if all four pairs of bits match!

Below is the schematic diagram of our `compare` entity and its internal gates. Notice how the dashed box represents the **Entity** boundary (its pins), while everything inside the box represents the **Architecture**:

<svg viewBox="0 0 720 325" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Schematic of 4-bit equality comparator entity compare containing four XNOR gates feeding into a 4-input AND gate" style="max-width: 100%; height: auto; display: block; margin: 1.5rem auto;">
  <!-- Background -->
  <rect x="0" y="0" width="720" height="325" rx="8" fill="#ffffff" stroke="#e5e7eb" stroke-width="1.5"/>

  <!-- Entity Dashed Boundary -->
  <rect x="115" y="22" width="480" height="282" rx="6" fill="#f8fafc" stroke="#334155" stroke-width="2" stroke-dasharray="8,5"/>
  <text x="128" y="42" font-family="monospace, sans-serif" font-size="13" font-weight="bold" fill="#1e293b">ENTITY: compare</text>

  <!-- === XNOR Gate u0 (cy = 85) === -->
  <line x1="88" y1="76" x2="232" y2="76" stroke="#1e293b" stroke-width="1.8"/>
  <line x1="88" y1="94" x2="232" y2="94" stroke="#1e293b" stroke-width="1.8"/>
  <circle cx="115" cy="76" r="3.5" fill="#0f172a"/>
  <circle cx="115" cy="94" r="3.5" fill="#0f172a"/>
  <text x="82" y="80" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_a(0)</text>
  <text x="82" y="98" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_b(0)</text>
  <!-- XNOR Symbol u0 -->
  <path d="M 222,67 Q 232,85 222,103" fill="none" stroke="#0f172a" stroke-width="1.8"/>
  <path d="M 229,67 Q 239,85 229,103 L 248,103 Q 272,101 282,85 Q 272,69 248,67 Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <circle cx="287" cy="85" r="4.5" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <text x="254" y="89" text-anchor="middle" font-family="monospace" font-size="11" font-weight="bold" fill="#0f172a">u0</text>
  <text x="254" y="61" text-anchor="middle" font-family="sans-serif" font-size="9.5" fill="#475569">xnor2</text>

  <!-- === XNOR Gate u1 (cy = 145) === -->
  <line x1="88" y1="136" x2="232" y2="136" stroke="#1e293b" stroke-width="1.8"/>
  <line x1="88" y1="154" x2="232" y2="154" stroke="#1e293b" stroke-width="1.8"/>
  <circle cx="115" cy="136" r="3.5" fill="#0f172a"/>
  <circle cx="115" cy="154" r="3.5" fill="#0f172a"/>
  <text x="82" y="140" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_a(1)</text>
  <text x="82" y="158" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_b(1)</text>
  <!-- XNOR Symbol u1 -->
  <path d="M 222,127 Q 232,145 222,163" fill="none" stroke="#0f172a" stroke-width="1.8"/>
  <path d="M 229,127 Q 239,145 229,163 L 248,163 Q 272,161 282,145 Q 272,129 248,127 Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <circle cx="287" cy="145" r="4.5" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <text x="254" y="149" text-anchor="middle" font-family="monospace" font-size="11" font-weight="bold" fill="#0f172a">u1</text>

  <!-- === XNOR Gate u2 (cy = 205) === -->
  <line x1="88" y1="196" x2="232" y2="196" stroke="#1e293b" stroke-width="1.8"/>
  <line x1="88" y1="214" x2="232" y2="214" stroke="#1e293b" stroke-width="1.8"/>
  <circle cx="115" cy="196" r="3.5" fill="#0f172a"/>
  <circle cx="115" cy="214" r="3.5" fill="#0f172a"/>
  <text x="82" y="200" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_a(2)</text>
  <text x="82" y="218" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_b(2)</text>
  <!-- XNOR Symbol u2 -->
  <path d="M 222,187 Q 232,205 222,223" fill="none" stroke="#0f172a" stroke-width="1.8"/>
  <path d="M 229,187 Q 239,205 229,223 L 248,223 Q 272,221 282,205 Q 272,189 248,187 Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <circle cx="287" cy="205" r="4.5" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <text x="254" y="209" text-anchor="middle" font-family="monospace" font-size="11" font-weight="bold" fill="#0f172a">u2</text>

  <!-- === XNOR Gate u3 (cy = 265) === -->
  <line x1="88" y1="256" x2="232" y2="256" stroke="#1e293b" stroke-width="1.8"/>
  <line x1="88" y1="274" x2="232" y2="274" stroke="#1e293b" stroke-width="1.8"/>
  <circle cx="115" cy="256" r="3.5" fill="#0f172a"/>
  <circle cx="115" cy="274" r="3.5" fill="#0f172a"/>
  <text x="82" y="260" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_a(3)</text>
  <text x="82" y="278" text-anchor="end" font-family="monospace" font-size="11.5" fill="#0f172a">i_bus_b(3)</text>
  <!-- XNOR Symbol u3 -->
  <path d="M 222,247 Q 232,265 222,283" fill="none" stroke="#0f172a" stroke-width="1.8"/>
  <path d="M 229,247 Q 239,265 229,283 L 248,283 Q 272,281 282,265 Q 272,249 248,247 Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <circle cx="287" cy="265" r="4.5" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <text x="254" y="269" text-anchor="middle" font-family="monospace" font-size="11" font-weight="bold" fill="#0f172a">u3</text>

  <!-- === Internal Wires (c_bit_match) connecting XNORs to AND4 === -->
  <!-- u0 -> AND4 pin 1 (y=148) -->
  <polyline points="292,85 415,85 415,148 460,148" fill="none" stroke="#0284c7" stroke-width="1.8"/>
  <text x="352" y="79" text-anchor="middle" font-family="monospace" font-size="10.5" fill="#0369a1">c_bit_match(0)</text>

  <!-- u1 -> AND4 pin 2 (y=166) -->
  <polyline points="292,145 395,145 395,166 460,166" fill="none" stroke="#0284c7" stroke-width="1.8"/>
  <text x="344" y="139" text-anchor="middle" font-family="monospace" font-size="10.5" fill="#0369a1">c_bit_match(1)</text>

  <!-- u2 -> AND4 pin 3 (y=184) -->
  <polyline points="292,205 395,205 395,184 460,184" fill="none" stroke="#0284c7" stroke-width="1.8"/>
  <text x="344" y="220" text-anchor="middle" font-family="monospace" font-size="10.5" fill="#0369a1">c_bit_match(2)</text>

  <!-- u3 -> AND4 pin 4 (y=202) -->
  <polyline points="292,265 415,265 415,202 460,202" fill="none" stroke="#0284c7" stroke-width="1.8"/>
  <text x="352" y="280" text-anchor="middle" font-family="monospace" font-size="10.5" fill="#0369a1">c_bit_match(3)</text>

  <!-- === 4-Input AND Gate u4 (cy = 175) === -->
  <path d="M 460,135 L 495,135 A 40,40 0 0 1 495,215 L 460,215 Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.8"/>
  <text x="492" y="179" text-anchor="middle" font-family="monospace" font-size="11.5" font-weight="bold" fill="#0f172a">u4</text>
  <text x="492" y="128" text-anchor="middle" font-family="sans-serif" font-size="9.5" fill="#475569">and4</text>

  <!-- === Output Wire o_equals === -->
  <line x1="535" y1="175" x2="630" y2="175" stroke="#1e293b" stroke-width="1.8"/>
  <circle cx="595" cy="175" r="3.5" fill="#0f172a"/>
  <text x="636" y="179" text-anchor="start" font-family="monospace" font-size="12" font-weight="bold" fill="#0f172a">o_equals</text>
</svg>

All three architectures below share this exact same **entity**:

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY compare IS
    PORT (
        i_bus_a, i_bus_b : IN  std_logic_vector(3 DOWNTO 0);
        o_equals         : OUT std_logic
    );
END compare;
```

### Style 1: Dataflow Architecture

**Dataflow** style describes a circuit directly using **Boolean equations** (with logic operators like `AND`, `OR`, `NOT`, `XOR`, and `XNOR`), showing how signals flow through combinational logic from inputs to outputs. (VHDL dataflow also supports *conditional signal assignments* such as `WHEN ... ELSE`, which you will encounter in upcoming labs, though Boolean equations give the clearest picture of how dataflow mirrors logic gates.)

Here is our 4-bit equality comparator written in Dataflow style using Boolean equations and an internal combinational signal `c_bit_match`:

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY compare IS
    PORT (
        i_bus_a, i_bus_b : IN  std_logic_vector(3 DOWNTO 0);
        o_equals         : OUT std_logic
    );
END compare;

ARCHITECTURE dataflow OF compare IS
    SIGNAL c_bit_match : std_logic_vector(3 DOWNTO 0);
BEGIN
    -- Compare each pair of bits using XNOR
    c_bit_match(0) <= i_bus_a(0) XNOR i_bus_b(0);
    c_bit_match(1) <= i_bus_a(1) XNOR i_bus_b(1);
    c_bit_match(2) <= i_bus_a(2) XNOR i_bus_b(2);
    c_bit_match(3) <= i_bus_a(3) XNOR i_bus_b(3);

    -- Output '1' only if ALL four bit pairs match
    o_equals <= c_bit_match(0) AND c_bit_match(1) AND c_bit_match(2) AND c_bit_match(3);
END dataflow;
```

**Key Concept — The Signal Assignment Operator (`<=`):**
In many software programming languages, `=` assigns a value to a variable and `==` tests for equality. In VHDL, **`=`** is *only* used for **equality comparison**. To drive a value onto a wire/signal, VHDL uses **`<=`**. Read `c_bit_match(0) <= i_bus_a(0) XNOR i_bus_b(0);` as: *"Wire `c_bit_match(0)` to the XNOR of `i_bus_a(0)` and `i_bus_b(0)`."* Because all five statements are concurrent, their order in the file does not matter.

### Style 2: Behavioral Architecture

**Behavioral** style describes *what* the circuit does at a higher, algorithmic level—without writing out individual gate equations. To use high-level constructs like `IF ... THEN ... ELSE`, we place them inside a **`PROCESS`** block.

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY compare IS
    PORT (
        i_bus_a, i_bus_b : IN  std_logic_vector(3 DOWNTO 0);
        o_equals         : OUT std_logic
    );
END compare;

ARCHITECTURE behavior OF compare IS
BEGIN
    comp : PROCESS (i_bus_a, i_bus_b)
    BEGIN
        IF i_bus_a = i_bus_b THEN
            o_equals <= '1';
        ELSE
            o_equals <= '0';
        END IF;
    END PROCESS comp;
END behavior;
```

**Understanding `PROCESS` and the Sensitivity List:**

* `comp :` is an optional label naming the process.
* `(i_bus_a, i_bus_b)` is called the **sensitivity list**. It tells VHDL which input signals can trigger a change in this circuit. Whenever `i_bus_a` or `i_bus_b` changes, the logic inside the process re-evaluates.
* **Software vs. Hardware Caution:** Inside a `PROCESS`, statements are evaluated in order from top to bottom (which looks very similar to traditional software code!). However, the `PROCESS` block as a whole still runs **concurrently** with everything else in the architecture, and when synthesized onto an FPGA, this `IF/ELSE` block becomes physical combinational logic gates—not software instructions executed by a CPU.

### Style 3: Structural Architecture

**Structural** style describes a circuit just like the **schematic diagram** shown above. Instead of writing equations or `IF` statements, you explicitly place pre-built **components** (chips) and wire them together.

Building a structural architecture always follows three steps:

1. **Declare Components (Stock Your Inventory):** Between `IS` and `BEGIN`, list every building-block component you plan to use, copying its exact `PORT` list.
2. **Declare Internal Wiring Signals:** Still between `IS` and `BEGIN`, declare any internal `SIGNAL` wires needed to connect the components to each other (using the `w_` ESEC prefix).
3. **Instantiate Components (Place and Wire the Chips):** After `BEGIN`, create uniquely named instances (`u0`, `u1`, etc.) and wire their pins using **named port mapping** (`PORT MAP`).

Notice below how each component has its own distinct port names (`i_in1`, `i_in2`, `o_xnor_out` on `xnor2`; `i_in1`–`i_in4`, `o_and_out` on `and4`), which makes it easy to distinguish the *component's pins* from the *`compare` entity's signals* (`i_bus_a`, `i_bus_b`, `c_bit_match`, `o_equals`):

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY compare IS
    PORT (
        i_bus_a, i_bus_b : IN  std_logic_vector(3 DOWNTO 0);
        o_equals         : OUT std_logic
    );
END compare;

ARCHITECTURE structure OF compare IS

    -- Step 1: Declare components ("stocking the inventory")
    COMPONENT xnor2 IS
        PORT (
            i_in1      : IN  std_logic;
            i_in2      : IN  std_logic;
            o_xnor_out : OUT std_logic
        );
    END COMPONENT xnor2;

    COMPONENT and4 IS
        PORT (
            i_in1     : IN  std_logic;
            i_in2     : IN  std_logic;
            i_in3     : IN  std_logic;
            i_in4     : IN  std_logic;
            o_and_out : OUT std_logic
        );
    END COMPONENT and4;

    -- Step 2: Declare internal wiring signals
    SIGNAL w_bit_match : std_logic_vector(3 DOWNTO 0);

BEGIN

    -- Step 3: Instantiate components and wire them using named port mapping
    u0 : xnor2
        PORT MAP (
            i_in1      => i_bus_a(0),
            i_in2      => i_bus_b(0),
            o_xnor_out => w_bit_match(0)
        );

    u1 : xnor2
        PORT MAP (
            i_in1      => i_bus_a(1),
            i_in2      => i_bus_b(1),
            o_xnor_out => w_bit_match(1)
        );

    u2 : xnor2
        PORT MAP (
            i_in1      => i_bus_a(2),
            i_in2      => i_bus_b(2),
            o_xnor_out => w_bit_match(2)
        );

    u3 : xnor2
        PORT MAP (
            i_in1      => i_bus_a(3),
            i_in2      => i_bus_b(3),
            o_xnor_out => w_bit_match(3)
        );

    u4 : and4
        PORT MAP (
            i_in1     => w_bit_match(0),
            i_in2     => w_bit_match(1),
            i_in3     => w_bit_match(2),
            i_in4     => w_bit_match(3),
            o_and_out => o_equals
        );

END structure;
```

**Understanding Named Port Mapping (`=>`):**
One of the most common mistakes beginners make in structural VHDL is thinking the `=>` symbol shows which way data flows. **It does not!** The `=>` symbol is the **association operator**—always read it simply as **"is wired to,"** following this fixed rule:

```text
   pin_on_the_component   =>   signal_in_this_architecture
        (LEFT side)                     (RIGHT side)
```

Let's trace two lines from `u0` (`xnor2`) to see why the direction of data does not change the order:
* **`i_in1 => i_bus_a(0)`** *(an input to `xnor2`)*: The `xnor2` component's `i_in1` pin (left) **is wired to** bit 0 of `compare`'s `i_bus_a` input bus (right). Even though data flows *into* `i_in1`, the component's pin still goes on the **left**.
* **`o_xnor_out => w_bit_match(0)`** *(an output from `xnor2`)*: The `xnor2` component's `o_xnor_out` pin (left) **is wired to** our internal wire `w_bit_match(0)` (right).
* Then, in **`u4`** (`and4`), **`i_in1 => w_bit_match(0)`** wires that exact same internal wire `w_bit_match(0)` to the `and4` component's `i_in1` pin—completing the physical connection shown in the schematic!

### Comparing the Three Styles

All three architectures above describe the **exact same 4-bit comparator**. In fact, a single VHDL architecture can even mix styles (for example, combining dataflow equations with structural component instances).

| Style | Best Used For | Key Syntax |
| :--- | :--- | :--- |
| **Dataflow** | Boolean equations and combinational logic | `<=`, `AND`, `OR`, `XNOR`, `NOT` |
| **Behavioral** | High-level, algorithmic decision-making | `PROCESS (...)`, `IF ... THEN ... ELSE` |
| **Structural** | Connecting existing modules into a larger system | `COMPONENT`, `SIGNAL`, `PORT MAP (... => ...)` |

### Worked Example 3: Writing a Dataflow Architecture

**Prompt:** Write a Dataflow architecture named `dataflow` for the `half_adder` entity you saw in Worked Example 2.
*Digital Logic Refresher:* In binary addition of two 1-bit numbers `i_a` and `i_b`:
* The sum bit is `1` when `i_a` and `i_b` are different (`i_a XOR i_b`).
* The carry-out bit is `1` only when both `i_a` and `i_b` are `1` (`i_a AND i_b`).

#### Solution Walkthrough

1. **Header:** Match the entity name: `ARCHITECTURE dataflow OF half_adder IS`.
2. **Internal signals:** None are needed since both outputs can be driven directly from the inputs.
3. **Concurrent equations:** After `BEGIN`, write the two Boolean equations using `<=`. Remember that because VHDL is concurrent, the order of these two lines does not matter.

```vhdl
ARCHITECTURE dataflow OF half_adder IS
BEGIN
    o_sum       <= i_a XOR i_b;
    o_carry_out <= i_a AND i_b;
END dataflow;
```

### Worked Example 4: Completing a Structural Architecture

**Prompt:** A **Full Adder** adds three 1-bit inputs (`i_bit_a`, `i_bit_b`, and `i_carry_in`) to produce `o_full_sum` and `o_carry_out`. It can be built structurally from two `half_adder` components and one 2-input `or2` gate:
* `u0` (`half_adder`) adds `i_bit_a` and `i_bit_b`, outputting partial sum `w_partial_sum` and partial carry `w_carry_1`.
* `u1` (`half_adder`) adds `w_partial_sum` and `i_carry_in`, outputting the final `o_full_sum` and partial carry `w_carry_2`.
* `u2` (`or2`) ORs `w_carry_1` and `w_carry_2` to produce `o_carry_out`.

Complete the missing parts of the structural architecture below (the `or2` component declaration, the internal signals, and the `u1` and `u2` instantiations).

```vhdl
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY full_adder IS
    PORT (
        i_bit_a, i_bit_b, i_carry_in : IN  std_logic;
        o_full_sum, o_carry_out      : OUT std_logic
    );
END full_adder;

ARCHITECTURE structure OF full_adder IS

    COMPONENT half_adder IS
        PORT (
            i_a, i_b    : IN  std_logic;
            o_sum       : OUT std_logic;
            o_carry_out : OUT std_logic
        );
    END COMPONENT half_adder;

    -- TODO 1: Declare the or2 component (ports: i_in1, i_in2, o_or_out)

    -- TODO 2: Declare internal wiring signals w_partial_sum, w_carry_1, and w_carry_2

BEGIN

    u0 : half_adder
        PORT MAP (
            i_a         => i_bit_a,
            i_b         => i_bit_b,
            o_sum       => w_partial_sum,
            o_carry_out => w_carry_1
        );

    -- TODO 3: Instantiate u1 (half_adder) and u2 (or2)

END structure;
```

#### Solution Walkthrough

1. **TODO 1 (Component Declaration):** Declare `or2` component with:
   * `i_in1, i_in2 : IN std_logic;`
   * `o_or_out : OUT std_logic` (no trailing semicolon).
2. **TODO 2 (Internal Signals):** Use `SIGNAL` keyword to declare for following `std_logic` internal signals:
   * `w_partial_sum`
   * `w_carry_1`
   * `w_carry_2`
3. **TODO 3 (Instantiations):** Instantiate the following components, remembering to always keep the **component's port on the left** of `=>` and the **`full_adder` signal/port on the right**:
   * For `u1`, map `half_adder`'s:
      * `i_a` to `w_partial_sum`
      * `i_b` to `i_carry_in`
      * `o_sum` to `o_full_sum`
      * `o_carry_out` to `w_carry_2`.
   * For `u2`, map `or2`'s:
      * `i_in1` to `w_carry_1`
      * `i_in2` to `w_carry_2`
      * `o_or_out` to `o_carry_out`.

```vhdl
ARCHITECTURE structure OF full_adder IS

    COMPONENT half_adder IS
        PORT (
            i_a, i_b    : IN  std_logic;
            o_sum       : OUT std_logic;
            o_carry_out : OUT std_logic
        );
    END COMPONENT half_adder;

    COMPONENT or2 IS
        PORT (
            i_in1, i_in2 : IN  std_logic;
            o_or_out     : OUT std_logic
        );
    END COMPONENT or2;

    SIGNAL w_partial_sum, w_carry_1, w_carry_2 : std_logic;

BEGIN

    u0 : half_adder
        PORT MAP (
            i_a         => i_bit_a,
            i_b         => i_bit_b,
            o_sum       => w_partial_sum,
            o_carry_out => w_carry_1
        );

    u1 : half_adder
        PORT MAP (
            i_a         => w_partial_sum,
            i_b         => i_carry_in,
            o_sum       => o_full_sum,
            o_carry_out => w_carry_2
        );

    u2 : or2
        PORT MAP (
            i_in1    => w_carry_1,
            i_in2    => w_carry_2,
            o_or_out => o_carry_out
        );

END structure;
```

### Checkpoint 3
*(Check your work against the Checkpoint Answers at the end of the chapter.)*

1. In VHDL, what is the difference between `=`, `<=`, and `=>`?
2. Look at the Behavioral architecture of `compare`. Why are `i_bus_a` and `i_bus_b` listed in `PROCESS (i_bus_a, i_bus_b)`?
3. In a Structural architecture, why is it an error to instantiate a component (like `u0 : xnor2 PORT MAP (...)`) without first writing a `COMPONENT xnor2 ... END COMPONENT;` declaration?

---

## 5. Designing Big Systems: Abstraction and Hierarchy

You just saw how two Half Adders and an OR gate can be wired together to create a Full Adder. Now imagine trying to design an entire computer processor—containing millions of logic gates—by writing every single gate in one massive file. It would be impossible to read, test, or debug.

Digital engineers tame this complexity using two foundational concepts:

### 1. Abstraction

Once an entity/architecture pair (like `full_adder`) has been designed and thoroughly tested, you **no longer need to think about the gates inside it**. You can treat it as a trusted "black box" and think of it strictly from the *"what do these pins do?"* point of view. That is **abstraction**.

### 2. Hierarchy

**Hierarchy** means building complex systems in layers out of increasingly complex, abstracted entities. Instead of jumping straight from basic gates to a 32-bit adder, you build step by step:

```text
  Level 1 (Gate Level):      XOR, AND, OR gates
                                   │
                                   ▼
  Level 2 (Half Adder):      Built from 1 XOR gate + 1 AND gate
                                   │
                                   ▼
  Level 3 (Full Adder):      Built from 2 Half Adders + 1 OR gate
                                   │
                                   ▼
  Level 4 (32-bit Adder):    Built from 32 Full Adders chained together
```

Notice how VHDL's **Structural** architecture directly supports both principles: you declare a tested lower-level entity as a `COMPONENT` (relying on **abstraction** so you only care about its pins), and then instantiate multiple copies of it inside a higher-level architecture (building **hierarchy**).

### Checkpoint 4
*(Check your work against the Checkpoint Answers at the end of the chapter.)*

1. Once you have built and verified your `full_adder` entity/architecture pair, how does **abstraction** help you when you move on to building a 32-bit adder?
2. Give an example of **hierarchy** from the `compare` circuit we built earlier in this chapter.

---

## Chapter Summary & Quick Reference

* **Concurrent Hardware:** VHDL describes physical digital circuits that run simultaneously on an FPGA, not sequential software instructions.
* **Entity/Architecture Pair:** Every VHDL module has an **Entity** (the external pins/ports of the IC chip) and an **Architecture** (the internal structure/behavior inside that chip).
* **Libraries & Types:** Include `LIBRARY ieee; USE ieee.std_logic_1164.ALL;` to use `std_logic` (1-bit wire, single quotes `'0'`/`'1'`) and `std_logic_vector` (multi-bit bus, double quotes `"..."`, typically `DOWNTO`, indexed with parentheses like `i_bus_a(0)`).
* **Port Modes:** `IN` (read only), `OUT` (assign only, cannot read internally), `INOUT` (bidirectional—can both read and assign), and `BUFFER` (output that can also be read internally—though using an internal `SIGNAL` assigned to an `OUT` port is preferred).
* **Three Architecture Styles:**
  * **Dataflow:** Concurrent Boolean equations (and conditional assignments).
  * **Behavioral:** Algorithmic descriptions inside a `PROCESS` block.
  * **Structural:** Explicit `COMPONENT` declarations, internal `SIGNAL` wires, and uniquely **Named Instances** connected via named `PORT MAP` (`component_pin => signal_in_this_architecture`).
* **Abstraction & Hierarchy:** Hide tested internal details behind an entity's pins, and assemble small building blocks into complex systems.

---

## Checkpoint Answers

### Checkpoint 1 Answers
1. VHDL is a hardware description language, not a software programming language. When synthesized for an FPGA, VHDL creates physical logic gates and wires that all operate **concurrently** (at the same time), rather than instructions executed one by one on a CPU.
2. **No.** Because dataflow statements represent independent physical circuits operating concurrently, the order in which they are written in the file does not change the hardware created.

### Checkpoint 2 Answers
1. **`i_reset_n`** is **legal**. The illegal ones are:
   * `4_bit_adder`: Starts with a digit (must start with a letter).
   * `o_sum_`: Ends with an underscore.
   * `w_bus__a`: Contains two consecutive underscores.
   * `out`: Reserved VHDL keyword.
2. **`o_leds(2) <= '1';`** You must use **single quotes** (`'1'`) because `o_leds(2)` selects a single wire (`std_logic`) from the 4-bit bus.
3. **`BUFFER`** technically allows data to go out of the entity and be read back internally. However, the recommended **best practice** is to declare an internal `SIGNAL`, read and drive it inside the architecture, and assign it to a standard `OUT` port.
4. Error 1: There is an extra semicolon after `o_result : OUT std_logic;` (the last port before `);` must not have a semicolon). Error 2: The closing line says `END gate;`, which does not match the entity name `my_gate` (it should be `END my_gate;`).

### Checkpoint 3 Answers
1. **`=`** tests for equality (like `==` in many software languages). **`<=`** is the signal assignment operator (drives a value onto a wire/signal). **`=>`** is the port mapping association operator (wires a component's pin on the left to a local signal on the right).
2. `(i_bus_a, i_bus_b)` is the **sensitivity list**. It tells VHDL that the logic inside the process depends on `i_bus_a` and `i_bus_b`, so the process should re-evaluate whenever either input changes.
3. Declaring a `COMPONENT` puts that building block into your architecture's "parts inventory." Without the declaration, VHDL does not know what pins the `xnor2` chip has when you try to instantiate and wire it.

### Checkpoint 4 Answers
1. Because `full_adder` is already designed and tested, abstraction lets you ignore its internal Half Adders and OR gate and think of it purely as a black box with three input pins (`i_bit_a`, `i_bit_b`, `i_carry_in`) and two output pins (`o_full_sum`, `o_carry_out`).
2. In the structural `compare` circuit, the higher-level 4-bit `compare` entity is built hierarchically out of lower-level `xnor2` and `and4` gate components.