IAE Academy

FOR CORE AND IT JOBS · CHAPTER 6 OF 7

Describing hardware with Verilog

Verilog describes circuits, not programs: every block runs at once, like the hardware it names. This chapter shows how to write combinational logic and registers, why = and <= differ, and what synthesis does with your code.

Updated 30 Oct 2026

What you will be able to do

You will read a Verilog module and say which gates and flip-flops it becomes. You will choose between = and <= from one rule and predict the result of a swap. You will write a 4-bit counter, a two-state machine and a testbench. You will spot code that simulates but cannot be built.

Before you start

  1. Chapter 4, Latches, flip-flops and timing

  2. Boolean algebra: AND, OR, NOT and XOR

  3. Binary counting and remainders (modulo arithmetic)

  4. Basic programming: variables, assignment and if-else

Modules and combinational logic

A program runs one line after another. A circuit does not: every gate works all the time. Verilog is written to match the circuit. A module is a box with ports (input and output pins) and, inside it, wires that connect its parts. Every assign line and every always block in a module is a separate piece of hardware, all active at once, so the order in which you write them does not change the circuit. A synthesis tool reads a module and builds the gates and flip-flops it describes.

An assign line is a continuous assignment: its left side follows its right side at all times, as a gate output follows its inputs. A 2:1 multiplexer passes when the select is 0 and when is 1. In Verilog, ~ is NOT, & is AND and | is OR.

module mux2 (input a, input c, input s, output y); // ports
  wire ns;                         // internal wire
  assign ns = ~s;                  // a NOT gate
  assign y  = (ns & a) | (s & c);  // y follows a, c and s
endmodule

CONTINUOUS ASSIGNMENT IS A BOOLEAN EQUATION

Holds at all times, ignoring gate delay. is the select, and are the data inputs, is the output, each 0 or 1.

For longer logic, use a procedural block. always @(*) means: re-run this block whenever any signal it reads changes. A signal assigned inside any always block must be declared reg, a Verilog rule that says nothing about flip-flops. Inside always @(*), assign with =. Every path through the block must assign the output; if one does not, the tools keep the old value and build a latch you did not intend.

reg y2;                          // inside mux2; assigned in a block
always @(*) begin                // combinational: no clock
  if (s) y2 = c;                 // both branches assign y2
  else   y2 = a;
end

WATCH OUT FOR

reg does not mean register. Test: is the block triggered by a clock edge, as in always @(posedge clk)? Only a block triggered by an edge (posedge or negedge) makes flip-flops. An always @(*) block makes gates, or the latch above.

Registers, and = against <=

A flip-flop copies its input D to its output Q on the clock edge and holds it otherwise (Chapter 4). In Verilog that is always @(posedge clk). All flip-flops on one clock sample D at the same edge, and their Q outputs change a clock-to-Q delay later. So, when the hold constraint of Chapter 4 is met, one flip-flop's new Q cannot reach another flip-flop at the same edge. The non-blocking assignment <= copies this: all right-hand sides are read first, then all left-hand sides update.

REGISTER UPDATE AT A CLOCK EDGE

is the value of just after the instant considered, the value just before the edge. Written always @(posedge clk) q <= d;.

The classic test is a swap of two registers and that hold 3 and 5 before the edge. The two lines below are alternatives; a design uses one.

always @(posedge clk) begin a <= b; b <= a; end  // swap
always @(posedge clk) begin a = b;  b = a;  end  // not a swap

With <= both right-hand sides use the old values. With = each line takes effect at once, so the second line reads the new .

SWAP WITH NON-BLOCKING ASSIGNMENT

Both right-hand sides are the values before the edge. and are the two registers.

SWAP ATTEMPTED WITH BLOCKING ASSIGNMENT

The second line reads after the first line has changed it, so the old value of is lost. Synthesis builds the same result: takes and keeps its value.

  1. Clocked block (always @(posedge clk)): use <= for every register.

  2. Combinational block (always @(*)): use =.

  3. Do not mix = and <= in one block.

  4. Why: <= models flip-flops that sample together. = in a combinational block describes a chain of logic: a value set on one line feeds the logic of the next, so line order inside a block matters. The gates it builds still all work at once.

WATCH OUT FOR

Code with = in a clocked block can simulate without any error. Test: work out the result once with every line reading the values before the edge, and once line by line. If the two differ, the code depends on line order and needs <=.

WORKED EXAMPLE

A 4-bit register q holds 14 and rst stays 0. The counter is written as q <= q + 1 in an always @(posedge clk) block. What does q hold after three rising edges?

Solution
  1. Each edge reads q before the edge and writes q + 1 after it, because <= samples first.

  2. Edge 1: 14 + 1 = 15, which still fits in 4 bits (maximum 15).

  3. Edge 2: 15 + 1 = 16, which needs 5 bits. Four bits keep only the remainder after dividing by 16, so 16 becomes 0.

  4. Edge 3: 0 + 1 = 1.

Answer: q = 1 (the count wraps from 15 to 0).

Counter, state machine, testbench and synthesis

A 4-bit counter with a synchronous reset: the reset acts only at a clock edge, so it is just another input to the logic in front of the flip-flops. [3:0] declares 4 bits, numbered 3 down to 0. A number such as 4'd1 gives its width in bits, a base (d decimal, b binary) and its value; so 1'b0 is the 1-bit binary 0.

