Verilog
Verilog
INTRODUCTION TO VERILOG:
Verilog is a hardware description language (HDL) that describes
the functionality of hardware design and the synthesis tool converts
hardware descriptions into an actual design that has combinational
and sequential elements. Verilog language is simpler than VHDL
Verilog is based on ‘C’ language, whereas VHDL is based on Ada and
Pascal languages.
Latest Verilog standards : IEEE standard 1364-2005.
Verilog is basically a structural language and defines four
abstraction levels to implement modules . With respect to the external
environment , the module is viewed as identical irrespective of
abstraction levels. But internal module implementation differ on
abstraction.
TECH EVOLUTION:
SSI (Small scale integration).
MSI (Medium scale integration).
LSI (Large scale integration).
VLSI (Very large scale integration).
HARDWARE DISCRIPTION LANGUAGE:
Hardware description language (HDL) used to model electronic
systems. It is most commonly used in the design and verification
of digital circuits at the register-transfer level of abstraction. It is also
used in the verification of analog circuits and mixed-signal circuits.
WHY VERILOG HDL’S:
Verilog HDL’S used to model electronic systems (or) to design
hardware circuits.
Verilog HDL allows different levels of abstraction to be mixed in the
same model.
Verilog HDL’s is similar to computer programming to understand.
Most synthesis tool support.
Easy to learn and easy to use.
VERILOG (VS ) VHDL:
PORTS:
An interface to communicate with other modules or a test bench
environment is called a port. In simple words, the input/ output
pins of digital design are known as ports.This interface is termed a
port interface or port list.
LEXICAL CONVENTIONS:
1) Comments
2) White spaces
3) Number specification
4) Strings
5) Identifiers
6) Keywords
7) Operators
COMMENTS:
Comments are used for better understanding and to describe any
functionality in simple language.
Comments are two types:
Single line comment ( starts with //)
//This is the single line comment
Multiple lines comment (multiple lines can be enclosed as
comments within /* and */ ).
/* This is the first line
This is the second line
This is the third line
….
*/
WHITE SPACES:
Is a term used to represent the characters for spaces, tabs, and
newlines.
It helps in making of code easier to read.
“\t” -tabs
“\n” - newlines
“\b” -blankspaces.
NUMBER SPECIFICATION:
The numbers can be specified in two ways
Sized
Un sized
SIZED:
Sized is specified
Underscore is used separate the bits for redability.
Syntax: <size>`<radix> <number>
Ex:4`b1111 - 4 bit binary number
12`habc-12`b1010_1011_1100 - 12 bit hexadecimal number
UNSIZED:
Size is not specified and tool it has the default number of bits
(usually 32-bits).
Syntax: `<radix> <number>
IDENTIFIERS:
The identifiers are the names given to the objects (or) written beside of
the keywords.
Program:
module bitwise_operators;
reg[3:0]a,b,c,d;
reg[2:0]x,y,z;
initial begin
$monitor(“x=%0b, y=%0b, z=%0b”,x,y,z);
a=2;b=5;c=3’b101;
x = a&b;
y =c|d;
z =a^b;
end
endmodule
LOGICAL:
Logical operators operates on individual bits of operands
Logical operators provides result either as true (or) false (or) either .
Program:
module logical_operator;
reg[3:0] a,b,c,d;
reg[3:0] x,y,z;
initial begin
$monitor(“x=%0b, y=%0b, z=%0b”,x,y,z);
a=6;b=2;c=4’b1001;d=4’b0000;
x=a&&b;
y=c||d;
z=!d;
end
endmodule
ARITHMETIC:
It performs arithmetic operation between two operands.
There are two types of operators unary and binary.
If the result of any expression is ‘x’ , the result of the expression
is ‘x’.
REDUCTION:
Reduction operator is used to convert vector to scalar .
Reduction operator , operates on all bits in a vector to convert the
answer in a single bit.
In reduction operator we are having only one operand.
Program:
module reduction_operator;
reg[2:0] a,b,c;
reg[2:0] x,y,z;
initial begin
a='b1001;b='b100;c='b0000;
x=(&a);
y=(~|b);
z=(|a);
$monitor("x=%b,y=%b,z=%b",x,y,z);
end
endmodule
RELATIONAL:
The relational operator compare operands (or) two values and
returns either 1(or) 0 as a result.
Program:
module relational_operators;
reg[3:0] a,b,c,d;
reg[3:0] w,x,y,z;
initial begin
a=5;b=3'b101;c=4'b1000;d=4'b1111;
x=a>b;
y=b<a;
z=c>=d;
w=d<=c;
$display("x=%b,y=%b,z=%b,w=%b",w,x,y,z);
end
Endmodule
EQUALITY OPERATOR:
The equality and in equality operator compares the values (or) two
operands and results 1 (or) 0.
If the values (or) either one operand is ‘x’ then it will return
the value as ‘x’.
case equality never produces an ‘x’ result it produces only ‘1’
(or) ‘0’.
Equality will produce an ‘x’ result if any of the operands has ‘x’
bits.
Program:
module equality_operators;
reg[3:0] a,b,c,d;
reg[3:0] w,x,y,z;
initial begin
a=6;b=2;c=4'b1001;d=4'0000;
x=a>b;
y=c<d;
z=a>=c;
w=d<=b;
$display("x=%b,y=%b,z=%b,w=%b",w,x,y,z);
end
endmodule
SHIFT OPERATOR:
Shift operator are used to shift the operators either right (or) left.
The shifted bits are lost and zero is filled.
(<<) - left shift
(>>) - right shift
Program:
module shift_operator;
reg[3:0] a;
reg[3:0] x;
initial begin
a=1001;
x=a<<2;
$monitor("x=%b,a=%b",x,a);
end
endmodule
CONCATENATION OPERATOR [{}]:
It combines bits of 2(or) more data
Ex: a=3’b101
b=2’b10
c=4’b1001
then y={a,b} = 5’b10110
Y={c,b,a}=9’b100110101
Program:
module concatenation_operators;
reg[2:0] a,b,c,d;
reg[4:0] w,x,y,z;
initial begin
a=1'b1;b=3'b101;c=2'b11;d=2'b10;
w={b,c};
x={a,b,c,2'b01};
y={a,b[2],c[0]};
z={b[1],d[1]};
$display("a=%b,b=%b,c=%b,d=%b,w=%b,x=%b,y=%b,z=%b",a,b,c,d,w,x,y,z);
end
endmodule
REPLICATION OPERATOR:
Program:
module replication_operators;
reg[2:0] a,b,c,d;
reg[15:0] x,y,z;
initial begin
a=1'b1;b=2'b00;c=2'b10;
x={4{a[0]}};
y={ {4{a[0]}},{2{b[1]}} };
z={ {4{a[0]}},{2{b}},c[1]};
$display("x=%b,y=%b,z=%b",x,y,z);
end
endmodule
CONDITIONAL OPERATOR:
It is used to evaluate the conditions (or) logical expression
Syntax:
<result>=<conditional_expression>?<true_expression>:
<false_expression>
Ex: 3’b101,b=3’b100
Y=(a>b) ? (a+b) : (a-b)
Y=(a= =b) ? (~a) : (a&b)
Program:
module conditional_operators;
reg a,b,x;
reg y;
initial begin
a=0;b=1;x=1;
assign y=x?a:b;
$display("the result y=%b",y);
end
endmodule
LEVELS OF ABSTARCTION:
Verilog has a capability of designing a module in several coding
styles depending on the needs of a design , each module can be
defined at four levels of abstraction.
They are :
1. Behaviour level
2. Data flow level
3. Gate level/structural level
4. Switch level
1.BEHAVIOUR LEVEL:
In behaviour model the module is implemented based on the
behaviour of the circuit without of the hardware implementation.
2.DATAFLOW LEVEL:
In data flow level the module is implemented by specifying the data
flow.
It is similar to the logical expression.
In data flow level by seeing the data we can realize how the data is
flowing from input to the output .
3.GATE LEVEL:
In these method the module is implemented in terms of the logic
gates and interconnections between the gates.
4.SWITCH LEVEL:
In these method the module is implemented in terms of the
transistors, switches and interconnections between them.
DATA TYPES:
The data storage and transmission elements found in digital
hardware are represented by data types.
Verilog data types are 4- state, they can take on 4 values they are
0,X,1,Z.
0 - Represents number ‘0’ (or) logic ‘0’ (or) false ‘0’.
1- Represents number ‘1’ (or) logic ‘1’ (or) false ‘1’.
X- Represents unknown logic value.
Z- Represents hign impedance.
The verilog data types are divided into 2 types .
They are:
NET
REG
NET:
The net data type represents the physical connection between
gates (or) modules (or) structural entities.The net data type does not
store any value.The value changes when the driver changes the value.
Net data types can only be assigned values by continuous assignment
or driven it from an output port.These net data types we have to use
with output ports. Default value of wire ‘z’.
SYNTAX: net_type[range] list_of_variables.
NET TYPES:
Wire , tri.
Supply 1, supply 0.
Wor , trior.
Wand , triand.
Tri 1, tri 0 are non- synthesizable.
REG:
Reg data type represents the storage.The register variables are used in
procedural block which values from one assignment to the next.
Reg data type is used to hold the value.Default value of reg is ‘x’.
REG TYPES:
integer
real
reg
time
real time
time and realtime for storing simulation times in test benches.
Syntax:
integer list _ of _register _ variables;
real list_ of_ variables;
reg [range] list _ of_ register_variables;
time list _ of_ register_variables;
COMPILER DIRECTIVES :
The compiler directives are used to specify certain information
and ask the complier to process it.Verilog has different types of
compiler directives to set the timescale of stimulation, control the
compiler flow.
Complier directives start with the ( ` ) back quote symbol.The most
commonly used compiler directives are `include,`define and `timescale.
`include:
Include directive is used when a module defined in a file needs to
be included in another file
Syntax:
`include “file_name”
`define:
This directive is used to declare a macro (or) to define a data type.
Macros are code that can be used to perform some tasks.
Syntax:
`define name code
`timescale:
This directive is used to define the timescale of the timescale.
Timescale is divided into two types:
Time unit
Time precision.
SYSTEM TASK:
The system tasks are used to perform some operations like
displaying the messages, terminating simulation, generating
random number etc.
Syntax: $<keyword>
$MONITOR:
The $monitor automatically print out variable (or) expression
values whenever the variable (or) expression in its argument list
changes.
Example:
module tb;
reg a,b;
monitor dut(a,b);
initial begin
$monitor("a=%b,b=%b,s=%b,c=%b",a,b,s,c);
a=0;b=0;
#5;
a=0;b=1;
#5;
a=1;b=0;
#5;
a=1;b=1;
#5;
end
endmodule
$DISPLAY:
Is the normal display, which executes its parameters wherever it is
present in the code.
$WRITE:
Is similar to $display except that $display displays the contents in
the next line (cursor moves to the next line before displaying),
unlike $write in which the contents are displayed in the same line.
$STROBE:
Executes only once in a time instant, when all processes in that
time instant have executed.
Example:
module tb;
reg a=0;b=0;
initial begin
a=0;
b=1;
$display(a,b);
$strobe(a,b);
b=0;
a=1;
$display(a,b);
$write(a,b);
#1 b=1;
$display(a,b);
end
endmodule
DATA FLOW MODELLING:
// IMPLICIT TYPE:
module and2(a,b,x);
input a,b;
output x;
wire x=a&b; //continuous implicit statement
endmodule
module and2(a,b,x);
input a,b;
output x;
wire #2 x=a&b; // continuous implicit delay statement
endmodule
PROGRAM:
// EXPLICIT TYPE:
module and2(a,b,x);
input a,b;
output x;
//output wire x;
wire x;
assign x=a&b;
endmodule
Logic gates:
module all_gates(a,b,p,q,r,s,t,u,v); // dut for all logic gates
input a,b;
output p,q,r,s,t,u,v;
assign p= (a&b);
assign q= (a|b);
assign r= ~(a&b);
assign s= ~(a|b);
assign t= (a^b);
assign u= ~(a^b);
assign v= !(a);
endmodule
HALF ADDER:
The half adder is a basic building block of adding two numbers and
produces two outputs.
The carry and sum are the two outputs .
BLOCK DIAGRAM:
TRUTH TABLE:
‘A’ and ‘B’ are the inputs and ‘sum’ and ‘carry’ are the outputs.
Sum = x'y+xy'
Carry = xy
PROGRAM:
module half_adder(a,b,s,c);
input a,b;
output s,c;
assign s=a^b;
assign c=a&b;
endmodule
STIMULATION RESULT:
FULL ADDER:
Full adder is used to add three inputs and produces two outputs.
The three inputs are A,B and Cin (input carry).
The two outputs are sum and carry(Cout).
BLOCK DIAGRAM:
TRUTH TABLE:
STIMULATION RESULT:
HALF SUBSTRACTOR:
The half substractor is a basic building block of subtracting two
binary numbers .
It has two inputs and produces the two outputs.
The difference and the borrow are the outputs of the half
substractor.
BLOCK DIAGRAM:
TRUTH TABLE:
EXPRESSION:
Diff= A'B+AB'
Borrow = A'B
‘A’ and ‘ B’ are the inputs .
‘ Diff ’ and ‘ Borrow ’ are the outputs.
Program:
//dut for half substractor
module half_substractor(a,b,D,B);
input a,b;
output D,B;
assign D=a^b;
assign B=~a&b;
Endmodule
STIMULATION RESULT:
FULL SUBSTRACTOR:
TRUTH TABLE:
‘ Diff ’ and ‘ Borrow ’ are the output variables that defines the
output values.
PROGRAM:
STIMULATION RESULT:
MULTIPLEXER:
Multiplexer(MUX) is a combinational logic circuit that has 2n input
lines and a single output line and selection line.
The binary information received from the input line and directed to
the output line.
On the basis of selection line, one of these data inputs will be
connected to the output.
2x1 MUX:
It has 2 inputs A1,A0 and 1 selection line (s) and 1 output ( Y ).
On the bases of the selection line (s) one of these inputs are
connected to the output.
Block diagram:
TRUTH TABLE:
EXPRESSION:
Y=S0'.A0+S0.A1
Program:
STIMULATION RESULT:
4x1 MUX:
In 4x1 mux there are 4 inputs A0,A1,A2,A3 and two selection
lines S0,S1 and single output Y.
BLOCK DIAGRAM:
TRUTH TABLE:
EXPRESSION:
Y=S1' S0' A0+S1' S0 A1+S1 S0' A2+S1 S0 A3
PROGRAM:
//dut for 4x1 mux
module mux_4x1(y,s1,s0,i3,i2,i1,i0);
output y;
input s1,s0,i3,i2,i1,i0;
assign y=(~s1&~s0&i0)|
(~s1&s0&i1)|
(s1&~s0&i2)|
(s1&s0&i3);
endmodule
// testbench for 4x1 mux
module tb_mux_4x1;
wire y;
reg s1,s0,i3,i2,i1,i0;
mux_4x1 dut (y,s1,s0,i3,i2,i1,i0);
initial begin
$monitor("time=%d,s1=%b,s0=%b,i3=%b,i2=%b,i1=%b,i0=%b,y=%b",$time,s1,s0,i3,i2
,i1,i0,y);
{s1,s0,i3,i2,i1,i0}=6'b000000;
#5;
{s1,s0,i3,i2,i1,i0}=6'b000001;
#5;
{s1,s0,i3,i2,i1,i0}=6'b010001;
#5;
{s1,s0,i3,i2,i1,i0}=6'b010011;
#5;
{s1,s0,i3,i2,i1,i0}=6'b100011;
#5;
{s1,s0,i3,i2,i1,i0}=6'b100111;
#5;
{s1,s0,i3,i2,i1,i0}=6'b110111;
#5;
{s1,s0,i3,i2,i1,i0}=6'b111111;
#5;
end
endmodule
STIMULATION RESULT:
DEMULTIPLEXER:
A demultiplexer is a combinational circuit that has only single
input and 2N output line and one selection line and one output.
The information received from the single input line and directed to
the output lines.
On the basis of the values of the selection lines , the input line is
connected to the one of these output lines.
1x2 demultiplexer:
The 1x2 demultiplexer has one single input ( A ) and two outputs
Y0, Y1 and one selection line.
BLOCK DIAGRAM:
TRUTH TABLE:
EXPRESSION:
Y0=S0'.A
Y1=S0.A
PROGRAM:
//dut for demux 1x2
module demux_1x2(y0,y1,a,s);
input a,s;
output y1,y0;
assign y0=~s&a;
assign y1=s&a;
endmodule
ENCODER:
The combinational circuit that change the binary information into
N output line.
The binary information is passed in the form of 2N input lines.
The output line defines the N- bit code for binary information.
4 to 2 encoder:
The 4 to 2 line encoder has 4 inputs Y0, Y1, Y2, Y3 and two
outputs A0,A1.
At any time from these four inputs only one input can be ‘1’ to get
the respective binary code at the output .
BLOCK DIAGRAM:
TRUTH TABLE:
EXPRESSION:
A1=Y3+Y2
A0=Y3+Y1
PROGRAM:
STIMULATION RESULT:
8 to 3 encoder:
It is also called as octal to binary encoder.
It has 8 inputs Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7.
It has 3 outputs A0,A1,A2.
BLOCK DIAGRAM:
TRUTH TABLE:
EXPRESSION:
A2=Y4+Y5+Y6+Y7
A1=Y2+Y3+Y6+Y7
A0=Y7+Y5+Y3+Y1
PROGRAM:
STIMULATION RESULT:
DECODER:
The combinational circuit that changes the binary information into
2N output lines.
The binary information is passed in the form of N input lines.
Decoder perform the reverse operation of encoder.
2 to 4 line decoder:
The 2 to 4 line decoder has 3 inputs A1,A0 and one enable E.
It has 4 outputs Y0,Y1,Y2,Y3.
For each combination of inputs, when the enable ‘E’ is set to 1,one
of these four outputs will be ‘1’.
BLOCK DIAGRAM:
TRUTH TABLE:
EXPRESSION:
Y3=E.A1.A0
Y2=E.A1.A0'
Y1=E.A1'.A0
Y0=E.A1'.A0'
PROGRAM:
3 to 8 decoder:
3 to 8 decoder is also known as binary to octal decoder.
It has 3 inputs A1,A0 and A2.
It has 8 outputs Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7.
BLOCK DIAGRAM:
TRUTH TABLE:
PROGRAM:
//dut for 3 to 8 decoder
module decoder_3x8(y7,y6,y5,y4,y3,y2,y1,y0,a,b,c);
output y7,y6,y5,y4,y3,y2,y1,y0;
input a,b,c;
assign y0=(~a)&(~b)&(~c);
assign y1=a&(~b)&(~c);
assign y2=(~a)&b&(~c);
assign y3=a&b&(~c);
assign y4=(~a)&(~b)&c;
assign y5=a&(~b)&c;
assign y6=(~a)&b&c;
assign y7=a&b&c;
endmodule
MAGNITUDE COMPARATOR:
1- bit Comparator:
A 1-bit comparator is used to compare two bits.
It has 2 inputs A and B and 3 outputs .
The three outputs are A>B, A=B,A<B.
TRUTH TABLE:
EXPRESSION:
A>B: AB'
A<B: A'B
A=B: A'B' + AB
2 - bit comparator:
2 bit comparator is used to compare two binary numbers each of
two bits.
It has four inputs A1,A0,B1,B0 and three outputs A>B,A<B and
A=B.
EXPRESSION:
A>B:A1B1’ + A0B1’B0’ + A1A0B0’
STIMULATION RESULT:
CODE CONVERTERS
Code converters are used convert one type of binary code to one
another.
There are different types of binary codes like BCD code,gray code,
Excess-3 code.
BCD TO 7- SEGMENT:
BLOCK DIAGRAM:
TRUTH TABLE:
PROGRAM:
STIMULATION RESULT:
BINARY to GRAY:
The Most Significant Bit (MSB) of the gray code is always equal to
the MSB of the given binary code.
Other bits of the output gray code can be obtained by XORing
binary code bit at that index and previous index.
TRUTH TABLE:
It has four binary inputs a,b,c,d.
It has four gray outputs g3,g2,g1,g0.
EXPRESSION:
g0=(~c&d|c&~d);
g1=(b&~c|~b&c);
g2=(a^b);
g3=a;
PROGRAM:
STIMULATION RESULT:
BCD TO DECIMAL:
The BCD to Decimal Converter is used to convert a BCD (Binary-
coded decimal) to a decimal (Base-10) integer.
Ex: (0010)2 = (2)10.
(0111)2 = (7)10
BCD to decimal converter has 3 bcd inputs and a nine decimal
outputs.
In BCD each bit is represented by a 4 bit binary number.
BCD also called as 8421 code.
In BCD to decimal converter 10 to 15 are don’t care conditions .
TRUTH TABLE:
EXPRESSION:
y0=~a&~b&~c&~d;
y1=~a&~b&~c&d;
y2=~a&~b&c&~d;
y3=~a&~b&c&d;
y4=~a&b&~c&~d;
y5=~a&b&~c&d;
y6=~a&b&c&~d;
y7=~a&b&c&d;
y8=a&~b&~c&~d;
y9=a&~b&~c&d;
PROGRAM:
STIMULATION RESULT:
ACTIVE EVENTS:
Events which occur at current stimulation time and can be executed
in any order.
These include:
Blocking assignments.
Continuous assignments.
$display commands.
The evaluations of non- blocking RHS expressions.
INACTIVE EVENTS:
Events which are processed after the processing of active events.
In this queue , #0 delay assignments are scheduled.
NON-BLOCKING ASSIGN UPDATE EVENTS:
Events that were evaluated during previous stimulation time, but
are assigned at this stimulation time after the processing of active
and inactive events.
It is these queue the LHS of non- blocking assignment is updated.
MONITOR EVENTS:
Events that are processed after all the active, in active and non
blocking assign update events have been processed.
This queue contains $monitor and $strobe assignments.
BEHAVIOUR LEVEL MODELLING :
Behavioural modelling is a higher abstraction in verilog
programming.It is a modelling style that allows the designer to
describe the behaviour and nature of the digital system without
knowing the gate-level design of the circuit.
In verilog, behaviour modelling contains procedural statements
that control the simulation and manipulate the data types of the
variables involved.
PROCEDURAL BLOCKS :
These are used to group a set of statements together and execute
them sequentially.
Verilog provides two structured procedural blocks.
1. Initial block
2. Always block
INITIAL BLOCK :
Starts its execution at the beginning of a simulation.The initial
block executes only once.It is a non-synthesizable block.Multiple
initial blocks are valid in a module and each block executes at zero
simulation time. An individual block may finiah their execution
independently.
Nested initial blocks are not valid.It can execute in zero time or
take some time depending on the delay involved.Multiple statements
are enclosed within ‘beign’ and ‘end’ keywords and statements are
executed sequentially.
ALWAYS BLOCK :
Always block is executed sequentially (similar to the initial
block ).Based on changes in the sensitivity list , always block is
executed repeated.
If no sensitivity list is specified, always block executes
repeatedly throughout the simulation.Multiple always blocks are valid
in a module and each block executes independently.
Nested always blocks are not valid.
Inter Delay :
Ex: always @ (a or b)
#3 c1 = (a & b); //after 3ns (a & b) evaluated and assigned to
‘c’.
//it doesnot executes at 0ns.
Ex: always @ (a or b)
c1 = #3 ( a & b); //here (a & b) are evaluated immediately (at
0ns) but assigned to ‘c’ after 3ns.
//after 3ns it checks for change,if (a & b)
changes in 3ns, the new value doesnot effect the output
and old value remains.
Rise Delay(t_rise) :
Turn-Off Delay(t_turn-off) :
//DUT
module _4x1_mux(y,s1,s0,i3,i2,i1,i0);
input s1,s0,i3,i2,i1,i0;
output y;
reg y;
always @(*)
begin
if(s1)
if(s0)
y=i3;
else
y=i2;
else
if(s0)
y=i1;
else
y=i0;
end
endmodule
//TESTBENCH
Module tb_4x1_mux;
wire y;
reg s1,s0,i3,i2,i1,i0;
_4x1_mux dut (y,s1,s0,i3,i2,i1,i0);
initial begin
$monitor("time=%d,s1=%b,s0=%b,i3=%b,i2=%b,i1=%b,i0=%b,y=%b",$time,s1
,s0,i3,i2,i1,i0,y);
{s1,s0,i3,i2,i1,i0}=6'b000001;
#5;
{s1,s0,i3,i2,i1,i0}=6'b010010;
#5;
{s1,s0,i3,i2,i1,i0}=6'b100100;
#5;
{s1,s0,i3,i2,i1,i0}=6'b111000;
#5;
end
endmodule
CASE STATEMENT:
Case statement is like an if-else statement that uses identity
operators.The case statement automatically breaks after the first
match.The case statement checks if the given expression matches the
other expressions in the list. It is mostly used to implement
multiplexer type logic.
If no cases match default statement is executed. But default is
optional. So if no default execution will exit the case block without
doing anything.If many conditions are there then if-else may not be a
suitable choice so we use case statement.
SYNTAX:
case(expression)
case value
------------
------------
default statement
endcase
PROGRAM:
//DUT
module case_mux_4x1(y,s,i3,i2,i1,i0);
input i3,i2,i1,i0;
input [1:0]s;
output y;
reg y;
always @ (*)
begin
case(s)
2'b00:y=i0;
2'b01:y=i1;
2'b10:y=i2;
default : y=i3;
Endcase
end
endmodule
//TESTBENCH
module case_mux_4x1_tb;
reg i3,i2,i1,i0;
reg [1:0]s;
wire y;
//instantiation
case_mux_4x1 dut (y,s,i3,i2,i1,i0);
initial begin
$monitor("s=%b,i3=%b,i2=%b,i1=%b,i0=%b, y=%b",s,i3,i2,i1,i0,y);
s=2'b00; {i3,i2,i1,i0}=4'b0001;
#5;
s=2'b01; {i3,i2,i1,i0}=4'b0010;
#5;
s=2'b10; {i3,i2,i1,i0}=4'b0100;
#5;
s=2'b11; {i3,i2,i1,i0}=4'b1000;
#5;
end
endmodule
// DUT
module priorityencoder83(d,y);
input [7:0]d;
output [2:0]y;
reg [2:0]y;
always@(*)
begin
casex(d)
8'b1xxx_xxxx: y=3'b111;
8'b01xx_xxxx: y=3'b110;
8'b001x_xxxx: y=3'b101;
8'b0001_xxxx: y=3'b100;
8'b0000_1xxx: y=3'b011;
8'b0000_01xx: y=3'b010;
8'b0000_001x: y=3'b001;
default: y=3'b000;
endcase
end
endmodule
//TESTBENCH
module priorityencoder83_tb;
reg [7:0]d;
wire [2:0]y;
priorityencoder83 dut(d,y);
initial begin
$monitor("d=%0b -> y=%0b",d,y);
repeat(5)
begin
d=$random; #1;
end
end
endmodule
STIMULATION RESULT:
CASEZ STATEMENTS:
The casez statements, the simulator does not compare a bit
position having a Z value.
PROGRAM:
//DUT
module casex_ex(a,b,c,d);
input [3:0] a,b,c,d;
output [3:0] y;
always@ (a or b or c or d)
casez(d)
4’b0000: y=a;
4’b0??1: y=b;
4’b111?: y=c;
4’b???1: y=c;
default: 4’b0000;
endcase
endmodule
//TESTBENCH
module casex_ex_tb;
reg [3:0] a,b,c,d;
wire y;
//instantiation
casex_ex dut (a,b,c,d,y);
initial begin
$monitor("a=%b, b=%b, c=%b,d=%b, y=%b",a,b,c,d,y);
{i3,i2,i1,i0}=4'b0001;
#5;
{i3,i2,i1,i0}=4'b0010;
#5;
{i3,i2,i1,i0}=4'b0100;
#5;
{i3,i2,i1,i0}=4'b1000;
#5;
end
endmodule
PARAMETRES:
Parameters are verilog constructs that allow a module to be
reused with a different specification. They are like arguments to a
function that are passed in during a function call. Parameters are
constants and do not belong to any other data type
we cannot modify parameter values at runtime , we can modify
only at the compilation time. Verilog allows changing parameter
values during compilation time using the ‘defparam’ keyword. The
‘defparam’ is used as overriding the parameter value using a
hierarchical name of the module instance.
For example, a 4- bit adder can be parameterized to accept a
value for the number of bits and new parameter values can be passed
in during module instantiation. So an N-bit adder can be become a 4-
bit , 8-bit (or) 16-bit adder.
SYNTAX:
Module <module_name> #(<parameter list>) <port_list>;
The parameter value can be updated in two ways:
By using module instance statement.
By using defparam statement.
Defparam:
Defparam is a verilog construct used to set the new parameter
values.
It is used in test bench simulations to quickly update the design
parameters without having to re instantiate the module.
Example: Parameter using ripple carry adder
To design N bit parameter using ripple carry adder we need 4 bit
full adder.
PROGRAM: parameter RCA
//dut for full adder
module fa(a,b,cin,s,cout);
input a,b,cin;
output s,cout;
assign s=a^b^cin;
assign cout=((a&b)|(b&cin)|(a&cin));
endmodule
GENERATE LOOP:
The generate loop is similar to the for loop statement, but it uses
genvar keyword as a loop variable
The genvar keyword is only used during the evaluation of generate
block and does not exist during the simulation of the design. It
needs to be used by a generate loop.
Generate loop provides flexibility to reduce code lines by replacing
repetitive statements to a single statement like for loop.
Similar to a for loop, generate loops also can be nested with
different genvar as an index variable.
FUNCTION RULES:
A function cannot contain any time - controlled statements like
#,@, wait, posedge, negedge.
A function cannot start a task because it may consume simulation
time, but can call another functions and returns only one value.
A function should have at least one input.
A function cannot have any triggers.
Example program for function:
VERILOG TASK:
module example(A,B,CIN,S,COUT);
input [3:0] A,B;
input CIN;
output reg[3:0]S;
output reg COUT;
reg c1,c2,c3;
task ADD;
input A,B,CIN;
output s,cout;
begin
S = A^B^CIN;
COUT= (A&B)|(A&CIN)|(B&CIN)
end
endtask
endmodule
CODES FOR SEQUENTIAL CIRCUIT:
FLIPFLOPS:
A flip-flop is a sequential digital electronic circuit having two
stable states that can be used to store one bit of binary data.
Flip-flops are the fundamental building blocks of all memory
devices.
TYPES OF FLIPFLOPS:
1) SR flip flop.
2) JK flip flop.
3) D flip flop
4) T flip flop
JK FLIPFLOP:
The JK flip flop is used to remove the drawback of the S-R flip flop,
i.e., undefined states.
The JK flip flop is formed by doing modification in the SR flip flop.
When S and R input is set to true, the SR flip flop gives an
inaccurate result. But in the case of JK flip flop, it gives the correct
output.
When the input J and K are different then the output Q takes the
value of J at the next clock edge.
When J and K both are low then NO change occurs at the output.
If both J and K are high, then at the clock edge, the output will
toggle from one state to the other.
LOGIC DIAGRAM:
TRUTH TABLE:
Program:
//dut for jk flipflop
module jk_ff(j,k,clk,q);
input j,k,clk;
output q;
reg q;
always@(posedge clk)begin
case({j,k})
2'b00 : q<=q;
2'b01 : q<=1'b0;
2'b10 : q<=1'b1;
default q<=~q;
endcase
end
endmodule
// test bench for jk flipflop
module jk_ff_tb;
reg j,k,clk;
wire q;
jk_ff dut(j,k,clk,q);
always #5 clk<=~clk;
initial begin
$monitor("$time=%0t,j=%0b,k=%0b,q=%0b",$time,j,k,q);
clk=0;
j<=0;k<=0;
#5;
j<=0;k<=1;
#5;
j<=1;k<=0;
#5;
j<=1;k<=1;
#5;
end
endmodule
STIMULATION RESULT:
D FLIPFLOP:
D flip flop is an electronic devices that is known as “delay flip flop”
or “data flip flop” which is used to store single bit of data.
The D flip flop has two inputs, data and clock input which
controls the flip flop.
when clock input is high, the data is transferred to the output of
the flip flop.
when the clock input is low, the output of the flip flop is held in
its previous state.
LOGIC DIAGRAM:
TRUTH TABLE:
REGISTERS:
Flip flops can be used to store a single bit of binary data (1 or
0). However, in order to store multiple bits of data, we need multiple
flip-flops. N flip flops are to be connected in order to store n bits of
data.
A Register is a device that is used to store such information.
It is a group of flip-flops connected in series used to store multiple
bits of data. The information stored within these registers can be
transferred with the help of shift registers.
SHIFT REGISTERS:
Shift Register is a group of flip flops used to store multiple bits of
data.
An n-bit shift register can be formed by connecting n flip-flops
where each flip-flop stores a single bit of data.
The registers which will shift the bits to the left are called “Shift
left registers”.
The registers which will shift the bits to the right are called “Shift
right registers”.
BLOCK DIAGRAM:
TRUTH TABLE:
PROGRAM:
STIMULATION RESULT:
BLOCK DIAGRAM:
The shift register which allows parallel input and parallel out.
The inputs A0, A1, A2, and A3, are directly passed to the data
inputs D0, D1, D2, and D3 of the respective flip flop.
BLOCK DIAGRAM:
PROGRAM:
TABLE:
OPERATION:
STIMULATION RESULT:
COUNTERS:
Counter is used to count pulses and it can be used as frequency
divider.Counters are wide applications of flipflops.A counter circuit is
usually constructed of several flipflops connected in a cascade.
Counters are used in digital electronics for counting purpose,
they can count specific event happening in the circuit.The main
properties of a counter are timing , sequencing , and counting.
Counters operates in three modes:
Up counter
Down counter
Up / down counter
CLASSIFICATION OF COUNTER:
Asynchronous counter.
Synchronous counter.
ASYNCHRONOUS COUNTER:
The Asynchronous counter is also known as the ripple
counter. In asynchronous counter we don’t use universal clock, only
first flip flop is driven by main clock and the clock input of rest of the
following flip flop is driven by output of previous flip flops.The
transition of the first stage ripples till the last flip-flop stage.
Hence, it is also known as the “Ripple Counter”.It is also called
as serial counter.In the asynchronous counter, individual flip-flop
stages are triggered with the different clocks. Due to this, it is slower
when compared with a synchronous counter.
SYNCHRONOUS COUNTER:
synchronous counter has one clock which drives each flip flop
so output changes in parallel. It is also called as parallel counter.
The clock signals produced by all the flip flops are the same as each
other.
Synchronous counter operates fastly.Johnson and ring counters
are examples of synchronous counters.In synchronous counter
counting is not fixed.Speed is high compared to asynchronous counter.
MOD - N COUNTER:
Modulus Counters, or MOD counters, are defined based on the
number of states that the counter will sequence before returning to its
original value.Therefore, a "Mod-N" counter will require the "N"
number of flip-flops connected to count a single data bit while
providing 2n different output states (n is the number of bits).
Note that N is always a whole integer value.
If there are only two bits ( n = 2 ) then the maximum number of
possible output states (maximum modulus) for the counter is 2n =
22 or 4.However, counters can be designed to count to any 2n states
in their sequence by cascading together multiple counting stages to
produce a single modulus or MOD-N counter.
TYPES OF MOD COUNTERS:
2-bit counter is called as mod -4.
3-bit counter is called as mod-8.
MOD - N counter is called as modulus of the counter.
STIMULATION RESULT:
RING COUNTER:
Ring counter is a typical application of shift register.
The only change is output of last flipflop is connected to input to
first flipflop.
we use 4 D flip flops. The same clock pulse is passed to the clock
input of all the flip flops as a synchronous counter.
number states in a ring counter = number of flipflops .
LOGIC DIAGRAM:
PR=0, Q=1
CLR=0 , Q=1
WORKING:
The ORI input is passed to the PR input of the first flip flop, i.e.,
FF-0, and it is also passed to the clear input of the remaining
three flip flops, i.e., FF-1, FF-2, and FF-3.
The pre-set input set to 0 for the first flip flop. So, the output of
the first flip flop is one, and the outputs of the remaining flip flops
are 0.
The output of the first flip flop is used to form the ring in the ring
counter and referred to as Pre-set 1.
TRUTH TABLE:
PROGRAM:
// dut for ring counter
module ringcounter(clk, rst,count);
parameter N=4; //using parameter
input clk,rst;
output reg[N-1:0]count;
always@(posedge clk)
begin
if(rst)
count<=4'b0001;
else
count<={count[0],count[N-1:1]};
end
endmodule
module ringcounter_tb;
parameter N=4;
reg clk,rst;
wire [N-1:0]count;
ringcounter dut(clk,rst,count);
always#5clk=~clk;
initial begin
$monitor("$time=%0t,clk=%0b,rst=%0b,count=%0b",$time,clk,rst,count);
clk=0;
#5;
rst=1;
#5;
rst=0;
#5;
end
endmodule
STIMULATION RESULT:
JOHNSON COUNTER :
The Johnson counter is similar to the Ring counter.
The only difference between the Johnson counter and the ring
counter is that the outcome of the last flip flop is passed to the
first flip flop as an input.
In Johnson counter, the inverted outcome Q' of the last flip flop is
passed as an input.
Johnson counter is called creeping counter.
Four D flip flops are used in the 4-bit Johnson counter, and the
same clock pulse is passed to all the input of the flip flops.
LOGIC DIAGRAM:
TRUTH TABLE:
PROGRAM:
STIMULATION RESULT:
DECADE COUNTER(MOD 10):
A binary coded decimal (BCD) is a serial digital counter that
counts ten digits and it resets for every new clock input.
As it can go through 10 unique combinations of output, it is also
called as “Decade counter”.
A BCD counter can count 0000, 0001, 0010, 1000, 1001, 1010,
1011, 1110, 1111, 0000, and 0001 and so on.
BLOCK DIAGRAM:
TRUTH TABLE:
MEMORIES:
Memory is the faculty of the brain by which data (or)
information is encoded , stored and retrieved when needed.Memories
are digital storage elements, that helps to store data and information
in digital circuits.Memory is the most essential element of a
computing system.
Basically each memory cells have either ‘1’ or ‘0’.The memory is
divided into large number of small parts called cells.Each location (or)
cell has a unique address.Memory is an internal storage area in a
computer, which is used to store data and programs either
permanently (or) temporarily.
ROM SIZE:
2X X Y
x = number of inputs
Y = number of outputs
Internal Structure of ROM
The internal structure of ROM have two basic components.
Decoder
OR gates
STIMULATION RESULT:
RAM:
RAM, which stands for Random Access Memory.
RAM is a volatile memory, which means it does not store data or
instructions permanently.
RAM is the main memory in a computer , it is must faster to read
and write .
BLOCK DIAGRAM:
A RAM has a data bus form which we can access content from the
RAM or we can put a content on this, and if we give write
command to the RAM it will write RAM with the content on the
data bus.
RAM has lots of addresses and these addresses can be accesssed
by the address bus.
Other than data and address bus, RAM has one more input
read/write.
If the r/w is high , the content at data bus is written to the
address provided to the RAM from address bus else the content of
the address provided by address bus is reflected to the data bus.
PROGRAM:
STIMULATION RESULT:
Fig RAM
FINITE STATE MACHINE (FSM):
SEQUENCE DETECTOR:
STATE DIAGRAM: