HDL & Synthesis
FPGA & Programmable Logic · Hardware Foundations track
12 min read
In this module
Describing Hardware in Code
A processor chews through software instructions one (or a few) at a time, depending on superscalar tricks. An FPGA implements a circuit where everything happens at once. Every LUT evaluates its inputs, every flip-flop samples, every wire carries its signal — all simultaneously on every clock edge. Describing that parallel behaviour is the job of a Hardware Description Language (HDL).
Two dominate the field: Verilog (with its successor SystemVerilog) and VHDL. Both describe the same hardware concepts — combinational logic, sequential logic, modules, interconnect — through different syntax and design philosophies. Verilog reads C-ish and tends to be terser. VHDL leans Ada, so it's wordier but strongly typed. The security and embedded world skews Verilog/SystemVerilog, so that's what we'll use throughout.
RTL Design: Register Transfer Level
The standard abstraction for FPGA (and ASIC) design is Register Transfer Level (RTL). At RTL, the designer describes:
- Registers (flip-flops) - where data is stored
- Combinational logic - how data is transformed between registers
- Data flow - how data moves from one register through combinational logic to another register on each clock edge
A natural fit for synchronous digital design. On every rising clock edge, each register grabs its new value — the one the combinational logic worked out from other registers' values during the previous cycle.
When writing HDL, you are not writing a program that executes step by step. You are describing a circuit that exists physically and operates continuously. Every always block in Verilog describes a piece of hardware that runs in parallel with every other always block. A common mistake for software engineers is writing sequential-looking HDL that synthesises into something entirely different from what they intended.
Verilog Basics
Combinational Logic
Combinational logic has no memory. The output depends only on the current inputs. In Verilog, you describe it with continuous assignments or always @(*) blocks:
// Continuous assignment: a 2-input multiplexerassign y = sel ? b : a;
// always block: priority encoderalways @(*) begin casez (request) 4'b1???: grant = 2'd3; 4'b01??: grant = 2'd2; 4'b001?: grant = 2'd1; 4'b0001: grant = 2'd0; default: grant = 2'd0; endcaseendThe always @(*) says "re-evaluate this block whenever any input changes." Synthesis then infers combinational logic — a network of LUTs implementing the function. This lesson sticks to Verilog-2001 style because it's what you'll meet in most existing code, but new SystemVerilog designs write always_comb and always_ff @(posedge clk) instead. They say what you mean, and tools flag an always_comb block that accidentally infers a latch.
One trap catches nearly everyone once. If some path through an always @(*) block doesn't assign the output, the hardware has to remember the old value, and synthesis quietly infers a latch. Which of these three blocks infers one?
// Aalways @(*) begin if (sel) y = a;end
// B: op is 2 bits widealways @(*) begin case (op) 2'b00: y = a; 2'b01: y = b; 2'b10: y = c; endcaseend
// Calways @(*) begin y = a; if (sel) y = b;endShow the answer
A and B both infer latches. A has no else, so when sel is 0 nothing drives y. B never covers op == 2'b11. C is clean: the default assignment at the top means every path sets y. The fixes are an else for A and a default: arm for B, or the C pattern for both. Synthesis tools warn about inferred latches, and on an FPGA you almost never want one.
Sequential Logic
Sequential logic uses flip-flops to store state. In Verilog, sequential logic is described with always @(posedge clk) blocks:
// 8-bit register with synchronous resetalways @(posedge clk) begin if (rst) count <= 8'd0; else if (enable) count <= count + 8'd1;endOn every rising edge of clk, the flip-flop grabs the new value. The <= operator is the non-blocking assignment: "schedule this update for the end of the current clock edge." That's how the physical behaviour of flip-flops looks — they all sample at once.
In Verilog, = is a blocking assignment (executes immediately, like a software variable) and <= is a non-blocking assignment (schedules the update for the end of the time step). The rule is simple: use <= in always @(posedge clk) blocks (sequential logic) and = in always @(*) blocks (combinational logic). Mixing them incorrectly causes simulation mismatches and unpredictable synthesis results.
Here's what goes wrong when you mix them up, simulated in C. Same three-stage shift register, written once with blocking semantics and once with non-blocking:
Modules and Hierarchy
HDL designs are structured as a hierarchy of modules. Each module has defined inputs and outputs and can instantiate other modules:
module aes_sbox ( input wire [7:0] data_in, output wire [7:0] data_out); // S-box implemented as a look-up table // The synthesis tool maps this to FPGA LUTs reg [7:0] sbox [0:255];
initial begin sbox[8'h00] = 8'h63; sbox[8'h01] = 8'h7c; // ... remaining 254 entries end
assign data_out = sbox[data_in];endmoduleThe initial block works here because FPGA synthesis tools turn it into the power-up contents of the LUTs or block RAM, which get loaded with the bitstream. An ASIC flow has no bitstream to carry those values, so it ignores initial blocks; there you'd write the table as a case statement or use a ROM generator.
module aes_round ( input wire clk, input wire [127:0] state_in, input wire [127:0] round_key, output reg [127:0] state_out); wire [127:0] after_sbox;
// Instantiate 16 S-boxes (one per byte) - all run in parallel genvar i; generate for (i = 0; i < 16; i = i + 1) begin : sbox_inst aes_sbox u_sbox ( .data_in (state_in[i*8 +: 8]), .data_out (after_sbox[i*8 +: 8]) ); end endgenerate
// Toy stage: SubBytes and AddRoundKey only. A real AES round // also has ShiftRows and (except the last round) MixColumns. // Register the result always @(posedge clk) begin state_out <= after_sbox ^ round_key; endendmoduleNotice that all 16 S-box instances run in parallel. That's the fundamental win of hardware. This toy stage leaves out ShiftRows and MixColumns, but a full AES round built the same way still chews through all 16 bytes at once, where a CPU plods through them sequentially (unless AES-NI is doing the heavy lifting).
The FPGA Design Flow
Turning HDL source code into a programmed FPGA goes through several automated stages:
1. Synthesis
The synthesis tool (Vivado Synthesis for Xilinx/AMD, Quartus for Intel/Altera) reads the HDL and spits out a technology-mapped netlist — a description of the design in the target FPGA's primitive elements:
- Boolean expressions map to LUTs
- Sequential blocks map to flip-flops
- Arithmetic operations map to carry chains and DSP slices
- Memory arrays map to BRAM or distributed RAM
- Module hierarchy is flattened or preserved based on optimisation settings
Synthesis also runs logic optimisation: constant propagation, dead code elimination, resource sharing, state machine encoding.
2. Place and Route
The place step pins each primitive element from the netlist to a specific physical location on the die. The placer tries to keep connected elements near each other so routing delay stays low.
The route step configures the programmable interconnect switches to wire those placed elements together. Every net has to find a path through available wire segments and switch boxes without bumping into another signal on the same wire.
HDL Source Code | v Synthesis -----> Gate-level Netlist (LUTs, FFs, BRAM, DSP) | v Placement -----> Each element assigned to a physical site | v Routing -------> Interconnect switches configured | v Bitstream Gen -> Binary configuration file (.bit / .bin) | v FPGA Config ---> Device programmed via JTAG or SPI flash3. Timing Analysis
After place and route, the tools run Static Timing Analysis (STA). STA walks every path — flip-flop output, through the combinational logic and routing, into the next flip-flop input — and checks whether each one meets its timing constraints.
A path meets timing if:
T_clk > T_clk-to-q + T_logic + T_routing + T_setup - T_skew
Where: T_clk = clock period (1/frequency) T_clk-to-q = flip-flop clock-to-output delay T_logic = combinational logic delay (LUT lookups) T_routing = interconnect delay (wire + switch delay) T_setup = flip-flop setup time requirement T_skew = how much later the clock reaches the capturing flop than the launching one (positive skew helps setup, hurts hold)If any path violates this inequality, the design has a timing failure and won't operate reliably at the target frequency. Your options: drop the clock frequency, drop pipeline registers in to break long paths, or restructure the logic.
FPGA timing closure faces a unique challenge: the routing delay is determined by the place-and-route tool, not by the designer. In an ASIC, the designer has more control over wire lengths and can insert buffers. In an FPGA, the routing delay depends on which wire segments and switch boxes the router selects, and re-running place-and-route with different seed values can produce significantly different timing results. This non-determinism makes FPGA timing closure partly a matter of tool tuning.
4. Bitstream Generation
The final step converts the placed-and-routed design into a bitstream: a binary file holding the configuration for every SRAM cell in the FPGA. The bitstream encodes:
- The truth table contents of every LUT
- The configuration of every flip-flop (initial value, clock enable, reset)
- The state of every routing switch (on or off)
- BRAM initialisation contents
- DSP slice configuration
- I/O standard, drive strength, and termination settings for every pin
- Clock management tile (PLL/MMCM) configuration
Simulation and Verification
Before loading a bitstream onto hardware, the design is verified through simulation:
Functional simulation runs the HDL in a simulator (Vivado Simulator, ModelSim, or open-source Verilator) without any timing information. It checks the logic.
Timing simulation plugs in the real delays pulled from the placed-and-routed design to verify the circuit still works under real-world timing, setup/hold violations and glitches included.
Testbenches
A testbench is a Verilog module that instantiates the design under test, drives its inputs, and checks its outputs:
module tb_counter; reg clk, rst, enable; wire [7:0] count;
// Instantiate the design under test counter dut ( .clk (clk), .rst (rst), .enable (enable), .count (count) );
// Generate clock: 10 ns period (100 MHz) initial clk = 0; always #5 clk = ~clk;
// Stimulus initial begin rst = 1; enable = 0; #20 rst = 0; #10 enable = 1; #100;
// Check: after 10 clock cycles with enable=1, // count should be 10 if (count !== 8'd10) begin $display("FAIL: expected 10, got %d", count); $finish; end
$display("PASS"); $finish; endendmodule#20 says "wait 20 time units." Testbenches aren't synthesisable. They live only in simulation to drive and check the design.
Waveform Analysis
Simulators dump waveform files (VCD or FST format) showing every signal's value over time. Waveform viewers (GTKWave, Vivado's own viewer) display these as timing diagrams so you can trace signal transitions and debug logic visually.
Waveform analysis is the hardware equivalent of stepping through code in a debugger. When a design misbehaves, you stare at the waveforms, find where the wrong value first appears, then chase it back to its source.
A real FPGA evaluates all logic simultaneously on every clock edge. A simulator, running on a conventional CPU, must evaluate signals sequentially using an event-driven scheduling algorithm. This means simulation is orders of magnitude slower than real hardware. A design that runs at 200 MHz on an FPGA might simulate at only a few kilohertz. This speed gap is why FPGAs are used as hardware emulators for ASIC verification: they can run the design at near-real-time speeds.