Catto / Topic Map · Digital Techniques I Level 3
Digital Techniques I · 96 h · Topic 3 of 4

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.

VHDL Verilog Concurrency Nonblocking assignment Synthesis

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:

VHDLVerilog / SystemVerilog
OriginU.S. Department of Defense, 1983; IEEE 1076 standardPrivate industry, 1984; IEEE 1364 standard, today IEEE 1800
StyleStrongly typed and verbose, derived from AdaCompact, C-like syntax
Where it is used mostEurope, aerospace, defense, many universitiesSemiconductor 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:

AssignmentSemanticsCorrect 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-flopsSequential, clocked logic
Blocking b = a;Executes in order: the next line already sees the new value, as in CCombinational logic
Lab · the same code, two circuits

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:

ConstructIn simulationIn synthesis
Delays such as #10 or after 10 nsRespectedIgnored: the delay comes from the silicon
Reading and writing filesUseful in testbenchesDoes not exist
if without else in combinational logicKeeps the previous valueGenerates an unwanted latch
for loop of 8 iterationsRepeats eight timesIt 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, PythonVHDL, Verilog
ExecutionSequential, one instruction at a timeConcurrent: everything works at the same time
TimeImplicit, set by the processorExplicit: clock edges and delays
What it producesInstructions for a processor that already existsThe circuit itself
A loopRepeats in timeReplicates in space
Dynamic memory, recursionNaturalImpossible: the hardware does not grow while it runs
Typical errorA runtime exceptionA circuit that simulates fine and does something else on the board

06Where it shows up in electronics

CaseThe role of HDL
FPGA designThe 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 verificationSystemVerilog 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

Activity 1 · Simulating the counter

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.

Activity 2 · Provoking a latch

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.

Activity 3 · Blocking versus nonblocking

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 if without else in combinational logic. It generates a latch.
  • Expecting #10 to 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.
Development of the topic “Hardware description languages” of Digital Techniques I (Level 3), based on the curriculum of the UTN Electronic Engineering program, 2023 curriculum — Ordinance No. 1849 of the UTN Higher Council. Back to the Topic Map · catto.ar