Signals of type wire
or a similar wire like data type requires the continuous assignment of a value. For example, consider an electrical wire used to connect pieces on a breadboard. As long as the +5V battery is applied to one end of the wire, the component connected to the other end of the wire will get the required voltage.

In Verilog, this concept is realized by the assign
statement where any wire
or other similar wire like data-types can be driven continuously with a value. The value can either be a constant or an expression comprising of a group of signals.
An always
block is one of the procedural blocks in Verilog. Statements inside an always block are executed sequentially.
Syntax
always @ (event)
[statement]
always @ (event) begin
[multiple statements]
end
The always
block is executed at some particular event. The event is defined by a sensitivity list.
What is the sensitivity list ?
A sensitivity list is the expression that defines when the always block should be executed and is specified after the @
operator within parentheses ( )
. This list may contain either one or a group of signals whose value change will execute the always block.
In the code shown below, all statements inside the always
block get executed whenever the value of signals a or b change.
// Execute always block whenever value of "a" or "b" change
always @ (a or b) begin
[statements]
end
What is the always block used for ?
An always
block can be used to realize combinational or sequential elements. A sequential element like flip flop becomes active when it is provided with a clock and reset. Similarly, a combinational block becomes active when one of its input values change. These hardware blocks are all working concurrently independent of each other. The connection between each is what determines the flow of data. To model this behavior, an always
block is made as a continuous process that gets triggered and performs some action when a signal within the sensitivity list becomes active.
In the following example, all statements within the always block get executed at every positive edge of the signal clk.
// Execute always block at positive edge of signal "clk"
always @ (posedge clk) begin
[statements]
end
What happens if there is no sensitivity list ?
The always
block repeats continuously throughout the duration of a simulation. The sensitivity list brings along a certain sense of timing i.e. whenever any signal in the sensitivity list changes, the always block is triggered. If there are no timing control statments within an always block, the simulation will hang because of a zero-delay infinite loop !
Example
The example shown below is an always block that attempts to invert the value of the signal clk. The statement is executed after every 0 time units. Hence, it executes forever because of the absence of a delay in the statement.
// always block is started at time 0 units
// But when is it supposed to be repeated ?
// There is no time control, and hence it will stay and
// be repeated at 0 time units only. This continues
// in a loop and simulation will hang !
always clk = ~clk;
Even if the sensitivity list is empty, there should be some other form of time delay. Simulation time is advanced by a delay statement within the always
construct as shown below. Now, the clock inversion is done after every 10 time units.
always #10 clk = ~clk;
Note: Explicit delays are not synthesizable into logic gates !
Hence real Verilog design code always require a sensitivity list.
Sequential Element Design Example
The code shown below defines a module called tff that accepts a data input, clock and active-low reset. The output gets inverted whenever d is found to be 1 at the positive edge of clock. Here, the always
block is triggered either at the positive edge of clk or the negative edge of rstn.
What happens at the positive edge of clock ?
The following events happen at the positive edge of clock and is repeated for all positive edge of clock.
- First
if
block checks value of active-low reset rstn - If rstn is zero, then output q should be reset to default value of 0
- If rstn is one, then it means reset is not applied and should follow default behavior
- If the previous step is false:
- Check value of d and if it is found to be one, then invert value of q
- If d is 0, then maintain value of q
module tff (input d,
clk,
rstn,
output reg q);
always @ (posedge clk or negedge rstn) begin
if (!rstn)
q <= 0;
else
if (d)
q <= ~q;
else
q <= q;
end
endmodule
What happens at the negative edge of reset ?
The following events happen at negative edge of rstn and happen at all such occurrences.
- First
if
block checks value of active-low reset rstn. At negative edge of the signal, its value is 0. - If value of rstn is 0, then it means reset is applied and output should be reset to default value of 0
- The case where value of rstn is 1 is not considered because the current event is negative edge of the rstn
Combinational Element Design Example
An always
block can also be used in the design of combinational blocks. For example the following digital circuit represents a combination of three different logic gates that provide a certain output at signal o.
The code shown below is a module
with four input ports and a single output port called o. The always
block is triggered whenever any of the signals in the sensitivity list changes in value. Output signal is declared as type reg
in the module port list because it is used in a procedural block. All signals used in a procedural block should be declared as type reg
.
module combo ( input a,
input b,
input c,
input d,
output reg o);
always @ (a or b or c or d) begin
o <= ~((a & b) | (c^d));
end
endmodule
See that the signal o becomes 1 whenever the combinational expression on the RHS becomes true. Similarly o becomes 0 when RHS is false.
Simulation OutputSynthesis
It is possible for an always
block to not be synthesis friendly, if it does not follow one of the following templates.
// Template #1: Use for combinational logic, all inputs mentioned in
// sensitivity list ensures that it infers a combo block
always @ (all_inputs) begin
// Combinational logic
end
// Template #2: Use of a if condition without else can cause a latch
// because the previous value has to be held since new value is not
// defined by a missing else clause
always @ (all_inputs) begin
if (enable) begin
// latch value assignments
end
end
// Template #3: Use clock in sensitivity list for sequential elements
always @ (posedge clk) begin
// behavior to do at posedge clock
end
// Template #4: Use clock and async reset in sensitivity list
always @ (posedge clk or negedge resetn) begin
if (! resetn) begin
// behavior to do during reset
end else begin
// behavior when not in reset
end
end
A set of Verilog statements are usually executed sequentially in a simulation. These statements are placed inside a procedural block. There are mainly two types of procedural blocks in Verilog - initial and always
Syntax
initial
[single statement]
initial begin
[multiple statements]
end
What is the initial block used for ?
An initial
block is not synthesizable and hence cannot be converted into a hardware schematic with digital elements. Hence initial blocks do not serve much purpose than to be used in simulations. These blocks are primarily used to initialize variables and drive design ports with specific values.
When does an initial block start and end ?
An initial
block is started at the beginning of a simulation at time 0 unit. This block will be executed only once during the entire simulation. Execution of an initial
block finishes once all the statements within the block are executed.
The image shown above has a module
called behave which has two internal signals called a and b. The initial
block has only one statement and hence it is not necessary to place the statement within begin
and end
. This statement assigns the value 2'b10 to a when the initial block is started at time 0 units.
What happens if there is a delay element ?
The code shown below has an additional statement that assigns some value to the signal b. However this happens only after 10 time units from execution of previous statement. This means that a is assigned first with the given value and then after 10 time units, b is assigned to 0.

