Inferring Flip-Flops
Sections 5.2 through 5.4 discussed combinational logic inside procedural blocks. This section examines sequential logic. In Verilog, three lines of behavioral code describe a physical edge-triggered D flip-flop:
always @(posedge clk) begin
q <= d;
end
The synthesis tool recognizes this pattern and maps it directly to a library flip-flop cell.
Edge-Triggered Sensitivity Lists
A combinational always block evaluates whenever any input signal changes. In contrast, a sequential block triggers only on a transition edge, specified by posedge clk or negedge clk.
The edge keyword informs the synthesis tool that the block describes sequential storage:
- The block evaluates only at the rising transition of
clk. - At that clock transition, the current value of signal
dtransfers to registerq. - Between clock transitions, signal
qretains its stored state.
The nonblocking assignment operator (<=) schedules updates to occur after current-cycle evaluations complete. This behavior models the clock-to-output propagation delay ($t_{\text{cq}}$) of physical flip-flops.
Falling-Edge Triggering
You can also specify negative-edge clock triggering:
always @(negedge clk) begin
q <= d;
end
This construct updates state on the falling edge of the clock signal. Double data rate (DDR) interfaces sample data on both edges. Most synchronous digital designs use rising-edge triggering uniformly to avoid timing complications.
Inferring Multiple Registers in a Single Block
A single sequential block can update multiple signals:
always @(posedge clk) begin
a_reg <= a_in;
b_reg <= b_in ^ a_in;
count <= count + 1;
end
This code creates three distinct registers controlled by a common clock:
- Register
a_regcaptures input signala_in. - Register
b_regcaptures the output of an XOR gate evaluatingb_in ^ a_in. - Register
countcaptures the incremented value of its own previous state.
The block infers three flip-flops, an XOR gate, and an adder circuit.
Combinational Logic Between Sequential Registers
In synchronous design, a flip-flop captures the value that combinational logic produces during the preceding clock period (Figure 1). Each register stage receives input from a network of gates driven by earlier register outputs.
Timing Constraints in Hardware
Sequential circuits must satisfy physical timing requirements (Figure 2):
- Setup time ($t_{\text{su}}$): Input data must remain stable before the active clock edge.
- Hold time ($t_{\text{hold}}$): Input data must remain stable after the active clock edge.
- Clock-to-output delay ($t_{\text{cq}}$): Output data updates after the clock transition.
The statement q <= d implies that data on d settles before the setup window on every active clock edge. Module 8 examines static timing analysis (STA) methods that verify these constraints.
Verification with Yosys Synthesis
You can inspect the generated hardware cells using the open-source Yosys synthesis tool:
yosys -p "read_verilog dff.v; synth; stat"
The output report displays the mapped cell types:
Number of cells: 1
$_DFF_P_ 1
The cell $_DFF_P_ represents a positive-edge triggered D flip-flop primitive in Yosys. Its data, clock, and output pins map to d, clk, and q. The show command in Yosys generates a graphical schematic diagram of the synthesized circuit.
Practice Exercise
Analyze the following sequential block. Identify the inferred hardware components, then verify the structure with Yosys.
always @(posedge clk) begin
sum_reg <= a + b;
max_reg <= (a > b) ? a : b;
tick <= ~tick;
end
Solution
This block infers three flip-flops preceded by input logic:
sum_reg: An adder circuit computesa + band feeds the register input.max_reg: A comparator drives a 2-to-1 multiplexer that selects betweenaandb.tick: An inverter connects the output oftickback to its input, forming a toggle register.
Yosys reports three sequential flip-flop cells alongside the adder, comparator, multiplexer, and inverter gates.
Sequential flip-flops power up in an unknown logic state without explicit initialization. The next article examines synchronous and asynchronous reset strategies.