Lab 05 Handout Combinational & Sequential
Lab 05 Handout Combinational & Sequential
Islamabad
Digital System Design LAB
Name of Student:
Roll No.:
Date of Experiment:
Marks obtained:
Remarks:
Instructor’s Signature:
2. Resources Required
• A Computer
• Xilinx ISE
• ModelSim
3. Introduction
Logic circuits for digital systems may be divided into two categories:
a) Combinational
b) Sequential
Some remaining topics of the Behavioral Modeling (that were not discussed in last lab) are also
discussed in this lab.
The left-hand side of a procedural assignment <lvalue> can be one of the following:
The right-hand side can be any expression that evaluates to a value. In behavioral modeling, all
operators discussed in Dataflow Modeling can be used in behavioral expressions. There are two
types of procedural assignment statements: blocking and nonblocking.
a) Blocking Assignments
Blocking assignment statements are executed in the order they are specified in a sequential block.
A blocking assignment will not block execution of statements that follow in a parallel block. The
= operator is used to specify blocking assignments.
In above example, the statement y = 1 is executed only after x = 0 is executed. The behavior in a
particular block is sequential in a begin-end block if blocking statements are used, because the
statements can execute only in sequence. The statement count = count + 1 is executed last. The
simulation times at which the statements are executed are as follows:
Note that for procedural assignments to registers, if the right-hand side has more bits than the
register variable, the right-hand side is truncated to match the width of the register variable. The
least significant bits are selected and the most significant bits are discarded. If the right-hand
side has fewer bits, zeros are filled in the most significant bits of the register variable.
b) Nonblocking Assignments
Nonblocking assignments allow scheduling of assignments without blocking execution of the
statements that follow in a sequential block. A <= operator is used to specify nonblocking
assignments. Note that this operator has the same symbol as a relational operator,
less_than_equal_to. The operator <= is interpreted as a relational operator in an expression
and as an assignment operator in the context of a nonblocking assignment. To illustrate the
behavior of nonblocking statements and its difference from blocking statements, let us consider
the following example.
Thus, the simulator schedules a nonblocking assignment statement to execute and continues to the
next statement in the block without waiting for the nonblocking statement to complete execution.
Typically, nonblocking assignment statements are executed last in the time step in which they are
scheduled, that is, after all the blocking assignments in that time step are executed.
In the example above, we mixed blocking and nonblocking assignments to illustrate their
behavior. However, it is recommended that blocking and nonblocking assignments not be
mixed in the same always block.
Consider the following example which is intended to swap the values of registers a and b at each
positive edge of clock, using two concurrent always blocks.
In the above example, in Illustration 1, there is a race condition when blocking statements are
used. Either a = b would be executed before b = a, or vice versa, depending on the simulator
implementation. Thus, values of registers a and b will not be swapped. Instead, both registers will
get the same value (previous value of a or b), based on the Verilog simulator implementation.
However, nonblocking statements used in Illustration 2 eliminate the race condition. At the
positive edge of clock, the values of all right-hand-side variables are "read," and the right-hand-
side expressions are evaluated and stored in temporary variables. During the write operation, the
values stored in the temporary variables are assigned to the left-hand-side variables. Separating
the read and write operations ensures that the values of registers a and b are swapped correctly,
regardless of the order in which the write operations are performed.
The following example shows how nonblocking assignments shown in Illustration 2 could be
emulated using blocking assignments.
3.6 Loops
There are four types of looping statements in Verilog: while, for, repeat, and forever. The syntax
of these loops is very similar to the syntax of loops in the C programming language. All looping
statements can appear only inside an initial or always block. Loops may contain delay expressions.
For more detail, see Chapter 7 of Verilog HDL by Samir Palinitkar.
a) Sequential blocks
The keywords begin and end are used to group statements into sequential blocks. The statements
in a sequential block are processed in the order they are specified. A statement is executed only
after its preceding statement completes execution.
b) Parallel blocks
Parallel blocks, specified by keywords fork and join, provide interesting simulation features.
Parallel blocks provide a mechanism to execute statements in parallel. Statements in a parallel
block are executed concurrently. Ordering of statements is controlled by the delay or event control
assigned to each statement. If delay or event control is specified, it is relative to the time the block
was entered.
Notice the fundamental difference between sequential and parallel blocks. All statements in a
parallel block start at the time when the block was entered. Thus, the order in which the statements
are written in the block is not important.
For more detail, see Chapter 7 of Verilog HDL by Samir Palinitkar.
output CO;
output [3:0] q_out;
input Clk, Clr, Count;
reg [3:0] q_out;
assign CO = Count & (q_out == 4’b1111);
//always block makes sure that whenever there is a change in Clk, we enter this block
//The following code is for a synchronous counter. By using “always @ (posedge Clk or
//negedge Clr)” will make it an Asynchronous counter.
always @ (posedge Clk)
begin
if(~Clr)
begin
q_out <= 4’b0000;
end
else if (Count)
begin
q_out <= q_out + 1’b1;
end
else
begin
q_out <= q_out;
end
end
endmodule
4.2 Stimulus
module Stimulus;
wire CO;
wire [3:0] q_out;
reg Clk, Clr, Count;
//Module Instantiation
Counter C1(q_out, CO, Count, Clk, Clr);
5. Lab Task
Implement a 16-bit Counter with parallel load. The Inputs and their functions are given in
the truth table. Also simulate your design for verification (Create a proper Stimulus or Test
Bench file).
Hint: Introduce a 16 bit input named value. When load is 1, then value is assigned to q_out which
must also be 16-bit. The rest is nearly identical to the code in Section 4. Remember it is positive
edge-triggered counter.
6. Home Work
Implement an Asynchronous Up-Down 8-bit Counter. Submit the code and wave files in the
next lab. Simulate in either ModelSim or Xilinx ISE.
Hint: Use the same approach as given in the Verilog codes given in Section 4. Introduce another
input up_down that when 1 increments q_out otherwise decrement it. This condition must be
nested under the condition where value of count is being checked as it behaves like an enable
switch. q_out’s size is 8-bit in this case. Remember it is positive edge-triggered counter.
Note:
a) This assignment must be submitted before the next lab.
b) The assignment submitted must be in proper format as instructed by the teacher to get
maximum marks.
c) Marks will be deducted on late submissions.
d) Cheating or using any unfair means will award ZERO marks.
Q.1 What are the types of logic circuits? Differentiate between them.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Q.3 How many blocks are available in Verilog? Differentiate between them.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________