How many initial blocks are allowed in a module ?
There are no limits to the number of initial
blocks that can be defined inside a module.
The code shown below has three initial
blocks all of which are started at the same time and run in parallel. However, depending on the statements and the delays within each initial block, the time taken to finish the block may vary.
In this example, the first block has a delay of 20 units, while the second has a total delay of 50 units (10 + 40) and the last block has a delay of 60 units. Hence the simulation takes 60 time units to complete since there is atleast one initial block still running until 60 time units.
$finish
is a Verilog system task that tells the simulator to terminate the current simulation.
If the last block had a delay of 30 time units like shown below, the simulation would have ended at 30 time units thereby killing all the other initial
blocks that are active at that time.
initial begin
#30 $finish;
end
Synthesis
An initial
block is not synthesizable.
A digital element such as a flip-flop can be represented with combinational gates like NAND and NOR. The functionality of a flip-flop is achieved by the connection of a certain set of gates in a particular manner. How the gates have to be connected is usually figured out by solving K-map from the truth table. The truth table is nothing but a table that tells us what inputs combine together to give what values of output. Shown in the image below is an electronic circuit that represents a D-flip flop and the corresponding truth table. The output q becomes 1 only when rstn and d are both having a value of 1.
What is a hardware schematic ?
A hardware schematic is a diagram that shows how the combinational gates should be connected to achieve a particular hardware functionality. In this case, it is the set of NAND gates connected like shown towards the left in the image above. However, if we know what values of inputs contribute to make the output have a value of 1, then we can essentially hide the internal details of the connections and encapsulate it into a black-box. This block provides us with certain inputs and outputs that is similar to the hardware schematic made up of combinational gates.
What is a Hardware Description Language ?
It will be easier if we can describe how this block should behave and then let software tools convert that behavior into actual hardware schematic. The language that describes hardware functionality is called Verilog, and is classified as a Hardware Description Language.
What is meant by design functionality ?
Some typical behavioral requirements for a D-flip-flop are :
- clock should be an input to the flop
- if the active-low reset is 0, then the flop should reset
- if the active-low reset is 1, then the flop output 'q' should follow input 'd'
- output 'q' should get a new value only at the posedge of clock
An important question comes to mind : how do we know whether the behavior described in Verilog accurately reflects the intended behavior of the design ?
What is verification ?
This is checked by different methods and is collectively called as verification. The most common and widely practiced method of verification is circuit simulation. There are software tools to understand how a hardware described in Verilog should behave and provide various input stimuli to the design model. The output of the design is then checked against expected values to see if the design is functionally correct.
All simulations are performed by EDA (Electronic Design Automation) software tools and the Verilog design RTL is placed inside an entity called as testbench. Within the testbench, various tests provide different stimuli to the design. Such a testbench is shown in the image below.
Sections of Verilog code
All behavior code should be described within the keywords module
and endmodule
. Rest of the design code would mostly follow the given template.
Verilog sections template
- Module definition and port list declaration
- List of input and output ports
- Declaration of other signals using allowed Verilog data types
- Design may depend on other Verilog modules and hence their instances are created by module instantiations
- The actual Verilog design for this module that describes its behavior
module [design_name] ( [port_list] );
[list_of_input_ports]
[list_of_output_ports]
[declaration_of_other_signals]
[other_module_instantiations_if_required]
[behavioral_code_for_this_module]
endmodule
Example
The code shown below describes the behavior of a D type flip-flop. The first few lines declare a new module
called dff and define the input and output ports. The only other signal used in this design is q and is declared next. Since this is a simple design, it does not depend on any other module and hence there are no module instantiations. The always
block describes how the hardware should behave during certain events and hence is behavioral code.
// "dff" is the name of this module
module dff ( input d, // Inputs to the design should start with "input"
rstn,
clk,
output q); // Outputs of the design should start with "output"
reg q; // Declare a variable to store output values
always @ (posedge clk) begin // This block is executed at the positive edge of clk 0->1
if (!rstn) // At the posedge, if rstn is 0 then q should get 0
q <= 0;
else
q <= d; // At the posedge, if rstn is 1 then q should get d
end
endmodule // End of module
Testbench code
The testbench is the Verilog container module that allows us to drive the design with different inputs and monitor its outputs for expected behavior. In the example shown below, we have instantiated the flop design illustrated above and connected it with testbench signals denoted by tb_*. These testbench signals are then assigned certain values and are eventually driven as inputs to the design.
module tb;
// 1. Declare input/output variables to drive to the design
reg tb_clk;
reg tb_d;
reg tb_rstn;
wire tb_q;
// 2. Create an instance of the design
// This is called design instantiation
dff dff0 ( .clk (tb_clk), // Connect clock input with TB signal
.d (tb_d), // Connect data input with TB signal
.rstn (tb_rstn), // Connect reset input with TB signal
.q (tb_q)); // Connect output q with TB signal
// 3. The following is an example of a stimulus
// Here we drive the signals tb_* with certain values
// Since these tb_* signals are connected to the design inputs,
// the design will be driven with the values in tb_*
initial begin
tb_rsnt <= 1'b0;
tb_clk <= 1'b0;
tb_d <= 1'b0;
end
endmodule
A previous article showed different examples of using an always
block to implement combinational logic. An always
block is also mainly used to implement sequential logic which has memory elements like flip flops that can hold values.
JK Flip Flop
A JK flip flop is one of the many types of flops used to store values and has two data inputs j and k along with one for reset rstn and another for clock clk . The truth table for a JK flop is shown below and is typically implemented using NAND gates.
rstn | j | k | q | Comments |
---|---|---|---|---|
0 | 0 | 0 | 0 | When reset is asserted, output is always zero |
1 | 0 | 0 | Hold value | When both j and k are 0, output remains the same as before |
1 | 0 | 1 | 1 | When k=1, output becomes 1 |
1 | 1 | 0 | 0 | When k=0, output becomes 0 |
1 | 1 | 1 | Toggle value | When j=1,k=1 output toggles current value |
The behavioral Verilog code for a JK flip-flop can be written as shown below
module jk_ff ( input j, // Input J
input k, // Input K
input rstn, // Active-low async reset
input clk, // Input clk
output reg q); // Output Q
always @ (posedge clk or negedge rstn) begin
if (!rstn) begin
q <= 0;
end else begin
q <= (j & ~q) | (~k & q);
end
end
endmodule
Testbench
First declare all variables used in the testbench and start a clock using a simple always
block that can be driven to the design. Then instantiate the design and connect its ports with corresponding testbench variables. Note that q is of type wire
because it is connected to an output of the design which will be actively driving it. All other inputs to the design are of type reg
so that they can be driven within a procedural block such as initial
.
The stimulus first initializes all inputs to the design to zero and then de-asserts reset after some time. A for
loop is used to drive different values to j and k which are driven at random times. Once the loop is done, wait for some more time and finish the simulation.
module tb;
// Declare testbench variables
reg j, k, rstn, clk;
wire q;
integer i;
reg [2:0] dly;
// Start the clock
always #10 clk = ~clk;
// Instantiate the design
jk_ff u0 ( .j(j), .k(k), .clk(clk), .rstn(rstn), .q(q));
// Write the stimulus
initial begin
{j, k, rstn, clk} <= 0;
#10 rstn <= 1;
for (i = 0; i < 10; i = i+1) begin
dly = $random;
#(dly) j <= $random;
#(dly) k <= $random;
end
#20 $finish;
end
endmodule
Note from the simulation wave that at the posedge of clock, output q changes value based on the state of inputs j and k as given in the truth table.

