Module 5 PDF
Module 5 PDF
Module 5 PDF
The two main HDLs are Verilog and VHDL for digital
system design.
MP Sim
Verilog Code
port declarations
— Interface: consisting of port and parameter declarations
parameter declarations
`include directives
— optional add-ons
variable declarations
assignments
— body: specification of internal low-level module instantiation
part of the module initial and always blocks
task and function
endmodule
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 13
MODULE - EXAMPLE
It begin with back slash (\) and end with white space
VALUE SET
Unsized numbers
Default base is decimal
Default size is at least 32 (depends on Verilog compiler)
Examples
23232
’habc
’o234
Negative numbers
Put the sign before the <size>
Examples:
-6’d35
4’d-12 // illegal
Strings
As in C, use double-quotes
Examples:
“Hello world!”
“a / b”
“text\tcolumn1\bcolumn2\n”
Nets are one-bit value unless they are declared as vectors and
default value of net is Z
Examples: wire a; wire b, c; wire d=1’b0;
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 30
DATA TYPES
NET TYPES
A net declaration starts with keyword wire addr
……
Processor
Memory
wire r_w; // scalar signal data
wire [7:0] data; // vector signal
wire [9:0] addr; // vector signal r_w
……
Different kinds of net data types are:
Processor
inout [7:0] i_o;
Memory
wire r_w; data[7:0]
wire [7:0] data; status[3:0]
wire [9:0] addr; i_o[7:0] r_w
……
endmodule
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 32
DATA TYPES
REGISTERS
A register type represents data storage elements or a variable that can
hold a value
It can assign a value only within always or initial statement and default
value of register is X
Examples:
integer count;
integer addr [3:0];
initial
begin
count=count+1;
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 34
DATA TYPES
REGISTERS TYPES
(b) REAL
Real registers are used to specify the variable in decimal or scientific
notation and they are declared with the keyword real
Real numbers cannot have a range declaration and their default value is 0,
its syntax is given as
Real real_reg1, real_reg2,……………….real_regn
Examples:
real pwd;
initial
begin
pwd=3e5;
pwd=2.13;
end
They are used to store simulation time and it is declared with the keyword
time
Examples:
time currtime;
initial
begin
currtime=$time;
end
Array types are used for reg, integer, data types and array are accessed by:
<array_name>[<subscript>]
Each element of array is known as element or word and it is addressed by single array
index
Parameter can be assigned a value only once using declaration and its form is
Example:
……
parameter bussize = 8;
Parameter bit=1, byte=8;
reg [bussize-1 : 0] databus;
……
• Behavioral Modeling
– Can use only reg data type
(within initial and always constructs)
– Cannot use wire data type
+, -, *, /, %,**
If any operand is x the result is x
Negative registers:
-regs can be assigned negative but are treated as unsigned
reg [15:0] regA;
..
regA = -4’d12; // stored as 216-12 = 65524
regA/3 evaluates to 21861
|| logical OR
! logical NOT
| OR
^ XOR
~& NAND
~| NOR
~^ or ^~ XNOR
Replication ..
catr = {4{a}, b, 2{c}}; // catr = 1111_010_101101
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 50
OPERATORS
CONDITIONAL OPERATORS
cond_expr ? true_expr : false_expr
A
1
Y
B Y = (sel)? A : B;
0
sel
Use parentheses to
enforce your priority
STRUCTURAL
BEHAVIORAL
SWITCH
OR GATE
// Module Name: Orgate
module Orgate(i1, i2, out);
input i1;
input i2;
output out;
or(out,i1,i2);
endmodule
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 56
STRUCTURAL MODELING
NAND GATE
// Module Name: Nandgate
module Nandgate(i1, i2, out);
input i1;
input i2;
output out;
nand(out,i1,i2);
endmodule
NOR GATE
// Module Name: Norgate
module Norgate(i1, i2, out);
input i1;
input i2;
output out;
nor(out,i1,i2);
endmodule
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 57
STRUCTURAL MODELING
XOR GATE
BUFFER GATE
// Module Name: Buffer
module Buffer(in, out);
input in;
output out;
buf(out,in);
endmodule
NOT GATE
// Module Name: Notgate
module Notgate(in, out);
input in;
output out;
not(out,in);
endmodule
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 59
STRUCTURAL MODELING
Basic syntax for tristate-gates is:
Tristate_type [instance_name] (output,input,control);
BUFIF1 GATE
// Module Name: Bufif1
in out module Bufif1(in, out, con);
input in,con;
Out=in if con=1
output out;
con Else out=Z
bufif1(out,in,con);
endmodule
BUFIF0 GATE
// Module Name: Bufif0
module Bufif0(in, out, con);
in out input in,con;
Out=in if con=0 output out;
con Else out=Z bufif0(out,in,con);
endmodule
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 60
STRUCTURAL MODELING
NOTIF1 GATE
NOTIF0 GATE
Binary Subtractor
Parallel Adder
Binary Multiplier
Magnitude Comparator-4 bit
Decoders
Encoders
Mux
Demux
Parity generator and checker.
The if, case, for loop, and while loop must appear inside an always
block
For modules that have multiple always blocks all of the always
blocks are executed in parallel
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 81
BEHAVIORAL MODELING
PROCEDURAL BLOCK - ALWAYS STATEMENT
Always blocks are repeated executed until simulation is stopped. Similar to
initial block it begin its execution at start of simulation time 0.
module clock_gen;
reg clock, temp;
Blocking procedural assignment is executed before any of the statement that follow it are
executed
Example: // Full adder using blocking procedural statement
module FA(sum,cout,A,B,C)
input A,B,C;
output sum, cout;
always @ ( A or B or C)
begin
reg C1,C2,C3;
sum=A&C;
C1=A&B;
C2=B&C;
C3=A&C;
Cout= C1|C2|C3;
end
endmodule
The sum assignment occurs first, sum is computed, then second statement is executes C1
is assigned and then third executed and C2 is assigned and so on
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 84
BEHAVIORAL MODELING
NON-BLOCKING PROCEDURAL ASSIGNMENT
The main characteristics of non-blocking assignment statement is
execution is performed concurrently
Example: c<=a&b;
In non-blocking assignment the assignment symbol “<=“ is used
Example:
always @ ( posedge clk)
begin
a<=b;
@ (negedge clk)
c<=b&(~c);
#2 b<=c;
endmodule
‘a’ is assigned the stored value of ‘b’ this activity is carried out concurrently
At the negative edge clock ‘c’ is assigned a value of b&(~c)
Two nanoseconds later positive edge clock assign ‘c’ value to ‘b’
Blocking Non-blocking
<variable> = <statement> <variable> <= <statement>
The keywords begin and end are used to group statements into sequential blocks.
Sequential blocks have the following characteristics:
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
If delay or event control is specified, it is relative to the simulation time when the previous statement in the
block completed execution
//Illustration 1: Sequential block without delay //Illustration 2: Sequential blocks with delay
reg X, Y; reg x, y;
reg [1:0] z, w; reg [1:0] z , w;
initial initial
Begin begin
x = l'bO; x = l'bo; //completes at simulation time 0
y = l‘b1; #5 y = l'bl; //completes at simulation time 5
z = {x, y}; #10 z = {x, y}; //completes at simulation time 15
w = {y, x}; #20 w = {y, x); //completes at simulation time 35
End end
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. //Example : Parallel blocks with delay
reg x, y;
reg [ 1 : 0 ] z, w;
The result of simulation remains the same initial
fork
except that all statements start in parallel x = l'bO; //completes at simulation time 0
#5 y = l'b1; //completes at simulation time 5
at time 0. Hence, the block finishes at #10 z {x, y}; //completes at simulation time 10
time 20 instead of time 35. #20 w = {y, x}; //completes at simulation time
join
These conditions are used to decide whether or not a statement should execute
The expression is compared to the alternatives in the order they are written
For the first alternative that matches, the corresponding statement or block is
executed. If none of the alternatives match, the default_statement is executed.
The default_statement is optional. module mux4_to_1 (out, iO, i1, i2, i3, sl, sO);
input iO, i1, i2, i3;
input s1. sO; //Port declarations from the I/O diagram
output out;
syntax:
reg out;
always @(sl or sO or iO or i1 or i2 or i3)
case(case_expression)
case ({sl, sO}) //Switch based on concatenation signals
case_item_expression {case_item_expression}
2'dO out iO;
procedural statement
2'd1 out i1;
………
2'd2 out i2;
………
2'd3 out i3;
default:procedural_statement
default: $display("Invalid control signals");
endcase
endcase
endmodule
The while loop executes until the while-expression becomes false. If the loop is
entered when the while-expression is false, the loop is not executed at all
A repeat construct cannot be used to loop on a general logical expression. A while loop is
used for that purpose.
A repeat construct must contain a number, which can be a constant, or a variable value.
However, if the number is a variable or signal value, it is evaluated only when the loop
starts and not during the loop execution. //EXAMPLE : increment and display count from a to 127
integer count;
initial
begin
syntax:
count = 0;
repeat(128)
repeat [loop count] begin
procedural_statement $display("Count = %d", count);
count = count + 1;
end
end
The loop does not contain any expression and executes forever until the $finish
task is encountered.
The loop is equivalent to a while loop with an expression that always true, e.g.,
while (1).
The assignment statement start with the keyword assign and results are
assigned to nets
Continuous assignment – most basic statement used to drive value onto net
Implicit net declaration – if a signal name of the left hand side of the continuous
assignment statement is not declared the verilog simulator assign an implicit net
declaration for the net
Net declaration delay – a delay can be specified on a net when it is declared without
putting a continuous assignment on the net
Example: wire #5 c;
assign c=a&b;
OPERANDS
Operands are the data types used in the expression
An operands can be constant, net, parameter, register, memory, bit select
Example: c = a + b // a, b, c are real operands
OPERATORS
Operators act on operands to produce desired result
Various types: arithmetic, logical, relational, equality, bitwise, shift, etc.,
Example: c = a % b // % is operator to perform modules operation on a,b
MODULE-5 ECE2003 – DIGITAL LOGIC DESIGN 107
DATAFLOW MODELING
module mux(y,s,i);
input [2:0]s;
input[7:0]i;
output y;
assign y= (~s[2] & ~s[1] & ~s[0] & i[1]) | (~s[2] &
~s[1] & s[0] & i[2]) | (~s[2] & s[1] & ~s[0] &
i[3]) | (~s[2] & s[1] & s[0] & i[4]) | (s[2] & ~s[1] &
~s[0] & i[5]) | (~s[2] & s[1] & ~s[0] & i[6]) | (s[2] &
s[1] & ~s[0] & i[7]) | (s[2] & s[1] & s[0] & i[8]);
endmodule
module binary_to_gray(g,b);
input [3:0]b;
output[3:0]g;
assign g[3]= b[3];
assign g[2]= b[2]^b[3];
assign g[1]= b[1]^b[2];
assign g[0]= b[0]^b[1];
endmodule
Examples:
$display("%d %b %b",A,B,C);
$monitor($time„"%b %b %h" , s1,s2,outvalue);
TEST BENCH:
module dflipflopt_b;
reg d;
D FLIPFLOP reg clk;
module dflipflopmod(q, d, clk);
wire q;
output q;
input d;
dflipflopmod uut (.q(q),.d(d), .clk(clk) );
input clk; initial begin
reg q; // Initialize Inputs
always @(posedge clk) d = 0;
q=d; clk = 0;
endmodule end
always #3 clk=~clk;
always #5 d=~d;
endmodule
Examine the code below and identify any errors that would prevent this
code from compiling and synthesizing properly. These errors may include
syntax errors, logical design errors or needed lines that are missing from
the code. Indicate the modifications you would make to the code to make
it work. Include comments beside these modifications or the portion of
the code that is incorrect. There are 20 errors in the code below.
over_flow = data_out(7);
end module