Hardware description languages
In Verilog, changing a “<=” to an “=” turns a shift register into three copies of the same flip-flop. An HDL is not executed: it describes a circuit where everything happens at the same time.
01Describing hardware, not programming it
A hardware description language (HDL) looks like a programming language and is not one. A C program is a list of instructions that a processor executes one at a time, in order. A description in VHDL or Verilog is the description of a circuit: each statement is a piece of hardware that exists and works at the same time as all the others. There is no “now this line runs”; there are gates and flip-flops that all react at once to their inputs.
That change of mindset is the main difficulty of this unit, and almost all the mistakes of someone learning HDL come from writing as if it were C: expecting things to happen in order, using variables as in a program, or writing a loop expecting it to repeat in time when in fact it will be unrolled into copied hardware.
02VHDL and Verilog
Two languages have dominated digital design since the 1980s, and every FPGA tool accepts both:
| VHDL | Verilog / SystemVerilog | |
|---|---|---|
| Origin | U.S. Department of Defense, 1983; IEEE 1076 standard | Private industry, 1984; IEEE 1364 standard, today IEEE 1800 |
| Style | Strongly typed and verbose, derived from Ada | Compact, C-like syntax |
| Where it is used most | Europe, aerospace, defense, many universities | Semiconductor industry in the U.S. and Asia, verification |
The same four-bit counter with synchronous reset, in both:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity counter is
port (clk, rst : in std_logic;
q : out unsigned(3 downto 0));
end;
architecture rtl of counter is
signal c : unsigned(3 downto 0);
begin
process (clk) begin
if rising_edge(clk) then
if rst = '1' then c <= (others => '0');
else c <= c + 1;
end if;
end if;
end process;
q <= c;
end;VHDL
module counter (
input clk, rst,
output reg [3:0] q
);
always @(posedge clk)
if (rst) q <= 4'd0;
else q <= q + 4'd1;
endmoduleVerilog
Both describe exactly the same circuit: four D flip-flops, a four-bit adder that adds one, and a multiplexer that chooses between the sum and zero depending on the reset. Neither of them “counts” in the sense of a program: they describe the connections that, edge by edge, make the hardware count.
03Blocking and nonblocking
In Verilog there are two ways to assign inside a clocked block, and confusing them produces circuits different from the ones intended:
| Assignment | Semantics | Correct use |
|---|---|---|
Nonblocking b <= a; | All right-hand sides are read before the edge and all left-hand sides are updated together afterward: like real flip-flops | Sequential, clocked logic |
Blocking b = a; | Executes in order: the next line already sees the new value, as in C | Combinational logic |
You choose the example and the type of assignment, and step through one clock edge at a time. The table shows the value of each register after each edge, and the note below explains which circuit the tool would synthesize.
04Simulation and synthesis
One HDL file has two destinations. The simulator runs it to see how it behaves over time; the synthesizer translates it into real gates and flip-flops. Not everything that simulates can be synthesized:
| Construct | In simulation | In synthesis |
|---|---|---|
Delays such as #10 or after 10 ns | Respected | Ignored: the delay comes from the silicon |
| Reading and writing files | Useful in testbenches | Does not exist |
if without else in combinational logic | Keeps the previous value | Generates an unwanted latch |
for loop of 8 iterations | Repeats eight times | It is unrolled: eight copies of the hardware in parallel |
That is why every design is verified with a testbench (test bench): a module that is not synthesized, that generates the stimuli, observes the outputs and compares them with what is expected. In industry, writing the verification takes more work than writing the design.
05Differences from a procedural language
| C, Python | VHDL, Verilog | |
|---|---|---|
| Execution | Sequential, one instruction at a time | Concurrent: everything works at the same time |
| Time | Implicit, set by the processor | Explicit: clock edges and delays |
| What it produces | Instructions for a processor that already exists | The circuit itself |
| A loop | Repeats in time | Replicates in space |
| Dynamic memory, recursion | Natural | Impossible: the hardware does not grow while it runs |
| Typical error | A runtime exception | A circuit that simulates fine and does something else on the board |
06Where it shows up in electronics
| Case | The role of HDL |
|---|---|
| FPGA design | The whole design is written in HDL and the tool synthesizes it for the device |
| Custom integrated circuits (ASIC) | The same HDL, synthesized onto a foundry’s cell library |
| Processor verification | SystemVerilog with randomized testbenches and assertions |
| High-level synthesis (HLS) | You write in C or C++ and a tool generates the HDL: useful for filters and signal processing |
07In the lab
Simulate the counter from section 2 with a free simulator (GHDL or Icarus Verilog) and a testbench that applies reset and twenty clock edges. See in GTKWave that it returns to zero after 15.
Write a combinational multiplexer with an if without else, synthesize it
and look in the report for the inferred-latch warning. Fix it and compare.
Synthesize the two versions of the lab’s shift register and compare in the schematic viewer how many flip-flops each one generates.
08Common mistakes
- Thinking of statements as steps in time. They are parts of a circuit that work at the same time.
- Using blocking assignment in a clocked block. It collapses registers and breaks shifts and swaps.
- Leaving an
ifwithoutelsein combinational logic. It generates a latch. - Expecting
#10to produce a delay in the FPGA. The synthesizer ignores it. - Not writing a testbench. What was not simulated fails on the board.
09Self-assessment
What is the fundamental difference between a program in C and a description in VHDL?
The program runs in order on a processor; the description defines a circuit whose parts all work at the same time.
What does b <= a; c <= b; do in a clocked block?
A shift register: on each edge, c takes the previous value of
b and b that of a.
And with blocking assignments?
c sees the new value of b, which is already a:
both registers take a on the same edge and the shift disappears.
What does an if without else generate in combinational
logic?
A latch, because the output has to remember its value when the condition is not met.
What is a testbench?
A module that is not synthesized, that applies stimuli to the design and checks its outputs in simulation.
10Further reading
- Pong P. Chu. FPGA Prototyping by VHDL Examples. 2nd ed., Wiley, 2017. VHDL learned by building real circuits on a board, with synthesis always in view.
- Samir Palnitkar. Verilog HDL: A Guide to Digital Design and Synthesis. 2nd ed., Prentice Hall, 2003. The classic Verilog text, with its chapter on blocking and nonblocking assignments.
- Clifford E. Cummings. Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!. SNUG San José, 2000. The paper that set the style rules the whole industry uses today.