Introduction to Verilog

Drawing schematics works for a few gates, but real designs contain thousands of them. A hardware description language (HDL) lets you describe a circuit as text, simulate it, and then synthesize it into gates inside a CPLD, an FPGA or a chip. Verilog is one of the two most widely used HDLs, along with VHDL. This lesson covers the small core of Verilog that you need to read and write simple designs.

Learning objectives

By the end of this lesson you will be able to:

  • Write a Verilog module with input and output ports.
  • Read and write number literals such as 4'b1010 and 8'hA5.
  • Use continuous assignments, operators and the conditional operator.
  • Describe a flip-flop with an always block.
  • Explain how Verilog differs from a normal programming language.

Verilog describes hardware, not a program

In a software language, statements run one after another. In Verilog, a design is a set of hardware pieces that all exist and operate at the same time. Two assign statements are two circuits, not two steps. Keeping this picture in mind avoids most beginner confusion.

Verilog code is used in two ways. Simulation runs the text with a test bench to check behaviour. Synthesis turns the text into real logic. Only a subset of Verilog can be synthesized, and this lesson stays inside that subset.

Modules and ports

The basic building block is the module. It has a name, a list of ports and a body.

module mux2 (
    input  a,
    input  b,
    input  s,
    output y
);
    assign y = s ? b : a;
endmodule

Each port has a direction: input, output or inout. A width is given in square brackets, so input [3:0] a is a 4-bit bus with bits a[3] down to a[0].

Data types: wire and reg

  • A wire represents a physical connection. It is driven by an assign statement or by another module's output.
  • A reg is a variable that holds a value inside an always block. Despite the name, a reg does not always become a flip-flop. It becomes one only when the block is clocked.

In mux2, the output y is a wire by default because it is driven by assign.

Number literals

Verilog numbers have the form <size>'<base><digits>.

Literal Meaning Decimal value
4'b1010 4 bits, binary 10
6'b101101 6 bits, binary 45
8'hFF 8 bits, hexadecimal 255
8'hA5 8 bits, hexadecimal 165
4'd9 4 bits, decimal 9

The hex digits of 8'hA5 are A = 1010 and 5 = 0101, so the binary form is 10100101. Use the number converter to check conversions like this.

Operators

Type Operators Example
Bitwise ~ & | ^ a & b
Reduction & | ^ applied to one vector &a is 1 only if every bit of a is 1
Arithmetic + - * a + b
Comparison == != < > a == b
Conditional ? : s ? b : a
Concatenation { } {cout, sum}

Reduction operators collapse a vector to one bit. For a = 4'b1011: &a is 0, |a is 1, and ^a is 1, because a has three ones and an odd count gives parity 1.

Worked example 1: A 2-to-1 multiplexer

The module mux2 above uses the conditional operator. If s is 1 the output is b, otherwise it is a. As a Boolean expression this is y = s'a + sb. When s = 0, y = a; when s = 1, y = b. The structure is one NOT gate, two AND gates and one OR gate, or one LUT in an FPGA.

Worked example 2: A 4-bit adder with carry

module add4 (
    input  [3:0] a,
    input  [3:0] b,
    output [3:0] sum,
    output       cout
);
    assign {cout, sum} = a + b;
endmodule

Adding two 4-bit numbers can produce a 5-bit result. The concatenation {cout, sum} is a 5-bit target: cout takes the top bit and sum takes the lower four.

Trace a = 4'b1011 (11) and b = 4'b0110 (6):

  • 11 + 6 = 17.
  • 17 in binary is 10001.
  • So cout = 1 and sum = 0001.

If you assign the result to only a 4-bit target, the carry is silently lost, which is a classic bug.

Worked example 3: A D flip-flop with reset

Sequential logic uses an always block triggered by a clock edge.

module dff (
    input      clk,
    input      rst_n,
    input      d,
    output reg q
);
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n)
            q <= 1'b0;
        else
            q <= d;
    end
endmodule

How it behaves:

  1. When rst_n goes low, q becomes 0 immediately. This is an asynchronous active-low reset.
  2. Otherwise, on every rising edge of clk, q takes the value of d.
  3. At all other times q holds its value, which is what makes it a flip-flop.

The <= symbol is a non-blocking assignment. Use it for all clocked logic. Use = only for combinational always blocks, which you will study in the next lessons.

A minimal test bench

A test bench is a module with no ports that drives the design and watches the result.

module tb;
    reg  [3:0] a, b;
    wire [3:0] sum;
    wire       cout;
    add4 dut (.a(a), .b(b), .sum(sum), .cout(cout));
    initial begin
        a = 4'd11; b = 4'd6;
        #10 $display("sum=%b cout=%b", sum, cout);
    end
endmodule

The initial block runs once, #10 waits ten time units, and $display prints the values. Test benches are for simulation only and are never synthesized.

Common mistakes

  • Thinking assign statements run in order. They all operate in parallel, so their order in the file does not matter.
  • Mismatched widths. Connecting a 5-bit result to a 4-bit signal drops the top bit without an error.
  • Using = in a clocked block. Use <= for flip-flops so that all registers update together.
  • Forgetting endmodule or a semicolon. Missing punctuation causes confusing errors on the following line.
  • Confusing & and &&. & is a bitwise AND, while && is a logical AND that returns a single bit.

Practice problems

Problem 1. What is the decimal value of 6'b101101?

Answer: 32 + 8 + 4 + 1 = 45.

Problem 2. Write 8'h3C in binary.

Answer: 8'b00111100, since 3 is 0011 and C is 1100.

Problem 3. How many bits does the result of adding two 8-bit numbers need so that no carry is lost?

Answer: 9 bits. The largest sum is 255 + 255 = 510, which needs 9 bits.

Problem 4. For a = 4'b1010, what are ~a and ^a?

Answer: ~a = 4'b0101, and ^a = 0 because a contains two ones, an even count.

Frequently asked questions

Is Verilog a programming language? It is a hardware description language. It looks like code, but it describes circuits that operate in parallel rather than instructions that run in sequence.

Should I learn Verilog or VHDL first? Either works, and the concepts transfer. Verilog has a shorter, C-like syntax that many beginners find easier, while VHDL is stricter about types.

Can all Verilog code be turned into hardware? No. Constructs such as delays and $display exist for simulation only. Synthesis tools accept a defined subset, which is what you write for real designs.

Next lesson: continue with Behavioral vs Structural Modeling to see different ways of describing the same circuit.