module counter4 (input clk, input rst, output reg [3:0] q);
  always @(posedge clk) begin
    if (rst) q <= 4'd0;         // checked only at the edge
    else     q <= q + 4'd1;     // wraps from 15 to 0
  end
endmodule

COUNTER NEXT STATE

Evaluated at each rising edge. is the 4-bit count (0 to 15). The is the wrap that keeping only 4 bits of performs by itself.

A finite-state machine stores its state in flip-flops. At each edge the state takes the next state, which logic computes from the present state and the inputs. This two-state machine toggles when input is 1 and holds when is 0. One clocked block holds the state register, one always @(*) block the next-state logic, and an assign gives the output.

module fsm2 (input clk, input rst, input x, output z);
  reg state, next_state;         // state is 0 or 1
  always @(posedge clk)          // state register
    if (rst) state <= 1'b0; else state <= next_state;
  always @(*)                    // next-state logic
    if (x) next_state = ~state; else next_state = state;
  assign z = state;              // output depends on state only
endmodule

TWO-STATE MACHINE NEXT STATE

At each rising edge with ( gives ). is the state bit (0 or 1), the input, is XOR: flips when and stays when . is the output.

A testbench is a module, usually with no ports, that drives your design and runs only in simulation. It may use delays (#) and initial blocks, which describe time. Synthesis never reads it.

`timescale 1ns/1ps               // # delays count in ns
module tb;
  reg clk = 0, rst = 1;  wire [3:0] q;
  counter4 dut (.clk(clk), .rst(rst), .q(q)); // port clk to signal clk, and so on
  always #5 clk = ~clk;          // toggles every 5 ns: period 10 ns
  initial begin                  // runs once, from time 0
    $monitor("t=%0t q=%0d", $time, q); // %0t: time, %0d: q in decimal; prints when q changes
    #12 rst = 0;                 // release reset between edges
    #200 $finish;                // stop the simulation
  end
endmodule

Code

What synthesis builds

assign, always @(*)

Gates (a latch if a path leaves an output unassigned)

always @(posedge clk) with <=

Flip-flops for the bits assigned, with logic at each D input

if (rst) inside a clocked block

Extra gating at D (synchronous reset)

# delay, such as #5

Ignored: delay comes from the real gates

initial block

Simulation only (some FPGA tools read an initial value as a register's power-up value)

CLOCK PERIOD OF THE COUNTER

Setup constraint of Chapter 4 with skew taken as zero. is the clock period, the clock-to-Q delay, the setup time and the longest delay of the incrementer and reset gating between the counter's flip-flops. All times in ns.

WORKED EXAMPLE

For counter4 the synthesised flip-flops have t_clk-q = 0.3 ns and t_setup = 0.1 ns. The incrementer plus reset gating has a longest delay of 0.6 ns. Skew is zero. What is the highest clock frequency?

Solution
  1. The longest path goes from a counter flip-flop, through the incrementer and reset gating, back to a counter flip-flop D input.

  2. Add the three delays: 0.3 + 0.6 + 0.1 = 1.0 ns, so T_clk >= 1.0 ns.

  3. Frequency is the reciprocal: 1 / 1.0 ns = 1 GHz.

Answer: T_clk = 1.0 ns at least, so at most 1 GHz.

Check yourself

CHECK YOURSELF 1One or more options are correct

Which statements are correct?

  1. (A)

    always @(*) is used for combinational logic.

  2. (B)

    A #5 delay in code makes the synthesised gate 5 ns slower.

  3. (C)

    Registers in an always @(posedge clk) block are normally assigned with <=.

  4. (D)

    Code in an initial block that uses # delays is turned into logic by synthesis.

Answer and solution

Answer: (A), (C)

  1. A is true: the block re-runs on any input change, like gates.

  2. B is false: synthesis ignores delays; real gates set the timing.

  3. C is true: <= makes all flip-flops sample the old values.

  4. D is false: delays and initial blocks are simulation only.

CHECK YOURSELF 2

A 4-bit counter with synchronous reset (q <= q + 1 when rst = 0) is cleared by rst = 1 at one rising edge, then rst = 0 for the next 20 rising edges. What decimal value does q hold after those 20 edges?

Answer and solution

Answer:

  1. After the reset edge q = 0.

  2. Each later edge adds 1, wrapping after 15: q = 20 mod 16.

  3. 20 - 16 = 4.

CHECK YOURSELF 3

A register-to-register path has t_clk-q = 0.2 ns, longest logic delay 0.9 ns and t_setup = 0.1 ns. Skew is zero. What is the highest clock frequency in MHz?

Answer and solution

Answer: (accepted range )

  1. T_clk >= 0.2 + 0.9 + 0.1 = 1.2 ns.

  2. f = 1 / 1.2 ns = 833.3 MHz.

CHECK YOURSELF 4

Inside always @(posedge clk) the code is: a = in; b = a; (blocking). Before a rising edge a = 0 and b = 0, and in = 1, held steady around the edge. What value does b hold after the edge?

  1. (A)

    0, the old a

  2. (B)

    0, because b copies a one edge late

  3. (C)

    1, the new value of in

  4. (D)

    Undefined

Answer and solution

Answer: (C)

  1. With =, a takes in (1) at once.

  2. The next line then reads that new a, so b = 1 on the same edge.

  3. Both end up equal; with <= b would have received the old a, 0.

Used in these GATE topics