Verilog Notes
Verilog Notes
• The port list of a module is the interface between the module and its environment.
xor G1 ( w1 , A , B ) ;
and G2 ( w2 , A , B ) ;
xor G3 (S , w1 , Ci ) ;
and G4 ( w3 , w1 , Ci ) ;
or G5 ( Co , w2 , w3 ) ;
endmodule
– Dataflow modeling
module Full_Adder_D (S , Co , A , B , Ci ) ;
output S , Co ;
input A , B , Ci ;
assign S =( A & B & Ci ) |(( A ) &(∼B ) & Ci ) |((∼A ) & B &(∼Ci ) ) |( A &(∼B )
&(∼Ci ) ) ;
assign Co =( A & B ) |( B & Ci ) |( A & Ci ) ;
endmodule
Digital Design Dr. Pravin Mane Page 2 of 34
– Behavioral modeling
module Full_Adder_B (S , Co , A , B , Ci ) ;
output S , Co ;
input A , B , Ci ;
reg S , Co ;
always @ ( A or B or Ci )
begin
{ Co , S }= A + B + Ci ;
end
endmodule
• Predefined primitives : and, or, not, buf, xor, xnor, nand, nor, bufif0, bufif1, notif0, notif1
• The output of a primitive gate is always listed first, followed by the inputs.
• The output of a primitive must be listed first, but the inputs and outputs of a module may be listed in
any order.
– It is declared with the keyword primitive , followed by a name and port list.
– There can be only one output, and it must be listed first in the port list and declared with
keyword output.
– There can be any number of inputs. The order in which they are listed in the input declaration
must conform to the order in which they are given values in the table that follows.
– The truth table is enclosed within the keywords table and endtable.
– The values of the inputs are listed in order, ending with a colon (:). The output is always the last
entry in a row and is followed by a semicolon (;).
– The declaration of a UDP ends with the keyword endprimitive.
primitive UDP_1247 (S , A , B , Ci ) ;
output S ;
input A , B , Ci ;
table
0 0 0 : 0;
0 0 1 : 1;
0 1 0 : 1;
0 1 1 : 0;
1 0 0 : 1;
1 0 1 : 0;
1 1 0 : 0;
Digital Design Dr. Pravin Mane Page 3 of 34
1 1 1 : 1;
endtable
endprimitive
primitive UDP_3567 ( Co , A , B , Ci ) ;
output Co ;
input A , B , Ci ;
table
0 0 0 : 0;
0 0 1 : 0;
0 1 0 : 0;
0 1 1 : 1;
1 0 0 : 0;
1 0 1 : 1;
1 1 0 : 1;
1 1 1 : 1;
endtable
endprimitive
module FA_UDP (S , Co , A , B , Ci ) ;
output S , Co ;
input A , B , Ci ;
UDP_3567 Carry ( Co , A , B , Ci ) ;
UDP_1247 Sum (S , A , B , Ci ) ;
endmodule
Gate-Level Modeling
• It is done using instantiations of predefined and user-defined primitive gates. In this type of representation,
a circuit is specified by its logic gates and their interconnections.
• There are 12 basic gates as predefined primitives: and, or, not, buf, xor, xnor, nand, nor, bufif0,
bufif1, notif0, notif1
• Four of these primitive gates are of the three-state type : bufif0, bufif1, notif0, notif1
Digital Design Dr. Pravin Mane Page 4 of 34
– The bufif1 gate behaves like a normal buffer if control = 1. The output goes to a high-impedance
state z when control = 0.
– The bufif0 gate behaves in a similar fashion, except that the high-impedance state occurs when
control = 1.
– The two notif gates operate in a similar manner, except that the output is the complement of the
input when the gate is not in a high-impedance state.
– The gates are instantiated with the statement: gate name(output, input, control);
– The outputs of three-state gates can be connected together to form a common output line. To
identify such a connection, Verilog HDL uses the keyword tri (for tristate) to indicate that the
output has multiple drivers.
• Primitives such as and are n-input primitives. They can have any number of scalar inputs.
• The buf and not primitives are n-output primitives. A single input can drive multiple output lines
distinguished by their identifiers.
• The logic of each gate is based on a four-valued system : 0, 1, x (unknown), z (high impedance)
and 0 1 x z or 0 1 x z
0 0 0 0 0 0 0 1 x x
1 0 1 x x 1 1 1 1 1
x 0 x x x x x 1 x x
z 0 x x x z x 1 x x
• Note that the keywords not and nand can be written only once and do not have to be repeated for
each gate, but commas must be inserted at the end of each of the gates in the series, except for the
last statement, which must be terminated with a semicolon.
• Two or more modules can be combined to build a hierarchical description of a design. There are two
basic types of design methodologies:
– In a top-down design, the top-level block is defined and then the subblocks necessary to build
the top-level block are identified.
– In a bottom-up design, the building blocks are first identified and then combined to build the
top-level block.
endmodule
// module full_adder (S , C , x , y , z ) ;
// output S , C ;
// input x , y , z ;
• Modules can be instantiated (nested) within other modules, but module declarations cannot be nested;
that is, a module definition (declaration) cannot be placed within another module declaration.
Dataflow Modeling
• Dataflow modeling of combinational logic uses a number of operators that act on binary operands to
produce a binary result.
• Dataflow modeling uses continuous assignments and the keyword assign. A continuous assignment
is a statement that assigns a value to a net.
// Dataflow description of two - to - four - line decoder
module binary_adder (
output [3:0] Sum ,
output C_out ,
input [3: 0] A , B ,
input C_in
);
module mag_compare (
output A_lt_B , A_eq_B , A_gt_B ,
input [3: 0] A , B
);
assign A_lt_B = ( A < B ) ;
assign A_gt_B = ( A > B ) ;
assign A_eq_B = ( A == B ) ;
endmodule
Behavioral Modeling
• Behavioral modeling represents digital circuits at a functional and algorithmic level.
• Behavioral descriptions use the keyword always, followed by an optional event control expression
and a list of procedural assignment statements.
• The event control expression specifies when the statements will execute.
• The target output of a procedural assignment statement must be of the reg data type.
• A reg data type retains its value until a new value is assigned.
Digital Design Dr. Pravin Mane Page 9 of 34
• The procedural assignment statements inside the always block are executed every time there is a
change in any of the variables listed after the @ symbol.
// Behavioral description of two - to - one - line multiplexer
module mux_2x1_beh ( m_out , A , B , select ) ;
output m_out ;
input A , B , select ;
reg m_out ;
always @ ( A or B or select )
if ( select == 1) m_out = A ;
else m_out = B ;
endmodule
• Use of case:
module mux_4x1_beh (
output reg m_out ,
input in_0 , in_1 , in_2 , in_3 ,
input [1: 0] select
);
• Numbers are stored as a bit pattern in memory, but they can be referenced in decimal, octal, or
hexadecimal formats with the letters d’ o’ and h’ respectively.
• If the base of the number is not specified, its interpretation defaults to decimal.
• A test module is written like any other module, but it typically has no inputs or outputs.
• The signals that are applied as inputs to the design module for simulation are declared in the stimulus
module as local reg data type.
• The outputs of the design module that are displayed for testing are declared in the stimulus module
as local wire data type.
• The module under test is then instantiated, using the local identifiers in its port list.
• In addition to employing the always statement, test benches use the initial statement to provide a
stimulus to the circuit being tested.
• Actually, always is a Verilog language construct specifying how the associated statement is to execute
(subject to the event control expression). The always statement executes repeatedly in a loop.
• The initial statement executes only once, starting from simulation time 0, and may continue with any
operations that are delayed by a given number of time units, as specified by the symbol #.
Digital Design Dr. Pravin Mane Page 11 of 34
• For example, consider the initial block
initial
begin
A = 0; B = 0;
#10 A = 1;
#20 A = 0; B = 1;
end
The block is enclosed between the keywords begin and end. At time 0, A and B are set to 0. Ten
time units later, A is changed to 1. Twenty time units after (at t=30), A is changed to 0 and B to 1.
• As an another example, inputs specified by a three-bit truth table can be generated with the initial
block:
initial
begin
D = 3 ’ b000 ;
repeat (7)
#10 D = D + 3 ’ b001 ;
end
When the simulator runs, the three-bit vector D is initialized to 000 at time = 0. The keyword repeat
specifies a looping statement: D is incremented by 1 seven times, once every 10 time units. The result
is a sequence of binary numbers from 000 to 111.
initial
begin
t_select = 1; t_A = 0; t_B = 1;
#10 t_A = 1; t_B = 0;
Digital Design Dr. Pravin Mane Page 12 of 34
#10 t_select = 0;
#10 t_A = 0; t_B = 1;
end
endmodule
• Single-pass behavior
– Behavior declared by the keyword initial is called single-pass behavior and specifies a single
statement or a block statement.
– A single-pass behavior expires after the associated statement executes.
– In practice, designers use single-pass behavior primarily to prescribe stimulus signals in a test
bench-never to model the behavior of a circuit-because synthesis tools do not accept descriptions
that use the initial statement.
– The initial statement is useful for generating input signals to simulate a design.
– In simulating a sequential circuit, it is necessary to generate a clock source for triggering the
flip-flops. The following are one of the possible ways to provide a free-running clock that
operates for a specified number of cycles:
initial
begin
clock = 1 ’ b0 ;
repeat (30)
#10 clock = ∼clock ;
end
initial
begin
clock = 1 ’ b0 ;
end
initial
begin
clock = 0;
forever #10 clock = ∼clock ;
end
Digital Design Dr. Pravin Mane Page 14 of 34
• Cyclic behavior
• They execute concurrently with respect to each other, starting at time 0, and may interact through
common variables.
• The activity associated with either type of behavioral statement can be controlled by a delay operator
that waits for a certain time or by an event control operator that waits for certain conditions to become
true or for specified events (changes in signals) to occur.
• Time delays specified with the # delay control operator are commonly used in single-pass behaviors.
The delay control operator suspends execution of statements until a specified time has elapsed.
• Another operator @ is called the event control operator and is used to suspend activity until an event
occurs. An event can be an unconditional change in a signal value (e.g., @ A) or a specified transition
of a signal value (e.g., @ (posedge clock)).
• The event control expression specifies the condition that must occur to launch execution of the
procedural assignment statements. The variables in the left-hand side of the procedural statements
must be of the reg data type and must be declared as such. The right-hand side can be any expression
that produces a value using Verilog-defined operators.
• The event control expression (also called the sensitivity list) specifies the events that must occur to
initiate execution of the procedural statements associated with the always block.
• Statements within the block execute sequentially from top to bottom. After the last statement executes,
the behavior waits for the event control expression to be satisfied. Then the statements are executed
again.
• The sensitivity list can specify level-sensitive events, edge-sensitive events, or a combination of the
two. In practice, designers do not make use of the third option, because this third form is not one that
synthesis tools are able to translate into physical hardware.
• Level-sensitive events occur in combinational circuits and in latches. For example, the statement
always @ ( A or B or C )
Digital Design Dr. Pravin Mane Page 15 of 34
will initiate execution of the procedural statements in the associated always block if a change occurs
in A, B, or C.
will initiate execution of the associated procedural statements only if the clock goes through a positive
transition or if reset goes through a negative transition.
– Blocking assignment
The first statement transfers the value of A into B. The second statement increments the
value of B and transfers the new value to C. At the completion of the assignments, C
contains the value of A + 1.
* Use blocking assignments when sequential ordering is imperative and in cyclic behavior
that is level sensitive (i.e., in combinational logic).
– Nonblocking assignment
When the statements are executed, the expressions on the right-hand side are evaluated and
stored in a temporary location. The value of A is kept in one storage location and the value
of B + 1 in another. After all the expressions in the block are evaluated and stored, the
assignment to the targets on the left-hand side is made. In this case, C will contain the
original value of B, plus 1.
Digital Design Dr. Pravin Mane Page 16 of 34
• D-Type Flip-Flop
– The second module includes an asynchronous reset input in addition to the synchronous clock.
– A specific form of an if statement is used to describe such a flip-flop so that the model can be
synthesized by a software tool.
– The event expression after the @ symbol in the always statement may have any number of edge
events, either posedge or negedge.
– For modeling hardware, one of the events must be a clock event. The remaining events specify
conditions under which asynchronous logic is to be executed.
– The designer knows which signal is the clock, but clock is not an identifier that software tools
automatically recognize as the synchronizing signal of a circuit. The tool must be able to infer
which signal is the clock, so you need to write the description in a way that enables the tool to
infer the clock correctly. The rules are simple to follow:
There are two edge events in the second module of HDL. The negedge rst (reset) event is
asynchronous, since it matches the if (! rst) statement. As long as rst is 0, Q is cleared to 0. If
Clk has a positive transition, its effect is blocked. Only if rst = 1 can the posedge clock event
synchronously transfer D into Q.
– Hardware always has a reset signal. It is strongly recommended that all models of edge-sensitive
behavior include a reset (or preset) input signal; otherwise, the initial state of the flip-flops of
the sequential circuit cannot be determined. A sequential circuit cannot be tested with HDL
simulation unless an initial state can be assigned with an input signal.
– Following example shows another way to describe a JK flip-flop. Here, we describe the flip-
flop by using the characteristic table rather than the characteristic equation. The case multiway
branch condition checks the two-bit number obtained by concatenating the bits of J and K. The
case expression ({J, K}) is evaluated and compared with the values in the list of statements that
follows. The first value that matches the true condition is executed. Since the concatenation of
J and K produces a two-bit number, it can be equal to 00, 01, 10, or 11. The first bit gives the
value of J and the second the value of K. The four possible conditions specify the value of the
next state of Q after the application of a positive-edge clock.
Digital Design Dr. Pravin Mane Page 19 of 34
module t_Mealy_Zero_Detector ;
wire t_y_out ;
reg t_x_in , t_clock , t_reset ;
initial
begin
t_clock = 0;
forever #5 t_clock = ∼t_clock ;
Digital Design Dr. Pravin Mane Page 22 of 34
end
initial
fork
t_reset = 0;
#2 t_reset = 1;
#87 t_reset = 0;
#89 t_reset = 1;
#10 t_x_in = 1;
#30 t_x_in = 0;
#40 t_x_in = 1;
#50 t_x_in = 0;
#52 t_x_in = 1;
#54 t_x_in = 0;
#70 t_x_in = 1;
#80 t_x_in = 1;
#70 t_x_in = 0;
#90 t_x_in = 1;
#100 t_x_in = 0;
#120 t_x_in = 1;
#160 t_x_in = 0;
#170 t_x_in = 1;
join
endmodule
– Its Verilog model uses three always blocks that execute concurrently and interact through common
variables.
– The first always statement resets the circuit to the initial state S0 = 00 and specifies the synchronous
clocked operation. The statement state <= next state is synchronized to a positive-edge transition
of the clock. This means that any change in the value of next state in the second always block
can affect the value of state only as a result of a posedge event of clock.
– The second always block determines the value of the next state transition as a function of the
present state and input. The value assigned to state by the nonblocking assignment is the value
of next state immediately before the rising edge of clock.
– The third always block specifies the output as a function of the present state and the input. Note
that the value of output y out may change if the value of input x in changes while the circuit is
in any given state.
– Summary
* At every rising edge of clock, if reset is not asserted, the state of the machine is updated by
the first always block; when state is updated by the first always block, the change in state
is detected by the sensitivity list mechanism of the second always block; then the second
always block updates the value of next state (it will be used by the first always block at the
Digital Design Dr. Pravin Mane Page 23 of 34
next tick of the clock); the third always block also detects the change in state and updates
the value of the output.
* In addition, the second and third always blocks detect changes in x in and update next state
and y out accordingly.
– The description of waveforms in the test bench uses the fork . . . join construct. Statements
with the fork . . . join block execute in parallel, so the time delays are relative to a common
reference of t = 0, the time at which the block begins execution.
– It is usually more convenient to use the fork . . . join block instead of the begin . . . end block
in describing waveforms.
* The first always block corresponds to a D flip-flop implementation of the state register.
* The second always block is the combinational logic block describing the next state.
* The third always block describes the output combinational logic of the zero-detecting
Mealy machine.
* The register operation of the state transition uses the nonblocking assignment operator
(<=) because the (edge-sensitive) flip-flops of a sequential machine are updated concurrently
by a common clock.
* The second and third always blocks describe combinational logic, which is level sensitive,
so they use the blocking (=) assignment operator. Their sensitivity lists include both the
state and the input because their logic must respond to a change in either or both of them.
–
// Moore model FSM Verilog 2001 , 2005 syntax
module Moore_Model (
output [1: 0] y_out ,
input x_in , clock , reset
);
reg [1: 0] state ;
parameter S0 = 2 ’ b00 , S1 = 2 ’ b01 , S2 = 2 ’ b10 , S3 = 2 ’
b11 ;
– Combinational logic circuits can be described in Verilog by a connection of gates (primitives and
UDPs), by dataflow statements (continuous assignments), or by level- sensitive cyclic behaviors
(always blocks).
– Sequential circuits are composed of combinational logic and flip-flops, and their HDL models
use sequential UDPs and behavioral statements (edge-sensitive cyclic behaviors) to describe the
operation of flip-flops.
– One way to describe a sequential circuit uses a combination of dataflow and behavioral statements.
The flip-flops are described with an always statement. The combinational part can be described
with assign statements and Boolean equations.
– The separate modules can be combined to form a structural model by instantiation within a
module.
endmodule
// Structural model
module Moore_Model_STR_counter (
output y_out , A , B ,
input x_in , clock , reset
);
wire TA , TB ;
// Output equation
assign y_out = A & B ;
endmodule
// endmodule
module t_Moore_counter ;
wire t_y_out_2 , t_y_out_1 ;
reg t_x_in , t_clock , t_reset ;
Moore_Model_counter M1 ( t_y_out_1 , t_x_in , t_clock ,
t_reset ) ;
Moore_Model_STR_counter M2 ( t_y_out_2 , A , B , t_x_in ,
t_clock , t_reset ) ;
initial
begin
t_reset = 0;
t_clock = 0;
#5 t_reset = 1;
repeat (16)
#5 t_clock = ∼t_clock ;
end
initial
begin
t_x_in = 0;
#15 t_x_in = 1;
repeat (8)
#10 t_x_in = ∼t_x_in ;
end
endmodule
Digital Design Dr. Pravin Mane Page 28 of 34
• Behavioral modeling describes only the operations of the register, as prescribed by a function table,
without a preconceived structure.
• A structurallevel description shows the circuit in terms of a collection of components such as gates,
flip flops, and multiplexers. The various components are instantiated to form a hierarchical description
of the design similar to a representation of a multilevel logic diagram.
• When a machine is complex, a hierarchical description creates a physical partition of the machine
into simpler and more easily described units.
• Shift Register
s1 s0 Register Operation
0 0 No change
0 1 Shift right
1 0 Shift left
– Behavioral model
– Structural model
always @ ( select , i0 , i1 , i2 , i3 )
case ( select )
2 ’ b00 : mux_out = i0 ;
2 ’ b01 : mux_out = i1 ;
2 ’ b10 : mux_out = i2 ;
2 ’ b11 : mux_out = i3 ;
endcase
endmodule
• Synchronous Counter
0 X X X Clear to 0
1 ↑ 1 X Load inputs
1 ↑ 0 0 No change
Digital Design Dr. Pravin Mane Page 32 of 34
• Ripple Counter
// Ripple counter
‘timescale 1 ns / 100 ps
initial
begin
Count = 1 ’ b0 ;
Reset = 1 ’ b1 ;
#4 Reset = 1 ’ b0 ;
end