Thinking in Hardware
Engineers with software programming backgrounds frequently mistake Verilog for a procedural programming language.
Verilog shares syntactic elements with C, such as if, else, case, and semicolons. However, software programming and hardware modeling operate on fundamentally different principles.
Software executes sequential instructions on a microprocessor. Verilog describes physical electronic circuits: logic gates, signal wires, and flip-flops operating concurrently.
Principle 1: Concurrency (All Statements Operate Simultaneously)
In a software language, statement execution is sequential. In digital hardware, all logic gates evaluate input signals continuously and simultaneously.
`timescale 1ns / 1ps
`default_nettype none
module parallel_example (
input wire a,
input wire b,
input wire c,
output wire y1,
output wire y2,
output wire y3
);
// Three independent logic gates operating concurrently
assign y1 = a & b;
assign y2 = b | c;
assign y3 = a ^ c;
endmodule
When input signal a transitions, output nets y1 and y3 update concurrently. Continuous assignments do not execute in textual sequence. Writing the statements in different orders describes identical physical hardware.
Principle 2: Description versus Instruction
The statement assign y = a & b; is not a procedural instruction to execute an operation.
It is a declarative netlist description: an AND gate exists permanently, its inputs connect to wires a and b, and its output drives wire y. The gate operates continuously while power is applied.
The same declarative principle applies to procedural blocks:
always @(posedge clk) begin
q <= d; // Describes an edge-triggered D flip-flop
end
The synthesizer infers an edge-triggered D flip-flop. The sensitivity @(posedge clk) specifies the clock input of the physical register, not an interrupt callback.
Principle 3: Physical Clock Timing and Static Analysis
In software systems, execution speed depends on microprocessor instruction cycles. In digital hardware, timing depends on physical propagation delays between flip-flops:
`timescale 1ns / 1ps
`default_nettype none
module timing_example (
input wire clk, // 100 MHz clock (10 ns clock period)
input wire [7:0] data_in,
output reg [7:0] data_out
);
wire [7:0] processed;
// Combinational logic path: Must settle within the 10 ns clock period
assign processed = data_in + 8'd1;
// Edge-triggered sampling register
always @(posedge clk) begin
data_out <= processed;
end
endmodule
The propagation delay through combinational logic must settle before the setup time window of the destination flip-flop. Meeting this requirement is called timing closure.
Principle 4: Signals Are Physical Connections, Not Memory Variables
In software, a variable represents a memory location that programs overwrite sequentially.
In Verilog, a wire represents an electrical conductor with a single driver. Connecting multiple active drivers to a wire produces electrical contention ($X$ in simulation, physical short-circuits in silicon).
The keyword reg denotes a signal driven from a procedural block. It does not necessarily infer a physical storage register:
- A
regassigned in analways @(posedge clk)block synthesizes into an edge-triggered D flip-flop. - A
regassigned in a combinationalalways @*block synthesizes into pure combinational logic without storage elements.
Engineers must examine the procedural sensitivity list and assignment statements to determine the resulting hardware structure.
Principle 5: Synthesis Optimizes Boolean Functionality
A logic synthesizer optimizes physical gate structures rather than preserving line-by-line textual structure.
The tool extracts Boolean truth tables and minimizes the network to meet timing, area, and power constraints. Writing (a & b) | (a & c) can synthesize into a & (b | c) or equivalent NAND-NAND structures depending on target standard-cell library cells.
Application Example: Simultaneous Register Swapping
Consider swapping the contents of two registers on an active clock edge:
`timescale 1ns / 1ps
`default_nettype none
module register_swap (
input wire clk,
output reg [7:0] a,
output reg [7:0] b
);
always @(posedge clk) begin
a <= b;
b <= a;
end
endmodule
A procedural software reading suggests sequential overwrite: variable a takes the value of b, and variable b takes the new value of a.
The hardware interpretation describes two edge-triggered flip-flops with inputs cross-connected (Figure 1).
The non-blocking assignment (<=) models this behavior accurately:
- The right-hand expressions evaluate using values present before the clock edge.
- The left-hand registers update simultaneously after the clock edge.
The two registers swap values cleanly in a single clock cycle without requiring a temporary variable.
Best Practice Checklist
Before writing Verilog code, verify these five hardware principles:
- Physical Component Identification: Verify the physical hardware inferred by each statement (gate, multiplexer, or flip-flop).
- Independence from Statement Order: Ensure continuous
assignstatements do not rely on procedural text order. - Consistent Assignment Usage: Use non-blocking assignments (
<=) for sequential clocked blocks, and blocking assignments (=) for combinational blocks. - Complete Combinational Branches: Include
elseor default assignments in combinational procedural blocks to prevent unintended latches. - Single Driver Rule: Verify that each internal net connects to exactly one active driver.
Practice Exercises
Problem 1
Analyze this procedural code block. Identify the inferred hardware, explain the defect, and provide the correct non-blocking implementation:
always @(posedge clk) begin
temp = a;
a = b;
b = temp;
end
Solution
Using blocking assignments (=) inside a sequential clocked block introduces race conditions:
- In simulation, blocking assignments execute sequentially in textual order. Signal
temptakesa,atakesb, andbtakestemp. - A synthesis tool optimizes out
tempand infers two cross-connected flip-flops. - However, if external blocks read
temp, simulation results become dependent on simulator scheduling order, creating a simulation-synthesis mismatch.
Correct the code by using non-blocking assignments (<=) without a temporary variable:
always @(posedge clk) begin
a <= b;
b <= a;
end
Both flip-flops sample their inputs simultaneously on the active clock edge.
The next section examines Verilog basics: module syntax, port declarations, data types, and literal constants.