DSD (U5)
DSD (U5)
DSD (U5)
UNIT V
Digital Design with Verilog HDL: Hierarchical Modeling concepts – 4-bit ripple carry counter
– modules – instances – Data types – Arrays – System tasks – directives – Modules and Ports –
Gate-Level Modeling – Dataflow Modeling – Design of Multiplexers, counters and full adders –
Introduction to Behavioral Modeling.
2 MARKS
1. What is VHDL? (APRIL 2013) (APRIL/MAY 2012)
Used to have an identifier name for a constant value. The value cannot be changed by
any executable code.
SYNTAX
Example
constant Pi : real := 3.14159;
constant Half_Pi : real := Pi/2.0;
constant cycle_time : time := 2 ns;
constant N, N5 : integer := 5;
package_declaration
PACKAGE identifier IS
package_declarative_part
END [ PACKAGE ] [ package_simple_name ] ;
package_declarative_part
{ package_declarative_item }
Within a package declaration all package information may be gathered. This information
is declared only once and should not be subsequently modified. They are visible also in
the package body. The package declaration builds in this way the interface of the
package.
Entity
Entity can be seen as the black box view of the system. We define the inputs and outputs
of the system which we need to interface.
Entity ANDGATE is
Port (A : In std_logic;
B: in std_logic;
Y : out std_logic);
End entity ANDGATE;
Entity name ANDGATE is given by the programmer, each entity must have a name.
The ripple carry counter shown in Figure 2-3 is made up of negative edge-triggered
toggle flip flops (T_FF).
7. Define modules
module<module_name> (<module_terminal_list>);
...
<module internals>
...
...
Endmodule
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Specifically, the T-flipflop could be defined as a module as follows:
module T_FF (q, clock, reset);
.
.
<functionality of T-flipflop>
.
.endmodule
8. Define instances.
A module provides a template from which you can create actual objects. When a module
is invoked, Verilog creates a unique object from the template. Each object has its own name,
variables, parameters, and I/O interface. The process of creating objects from a module template
is called instantiation, and the objects are called instances.
Data types in Verilog model actual data storage and switch elements in hardware very
closely.data types such as nets, registers, vectors, numbers, simulation time, arrays, parameters,
memories, and strings.
In Verilog, the term register merely means a variable that can hold a value. Unlike a net,
a register does not need a driver. Verilog registers do not need a clock as hardware registers do.
Values of registers can be changed anytime in a simulation by assigning a new value to the
register.
Register data types are commonly declared by the keyword reg.
The default value for areg data type is x.
Example of Register
reg reset; // declare a variable reset that can hold its value
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
initial // this construct will be discussed later
begin
reset = 1'b1; //initialize reset to 1 to reset the digital circuit.
#100 reset = 1'b0; // after 100 time units reset is deasserted.
End
12. Define Vectors.
Nets or reg data types can be declared as vectors (multiple bit widths).
If bit width is notspecified, the default is scalar (1-bit).
Example :
wire a; // scalar net variable, default
wire [7:0] bus; // 8-bit bus
wire [31:0] busA,busB,busC; // 3 buses of 32-bit width.
reg clock; // scalar register, default
reg [0:40] virtual_addr; // Vector register, virtual address 41 bits
Vectors can be declared at [high# : low#] or [low# : high#], but the left number in the
Squared brackets is always the most significant bit of the vector. In the example
shownabove, bit 0 is the most significant bit of vector virtual_addr.
13. Define numbers and Time in VHDL.
Integer, real, and time register data types are supported in Verilog.
An integer is a general purpose register data type used for manipulating quantities.
Integers are declared by the keyword integer.
Registers declared as data type reg store values as unsigned quantities, whereas integers
store values as signed quantities.
integer counter; // general purpose variable used as a counter.
counter = -1; // A negative one is stored in the counter
Verilog simulation is done with respect to simulation time. A special time register data
type is used in Verilog to store simulation time. A time variable is declared with the keyword
time.
The width for time register data types is implementation-specific but is atleast 64 bits.The
system function $time is invoked to get the current simulation time.
Real number constants and real register data types are declared with the keyword
real.They can be specified in decimal notation (e.g., 3.14) or in scientific notation (e.g.,
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
3e6,which is 3 x 106 ). Real numbers cannot have a range declaration, and their default valueis 0.
When a real value is assigned to an integer, the real number is rounded off to thenearest integer.
real delta; // Define a real variable called delta
initial
begin
delta = 4e10; // delta is assigned in scientific notation
delta = 2.13; // delta is assigned a value 2.13
end
integer i; // Define an integer i
initial
i = delta; // i gets the value 2 (rounded value of 2.13)
Arrays are allowed in Verilog for reg, integer, time, real, realtime and vector register data
types. Multi-dimensional arrays can also be declared with any number of dimensions.
Arrays of nets can also be used to connect ports of generated instances. Each element of
the array can be used in the same fashion as a scalar or vector net. Arrays are accessed by
<array_name>[<subscript>]. For multi-dimensional arrays, indexes need to be provided for each
dimension.
Example
integer count[0:7]; // An array of 8 count variables
regbool[31:0]; // Array of 32 one-bit boolean register variables
timechk_point[1:100]; // Array of 100 time checkpoint variables
11 MARKS
Hierarchical Modeling concepts
There are two basic types of digital design methodologies:
a top-down design methodology and
a bottom-up design methodology.
In a top-down design methodology, we define the top-level block and identify the sub-blocks
necessary to build the top-level block.
We further subdivide the sub-blocks until we come to leaf cells, which are the cells that cannot
further be divided.
Figure 2-1 shows the top-down design process.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Figure 2-1. Top-down Design Methodology
In a bottom-up design methodology, we first identify the building blocks that are available to us.
These cells are then used for higher-level blocks until we build the top-level block in the design.
Figure 2-2 shows the bottom-up design process.
FIGURE: T-flipflop
Thus, the ripple carry counter is built in a hierarchical fashion by using building blocks. The
diagram for the design hierarchy is shown in Figure 2-5.
Figure 2-5. Design Hierarchy
In a top-down design methodology, we first have to specify the functionality of the ripple
carry counter, which is the top-level block.
Then, we implement the counter with T_FFs. We build the T_FFs from the D_FF and an
additional inverter gate.
Thus, we break bigger blocks into smaller building sub-blocks until we decide that we
cannot break up the blocks any further.
A bottom-up methodology flows in the opposite direction.
We combine small building blocks and build bigger blocks; e.g., we could build D_FF
from and and or gates, or we could build a custom D_FF from transistors.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Thus, the bottom-up flow meets the top-down flow at the level of the D_FF.
Levels of Modules
Verilog is both a behavioral and a structural language. Internals of each module can bedefined at
four levels of abstraction, depending on the needs of the design. The modulebehaves identically
with the external environment irrespective of the level of abstractionat which the module is
described. The internals of the module are hidden from theenvironment.
Thus, the level of abstraction to describe a module can be changed withoutany change in the
environment.
Gate level
• The module is implemented in terms of logic gates and interconnections betweenthese gates.
Design at this level is similar to describing a design in terms of agate-level logic diagram.
Switch level
• This is the lowest level of abstraction provided by Verilog. A module can beimplemented in
terms of switches, storage nodes, and the interconnectionsbetween them. Design at this level
requires knowledge of switch-levelimplementation details.
Instances
A module provides a template from which you can create actual objects. When a moduleis
invoked, Verilog creates a unique object from the template. Each object has its ownname,
variables, parameters, and I/O interface. The process of creating objects from amodule template
is called instantiation, and the objects are called instances.
In Example 2-1, the top-level block creates four instances from the T-flipflop (T_FF) template.
Each
T_FF instantiates a D_FF and an inverter gate. Each instance must be given a uniquename. Note
that // is used to denote single-line comments.
Data Types
Value Set
Verilog supports four values and eight strengths to model the functionality of realhardware. The
four value levels are listed in Table 3-1.
Value Levels
In addition to logic values, strength levels are often used to resolve conflicts betweendrivers of
different strengths in digital circuits. Value levels 0 and 1 can have the strengthlevels listed in
Table 3-2.
Table 3-2. Strength Levels
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
If two signals of unequal strengths are driven on a wire, the stronger signal prevails.
Forexample, if two signals of strength strong1 and weak0 contend, the result is resolved
as a
strong1.
If two signals of equal strengths are driven on a wire, the result is unknown. Iftwo signals
of strength strong1 and strong0 conflict, the result is an x.
Nets
Nets represent connections between hardware elements.
Just as in real circuits, nets havevalues continuously driven on them by the outputs of
devices that they are connected to.
In Figure 3-1 net a is connected to the output of and gate g1.
Net a will continuouslyassume the value computed at the output of gate g1, which is b &
c.
Figure 3-1. Example of Nets
Nets are declared primarily with the keyword wire. Nets are one-bit values by
defaultunless they are declared explicitly as vectors.
The terms wire and net are often usedinterchangeably. The default value of a net is z.
Nets get the output value of their drivers. If a net has no driver, it gets the value z.
Registers
Registers represent data storage elements. Registers retain value until another value
isplaced onto them.
In Verilog, the term register merely means a variable that can hold a value. Unlike a net,
a register does not need a driver.
Verilog registers do not need a clock as hardware registers do. Values of registers can
bechanged anytime in a simulation by assigning a new value to the register.
Register data types are commonly declared by the keyword reg. The default value for
areg data type is x. An example of how registers are used is shown Example 3-1.
Vectors
Nets or reg data types can be declared as vectors (multiple bit widths). If bit width is
notspecified, the default is scalar (1-bit).
wire a; // scalar net variable, default
wire [7:0] bus; // 8-bit bus
wire [31:0] busA,busB,busC; // 3 buses of 32-bit width.
reg clock; // scalar register, default
reg [0:40] virtual_addr; // Vector register, virtual address 41 bits
wide
Vectors can be declared at [high# : low#] or [low# : high#], but the left number in thesquared
brackets is always the most significant bit of the vector. In the example shownabove, bit 0 is the
most significant bit of vector virtual_addr.
Integer
An integer is a general purpose register data type used for manipulating quantities.Integers are
declared by the keyword integer.
integer counter; // general purpose variable used as a counter.
initial
counter = -1; // A negative one is stored in the counter
Real
Real number constants and real register data types are declared with the keyword real.They can
be specified in decimal notation (e.g., 3.14) or in scientific notation (e.g., 3e6,which is 3 x 106 ).
Real numbers cannot have a range declaration, and their default valueis 0. When a real value is
assigned to an integer, the real number is rounded off to thenearest integer.
real delta; // Define a real variable called delta
initial
begin
delta = 4e10; // delta is assigned in scientific notation
delta = 2.13; // delta is assigned a value 2.13
end
integer i; // Define an integer i
initial
i = delta; // i gets the value 2 (rounded value of 2.13)
Time
Verilog simulation is done with respect to simulation time. A special time register datatype is
used in Verilog to store simulation time.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
A time variable is declared with thekeyword time. The width for time register data types is
implementation-specific but is atleast 64 bits.The system function $time is invoked to get the
current simulation time.
time save_sim_time; // Define a time variable save_sim_time
initial
save_sim_time = $time; // Save the current simulation time
Simulation time is measured in terms of simulation seconds. The unit is denoted by s, the
same as real time.
Arrays
Arrays are allowed in Verilog for reg, integer, time, real, realtime and vector register datatypes.
Multi-dimensional arrays can also be declared with any number of dimensions.Arrays of nets can
also be used to connect ports of generated instances. Each element ofthe array can be used in the
same fashion as a scalar or vector net. Arrays are accessed by<array_name>[<subscript>]. For
multi-dimensional arrays, indexes need to be providedfor each dimension.
It is important not to confuse arrays with net or register vectors. A vector is a singleelement that
is n-bits wide. On the other hand, arrays are multiple elements that are 1-bitor n-bits wide.
System tasks
System Tasks
Verilog provides standard system tasks for certain routine operations. All system tasksappear in
the form $<keyword>. Operations such as displaying on the screen, monitoringvalues of nets,
stopping, and finishing are done by system tasks.
Displaying information
$display is the main system task for displaying values of variables or strings orexpressions.
This is one of the most useful tasks in Verilog.
p1, p2, p3,..., pn can be quoted strings or variables or expressions. The format of $displayis very
similar to printf in C.
Table 3-4. String Format Specifications
Example 3-3 shows some examples of the $display task. If variables contain x or zvalues, they
are printed in the displayed string as "x" or "z".
Examples of monitoring statements are given in Example 3-5. Note the use of $time in
the $monitor statement.
Compiler Directives
Compiler directives are provided in Verilog. All compiler directives are defined by using
the `<keyword> construct. We deal with the two most useful compiler directives.
`define
The `define directive is used to define text macros in Verilog (see Example 3-7). TheVerilog
compiler substitutes the text of the macro wherever it encounters a`<macro_name>. This is
similar to the #define construct in C. The defined constants ortext macros are used in the Verilog
code by preceding them with a ` (back tick).
Example 3-7 `define Directive
//define a text macro that defines default word size
//Used as 'WORD_SIZE in the code
'define WORD_SIZE 32
//define an alias. A $stop will be substituted wherever 'S appears
'define S $stop;
//define a frequently used text string
'define WORD_REG reg [31:0]
// you can then define a 32-bit register as 'WORD_REG reg32;
`include
The `include directive allows you to include entire contents of a Verilog source file inanother
Verilog file during compilation. This works similarly to the #include in the Cprogramming
language. This directive is typically used to include header files, whichtypically contain global or
commonly used definitions (see Example 3-8).
Example 3-8 `include Directive
// Include the file header.v, which contains declarations in the
// main verilog file design.v.
'include header.v
...
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
...
<Verilog code in file design.v>
...
...
Two other directives, `ifdef and `timescale, are used frequently.
The SR latch has S and R as the input ports and Q and Qbar as the output ports. The SRlatch and
its stimulus can be modeled as shown in Example 4-1.
Example 4-1 Components of SR Latch
// This example illustrates the different components of a module
// Module name and port list
// SR_latch module
module SR_latch(Q, Qbar, Sbar, Rbar);
//Port declarations
output Q, Qbar;
input Sbar, Rbar;
// Instantiate lower-level modules
// In this case, instantiate Verilog primitive nand gates
// Note, how the wires are connected in a cross-coupled fashion.
nand n1(Q, Sbar, Qbar);
nand n2(Qbar, Rbar, Q);
// endmodule statement
endmodule
// Module name and port list
// Stimulus module
module Top;
// Declarations of wire, reg, and other variables
wire q, qbar;
reg set, reset;
// Instantiate lower-level modules
// In this case, instantiate SR_latch
// Feed inverted set and reset signals to the SR latch
SR_latch m1(q, qbar, ~set, ~reset);
// Behavioral block, initial
initial
begin
$monitor($time, " set = %b, reset= %b, q= %b\n",set,reset,q);
set = 0; reset = 0;
#5 reset = 1;
#5 reset = 0;
#5 set = 1;
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
endendmodule
Notice the following characteristics about the modules defined above:
• In the SR latch definition above , notice that all components described in Figure4-1 need not be
present in a module. We do not find variable declarations,dataflow (assign) statements, or
behavioral blocks (always or initial).
• However, the stimulus block for the SR latch contains module name, wire, reg,and variable
declarations, instantiation of lower level modules, behavioral block(initial), and endmodule
statement but does not contain port list, port declarations,and data flow (assign) statements.
• Thus, all parts except module, module name, and endmodule are optional and canbe mixed and
matched as per design needs.
Ports
Ports provide the interface by which a module can communicate with its environment.
For example, the input/output pins of an IC chip are its ports.
The environment caninteract with the module only through its ports. The internals of the
module are notvisible to the environment.
This provides a very powerful flexibility to the designer. Theinternals of the module can
be changed without affecting the environment as long as theinterface is not modified.
Ports are also referred to as terminals.
List of Ports
A module definition contains an optional list of ports.
If the module does not exchangeany signals with the environment, there are no ports in
the list. Consider a 4-bit full adderthat is instantiated inside a top-level module Top.
The diagram for the input/output portsis shown in Figure 4-3.
Notice that in the above figure, the module Top is a top-level module.
The modulefulladd4 is instantiated below Top. The module fulladd4 takes input on ports a, b,
andc_in and produces an output on ports sum and c_out.
Thus, module fulladd4 performs anaddition for its environment. The module Top is a top-level
module in the simulation anddoes not need to pass signals to or receive signals from the
environment.
Thus, it does nothave a list of ports. The module names and port lists for both module
declarations inVerilog are as shown in Example 4-2.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Example 4-2 List of Ports
module fulladd4(sum, c_out, a, b, c_in); //Module with a list of ports
module Top; // No list of ports, top-level module in simulation 4.2.2 Port
Declaration
All ports in the list of ports must be declared in the module. Ports can be declared asfollows:
Each port in the port list is defined as input, output, or inout, based on the direction of theport
signal. Thus, for the example of the fulladd4 in Example 4-2, the port declarationswill be as
shown in Example 4-3.
Example 4-3 Port Declarations
module fulladd4(sum, c_out, a, b, c_in);
//Begin port declarations section
output[3:0] sum;
output c_cout;
input [3:0] a, b;
input c_in;
//End port declarations section
...
<module internals>
...
endmodule
Note that all port declarations are implicitly declared as wire in Verilog. Thus, if a port
isintended to be a wire, it is sufficient to declare it as output, input, or inout. Input or inoutports
are normally declared as wires.
Inputs
Internally, input ports must always be of the type net. Externally, the inputs can beconnected to a
variable which is a reg or a net.
Outputs
Internally, outputs ports can be of the type reg or net. Externally, outputs must always
beconnected to a net. They cannot be connected to a reg.
Inouts
Internally, inout ports must always be of the type net. Externally, inout ports must alwaysbe
connected to a net.
Width matching
It is legal to connect internal and external items of different sizes when making intermoduleport
connections. However, a warning is typically issued that the widths do notmatch.
Unconnected ports
Verilog allows ports to remain unconnected. For example, certain output ports might besimply
for debugging, and you might not be interested in connecting them to the externalsignals. You
can let a port remain unconnected by instantiating a module as shownbelow.
Notice that the external signals SUM, C_OUT, A, B, and C_INappear in exactly the same order
as the ports sum, c_out, a, b, and c_in in moduledefinition of fulladd4.
Example 4-7 Connection by Ordered List
module Top;
//Declare connection variables
reg [3:0]A,B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
//Instantiate fulladd4, call it fa_ordered.
//Signals are connected to ports in order (by position)
fulladd4 fa_ordered(SUM, C_OUT, A, B, C_IN);
...
<stimulus>
...
Endmodule
GATE-LEVEL MODELING
Gate types
A logic circuit can be designed by use of logic gates. Verilog supports basic logic gatesas
predefined primitives. These primitives are instantiated like modules except that theyare
predefined in Verilog and do not need a module definition. All logic circuits can bedesigned by
using basic gates. There are two classes of basic gates: and/or gates andbuf/not gates.
1. And/Or Gates
And/or gates have one scalar output and multiple scalar inputs. The first terminal in thelist of
gate terminals is an output and the other terminals are inputs. The output of a gateis evaluated as
soon as one of the inputs changes. The and/or gates available in Verilogare shown below.
and or xor
nand nor xnor
The corresponding logic symbols for these gates are shown in Figure 5-1. We consider
gates with two inputs. The output terminal is denoted by out. Input terminals are denoted
by i1 and i2.
These gates are instantiated to build logic circuits in Verilog. Examples of gateinstantiations are
shown below. In Example 5-1, for all instances, OUT is connected tothe output out, and IN1 and
IN2 are connected to the two inputs i1 and i2 of the gateprimitives. Note that the instance name
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
does not need to be specified for primitives. Thislets the designer instantiate hundreds of gates
without giving them a name.More than two inputs can be specified in a gate instantiation. Gates
with more than twoinputs are instantiated by simply adding more input ports in the gate
instantiation (seeExample 5-1). Verilog automatically instantiates the appropriate gate.
1.buf 2. not
These gates are instantiated in Verilog as shown Example 5-2. Notice that these gates canhave
multiple outputs but exactly one input, which is the last terminal in the port list.
Bufif/notif
Gates with an additional control signal on buf and not gates are also available.
bufif1 notif1
bufif0 notif0
These gates propagate only if their control signal is asserted. They propagate z if theircontrol
signal is deasserted. Symbols for bufif/notif are shown in Figure 5-3.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Figure 5-3. Gates Bufif and Notif
These gates are used when a signal is to be driven only when the control signal is asserted. Such
a situation is applicable when multiple drivers drive the signal.
These drivers are designed to drive the signal on mutually exclusive control signals.
Example 5-3 shows examples of instantiation of bufif and notif gates.
Gate-level multiplexer
We will design a 4-to-1 multiplexer with 2 select signals. Multiplexers serve a usefulpurpose in
logic design. They can connect two or more sources to a single destination.They can also be used
to implement boolean functions. We will assume for this examplethat signals s1 and s0 do not
get the value x or z. The I/O diagram and the truth table forthe multiplexer are shown in Figure
5-4. The I/O diagram will be useful in setting up theport list for the multiplexer.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Figure 5-4. 4-to-1 Multiplexer
We will implement the logic for the multiplexer using basic logic gates. The logicdiagram for the
multiplexer is shown in Figure 5-5.
This multiplexer can be tested with the stimulus shown in Example 5-6. The stimuluschecks that
each combination of select signals connects the appropriate input to theoutput. The signal
OUTPUT is displayed one time unit after it changes. System task$monitor could also be used to
display the signals when they change values.
The output of the simulation is shown below. Each combination of the select signals is
tested.
IN0= 1, IN1= 1, IN2= 1, IN3= 1
S1 = 0, S0 = 0, OUTPUT = 1
S1 = 0, S0 = 1, OUTPUT = 1
S1 = 1, S0 = 0, OUTPUT = 1
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
S1 = 1, S0 = 1, OUTPUT = 1
DATAFLOW MODELING
For small circuits, the gate-level modeling approach works very well because the numberof gates
is limited and the designer can instantiate and connect every gate individually.Also, gate-level
modeling is very intuitive to a designer with a basic knowledge of digitallogic design. However,
in complex designs the number of gates is very large. Thus,designers can design more effectively
if they concentrate on implementing the function ata level of abstraction higher than gate level.
Dataflow modeling provides a powerful wayto implement a design. Verilog allows a circuit to be
designed in terms of the data flowbetween registers and how a design processes data rather than
instantiation of individualgates.
Continuous Assignments
A continuous assignment is the most basic statement in dataflow modeling, used to drivea value
onto a net. This assignment replaces gates in the description of the circuit anddescribes the
circuit at a higher level of abstraction. The assignment statement starts withthe keyword assign.
The syntax of an assign statement is as follows.
Examples of continuous assignments are shown below. Operators such as &, ^, |, {, } and
+ used in the examples, Operator Types. At this point,concentrate on how the assign statements
are specified.
Delays
Delay values control the time between the change in a right-hand-side operand and whenthe new
value is assigned to the left-hand side. Three ways of specifying delays incontinuous assignment
statements are regular assignment delay, implicit continuousassignment delay, and net
declaration delay.
EXAMPLE:
The waveform in Figure 6-1 is generated by simulating the above assign statement. Itshows the
delay on signal out. Note the following change:
1. When signals in1 and in2 go high at time 20, out goes to a high 10 time units later(time = 30).
2. When in1 goes low at 60, out changes to low at 70.
3. However, in1 changes to high at 80, but it goes down to low before 10 time unitshave elapsed.
4. Hence, at the time of recomputation, 10 units after time 80, in1 is 0. Thus, outgets the value 0.
A pulse of width less than the specified assignment delay is notpropagated to the output.
Operators
Operators act on the operands to produce desired results. Verilog provides various typesof
operators. Operator types are discussed in detail in Section 6.4, Operator Types.d1 && d2 // &&
is an operator on operands d1 and d2
!a[0] // ! is an operator on operand a[0]
B >> 1 // >> is an operator on operands B and 1
Operator Types
Verilog provides many different operator types. Operators can be arithmetic, logical,relational,
equality, bitwise, reduction, shift, concatenation, or conditional. Some of theseoperators are
similar to the operators used in the C programming language. Each operatortype is denoted by a
symbol.
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
Design of Multiplexers, counters and full adders
Design of halfadder
1. Logic circuit of half adder
2. Gate level code for half adder
halfadder.v
module
halfadder(a,b,sum,carry);
input a,b;
output sum, carry;
wire sum, carry;
assign sum = a^b; // sum bit
assign carry = (a&b) ; //carry bit
endmodule
Design of fulladder
1. Logic circuit of full adder
2. Gate level code for fulladder
Fulladder.v
module fulladder(a,b,c,sum,carry);
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
input a,b,c;
output sum,carry;
wire sum,carry;
assign sum=a^b^c; // sum bit
assign carry=((a&b) | (b&c) | (a&c)); //carry bit
endmodule
Design of multiplexer
1. Logic circuit of multiplexer
2. Gate level code for multiplexer
mux4_to_1.v
// Module 4-to-1 multiplexer. Port list is taken exactly from the I/O diagram.
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
// Port declarations from the I/O diagram
output out;
input i0, i1, i2, i3;
input s1, s0;
// Internal wire declarations
wire s1n, s0n;
wire y0, y1, y2, y3;
// Gate instantiations
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
// Create s1n and s0n signals.
not (s1n, s1);
not (s0n, s0);
// 3-input and gates instantiated
and (y0, i0, s1n, s0n);
and (y1, i1, s1n, s0);
and (y2, i2, s1, s0n);
and (y3, i3, s1, s0);
// 4-input or gate instantiated
or (out, y0, y1, y2, y3);
endmodule
Design of upcounter
1. Logic circuit of upcounter
2. Gate level code for upcounter
UPcounter.v
`define TICK #2
module downCntr(clk, reset, Q);
input clk, reset;
output [3:0] Q;
reg [3:0] Q;
//Behavioral Code for a Down Counter
always @ (posedgeclk) begin
if (~reset) begin
Q <= `TICK Q+1;
end
end
always @ (posedge reset) begin
Q <= 4'b0000;
end
endmodule
module main;
regclk, reset;
wire [3:0] Q;
downCntr dnCntr1(clk, reset, Q);
always @(posedgeclk)begin
$display(" clk=%b, reset=%b,Q=%b\n", clk, reset, Q);
end
initial begin
forever begin
clk<= 0;
#5
clk<= 1;
//#5
//clk<= 0;
end
end
initial begin
reset = 1;
#2
reset = 0;
#85 $finish;
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
//#17
//reset = 1;
//#12
//reset = 0;
//#85 $finish;
end
initial begin
$dumpfile ("upcounter.vcd");
$dumpvars;
end
endmodule
Design of downcounter
1. Logic circuit of downcounter
2. Gate level code for downcounter
downcounter.v
`define TICK #2
module downCntr(clk, reset, Q);
input clk, reset;
output [3:0] Q;
reg [3:0] Q;
//Behavioral Code for a Down Counter
always @ (posedgeclk) begin
if (~reset) begin
Q <= `TICK Q-1;
end
end
always @ (posedge reset) begin
Q <= 4'b0000;
end
endmodule
module main;
regclk, reset;
wire [3:0] Q;
downCntr dnCntr1(clk, reset, Q);
always @(posedgeclk)begin
$display(" clk=%b, reset=%b,Q=%b\n", clk, reset, Q);
end
initial begin
forever begin
clk<= 0;
#5
clk<= 1;
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
//#5
//clk<= 0;
end
end
initial begin
reset = 1;
#2
reset = 0;
#85 $finish;
//#17
//reset = 1;
//#12
//reset = 0;
//#85 $finish;
end
initial begin
$dumpfile ("downCntr.vcd");
$dumpvars;
end
endmodule
Structured Procedures
There are two structured procedure statements in Verilog: always and initial. Thesestatements
are the two most basic statements in behavioral modeling. All other behavioralstatements can
appear only inside these structured procedure statements.
initial Statement
All statements inside an initial statement constitute an initial block. An initial block startsat time
0, executes exactly once during a simulation, and then does not execute again. Ifthere are
multiple initial blocks, each block starts to execute concurrently at time 0. Eachblock finishes
execution independently of other blocks. Multiple behavioral statementsmust be grouped,
typically using the keywords begin and end. If there is only onebehavioral statement, grouping is
not necessary. This is similar to the begin-end blocks inPascal programming language or the { }
grouping in the C programming language.
Example 7-1 illustrates the use of the initial statement.
Example 7-1 initial Statement
module stimulus;
reg x,y, a,b, m;
initial
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
m = 1'b0; //single statement; does not need to be grouped
initial
begin
#5 a = 1'b1; //multiple statements; need to be grouped
#25 b = 1'b0;
end
initial
begin
#10 x = 1'b0;
#25 y = 1'b1;
end
initial
#50 $finish;
Endmodule
In the above example, the three initial statements start to execute in parallel at time 0. If adelay
#<delay> is seen before a statement, the statement is executed <delay> time unitsafter the current
simulation time. Thus, the execution sequence of the statements insidethe initial blocks will be as
follows.
time statement executed
0 m = 1'b0;
5 a = 1'b1;
10 x = 1'b0;
30 b = 1'b0;
35 y = 1'b1;
50 $finish;
The initial blocks are typically used for initialization, monitoring, waveforms and otherprocesses
that must be executed only once during the entire simulation run. Thefollowing subsections
discussion how to initialize values using alternate shorthandsyntax. The use of such shorthand
syntax has the same effect as an initial block combinedwith a variable declaration.
always Statement
All behavioral statements inside an always statement constitute an always block. Thealways
statement starts at time 0 and executes the statements in the always blockcontinuously in a
looping fashion. This statement is used to model a block of activity thatis repeated continuously
in a digital circuit. An example is a clock generator module thattoggles the clock signal every
half cycle. In real circuits, the clock generator is activefrom time 0 to as long as the circuit is
powered on. Example 7-5 illustrates one method tomodel a clock generator in Verilog.
Example 7-5 always Statement
module clock_gen (output reg clock);
//Initialize clock at time zero
initial
clock = 1'b0;
//Toggle clock every half-cycle (time period = 20)
always
#10 clock = ~clock;
CS T34 DIGITAL SYSTEM DESIGN Y2/S3
initial
#1000 $finish;
endmodule
In Example 7-5, the always statement starts at time 0 and executes the statement clock =
~clock every 10 time units.
UNVERSITY QUESTIONS