Modulo-10 counter
Modulus(MOD) counters simply count upto a certain number before rolling back to zero. A MOD-N counter will count from 0 to N-1 and then roll back to zero and start counting again. Such counters typically require log2N number of flops to hold the count value. Shown below is the Verilog code for a MOD-10 counter that keeps counting up at every clock clk as long as reset rstn is deasserted.
Verilog parameters can be used to make a MOD-N counter which is more scalable.
module mod10_counter ( input clk,
input rstn,
output reg[3:0] out);
always @ (posedge clk) begin
if (!rstn) begin
out <= 0;
end else begin
if (out == 10)
out <= 0;
else
out <= out + 1;
end
end
endmodule
Testbench
The testbench first declares some variables that can be assigned some values and driven to the design inputs. The counter module is then instantiated and connected with the testbench signals which are later driven with some values in the stimulus. Since the counter also requires a clock, the testbench clock is modeled with an always
block. The stimulus simply sets default values at time 0ns, then deasserts reset after 10ns and the design is allowed to run for some time.
module tb;
reg clk, rstn;
reg [3:0] out;
mod10_counter u0 ( .clk(clk), .rstn(rstn), .out(out));
always #10 clk = ~clk;
initial begin
{clk, rstn} <= 0;
#10 rstn <= 1;
#450 $finish;
end
endmodule
See that the counter module counts from zero to 9, rolls over to zero and starts counting again.

