Module

Module 5: Behavioral Verilog for Synthesis

29 lessons
Start

Module 4 described circuits by wiring gates and by writing equations. Both make you think in gates. This module is about the third style, where you say what the hardware does at each clock edge and the synthesiser works out the gates. It is behavioural RTL, and it is what nearly all production Verilog looks like.

Still not a program

Every module so far has said that Verilog is not a programming language. This is the module where that is easiest to forget and most expensive to get wrong, because behavioural Verilog has if, case, for and variables that are assigned and read back, and it looks like C.

Two always blocks side by side are not two functions that run one after the other. They are two circuits, both powered, both running. If block A writes a signal that block B reads and block B writes one that A reads, that is a feedback loop in hardware, not mutual recursion. Keep the picture from Module 4: every always block, every assign, every instance is a machine bolted to the floor and running, and nothing waits for anything else.

The two rules

Everything in this module follows from two rules, and it is worth having them before writing an always block.

In a block clocked by an edge, always @(posedge clk), every assignment uses <=.

In a combinational block, always @(*), every assignment uses =.

They were not chosen for taste. They come from the way the simulator schedules events, and section 5.1 derives them from that, because following rules you do not understand gets you correct simple code and mysterious complex bugs.

The sections

Module 5 map
Figure 1: The six sections

Section Topic
5.1 Procedural blocks: concurrency, always and initial, blocking and non-blocking assignment, the scheduler
5.2 Sensitivity lists: edge and level, always @(*), incomplete lists, latches
5.3 if and case: priority and parallel logic, casez, full and parallel case
5.4 Loops: for and synthesis, the other loops, patterns, generate against procedural loops
5.5 Flip-flops and registers: inference, resets, enables and loads, patterns, pipelines
5.6 State machines: anatomy, one, two and three-process styles, encoding, three worked examples

Section 5.1 is the engine room. Sections 5.2 and 5.3 apply procedural code to combinational logic and to the latch you did not mean to build. Section 5.4 is loops. Section 5.5 is the sequential building blocks, and 5.6 is state machines, which is where all of it comes together.

What you will build

Design Section
parity generator with a for loop 5.4
D flip-flop with synchronous and with asynchronous reset 5.5
parameterised shift register 5.5
up/down counter with enable, load and clear 5.5
a pipeline with valid signals 5.5
traffic-light controller in three styles 5.6
serial sequence detector, Mealy, overlapping 5.6
vending machine with datapath and control 5.6

Every design comes with a testbench that dumps a waveform file, so you can watch the registers in GTKWave.

Tools

Same as Module 4: Icarus Verilog and GTKWave.

iverilog -o sim tb_design_name.v design_name.v
vvp sim
gtkwave tb.vcd

Every testbench in the module has

$dumpfile("tb.vcd");
$dumpvars(0, tb_design_name);

in its initial block; the 0 means every level of the hierarchy. Commercial simulators use their own compressed waveform formats because VCD files get large, but the idea is the same.

Section 5.1 opens with a module containing two always @(*) blocks that read each other’s outputs, and asks what it does. The answer is not what a programmer expects, and seeing why is the first step. Start at 5.1.

In this module

01 Everything Runs at Once 5 min 02 always and initial 5 min 03 Blocking Assignments 6 min 04 Non-Blocking Assignments 6 min 05 The Scheduler 5 min 06 The Sensitivity List 4 min 07 always @(*) 4 min 08 The Missing Signal 4 min 09 Accidental Latches 6 min 10 if and else 6 min 11 case 4 min 12 casez, and Why Not casex 3 min 13 full_case and parallel_case 3 min 14 Priority or Parallel 5 min 15 for Loops and Synthesis 4 min 16 while, repeat and forever 4 min 17 Loops as Hardware Generators 5 min 18 generate for and Procedural for 3 min 19 Inferring Flip-Flops 4 min 20 Reset 4 min 21 Enables and Loads 3 min 22 Register Patterns 4 min 23 Pipeline Registers 4 min 24 The State Machine in RTL 4 min 25 The One-Process Style 3 min 26 The Two-Process Style 3 min 27 The Three-Process Style 3 min 28 Encoding and Safe State Machines 3 min 29 Three Complete State Machines 8 min