Sequential Logic
HDL synthesizers recognize certain idioms and turn them into specific sequential circuits. Other coding styles may simulate correctly but synthesize into circuits with blatant or subtle errors. This section presents the proper idioms to describe registers and latches.
Registers
The vast majority of modern commercial systems are built with registers using positive edge-triggered D flip-flops. HDL Example 4.17 shows the idiom for such flip-flops.
In SystemVerilog/Verilog always statements, signals keep their old value until an event in the sensitivity list takes place that explicitly causes them to change. Hence, such code, with appropriate sensitivity lists, can be used to describe sequential circuits with memory. For example, the flip-flop includes only clk in the sensitive list. It remembers its old value of q until the next rising edge of the clk, even if d changes in the interim.
In contrast, SystemVerilog/Verilog continuous assignment statements (assign) are reevaluated anytime any of the inputs on the right hand side changes. Therefore, such code necessarily describes combinational logic.
module flop(input logic clk,
input logic [3:0] d,
output logic [3:0] q);
always_ff @(posedge clk)
q <= d;
endmoduleCode Explanation
In general, a SystemVerilog
alwaysstatement is written in the formalways @(sensitivity list) statement;The
statementis executed only when the event specified in thesensitivity listoccurs. In this example, the statement isq <= d(pronounced "q gets d"). Hence, the flip-flop copiesdtoqon the positive edge of the clock and otherwise remembers the old state ofq. Note that sensitivity lists are also referred to as stimulus lists.<=is called a nonblocking assignment. Think of it as a regular=sign for now; we'll return to the more subtle points later. Note that<=is used instead ofassigninside analwaysstatement.(SystemVerilog Specific) As will be seen in subsequent sections,
alwaysstatements can be used to imply flip-flops, latches, or combinational logic, depending on the sensitivity list and statement. Because of this flexibility, it is easy to produce the wrong hardware inadvertently. SystemVerilog introducesalways_ff,always_latch, andalways_combto reduce the risk of common errors.always_ffbehaves likealwaysbut is used exclusively to imply flip-flops and allows tools to produce a warning if anything else is implied.
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
always @(posedge clk)
q <= d;
endmoduleCode Explanation
Point 1-3 from SystemVerilog Code Explanation applies to Verilog also.
All signals on the left hand side of
<=or=in analwaysstatement must be declared asreg. In this example,qis both an output and areg, so it is declared asoutput reg [3:0].Declaring a signal as
regdoes not mean the signal is actually the output of a register!!! All it means is that the signal appears on the left hand side of an assignment in analwaysstatement. We will see later examples ofalwaysstatements describing combinational logic in which the output is declaredregbut does not come from a flip-flop.
Resettable Registers
When simulation begins or power is first applied to a circuit, the output of a flip flop or register is unknown. This is indicated with x in SystemVerilog/Verilog. Generally, it is a good practice to use resettable registers so that on powerup you can put your system in a known state. The reset may be either asynchronous or synchronous. Recall that asynchronous reset occurs immediately, wheras synchronous reset clears the output only on the next rising edge of the clock. HDL Example 4.18 demonstrates the idioms for flip-flops with asynchronous and synchronous resets.
Enabled Registers
Enabled registers respond to the clock only when the enable is asserted. HDL Example 4.19 shows an asynchronously resettable enabled register that retains its old value if both reset and en are FALSE.
Multiple Registers
A single always statement in SystemVerilog can be used to describe multiple pieces of hardware. For example, consider the synchronizer made of two back-to-back flip-flops, as shown in Figure 4.17.

HDL Example 4.20 describes the synchronizer. On the rising edge of clk, d is copied to n1. At the same time, n1 is copied to q.
Latches
Recall from previous section that a D latch is transparent when the clock is HIGH, allowing data to flow from input to output. The latch becomes opaque when the clock is LOW, retaining its old state. HDL Example 4.21 shows the idiom of a D latch.
Code Explanation
(SystemVerilog Specific)
always_latchis equivalent toalways @(clk, d)and is the preferred idiom for describing a latch in SystemVerilog. It evaluates any timeclkordchanges. Ifclkis HIGH,dflows through toq, so this code describes a positive level sensitive latch. Otherwise,qkeeps its old value.
Code Explanation
The sensitivity list contains both
clkandd, so thealwaysstatement evaluates any timeclkordchanges. Ifclkis HIGH,dflows through toq.qmust be declared to be aregbecause it appears on the left hand side of<=in analwaysstatement. This does not always mean thatqis the output of a register!
Not all synthesis tools supports latches well. Unless you know that your tool does support latches and you have a good reason to use them, avoid them and use edge-triggered flip-flops instead.
Last updated