4bit Left Shift Register
Shown below is a 4-bit left shift register that accepts an input d into LSB and all other bits will be shifted left by 1. For example, if d equals zero and the initial value of the register is 0011, it will become 0110 at the next edge of the clock clk .
module lshift_4b_reg ( input d,
input clk,
input rstn,
output reg [3:0] out
);
always @ (posedge clk) begin
if (!rstn) begin
out <= 0;
end else begin
out <= {out[2:0], d};
end
end
endmodule
Testbench
The testbench follows a similar template like the one shown before where some variables are declared, design module is instantiated and connected with the testbench signals. Then a clock is started and the stimulus is driven to the design using an initial
block. In this testbench example, different values of d has to be exercised and hence a for
loop is used to iterate 20 times and apply random values to the design.
module tb;
reg clk, rstn, d;
wire [3:0] out;
integer i;
lshift_4b_reg u0 ( .d(d), .clk(clk), .rstn(rstn), .out(out));
always #10 clk = ~clk;
initial begin
{clk, rstn, d} <= 0;
#10 rstn <= 1;
for (i = 0; i < 20; i=i+1) begin
@(posedge clk) d <= $random;
end
#10 $finish;
end
endmodule
Note that each bit is shifted to the left by 1 and the new value of d is applied to LSB.
