Verilog Lab Behavioral Level
Verilog Lab Behavioral Level
Visakhapatnam
It is the highest level of design abstraction and is essentially at the system level. The circuit
can be designed in terms of its key modular functions and their behaviour. The constructs are
similar to the “C” language. The design can be simulated, debugged, and finalized. The
design can be expanding by describing the modules in terms of components closer to the data
flow and gate level models. Simulate and debugging of each such component module, check
the functionality, integrate it with the main design and test conformity.
OPERATIONS AND ASSIGNMENTS:
The design at this level is done through a sequence of assignments called ‘procedural
assignments’ in contrast to the continuous assignments at the data flow level. Though it
appears similar to the assignments at the data flow level, the two are different. The procedure
assignment is characterized by the following:
• The assignment is done through the “=” or “<=” symbols, it was the case with the
continuous assignment earlier. An operation is carried out and the result assigned
through the “=” operator to an operand specified on the left side of the “=” sign.
• For example, N = ~N; Here the content of reg N is complemented and assigned to the
reg N itself.
• The operands on the right side can be of the net or variable type. They can be scalars
or vectors.
• The operand to the left of the “=” operator has to be of the variable (e.g. reg) type.
All procedural blocks are automatically activated at time 0.All procedural blocks are
executed concurrently
Procedural assignments:
The assignment statements that can be used inside the procedural blocks are called procedural
o Blocking assignment(=)
o Non-blocking assignment(<=)
initial #2 a=0
Block Statements: Block statements are used to group two or more statements together, so
that they act as one statement. There are two types of blocks:
• Sequential block.
• Parallel block.
Sequential block: This block is defined using the keywords begin and end. The procedural
statements in this block executes sequentially in the given order. In sequential block delay
values for each statement shall be treated relative to the simulation time of the execution of
the previous statement. The control will pass out of the block after the execution of last
statement.
Parallel block: The parallel block is defined using the keywords fork and join. The
procedural statements in parallel block will be executed concurrently. In parallel block delay
values for each statement are considered to be relative to the simulation time of entering the
block. The delay control can be used to provide time-ordering for procedural assignments.
The control shall pass out of the block after the execution of the last time-ordered statement.
Event Control: The always block is executed repeatedly and endlessly. It is necessary to
specify a condition or a set of conditions, which will steer the system to the execution of the
block. Alternately such a flagging-off can be done by specifying an event preceded by the
symbol “@”. The event can be a change in the variable specified in either direction or a
change in a specified direction. For example,
• @(negedge clk) : Executes the following block at the negative edge of the reg
(variable) clk
The “negedge” transition for a signal on a net can be of three different types:-
✓ 1 to 0
✓ 1 to x or z
✓ x or z to 0
• @(posedge clk) : Executes the following block at the positive edge of the reg
(variable) clk
The “posedge” transition for a signal on a net can be of three different types:
✓ 0 to1
✓ 0 to x or z
✓ x or z to 1
• @clk :Executes the following block at both the edges of clk
Conditional (if-else) Statement: The condition (if-else) statement is used to make a decision
whether a statement is executed or not. The keywords if and else are used to make
conditional statement. The conditional statement can appear in the following forms.
if ( condition_1 ) statement_1;
if ( condition_2 ) statement_2;
else statement_3;
if ( condition_3 ) statement_4;
else if ( condition_4 )statement_5;
else statement_6;
if ( condition_5 )
begin
statement_7; statement_8;
end
else
begin
statement_9; statement_10;
end
Case Statement: Case statement is a multi-way decision statement that tests whether an
expression matches one of the expressions and branches accordingly. Keywords case and
endcase are used to make a case statement. The syntax is as follows.
case (expression)
case_item_1: statement_1;
case_item_2: statement_2;
case_item_3: statement_3;
...
...
default: default_statement;
endcase
If there are multiple statements under a single match, then they are grouped using begin, and
end keywords. The default item is optional.
Case statement with don't cares: casez and casex
CaseZ: it treats high-impedance values (z) as don't cares.
CaseX: it treats both high-impedance (z) and unknown (x) values as don't care. Don't-care
values (z values for casez, z and x values for casex) in any bit of either the case expression or
the case items shall be treated as don't-care conditions during the comparison, and that bit
position shall not be considered. The don't cares are represented using the ? mark.
Loop Statements: There are four types of looping statements in Verilog:
• forever
• repeat
• while
• for
Forever Loop: this loop is defined using the keyword forever, which Continuously executes
a statement. It terminates when the system task $finish is called. A forever loop can also be
ended by using the disable statement.
initial
begin
clk = 1'b0;
forever #5 clk = ~clk;
end
In the above example, a clock signal with time period 10 units of time is obtained.
Repeat Loop: This loop is defined using the keyword repeat. The repeat loop block
continuously executes for a given number of times. The number of times the loop executes
can be mention using a constant or an expression. The expression is calculated only once,
before the start of loop and not during the execution of the loop. If the expression value turns
out to be z or x, then it is treated as zero, and hence loop block is not executed at all.
initial
begin
a = 10;
b = 5;
b <= #10 10;
i = 0;
repeat(a*b)
begin
$display("repeat in progress");
#1 i = i + 1;
end
end
In the above example the loop block is executed only 50 times, and not 100 times. It
calculates (a*b) at the beginning, and uses that value only.
While Loop: The while loop is defined using the keyword while. The while loop contains an
expression, the loop continues until the expression is true. It terminates when the expression
initial
begin
a = 20;
i = 0;
while (i< a)
begin
$display("%d",i);
i = i + 1;
a = a - 1;
end
end
In the above example the loop executes for 10 times. ( observe that a is decrementing by one
and i is incrementing by one, so loop terminated when both i and a become 10).
For Loop: The For loop is defined using the keyword for. The execution of for loop block is
controlled by a three step process, as follows:
The above example produces the same result as the example used to illustrate the
functionality of the while loop.
1.D Latch: A latch is a storage device used to store 1 bit of digital data. It is
the simplest form of flip flop without the use of a clock and latch is a level
sensitive device.
BLOCK DIAGRAM:
D Flip Flop with synchronous and asynchronous resets: In asynchronous reset the Flip
Flop does not wait for the clock and sets the output right at the edge of the reset. In
Synchronous Reset, the Flip Flop waits for the next edge of the clock (rising or falling as
designed), before applying the Reset of Data.
1. The Asynchronous implementation is fast, as it does not has to wait for the clock
signal to be applied. The adds only slight advantage in timing that too at the time of
reset.
2. In Synchronous implementation, we must make sure that the reset signal stays low (
or high as programmed) for it to take effect. If the duration is too short, it may miss
the next rising or falling edge of clock.
3. The asynchronous reset can lead to metastability issues. To understand the
metastability issue consider that the clock rising edge comes right after the reset edge.
The D flip Flop must have certain minimum time between reset edge and clock edge,
called reset recovery time. If this time duration is violated, the output is not
guaranteed. With synchronous implementation, this issue does not happen.
endmodule
T Flip-Flop:
Design Testbench
module T_FF(input clk,input rst, input t, module T_FF_tb;
output reg q); reg clk;
always @ (posedge clk) begin reg rst;
if (!rst) reg t;
q <= 0; wire q;
else T_FF uut(.clk(clk),.rst(rst),.t(t),.q(q));
if (t) always #5 clk = ~clk;
q <= ~q; initial begin
else {rst, clk, t} <= 0;
q <= q; #5 rst = 1'b1;
end #10 rst = 1'b0;
endmodule #15 rst = 1'b1;
#20 rst = 1'b0;
#45 $finish;
end
always #10 clk=~clk;
always #6t=~t;
always @(posedge clk,negedge rst)
$strobe("time =%0t \t INPUT VALUES \t t
=%b rst =%b \t OUTPUT VALUES q
=%d",$time,t,rst,q);
endmodule
Synchronous reset
Shift Registers: Shift registers used to shift the data either to right or left. Shift registers are
three types
❖ Right shift
❖ Left shift
❖ Bidirectional shift
Right shift register: To shift the least significant digit first, as when addition is to be carried
out serially. In that case a shift right register is used as in Figure 2 input data is applied to
stage D and shifted right. The shift operation is the same as discussed in Shift Left Register
except that data transfers to the right. Table 4 shows the action of shifting all logical 1 inputs
into an initially reset shift register.
RING COUNTER
Definition: A ring counter is a type of counter composed of a circular shift register. The
output of the last shift register is fed to the input of the first register. Ring counters are
implemented using shift registers. It is essentially a circulating shift register connected so that
the last flip-flop shifts its value into the first flip-flop. There is usually only a single 1
circulating in the register, as long as clock pulses are applied.
A Johnson counter is a digital circuit with a series of FFs connected together. The
complement output of the last FF is fed back to the input of first FF. This is similar to ring
counter except this. When the circuit is reset, all the FF outputs are made zero. An n-FF
Johnson counter has MOD-2n states. i.e the counter has 2n different states.
Counters:
UP COUNTER:
Down counter