Chuyển đến nội dung

Bài 3 · Getting Started

Your First Verilog Module: A Half Adder

Write, read and test your first Verilog design: a half adder module with ports, assign statements, a small testbench, and a C stand-in that checks the truth table.

A half adder adds two single bits and produces a sum and a carry. It is the smallest useful arithmetic circuit, and it is a perfect first Verilog module because it needs only two logic operators. In this lesson you read the module line by line, write a testbench for it, and check the expected results with a runnable C stand-in.

The truth table

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

The sum is a XOR b; the carry is a AND b. Two gates, no clock, no memory: this is combinational logic, where outputs depend only on current inputs.

flowchart LR
  a((a)) --> X["XOR"]
  b((b)) --> X
  a --> A["AND"]
  b --> A
  X --> s((sum))
  A --> c((carry))

The module

VERILOG
module half_adder (
    input  wire a,
    input  wire b,
    output wire sum,
    output wire carry
);
    assign sum   = a ^ b;
    assign carry = a & b;
endmodule

Read it from the top:

  • module half_adder (...) starts a module named half_adder. A module is a reusable block of hardware.
  • The parenthesized list declares ports. input wire a is a one-bit input; output wire sum is a one-bit output.
  • assign sum = a ^ b; is a continuous assignment: sum is permanently driven by a ^ b, like a gate.
  • endmodule closes the module. Notice there is no semicolon after it.

The comments // ... run to the end of the line, and /* ... */ can span lines.

A testbench

A design alone does nothing in a simulator, because nobody drives a and b. A testbench is a module with no ports that instantiates your design (the device under test, DUT), applies inputs, and prints or checks the outputs.

VERILOG
`timescale 1ns/1ps

module half_adder_tb;
    reg  a, b;
    wire sum, carry;

    half_adder dut (.a(a), .b(b), .sum(sum), .carry(carry));

    initial begin
        $display(" a b | sum carry");
        for (integer i = 0; i < 4; i = i + 1) begin
            {a, b} = i[1:0];
            #10;
            $display(" %b %b |  %b    %b", a, b, sum, carry);
        end
        $finish;
    end
endmodule

New pieces appear here, all explained properly later:

  • `timescale 1ns/1ps sets the time unit (1 ns) and precision (1 ps) for delays such as #10.
  • reg a, b; are variables the testbench can assign inside a procedural block. wire carries the DUT's outputs.
  • half_adder dut (.a(a), ...) creates an instance with named port connections.
  • initial begin ... end runs once at time 0. {a, b} = i[1:0]; packs two bits into the inputs.
  • #10; waits 10 ns so the combinational logic settles. $display prints; $finish stops the simulation.

When run in Icarus Verilog, the output would be the four rows of the truth table above. I have not run it on this site, so rely on the table.

Stand-in: the same circuit in C

This program is not Verilog. It models the circuit with the C bit operators ^ and &, which mean the same thing as in Verilog, and loops through all four input combinations. It also adds a one-line check that sum plus twice the carry equals the arithmetic sum.

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

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.

From half adder to full adder

A half adder cannot accept a carry from a previous column. A full adder has a third input cin. Here is one built with the operators you already know:

VERILOG
module full_adder (
    input  wire a, b, cin,
    output wire sum, cout
);
    assign sum  = a ^ b ^ cin;
    assign cout = (a & b) | (cin & (a ^ b));
endmodule

You can build one from two half adders and an OR gate. Chaining full adders gives a multi-bit adder, which you will build in the lesson on adders.

C Stand-in: full adder exhaustive check (C)

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.

Exhaustive testing is feasible here because three inputs give only 8 cases. The same habit pays off in testbenches.

Common beginner mistakes

  • Forgetting the semicolon after the port list's closing parenthesis ();) or putting one after endmodule.
  • Declaring an output wire and assigning it inside an always block (that needs reg; see wire and reg).
  • Naming an instance connection by position and swapping the order. Prefer named connections (.a(a)).
  • Giving the testbench ports; the top-level testbench has none.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does assign carry = a & b; create?

    A continuous assignment is permanent combinational hardware.
  2. 2. What is the sum output of a half adder for a=1, b=1?

    1 XOR 1 is 0; the carry output is 1 (1 + 1 = binary 10).
  3. 3. Why does a testbench instantiate the design?

    The testbench is the device-under-test's environment: it supplies stimulus and checks responses.
  4. 4. Which is correct for the end of a module?

    The keyword is endmodule, with no semicolon.

Summary

  • A half adder is two gates: sum = a ^ b, carry = a & b.
  • A module has a name, ports and a body; assign makes continuous, concurrent combinational logic.
  • A testbench has no ports; it instantiates the design, drives inputs with reg, and prints with $display.
  • A full adder adds a carry-in, and exhaustive truth-table checks are cheap for small circuits.

Next: Verilog Syntax Basics

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ị.