Verilog 1726400699
Verilog 1726400699
Properties of HDL
HDL supports concurrency and includes notion of time. Notion of time deals with the delays of the circuit. In real time
we have delays in the circuit design and HDL consists of the delays. Concurrency is nothing but parallel execution of two
events or statements. In C or C++ Language we don’t have concurrency i.e., they are sequential in nature.
Verilog HDL
Verilog is a hardware descriptive language with the current standard of IEEE 1364-200. Verilog is used to describe the
low-level language. Verilog is a flexible language where we have pre-defined basic gates and digital circuits which are
already saved in digital library of Verilog which are known as primitives which we can use directly. Syntax of Verilog is
similar to that of C language.
Verilog C
A Hardware Description Language(HDL) A general purpose programming language
used to model electronic system that allows structured programming
HDLs have a notion of time Do not have notion of time
Helps to design and describe digital system Helps to build operating systems,
databases, compilers, interpreters etc.
Files have .v file extension Files have .c file extension
Verilog VHDL
Used to model electronic system Used in electronic design automation to
describe digital and mixed signal system
Verilog is weakly typed VHDL is strongly typed
Based on C language Based on Pascal and Ada language
Case sensitive Not Case sensitive
Simple data types More complex data types
Verilog is newer than VHDL Older than Verilog
Less complex More complex
Video-2
Level of Abstraction
Verilog supports 4 levels of abstraction namely,
• Switch level
• Gate level
• Data flow level
• Behavioural level
In switch level the implementation is done with the help of switches. In gate level we talk about gate
interconnections. In data flow level design is done based on equations, which can be said that it is based on data
flow, how the data is flowing in the circuit, based on that the equations have written. Behavioural level is based on
behaviour of the circuit.
Switch Level: Module is implemented in terms of switches, we can represent the entire circuit as CMOS circuit.
Here PMOS and NMOS are used as switches for the design.
module inverter(Q,A);
input A;
output Q;
supply1 vdd;
supplt0 vss;
pmos p (Q,vdd,A);
nmos n (Q,vss,A);
endmodule
In syntax instance_name is optional. Consider the example of CMOS inverter, which has two supplies VDD and VSS;
in the figure we can clearly state that PMOS is connected to Vdd and NMOS is connected to VSS. So based on the
syntax PMOS input is A which is control, output is Q and supply is VDD so the line for pmos will be pmos (Q, VDD, A)
Similarly, for NMOS as well.
Gate level: Module is implemented in terms of gates, which is lowest level of abstraction. Basic logic gates are
available as predefined primitives. In digital library of Verilog these logic gates are already saved and can be used
directly like and, or, xor, nand, nor etc.
Example:
1. and G1(out,A,B);
2. nand G2(Y,A,B);
3. or G3(out,A,B);
Data flow level: Whenever RTL (Register transfer logic) is heard then data flow level comes into the action. At
this level module is designed by specifying the data flow, like how the data is flowing in the circuit based on that the
equation should be written. Signals are assigned by the data manipulating equations, design is implemented using
continuous assignment i.e. assign statement in used in this level, the assignments present in it are concurrent in
nature.
Keyword: “assign”
Example:
Behavioural level: This is the highest level of abstraction provided by HDL, this level describes the behaviour of
the system. Different elements like functions, tasks and blocks can be used, two important constructors are initial
and always.
always @(i0,i1,s)
begin
if(s) // if select line is true then output is i1
out = i1;
out = i0;
end
Video-3
input x,y;
output z;
statements;
…………….….;
…………….….;
endmodule
Verilog code starts with module and ends with endmodule. Here module signifies the declaration of the module and
endmodule signifies the termination of the module. After module, module name is written based upon the circuit with any
name. In parenthesis inputs and outputs are written i.e., list of ports, then declare the ports whether it is input, output or
inout port, after declaring the ports, statements should be written depending upon the level, which states the functionality
of the circuit. Semi-colon represents the termination of the line.
Instantiation: Instantiation allows the creation of hierarchy in Verilog description. With the help of sub-modules main
module can be built i.e. top block with the help of sub-modules which is hierarchy, which can be done using instantiation.
The process of creating object from a module template and the objects are called instances. Consider an example where full
adder is implemented using half adder. First module for half adder will be written and then we instantiate the half adder
module two times to create a copy of half adder circuit. In Verilog nesting of module is illegal i.e. defining one module in
another module.
module mu(i1,i2,s0,out);
input i1,i2,s0;
output out;
always@(i2,i1,s0)
begin
if(s0)
out = i1;
else
out = i0;
end
endmodule
Now 4*1 mux using 2*1 Mux
module mux_4(i0,i1,i2,i3,s0,s1,out);
input i0,i1,i2,i3,s0,s1;
output out;
wire a0,a1;
mu m1(i0,i1,s0,a0);
mu m2(i2,i3,s0,a0);
mu m3(a0,a1,s1,out);
endmodule
Verilog Keywords
Verilog is case sensitive in nature, “$”, “_”, alphanumeric characters can be used, The proper format for writing keyword is
having a word starting with lower case like module_new, module_new1, module$new. The incorrect format is stating key
word with “$”, “_” or with numbers.
• new_block
• new_block1
• new_block$
• 1new_block
Comments
Comments are basically used for documentation purpose, which improves the readability of the code. It is recommended to
use comments to know why we are writing the particular statement.
• Single-line comment – It starts with “//” . Verilog skips from that point to end of the line
• Multiline comment– It starts with “/*” and ends with “*/”, Multiline cannot be nested.
Example:
comment */
Video-4
Simulation, Synthesis and design methodologies
Simulation
It is used to verify the functionality of the digital design that is modelled using HDL like Verilog. If we have an RTL, then with
the help of simulation we can check whether the code is working correctly or not and for that simulation we need to write
the testbenches. In testbench some custom inputs are provided namely, stimulus, the output that is generated with the
custom inputs, evaluation is done based on that output waveforms and the complete process is called as simulation. For
simulation purpose we apply the different inputs to the design at different times, so that to check whether the RTL code
behaves in intended way or not. Verilog module and testbenches are required and based on the outputs, for the inputs
results are evaluated.
Synthesis
Synthesis is a process in which the digital design that is modelled using HDL is translated into an implementation consisting
logic gates or in other word synthesis converts RTL into netlist. It will just make an optimal design based on the working
strategy which are using and also give the consumption of resources are available. For synthesis we only need Verilog
module and no testbench is required. Either ASIC or FPGA used to perform the synthesis operation. Since there are no
testbench, we don’t have any concept of time delay in synthesis as already the functioning on hardware so time delay is
only considered in simulation and not in synthesis process.
Design Methodologies
In Verilog we have two types of methodologies:
Video-5
Data Types
Data type is a classification that specifies which type of value a variable has, what type of mathematical operation i.e.,
logical or arithmetical is applied to that variable so that it doesn’t cause any error in the system.
• Register
• Net
Register data types are used to store the values, it only holds the value but it is not a hardware register. Keyword used for
register data type is “reg”. The default value of reg is X (unknown). In register we have different classes like reg, integer,
real, time etc.
Net represents the connection between hardware elements and must be continuously driven i.e., cannot be used to store
the values. Keyword used for net data type is “wire” and default value is Z. Net represents different classes such as wire,
wand, wor, tri, triand etc.
The strength level increase from bottom level to top level. When two signals are applied to the input of wire then the
higher signal level is reflected at the output. When signal strength of the same level is applied then the output is unknown.
Port Assignments
Input: Unidirectional, within the module it is of wire data type and externally when it is assigned to this port then it can be
reg or net data type
Output: Unidirectional, internally it is of reg or net data type, externally it is of net data type
Video-6
Net data type
Net represents the connection between hardware elements and must be continuously driven i.e., cannot be used to store
the values. Keyword is wire and default value Z. Net represents different classes such as wire, wand, wor, tri, triand etc.
Consider an AND gate with inputs a and b, output c. The data type for output c is NET i.e., it is continuously driven, as the
input value changes the output value also changes accordingly and doesn’t store the value. Consider a HALF-ADDER circuit
with inputs a and b and sum as sum and carry as carry, the Verilog code for Half-adder is as follows:
module half_adder(a,b,sum,carry);
input a,b;
output sum,carry;
wire sum,carry;
endmodule
Sum and carry are taken as net data type because these are continuously driven and as stated previously for assign
statement the left variable should be a net data type.
In the above circuit the final output is Y, if we use simple wire data type the output will then it will give indeterministic value
as the output value y is assigned by two different values i.e., A & B and C | B where simulator will get confused that which
value it needs to take for assigning the output Y. SO when wand or wor is used then there will be no problem. In the above
example the if the ouput is used as wand then the output will be of and of A&B and C|D the output will be more accurate.
Similarly wor can be used.
Example: Consider a HALF-ADDER circuit with inputs a and b and sum as sum and carry as carry, the Verilog code for Half-
adder is as follows:
Half sum
adder carry
Sum and carry are taken as net data type because these are continuously driven and as stated
previously for assign statement the left variable should be a net data type.
Use of wandclass: Consider the following circuit:
Example:
Here inputs are A,B,C,D and output is Y. supply1 vdd and supply0 gnd, different logic gates or, and, xor.
Here inputs are A,B,C,D and output is Y. supply1 vdd and supply0 gnd, different logic gates are or, and, xor.
Numbers
Representation of numbers (Syntax) in Verilog code: <size>’<base><number>
Representation of bases:
Binary – b or B
Octal – o or O
Decimal – d orD
Hexadecimal – h or H
Use of “_” (underscore) is used to improve the readability of the number it is mainly used for higher bit numbers
such as 32 bit or 64-bit numbers.
Extensions:
The basic concept of extension is that whatever the total bits are present we have to complete the number of bits
i.e., size so when theactual number doesn’t consist of those number of bits then extension will be used.
Video-7
These reg data types are used to store the values. It only holds the value but it is not a hardware register,
keyword used is reg. The default value of reg is X (unknown). In register we have different classes like reg,
integer, real, time etc.
Example of reg data type:
Declaration of reg can be of single bit or multiple bits
Syntax:
reg count; // single bit reg variable
reg [7:0] bus // multi bit (8-bits)
Consider the following Verilog code
reg reset;
initial
begin
reset = 1’b0;
#10 reset = 1’b1;
end
In the above Verilog code we have a register data type reset, initially the reset value is 0 and then for 10 time
units it changes it values, the variable holds the value 1 until it will get the another value at additional time
units.
time new_time;
initial
begin
new_time = $time; // current simulation time will be stored
end
Video-8
Nets or reg data types are declared as vectors i.e., we can declare these two data types with single bit or multiple bits. If bit
width is not defined then the default size is 1 bit which is a scalar. Vector represents the buses. Vectors are declared by
specifying the range [r1 : r2] where r1 is MSB and r2 is LSB.
EXAMPLE:
Arrays
In Verilog arrays are allowed for reg, integer, time, real, vector register data types. Multi-dimensional arrays are declared
with any number of dimensions.
Example:
reg [7:0] a[15:0]; // here there are 16 reg with 8 bits in each reg
reg b[31:0]; // there are 32 one bit numbers
reg c[0:1][0:3] /*Two dimensional array where left vector represents the rows and the right vector represents the column */
Memories
Memories are modelled as one-dimensional of registers, these are basically used to model register files, ROMs and RAMs.
Each element of the array is known as an element or word. We can address these elements by the index number which can
be of one bit or multiple bit.
Example:
reg mem [0:1023]; // memory mem with 1k 1-bit words
Parameters
Parameters cannot be used as a variable as it is constant value, advantage of using parameter is that, if a variable is declared
as a parameter then it will help to customize the code.
Example:
String
Strings are stored in reg, strings are all about characters i.e., sequence of characters. In string each character is represented
by 8 bits and these sequence will be enclosed by “ ” .
Example:
Some special characters of string are newline, tabs and other displaying argument values .
Video-9
Operators in Verilog
In any programming languages like C, C++, Java there are some operators which are built-in. In that language it can operate
on data items like number, character and some values. In Verilog there are some set of operators which are similar to high
level languages. Some operators in Verilog are more specific which have direct connection with hardware that Verilog
supports to model. Some of the operators are:
• Arithmetic operators
• Logical operators
• Bitwise operators
• Equality operators
• Relation operators
• Reduction operators
• Shift operators
• Concatenation
• Conditional
Arithmetic Operators
➢ Unary: The operators + and – can work as unary operators. It is used to determine whether the given operand is positive
or negative.
Eg: -4 // negative 4
5 // positive 5
➢ Binary: In binary 2 operands are considered and then perform different binary operations.
- Subtract 2
* Multiply 2
/ Divide 2
% Modulus 2
** Power 2
If any operand bit has a value x, then the result of the entire expression is x.
a = 4'b001x ; b = 4'b1011 ;
Modulus operators produce the remainder from the division of two numbers.
17 % 4 // Evaluates to 1
16 % 4 // Evaluates to 0
Logical Operators
Logical operators are used to define the conditional statement where the result will be true or false. Different types of
logical operators.
Example:
So, if one or both the operands (even one of the bit) consists of unknown values then the output will be unknown.
Example:
Consider X = 4’b1010; Y = 4’b1101; Z = 4’b10x1
~X // negation of X which results in 4’b0101
Logical equality is synthesizable in nature and case equality are non synthesizable in nature.
The result of equality operator is either logic 0 or logic 1 i.e., of single bit only. The major difference between
logical and case equality is that logical equality doesn’t include X or Z whereas case equality includes X or
Z.
Example:
Consider A=4; B=3; X= 4’b1010; Y=4’b1101;
Z=4’b1xxz; M = 4’b1xxz; N = 4’b1xxx;
A==B // Results in logic0.
X==Z // Results in x
Relational Operators
There are 4 types of relational operators:
Y<Z // Results in x.
Consider operands with unknown values. So, if one or both the operands (even one of the bit) consists of unknown
values then the output will be unknown only.
Reduction operator
Different types of reduction operatorsare:
And (&)
Or (|)
Nand (~&)
Nor (~|)
Xor (^)
Xnor (~^)
It takes only one operand, reduction operator performs a bitwise operation on a single vector
operand and yield a 1- bit result
Example:
Consider X =4’b1010
&X // Equivalent to 1 & 0 & 1 & 0. Results in 1'b0
|X // Equivalent to 1 | 0 | 1 | 0. Results in 1'b1
^X // Equivalent to 1 ^ 0 ^ 1 ^ 0. Results in 1'b0
A reduction xor or xnor can be used for even or odd parity generation of a vector.
Shift Operator
There are 4 different types of shift operators.
Shift right(>>)
Left shift (<<)
Arithmetic left shift(<<<)
Arithmetic rightshift(>>>)
Regular shift operators shift a vector operand to the right or to the left by a specified number of bits. When the bits are
shifted, the vacant bits are filled with zeros.
Arithmetic shift operator uses the context of the expression to determine the value with which we can
fill the vacated bit i.e., signed bit is present or not is verified according to that vacant bit is filled.
Example:
Consider X=4’b1100;
Y = X>>1 // Y is 4’b0110 shift right by 1 bit MSB is filled by bit 0
Y = X<<2 // Y is 4’b0000. Shift left by 2 bits.
Y = X>>>1 // Y is4’b1110
Concatenation operator
The concatenate operator is ({,}). It provides the mechanism to append the multiple operands, which are
sized, unsized operands cannot be used. Operands can be scalar nets or registers, vector nets or registers,
bit-select, part-select or sized constants.
Example:
Consider A= 1’b1; B=2’b00; C=2’b10; D=3’b110
Y = {B,C} // Y results in 4’b0010 Y={A,B,3’b001} // Y results in 6’b100001
Conditional Operator
First the conditional expression is evaluated, if the evaluated conditional expression is true
then the true expression is evaluated else the false expression is evaluated.
Operator precedence
Video-11
Basic Gates
In these gates one scalar output and multiple scalar inputs are present. Output is evaluated as
soon as the input changes.
The and/or gates available in Verilog are: And, or, xor, nand, nor, xnor
Synatx:
Primitive_name instance_name(output,inputs);
Example:
module gate_ex(O1,O2,I1,I2,I3,I4);
input I1,I2,I3,I4;
output O1,O2;
and a (O1,I1,I2);
or b (O2,I3,I4);
endmodule
Buf/Not Gates
These gates have one scalar input and one or more scalar outputs. Below image shows the buffer and not
gates
Syntax: gate_name (output, input)
Example:
module gate_eg(O1,O2,I1,I2);
input I1,I2;
output O1,O2;
buf buf_gate(O1,I1);
not not_gate(O2,I2);
endmodule
Example:
output O1,O2,O3,O4;
or(O1,I1,I2);
nand(O2,I3,I4);
nor(O3,I5,I6);
xnor(O4,I7,I8);
endmodule
Bufif/Notif gates
These gates have additional control signals, The input propagate if the control signal is given
otherwise the output will be in high impedance state. There are 4 different types of bufif
notif gates bufif1, bufif0, notif1, notif1.
Example:
input I1,I2,I3,I4,Control;
output O1,O2,O3,O4;
bufif1 buf_gate1(O1,I1,Control);
notif1 not_gate1(O2,I2,Control);
bufif0 buf_gate2(O3,I3,Control);
notif0 not_gate2(O4,I4,Control);
endmodule
Gate Delays
In Verilog we introduce gate delays in the logic circuits, there are three different
delays.
Rise delay
Fall delay
Turn-off delay.
Rise Delay:
Fall Delay:
When there is a transition in gate outputto high impedance value from any other logic value. If the
gate output transmits to don’t care then minimum of three delays is considered.
Syntax:
For all transistion
Example:
Module D(a,b,c,out);
input a,b,c;
output out;
wire e;
or #(4) o1(out,e,c);
endmodule