EEE303-Week04 - Verilog
EEE303-Week04 - Verilog
1
12/16/2022
303
Lecture 04.1
Dr. Sajid Muhaimin Choudhury, Assistant Professor
Department of Electrical and Electronics Engineering
Bangladesh University of Engineering and Technology
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 4
Department of EEE, BUET
4
2
12/16/2022
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 5
Department of EEE, BUET
5
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 6
Department of EEE, BUET
6
3
12/16/2022
RTL Coding
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 7
Department of EEE, BUET
7
Introduction
• Hardware description language (HDL):
• specifies logic function only
• Computer-aided design (CAD) tool produces or synthesizes
the optimized gates
• Most commercial designs built using HDLs
• Two leading HDLs:
• SystemVerilog
– developed in 1984 by Gateway Design Automation
– IEEE standard (1364) in 1995
– Extended in 2005 (IEEE STD 1800-2009)
• VHDL 2008
– Developed in 1981 by the Department of Defense
– IEEE standard (1076) in 1987
– Updated in 2008 (IEEE STD 1076-2008)
4
12/16/2022
HDL to Gates
• Simulation
– Inputs applied to circuit
– Outputs checked for correctness
– Millions of dollars saved by debugging in simulation instead of
hardware
• Synthesis
– Transforms HDL code into a netlist describing the hardware (i.e.,
a list of gates and the wires connecting them)
IMPORTANT:
When using an HDL, think of the hardware the
HDL should produce.
5
12/16/2022
Verilog Module
Verilog Modules
a
Verilog SystemVerilog
b y
Module Module
c
6
12/16/2022
Structural vs Behavior
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 13
Department of EEE, BUET
13
Behavioral Verilog
SystemVerilog:
module example(input logic a, b, c,
output logic y);
assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c;
endmodule
7
12/16/2022
Verilog:
module example(a, b, c, y);
input a, b, c;
output y;
assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c;
endmodule
8
12/16/2022
HDL
Behavioral Synthesis
Verilog: Synthesis
Verilog:
module example(a, b, c, y);
input a, b, c;
output y;
assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c;
endmodule
Synthesis:
b
c y
un5_y y
un8_y
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 17
Department of EEE, BUET
17
9
12/16/2022
SystemVerilog Syntax
• Case sensitive
• Example: reset and Reset are not the same
signal.
• No names that start with numbers
• Example: 2mux is an invalid name
• Whitespace ignored
• Comments:
• // single line comment
• /* multiline
comment */
Assignments
module fulladder(a, b, cin, s, cout);
input a,b,cin;
output s, cout;
logic p, g; // internal nodes
assign p = a ^ b;
assign g = a & b;
assign s = p ^ cin;
assign cout = g | (p & cin);
endmodule
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 20
Department of EEE, BUET
20
10
12/16/2022
Assignment
• The three assign statements are independent and can execute in any order or concurrently.
• The right side of an assign statement is evaluated and its resulting value assigned to the signal
on the left side whenever one of the signals used in the right side changes value.
• This type of assignment is also called a continuous assignment.
• This method of interpreting the execution of assign statements is quite different
• from that use in conventional programming languages in the following ways:
• Two or more assignments can execute simultaneously. This is necessary to represent the
timing characteristics of hardware systems.
• An assignment executes whenever it is ready (i.e., has new data for the variables on its right
side)
• There is no concept of "locus of control" or “program counter” that determines the next
instruction to execute. Therefore, the order the assignments are written does not matter
• This method of assignment statement execution is sometimes called non-procedural or data-
driven execution, where conventional programming languages are said to be procedural.
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 21
Department of EEE, BUET
21
• The previous example is a behavioral descriptions because it specifies the logical values
of the circuit's outputs as logical equations with no reference to how the gates in a
possible implementation might be interconnected.
• It is also possible to specify a structural description in Verilog that specifies explicitly how a
set of smaller components (e.g., gates) are interconnected to form a larger system as
shown on the following slide.
• Note that the internal connections from gate outputs to gate inputs are declared to be of
type wire.
• These wire declarations could be omitted as long as the component modules are simple
gates. Verilog will assume that any gate output signal that is not declared is of type wire.
• Each gate is specified by its name (e.g., and, or, ...) and a list of ports or wires connected
to its terminals. All gates have a single output and it is always listed first in this list.
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 22
Department of EEE, BUET
22
11
12/16/2022
Structural Verilog
module circuit1s (A, B, C, D, E, X, Y, Z);
input A, B, C, D, E;
output X, Y, Z;
wire T1, T2, T3, T4, T5, T6, T7;
xor(T1, A, B);
or(T2,T1,C);
not(X, T2);
and(T3, B, C);
or(T4, T3, D);
not(T5, T4);
and(Y, T5, E);
or(T6, A, D);
and(T7, B, C);
or(Z, T6, T7);
endmodule
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 23
Department of EEE, BUET
23
12
12/16/2022
Bitwise Operators
SystemVerilog Verilog:
module gates(input logic [3:0] a, b, module gates(a, b, y1, y2, y3, y4, y5);
output logic [3:0] input [3:0]a; [3:0]b;
y1, y2, y3, y4, y5); output [3:0]y1, [3:0]y2, [3:0]y3,
/* Five different two-input logic [3:0]y4, [3:0]y5;
gates acting on 4 bit busses */ /* Five different two-input logic
assign y1 = a & b; // AND gates acting on 4 bit busses */
assign y2 = a | b; // OR
assign y1 = a & b; // AND
assign y3 = a ^ b; // XOR
assign y2 = a | b; // OR
assign y4 = ~(a & b); // NAND
assign y5 = ~(a | b); // NOR
assign y3 = a ^ b; // XOR
assign y4 = ~(a & b); // NAND
endmodule
Synthesis: assign y5 = ~(a | b); // NOR
endmodule
// single line comment
/*…*/ multiline comment
Reduction Operators
SystemVerilog:
module and8(input logic [7:0] a,
output logic y);
assign y = &a;
// &a is much easier to write than
// assign y = a[7] & a[6] & a[5] & a[4] &
// a[3] & a[2] & a[1] & a[0];
endmodule
Synthesis:
13
12/16/2022
Conditional Assignment
SystemVerilog:
module mux2(input logic [3:0] d0, d1,
input logic s,
output logic [3:0] y);
assign y = s ? d1 : d0;
endmodule
Synthesis:
Internal Variables
SystemVerilog:
module fulladder(input logic a, b, cin,
output logic s, cout);
logic p, g; // internal nodes
assign p = a ^ b;
assign g = a & b;
Synthesis:
assign s = p ^ cin; s
endmodule cin
cout
a
b
un1_cout cout
p
14
12/16/2022
Precedence
Highest ~ NOT
*, /, % mult, div, mod
+, - add,sub
<<, >> shift
<<<, >>> arithmetic shift
<, <=, >, >= comparison
==, != equal, not equal
&, ~& AND, NAND
^, ~^ XOR, XNOR
|, ~| OR, NOR
Lowest ?: ternary operator
Numbers
Format: N'Bvalue
N = number of bits, B = base
N'B is optional but recommended (default is decimal)
Number # Bits Base Decimal Stored
Equivale
nt
3'b101 3 binary 5 101
'b11 unsized binary 3 00…0011
8'b11 8 binary 3 00000011
8'b1010_1011 8 binary 171 10101011
3'd6 3 decimal 6 110
6'o42 6 octal 34 100010
8'hAB 8 hexadecimal 171 10101011
42 Unsized decimal 42 00…0101010
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 30
Department of EEE, BUET
30
15
12/16/2022
lsbmux
mux2
s
[7:4] [7:4]
d0[3:0] y[3:0]
[7:4]
d1[3:0]
msbmux
16
12/16/2022
Z: Floating Output
SystemVerilog:
module tristate(input logic [3:0] a,
input logic en,
output tri [3:0] y);
assign y = en ? a : 4'bz;
endmodule
Synthesis:
en
[3:0] [3:0] [3:0] [3:0]
a[3:0] y[3:0]
y_1[3:0]
Delays
module example(input logic a, b, c,
output logic y);
logic ab, bb, cb, n1, n2, n3;
assign #1 {ab, bb, cb} = ~{a, b, c};
assign #2 n1 = ab & bb & cb;
assign #2 n2 = a & bb & cb;
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
17
12/16/2022
Delays
module example(input logic a, b, c,
output logic y);
logic ab, bb, cb, n1, n2, n3;
assign #1 {ab, bb, cb} =
~{a, b, c};
assign #2 n1 = ab & bb & cb;
assign #2 n2 = a & bb & cb;
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
Delays
1
module example(input logic a, b, c,
output logic y);
logic ab, bb, cb, n1, n2, n3;
assign #1 {ab, bb, cb} =
~{a, b, c};
assign #2 n1 = ab & bb & cb;
assign #2 n2 = a & bb & cb;
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
18
12/16/2022
Delays
module example(input logic a, b, c,
2
output logic y);
logic ab, bb, cb, n1, n2, n3;
assign #1 {ab, bb, cb} =
~{a, b, c};
assign #2 n1 = ab & bb & cb;
assign #2 n2 = a & bb & cb;
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
Delays
module example(input logic a, b, c,
output logic y);
logic ab, bb, cb, n1, n2, n3;
assign #1 {ab, bb, cb} =
~{a, b, c};
assign #2 n1 = ab & bb & cb;
assign #2 n2 = a & bb & cb; 2
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
19
12/16/2022
Delays
module example(input logic a, b, c,
output logic y);
logic ab, bb, cb, n1, n2, n3;
assign #1 {ab, bb, cb} =
~{a, b, c};
assign #2 n1 = ab & bb & cb;
4
assign #2 n2 = a & bb & cb;
assign #2 n3 = a & bb & c;
assign #4 y = n1 | n2 | n3;
endmodule
20
12/16/2022
Reg variable
• Variables declared as type reg hold their value until they are assigned a
new value. It is said that the assigned value is registered in the variable
• Whenever x or y changes value, the always block is executed as follows:
• First, the statement s = x^y executes and registers a new value in s Next, the statement
z = x & s executes using the new value of s that
• it received when the first statement was executed.
• Then the block stops executing and waits for either x or y to change again.
21
12/16/2022
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 43
Department of EEE, BUET
43
• All output signals must be assigned a value every time the always block
executes
• These conditions guarantee that the input signals uniquely determine the
output signals, which is the very definition of a combinational circuit.
• A sensitivity list of the form @(*) is shorthand for a complete list. It is
recommended that you use this notation for combinational always block
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 44
Department of EEE, BUET
44
22
12/16/2022
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 45
Department of EEE, BUET
45
Logic Operation
12/16/2022
23
12/16/2022
Examples
a = b + c ;
a = 1 << 5;
a = !b ;
a = ~b ;
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 47
Department of EEE, BUET
47
If-else-end
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 48
Department of EEE, BUET
48
24
12/16/2022
Case
It's a good idea to have a default case. If the Verilog machine enters into
a non-covered statement, the machine hangs. Defaulting the statement
with a return to idle keeps us safe.
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 49
Department of EEE, BUET
49
While
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 50
Department of EEE, BUET
50
25
12/16/2022
While - counter
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 51
Department of EEE, BUET
51
For
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 52
Department of EEE, BUET
52
26
12/16/2022
Initial Block
12/16/2022
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 53
Department of EEE, BUET
53
303
Lecture 04.2
Dr. Sajid Muhaimin Choudhury, Assistant Professor
Department of Electrical and Electronics Engineering
Bangladesh University of Engineering and Technology
54
27
12/16/2022
Testbenches
• HDL that tests another module: device under test (dut)
• Not synthesizeable
• Types:
• Simple
• Self-checking
• Self-checking with testvectors
28
12/16/2022
Component Description
Generator Generates different input stimulus to be driven to DUT
Testbench Example
• Write SystemVerilog code to implement the
following function in hardware:
y = bc + ab
• Name the module sillyfunction
29
12/16/2022
Testbench Example
• Write SystemVerilog code to implement the
following function in hardware:
y = bc + ab
Simple Testbench
module testbench1();
logic a, b, c;
logic y;
// instantiate device under test
sillyfunction dut(a, b, c, y);
// apply inputs one at a time
initial begin
a = 0; b = 0; c = 0; #10;
c = 1; #10;
b = 1; c = 0; #10;
c = 1; #10;
a = 1; b = 0; c = 0; #10;
c = 1; #10;
b = 1; c = 0; #10;
c = 1; #10;
end
endmodule
30
12/16/2022
Self-checking Testbench
module testbench2();
logic a, b, c;
logic y;
sillyfunction dut(a, b, c, y); // instantiate dut
initial begin // apply inputs, check results one at a time
a = 0; b = 0; c = 0; #10;
if (y !== 1) $display("000 failed.");
c = 1; #10;
if (y !== 0) $display("001 failed.");
b = 1; c = 0; #10;
if (y !== 0) $display("010 failed.");
c = 1; #10;
if (y !== 0) $display("011 failed.");
a = 1; b = 0; c = 0; #10;
if (y !== 1) $display("100 failed.");
c = 1; #10;
if (y !== 1) $display("101 failed.");
b = 1; c = 0; #10;
if (y !== 0) $display("110 failed.");
c = 1; #10;
if (y !== 0) $display("111 failed.");
end
endmodule
31
12/16/2022
CLK
Assign Compare
Inputs Outputs to
Expected
• Testbench clock also used as clock for synchronous
sequential circuits
Testvectors File
• File: example.tv
• contains vectors of abc_yexpected
000_1
001_0
010_0
011_0
100_1
101_1
110_0
111_0
32
12/16/2022
1. Generate Clock
module testbench3();
logic clk, reset;
logic a, b, c, yexpected;
logic y;
logic [31:0] vectornum, errors; // bookkeeping variables
logic [3:0] testvectors[10000:0]; // array of testvectors
// generate clock
always // no sensitivity list, so it always executes
begin
clk = 1; #5; clk = 0; #5;
end
initial
begin
$readmemb("example.tv", testvectors);
vectornum = 0; errors = 0;
reset = 1; #22; reset = 0;
end
33
12/16/2022
34
12/16/2022
EDA Playground
Example
70
35
12/16/2022
https://www.edaplayground.com/x/3k2b
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 71
Department of EEE, BUET
71
36
12/16/2022
37
12/16/2022
https://www.edaplayground.com/x/3BES
EEE 303 – Digital Electronics Dr. Sajid Muhaimin Choudhury 75
Department of EEE, BUET
75
38
12/16/2022
** VVP Stop(0) **
** Flushing output streams.
** Current simulation time is 90 ticks.
39