Unit I 2nd Part
Unit I 2nd Part
Unit I 2nd Part
Suresh C
Assistant Professor
EEE Dept,RVCE
(Design Flow)
always @(posedge clk) begin
if (sel1) begin
if (sel2)
out=in1;
else
out=in2;
end
end
X
B
Y
2
C / matlab /
systemC …
3
Gate (Structural)
The structural level in Verilog is appropriate for small
components such as ASIC and FPGA cells.
◼ Verilog has built-in primitives, such as the AND gate, that describe
basic logic functions. (AND OR)
◼ You can describe your own User Defined Primitives (UDPs).
◼ The resulting netlist from synthesis is often purely structural, but
you can also use structural modeling for glue logic.
The function of the following structural model is represented in
Verilog primitives, or gates. It also contains propagation delays:
4
Dataflow
Describe the algorithm in terms of logical data flow. (
module mux2_1 (out, a, b, sel);
output out;
input a, b, sel;
assign out = (a&~sel) | (b&sel);
// or assign out = (sel == 0) ? a : b;
endmodule
5
Behavioral
Describe the algorithm without concern for the actual logic
required to implement it.
The behavior of the following MUX is : always, at any change in
signals a or b or sl, if sl is 0 pass the value of a to out, else pass the
value of b to out.
module DF_AND (in1 , in2 , Out) module Beh_AND (in1 , in2 , Out)
Memory
9
Key Language Features:
Verilog Module (2/2)
(1) Verilog Module
module
Modules are the basic building blocks in the design
hierarchy.
You place the descriptions of the logic being modeled statements
inside modules.
Modules can represent:
◼ A physical block such as an IC or ASIC cell
◼ A logical block such as the ALU portion of a CPU design
◼ The complete system
Every module description starts with the keyword module,
has a name (SN74LS74, DFF, ALU, etc.), and ends with
the keyword endmodule
Modules define a new scope (level of hierarchy) in Verilog. ports
Example is shown as below :
module ALU
…… Logic description(abstraction level)
……
endmodule
10
Key Language Features:
Module Ports
(2) Module Ports
Modules communicate with the outside world through ports.
Module ports are equivalent to the pins in hardware.
You list a module’s ports in parentheses “()” after the module name.
You declare ports to be input, output, or inout (bidirectional) in the
module description. module
DFF Example:
statements
The “unknown” logic value in Verilog is not the same as “don’t care”. It
represents a situation where the value of a node cannot be predicted.
In real hardware, this node will most be at either 1 or 0.
When the Z value is present at the input of a gate, or when it is
encountered in an expression, the effect is usually the same as an x
value.
12
TRISTATE BUFFER
Tri-state buffer with ENA High is Switch open or Hi-Z circuit. T
Tri-state buffer with ENA low is switch open, Truth Table and symbol below
Tri-state buffer acts as a switch in digital circuit by isolating a
signal path in a circuit. This switch can attain three logical
states. The three states are 0, 1 and ‘Z’.
The logical state 0 and 1 are possible when the switch is
CLOSE. The logical value ‘Z’ or high impedance is attained
when switch is OPEN.
So when switch is open the input to tristate buffer is isolated
from the circuit and output can be driven by some other
logical path on a shared connection/bus.
Inverting Tri-state buffer Truth Table, circuit and symbol below:
Integer Constants
In Verilog, constant values (literals) can be integers or reals.
Integers can be sized or unsized. Sized integers are represented as
<size>'<base><value>
◼ <size> is the size in bits.
◼ <base> can be b(binary), o(octal), d(decimal), or h(hexadecimal).
◼ <value> is any legal number in the selected base, including x and z bits.
Underscores(_) in number are ignored (4’b10_11 = 4’b1011)
◼ Underscores(_) can be put anywhere in a constant number, except the
$<identifier>
The ‘$’ sign denotes Verilog system tasks and functions.
A number of system tasks and functions are available to
perform different operations, such as:
◼ Finding the current simulation time ($time))
◼ Displaying/monitoring the values of the signal ($display,
$monitor)
◼ Stopping the simulation ($stop)
◼ Finishing the simulation ($finish)
$monitor ($time, “a = %b, b = %h”, a, b);
17
Special Language Tokens: #
Delay Specification
The pound sign (#) character denotes the delay specification for
procedural statements, and for gate instances but not module
instances.
The # delay for gate instances is referred to by many names,
including gate delay, propagation delay, intrinsic delay, and intra-
object delay.
module MUX2_1 (out, a, b, sel) ;
output out ;
input a, b, sel;
not #1 not1(sel_,sel);
and #2 and1(a1,a,sel_);
and #2 and2(b1,b,sel);
or #1 or1(out,a1,b1);
endmodule
18
Data Types
Nets
◼ Represent physical connection between devices (default = z)
◼ Verilog automatically propagates a new value onto a net when the
drivers on the net change value. Value is that of its drivers such
as a continuous assignment (data flow modeling)or a gate
wire [MSB:LSB] DAT; // vector wire
wire RDY, START;
wire [2:0] ADDR;
Registers
◼ Represent a variable that can hold a value. (default = x). (They are
used in procedural blocks (behavioral modeling).
reg A, B, C; // 1-bit scalars
22
Operator Types
The following table shows the operators in Verilog, in order of precedence.
Note that “and” operators always have higher precedence than “or” operators
of the same type.
Type of Operators Symbols Highest
Concatenate & Replicate {} {{ }}
Unary ! ~ & | ^
Arithmetic ** * / % + -
Logical shift <<< >>> << >> Precedence
Relational > < >= <=
Equality == === != !==
Binary bit-wise & | ^ ~^
Binary logical && ||
Conditional ?:
Lowest
{{ }} replication
a[5:0]
c[2:0]
Ex. a = {2{c}};
24
Unary Reduction Operators
& and module reduction();
| or reg val;
reg [3:0] rega, regb;
^ xor
initial begin
~^ xnor rega = 4'b0100;
^~ xnor regb = 4'b1111;
end
Reduction operators perform a bit- initial fork
wise operation on all the bits of a #10 val = ®a ; // val = 0
single operand. #20 val = |rega ; // val = 1
#30 val = ®b ; // val = 1
The result is always 1'b1,1'b0 or 1'bx. #40 val = |regb ; // val = 1
#50 val = ^rega ; // val = 1
#60 val = ^regb ; // val = 0
#70 val = ~|rega; // (nor) val = 0
#80 val = ~®a; // (nand) val = 1
#90 val = ^rega && ®b; // val = 1
#99 $finish;
join
endmodule 25
Arithmetic Operators
+ add module arithops ( );
integer ans, int;
- subtract
parameter five = 5;
* multiply reg [3:0] rega, regb;
/ divide reg [3:0] num;
% modulus initial begin
** exponent (power) (verilog-2001) rega = 3;
regb = 4'b1010;
An assignment of a negative result int = -3;
end
to a reg or other unsigned variable initial fork
uses the 2’s complement. #10 ans = five * int; // ans = -15
If any bit of any operand is #20 ans = (int + 5)/2; // ans = 1
unknown(x) or tristate(z), the result is #30 ans = five/int; // ans = -1
#40 num = rega + regb; // num = 1101
simply unknown(x). #50 num = rega + 1; // num = 0100
In integer division, the remainder is #60 num = int; // num = 1101
discarded. (2補數)
#70 num = regb % rega; // num = 1
#80 ans = 2**3 // ans = 8
#90 $finish;
join
endmodule
26
Shift Operators
module shift ();
reg [5:0] numa, numb, numc, numd, nume;
>> shift right
reg [3:0] rega;
<< shift left reg signed [3:0] regb;
>>> arithmetic shift right (verilog-2001) initial begin
rega = 4'b1011;
<<< arithmetic shift left (verilog-2001)
regb = 4’b1101;
a << n end
◼ a shift left n bits, and fills in zero bits b initial begin
>> n #10 numa = rega << 2 ; // rega = 1011
◼ b shift right n bits, and fills in zero bits c // numa= 1 0 1 1 0 0
<<< n
#20 numb = rega >> 3; // rega = 1011
◼ c shift left n bits, and fills in zero bits d
// numb= 0 0 0 0 0 1
>>> n
◼ d shift right n bits, and fills in signed bits #30 numc = regb <<< 1; // regb = 1101
// numc= 1 1 1 0 1 0
Shift operators perform left or right bit #40 numd = regb >>> 2; // regb = 1101
shifts to the first operand. // numd= 1 1 1 1 1 1
The second operand is treated as
#50 nume = regb >> 2; // regb = 1101
unsigned. // nume= 0 0 1 1 1 1
If the second operand has unknown or #300 $finish;
tristate bits, the result is unknown. end
endmodule 27
Relational Operators
> greater than module relationals ();
< less than reg [3:0] rega, regb, regc;
>= greater than or equal reg val;
<= less than or equal initial begin
rega = 4'b0011; // 3
The result is always 1'b1,1'b0 or regb = 4'b1010; // 10
1'bx. regc = 4'b0x10;
Note: relational operations have end
lower precedence than arithmetic initial fork
operations.
#10 val = regc > rega; // val = x
#20 val = regb < rega; // val = 0
a < size -1
same expression
a < (size -1) #30 val = regb >= rega; // val = 1
#40 val = regb > regc; // val = 1
size – (1 < a) different expression #50 $finish;
size – 1 < a
join
endmodule
29
Equality Operators (1/3)
== logical equality module equalities1();
!= logical inequality reg [3:0] rega, regb, regc;
reg val;
The result is always 1'b1,1'b0 or initial begin
1'bx rega = 4'b0011; // 3
regb = 4'b1010; // 10
== 0 1 x z regc = 4'b1x10;
0 1 0 x x end
1 0 1 x x initial fork
x x x x x #10 val = rega == regb ; // val = 0
#20 val = rega != regc; // val = 1
z x x x x
#30 val = regb != regc; // val = x
#40 val = regc == regc; // val = x
#50 $finish;
join
endmodule
30
Equality Operators (2/3)
=== identity (case equality) module equalities2();
!== nonidentity (case inequality) reg [3:0] rega, regb, regc;
reg val;
The result is always 1'b1 or 1'b0 initial begin
rega = 4'b0011; // 3
=== 0 1 x z regb = 4'b1010; // 10
regc = 4'b1x10;
0 1 0 0 0
end
1 0 1 0 0
Initial fork
x 0 0 1 0
#10 val = rega === regb; // val = 0
z 0 0 0 1 #20 val = rega !== regc; // val = 1
#30 val = regb !== regc; // val = 1
#40 val = regc === regc; // val = 1
#50 $finish;
join
endmodule
31
Equality Operators (3/3)
= is the assignment operator. It copies the value of the RHS of the
expression to the LHS.
== is the logical equality operator
◼ Which the logical equality operator, an X in either of the operand is
logicality unknown.
=== is the case equality operator
◼ With the case equality operator, the result can still evaluate to true (1) or
false (0) when x or z values are present in the operands.
== 0 1 x z === 0 1 x z
0 1 0 x x 0 1 0 0 0
1 0 1 x x 1 0 1 0 0
x x x x x x 0 0 1 0
z x x x x z 0 0 0 1
a = 1011
b = 0010
34
Logical Versus Bit-Wise
Negation
! logical (not) module negation();
reg [3:0] rega, regb;
~ bit-wise (not)
reg [3:0] bit;
reg log;
The logical negation will return 1'b0,
1'b1, or 1'bx.
initial begin
Bit-wise negation returns a value rega = 4'b1011;
with the same number of bits that regb = 4'b0000;
are in the operand.
end
initial fork
#10 bit = ~rega; // num = 0100
#20 bit = ~regb; // num = 1111
#30 log = !rega; // num = 0
#40 log = !regb; // num = 1
#50 $finish;
join
endmodule
35
Conditional Operator (1/2)
The syntax for the conditional operator is :
◼ <LHS> = <condition> ? <true_expression> :
<false_expression>;
This can be read as :
◼ If condition is TRUE,
then LHS = true_expression, else LHS = false_expression.
If the condition is unknown, and the true_expression and
false_expression are not equal, the output is unknown.
◼ if sel is 0 then out is set equal to a,
if sel is 1 then out is set equal to b,
if sel is unknown, a is 0, and b is 0, then out is set equal to 0,
if sel is unknown, a is 0, and b is 1, then out is unknown.
37
Dataflow Modeling
A Full Adder Example
//netlist declaration
xor u0 (sum, a, b, ci);
and u1 (net1, a, b);
and u2 (net2, b, ci);
and u3 (net3, ci, a);
or u4 (cout, net1, net2, net3);
endmodule
40
Continuous Assignments (1/4)
You can model combinational logic with continuous assignments,
instead of using gates and interconnect nets.
Use continuous assignments outside of a procedural block.
Use a continuous assignment to drive a value onto a net.
The LHS is updated at any change in the RHS expression, after a
specified delay.
◼ Continuous assignments can only contain simple, left-hand side
42
Continuous Assignments (3/4)
module assigns (o1, o2, eq, AND, OR, even, odd, one, SUM, COUT, a, b, in, sel, A, B, CIN);
output [7:0] o1, o2;
output [31:0] SUM;
output eq, AND, OR, even, odd, one, COUT;
input a, b, CIN;
input [1:0]sel;
input [7:0] in;
input [31:0] A, B;
wire [7:0] #3 o2; // No assignment yet, but a delay
tri AND = a&b, OR = a|b; // two assignments
wire #10 eq = (a == b); // implicit, with delay
wire [7:0] (strong1, weak0) #(3,5,2) o1 = in; // strength and delays
43
Continuous Assignments (4/4)
<assign> [#delay] [strength] <net_name> = <expression>
44