Prelim Module2 - B
Prelim Module2 - B
COURSE INTENDED On the completion of the course, student is expected to be able to do the
LEARNING OUTCOMES: following:
II. OBJECTIVES: By the end of this module you should be able to:
IV. CONTENTS:
Lesson Coverage:
DESIGN DESCRIPTIONS
Verilog language has the capability of designing a module in several coding styles. Depending on the needs of a
design, internals of each module can be defined at four level of abstractions. Irrespective of the internal abstraction
level, the module would behave exactly in the similar way to the external environment.
Following are the four different levels of abstraction which can be described by four different coding styles of
Verilog language:
Levels of Description
• Switch Level:
– layout of the wires, resistors and transistors on an IC chip
– Easiest to synthesize, very difficult to write, not really used
Example CODE
// Display a header
$display("Time A B C");
// Prints the values anytime a value of A, B or C changes
$monitor(" %0d %b %b %b", $time, A, B, C);
end
//main_process will loop until simulation is over always begin: main_process
always begin: main_process
#1 A = A + 1; // #1 means do after one unit of simulation time
#1 B[0:3] = ~A[4:7]; // ~ is bitwise "not" operator
#1 C = &A[6:7]; // bitwise "and" reduction of last two bits of A
end
endmodule
In module simple, we declared A and B as 8-bit registers and C a 1-bit register or flip flop.
Inside of the module, the one "always" and two "initial" constructs describe three threads of control, i. e., they
run at the same time or concurrently.
Within the initial construct, statements are executed sequentially much like in C or other traditional imperative
programming languages.
The always construct is the same as the initial construct except that it loops forever as long as the simulation
runs.
The notation #1 means to execute the statement after delay of one unit of simulated time.
Therefore, the thread of control caused by the first initial construct will delay for 20 time units before calling the
system task $stop and stop the simulation.
The $display system task allows the designer to print a message much like printf does in the language C.
Every time unit that one of the listed variables' value changes, the $monitor system task prints a message.
The system function $time returns the current value of simulated time.
Time A B C
0 00000000 xxxxxxxx x
1 00000001 xxxxxxxx x
2 00000001 1110xxxx x
3 00000001 1110xxxx 0
4 00000010 1110xxxx 0
5 00000010 1101xxxx 0
7 00000011 1101xxxx 0
8 00000011 1100xxxx 0
9 00000011 1100xxxx 1
10 00000100 1100xxxx 1
11 00000100 1011xxxx 1
12 00000100 1011xxxx 0
13 00000101 1011xxxx 0
14 00000101 1010xxxx 0
16 00000110 1010xxxx 0
17 00000110 1001xxxx 0
19 00000111 1001xxxx 0
Stop at simulation time 20
Lexical Conventions
Keywords, e. g., module, are reserved and in all lower-case letters. Verilog is case sensitive. Spaces are
important in that they delimit tokens in the language.
Numbers are specified in the traditional form of a series of digits with or without a sign but also in the following
form: <size><base format><number>
<base format>: is the single character ' followed by one of the following characters b, d, o and h, which stand
for binary, decimal, octal and hex, respectively.
<number>: contains digits which are legal for the <base format>
Examples:
▪ Arithmetic: +, - ! ~ * /
▪ Shift: << >> Relational: < <= > >= == != === !==
▪ Logical && ||.
Program Structure
A digital system as a set of modules where each module has an interface to other module (connectivity). GOOD
PRACTICE: place one module per file ( not a requirement) , Modules may run concurrently where usually one
top level module which invokes instances of other modules. And usually called a stimulus block.
MODULES
Represent bits of hardware ranging from simple gates to complete systems, e. g., a microprocessor. Can either
be specified behaviorally or structurally (or a combination of the two)
<port list> is a list of input, inout and output ports which are used to connect to other modules.
<declares> section specifies data objects as registers, memories and wires as wells as procedural constructs
such as functions and tasks.
Example :
• The continuous assignment assign continuously watches for changes to variables in its right hand side
and whenever that happens the right hand side is re-evaluated and the result immediately propagated to
the left hand side (out).
• The continuous assignment statement is used to model combinational circuits where the outputs change
when one wiggles the input.
• Here is a structural specification of a module AND obtained by connecting the output of one NAND to
both inputs of another one.
Endmodule
This module has two instances of the NAND module called NAND1 and NAND2 connected together by an
internal wire w1.
Ports
Port_list is a vital component of verilog module. Ports provide a way for a module to speak with the external
world through input and output. Every port within the port list must be declared as input, output or inout.
All ports declared together of the above is assumed to be a wire by default, to declare it otherwise it's necessary
to declare it again. as an example within the D-type flip flop we wish the output to carry on to its value until the
following clock edge so it's to be a register.
.......
endmodule
Note: by convention, outputs of the module are always first in the port list. This convention is also used in the
predefined modules in Verilog.
A different way ports can be declared as below other than the above one:
module d_ff(
output reg q,
input d,reset,clock);
.......
endmodule
The order of abstraction mentioned above are from highest to lowest level of abstraction. The top three would
be explained using a 4:1 mux.
https://www.researchgate.net/figure/MUX-graphical-symbol-a-truth-table-b_fig1_257799438
http://www.vlsifacts.com/wp-content/uploads/2016/01/Mux-behavioral.png
Verilog Code
module Mux_4to1(
input [3:0] i,
input [1:0] s,
output reg o
);
always @(s or i)
begin
case (s)
2'b00 : o = i[0];
2'b01 : o = i[1];
2'b10 : o = i[2];
2'b11 : o = i[3];
default : o = 1'bx;
endcase
end
endmodule
Here we are implementing the functionality of multiplexer at higher-level of abstraction without looking into
internal details of the design as in “Structural Modeling”
Dataflow level
http://www.vlsifacts.com/wp-content/uploads/2016/01/Mux-dataflow.png
Verilog Code
module Mux_4to1_df(
input [3:0] i,
input [1:0] s,
output o
);
assign o = (~s[1] & ~s[0] & i[0]) | (~s[1] & s[0] & i[1]) | (s[1] & ~s[0] & i[2]) | (s[1] & s[0] & i[3]);
endmodule
• Assign statement is a continuous statement where in any changes in signals on right hand side will
update the output signal.
• Any changes in the input signals will execute the assign statement and the updated value will be
reflected on the output “o”.
The module is implemented in terms of logic gates and interconnections between these gates.
http://www.vlsifacts.com/wp-content/uploads/2016/01/Mux-structural.png
A structural system representation is closer to the physical implementation than behavioral one but it is more
involved because of large number of details.
Since logic gate is most popular component, Verilog has a predefined set of logic gates known as primitives.
module Mux_4to1_gate(
input [3:0] i,
input [1:0] s,
output o
);
• In this approach, the multiplexer is represented in gate-level representation. It consists of ‘4’ AND gates,
‘2’ NOT gates and ‘1’ OR gate. Intermediate signals “NS1”, “NS0”, “Y3”, “Y2”, “Y1”, and “Y0” are
declared as wires.
• In this AND, OR and NOT gates are basic Verilog inbuilt primitives.
• These primitives are instantiated and connected in such a way, to get the functionality of multiplexer.
Switch level
This is the lowest level of abstraction provided by Verilog. A module can be implemented in terms of
transistors, switches, storage nodes, and the interconnections between them.
In Verilog HDL transistors are known as Switches that can either conduct or open. Design at this level requires
knowledge of switch-level implementation details. Verilog supports design that can be represented in different
modeling levels.
Describing the design at different levels is known as Mixed-level Modeling. Simulating the design consisting of
different modeling levels is known as Mixed-level Simulation.
Data Types
A driver is a data type which can drive a load. Basically, in a physical circuit, a driver would be anything that
electrons can move through/into.
Driver that can store a value (example: flip-flop). Driver that can not store value, but connects two points
(example: wire). The first type of driver is called a reg in Verilog (short for "register"). The second data type is
called a wire (for... well, "wire"). You can refer to tidbits section to understand it better.
There are lots of other data types - for instance, registers can be signed, unsigned, floating point... as a newbie,
don't worry about them right now.
Examples :
Operators
Verilog operators operate on several data types to produce an output , not all Verilog operators are synthesible
(can produce gates) , some operators are similar to those in the C language , remember, you are making gates, not
an algorithm (in most cases)
Arithmetic Operators
Binary operators:
Add (+)
Subtract (-)
Multiply (*)
Divide (/)
Power ( ** )
Modulus (%)
If any operand bit has a value” x”, the result of the expression is all” x”. If an operand is not fully known the
result cannot be either
Modulus operator yields the remainder from division of two numbers. It works like the modulus operator in C
Modulus is synthesible
3 % 2; //evaluates to 1
16 % 4; //evaluates to 0
-7 % 2; //evaluates to -1, takes sign of first operand
7 % -2; //evaluates to 1, takes sign of first operand
Unary operators
Operators ”+” and ”-” can act as unary operators , and they indicate the sign of an operand i.e.,
-4 // negative four
+5 // positive five
• coding style
• synthesis tool used
• synthesis constraints (more later on this)
• ripple-carry adder
• look-ahead-carry adder (how many bits of lookahead to be used?)
• carry-save adder
When writing RTL code, keep in mind what will eventually be needed continually thinking about structure,
timing, size, power.
Logical Operators
Operands not equal to zero are equivalent to one. Logical operators take variables or expressions as operators.
greater-than (>)
less-than (<)
greater-than-or-equal-to (>=)
less-than-or-equal-to (<=)
//let a = 4, b = 3, and...
//x = 4’b1010, y = 4’b1101, z = 4’b1xxx
!!! Note: These are expensive and slow operators at gate level !!!
module less8(
input [7:0] a,b,
output z
);
endmodule
Equality Operators
Equality operators return logical 1 if expression is true, else 0 . Operands are compared bit by bit , zero filling is
done if operands are of unequal length (Warning!)
Logical case inequality allows for checking of x and z values, checking for X and Z is most definitely non-
synthesible! .
//let a = 4, b = 3, and...
//x = 4’b1010, y = 4’b1101,
//z = 4’b1xxz, m = 4’b1xxz, n = 4’b1xxx
a == b //evaluates to logical 0
x != y //evaluates to logical 1
x == z //evaluates to x
z === m //evaluates to logical 1
z === n //evaluates to logical 0
m !== n //evaluates to logical 1
Bitwise Operators
Reduction Operators
And ( & ),
Nand ( ∼& ),
Or ( | ),
Nor ( ∼| ) xor ( ^ ),
Xnor ( ^∼,∼^ )
//let x = 4’b1010
Shift Operators
Shift operator shifts a vector operand left or right by a specified number of bits, filling vacant bit positions with
zeros. Shifts do not wrap around.
// let x = 4’b1100
y = x >> 1; // y is 4’b0110
y = x << 1; // y is 4’b1000
y = x << 2; // y is 4’b0000
Replication Operator { { } }
y = { 4{a} } // y = 4’b1111
y = { 4{a}, 2{b} } // y = 8’b11110000
y = { 4{a}, 2{b}, c } // y = 8’b1111000010
Conditional Operator ?:
assign z = (a | b);
assign a = in1 & sel;
assign b = in2 & ~sel;
assign z = (a | b);
assign a = in1 & sel;
assign b = in2 & ~sel;
V. REFERENCES: Roth, C.H. Jr. And John, L. K. (2018). Digital Systems Design Using VHDL (3rd
ed.). Texas, USA: Cengage Unlimited