Onur Digitaldesign 2019 Lecture7.2 HDL Afterlecture
Onur Digitaldesign 2019 Lecture7.2 HDL Afterlecture
2
Required Readings (Next Week)
n Von Neumann Model, LC-3, and MIPS
q P&P, Chapter 4, 5
q H&H, Chapter 6
q P&P, Appendices A and C (ISA and microarchitecture of LC-3)
q H&H, Appendix B (MIPS instructions)
n Programming
q P&P, Chapter 6
3
Agenda
n Hardware Description Languages
4
Hardware Description Languages
& Verilog (Combinational Logic)
5
2017: Intel Kaby Lake
• 64-bit processor
• 4 cores, 8 threads
• 14-19 stage
pipeline
• 3.9 GHz clock
• 1.75B transistors
• In ~47 years,
about 1,000,000-
fold growth in
https://en.wikichip.org/wiki/intel/microarchitectures/kaby_lake transistor count
and performance!
6
How to Deal with This Complexity?
n Hardware Description Languages!
n Verilog
q Developed in 1984 by Gateway Design Automation
q Became an IEEE standard (1364) in 1995
q More popular in US
8
Hardware Design Using Verilog
9
Hierarchical Design
https://techreport.com/review/21987/intel
n Design hierarchy of modules is built -core-i7-3960x-processor
using instantiation
q Predefined “primitive” gates (AND, OR, …)
q Simple modules are built by instantiating
these gates (components like MUXes)
q Other modules are built by instantiating
simple components, …
n Hierarchy controls complexity
q Analogous to the use of function
abstraction in SW
n Complexity is a BIG deal
q In real world how big is size of one “blob”
of random logic that we would describe as How many?
10
Top-Down Design Methodology
n We define the top-level module and identify the
sub-modules necessary to build the top-level module
n Subdivide the sub-modules until we come to leaf cells
q Leaf cell: circuit components that cannot further be divided
(e.g., logic gates, cell libraries)
Top-level
Module
… … … …
Leaf-cell Leaf-cell Leaf-cell Leaf-cell
11
Bottom-Up Design Methodology
n We first identify the building blocks that are available to us
n Build bigger modules, using these building blocks
n These modules are then used for higher-level modules until
we build the top-level module in the design
Top-level
Module
12
Defining a Module in Verilog
n A module is the main building block in Verilog
a
Verilog
b example y
Module
c
inputs output
13
Implementing a Module in Verilog
a
Verilog
b example y
Module
c
endmodule
14
A Question of Style
15
What If We Have Multi-bit Input/Output?
n You can also define multi-bit Input/Output (Bus)
q [range_end : range_start]
q Number of bits: range_end – range_start + 1
n Example:
input [31:0] a; // a[31], a[30] .. a[0]
output [15:8] b1; // b1[15], b1[14] .. b1[8]
output [7:0] b2; // b2[7], b2[6] .. b2[0]
input c; // single signal
// Concatenating is by {}
assign y = {a[2],a[1],a[0],a[0]};
17
Basic Syntax
n Verilog is case sensitive
q SomeName and somename are not the same!
n Names cannot start with numbers:
q 2good is not a valid name
n Whitespaces are ignored
/* Multiline comments
are defined like this */
18
Two Main Styles of HDL Implementation
n Structural (Gate-Level)
q The module body contains gate-level description of the circuit
q Describe how modules are interconnected
q Each module contains other modules (instances)
q … and interconnections between these modules
q Describes a hierarchy
n Behavioral
q The module body contains functional description of the circuit
q Contains logical and mathematical operators
q Level of abstraction is higher than gate-level
n Many possible gate-level realizations of a behavioral description
20
Structural HDL: Instantiating a Module
i_first
i_second
21
Structural HDL Example
n Module Definitions in Verilog
module top (A, SEL, C, Y); i_first
input A, SEL, C; i_second
output Y;
wire n1;
endmodule
22
Structural HDL Example
n Defining wires (module interconnections)
module top (A, SEL, C, Y); i_first
input A, SEL, C; i_second
output Y;
wire n1;
endmodule
23
Structural HDL Example
n The first instantiation of the “small” module
module top (A, SEL, C, Y); i_first
input A, SEL, C; i_second
output Y;
wire n1;
endmodule
24
Structural HDL Example
n The second instantiation of the “small” module
module top (A, SEL, C, Y); i_first
input A, SEL, C; i_second
output Y;
wire n1;
25
Structural HDL Example
n Short form of module instantiation
module top (A, SEL, C, Y); i_first
input A, SEL, C; i_second
output Y;
wire n1;
// alternative
small i_first ( A, SEL, n1 );
/* Shorter instantiation,
pin order very important */ module small (A, B, Y);
input A;
// any pin order, safer choice input B;
small i_second ( .B(C), output Y;
.Y(Y),
.A(n1) ); // description of small
endmodule endmodule
endmodule
27
Behavioral HDL
28
Recall: Two Main Styles of HDL Implementation
n Structural (Gate-Level)
q The module body contains gate-level description of the circuit
q Describe how modules are interconnected
q Each module contains other modules (instances)
q … and interconnections between these modules
q Describes a hierarchy
n Behavioral
q The module body contains functional description of the circuit
q Contains logical and mathematical operators
q Level of abstraction is higher than gate-level
n Many possible gate-level realizations of a behavioral description
endmodule
30
Behavioral HDL: Schematic View
A behavioral implementation still models a
hardware circuit!
a AND
AND OR y
b
AND
c
31
Bitwise Operators in Behavioral Verilog
module gates(input [3:0] a, b,
output [3:0] y1, y2, y3, y4, y5);
endmodule
32
Bitwise Operators: Schematic View
33
Reduction Operators in Behavioral Verilog
module and8(input [7:0] a,
output y);
assign y = &a;
endmodule
34
Reduction Operators: Schematic View
[0]
[1]
[2]
[3]
[7:0]
a[7:0] [4]
AND y
[5]
[6]
[7] 8-input AND gate
35
Conditional Assignment in Behavioral Verilog
module mux2(input [3:0] d0, d1,
input s,
output [3:0] y);
assign y = s ? d1 : d0;
// if (s) then y=d1 else y=d0;
endmodule
36
Conditional Assignment: Schematic View
37
More Complex Conditional Assignments
module mux4(input [3:0] d0, d1, d2, d3
input [1:0] s,
output [3:0] y);
endmodule
38
Even More Complex Conditional Assignments
module mux4(input [3:0] d0, d1, d2, d3
input [1:0] s,
output [3:0] y);
assign y = (s == 2’b11) ? d3 :
(s == 2’b10) ? d2 :
(s == 2’b01) ? d1 :
d0;
// if (s = “11” ) then y= d3
// else if (s = “10” ) then y= d2
// else if (s = “01” ) then y= d1
// else y= d0
endmodule
39
Precedence of Operations in Verilog
Highest
Lowest
40
How to Express Numbers ?
N Bxx
8 b0000_0001
n (N) Number of bits
q Expresses how many bits will be used to store the value
n (B) Base
q Can be b (binary), h (hexadecimal), d (decimal), o (octal)
n (xx) Number
q The value expressed in base
q Can also have X (invalid) and Z (floating), as values
q Underscore _ can be used to improve readability
41
Number Representation in Verilog
32 bits
(default)
42
Reminder: Floating Signals (Z)
n Floating signal: Signal that is not driven by any circuit
q Open circuit, floating wire
n Also known as: high impedance, hi-Z, tri-stated signals
module tristate_buffer(input [3:0] a,
input en,
output [3:0] y);
assign y = en ? a : 4'bz;
endmodule
en
[3:0] [3:0] [3:0] [3:0]
a[3:0] y[3:0]
y_1[3:0]
43
Recall: Tri-State Buffer
n A tri-state buffer enables gating of different signals onto a
wire
44
Recall: Example Use of Tri-State Buffers
n Imagine a wire connecting the CPU and memory
q At any time only the CPU or the memory can place a value on
the wire, both not both
q You can have two tri-state buffers: one driven by CPU, the
other memory; and ensure at most one is enabled at any time
45
Recall: Example Design with Tri-State Buffers
GateCPU
CPU
GateMem
Memory
Shared Bus
46
Truth Table for AND with Z and X
A
AND
0 1 Z X
0 0 0 0 0
1 0 1 X X
B Z 0 X X X
X 0 X X X
47
What Happens with HDL Code?
n Synthesis
q Modern tools are able to map synthesizable HDL code into
low-level cell libraries à netlist describing gates and wires
q They can perform many optimizations
q … however they can not guarantee that a solution is optimal
n Mainly due to computationally expensive placement and routing
algorithms
q Most common way of Digital Design these days
n Simulation
q Allows the behavior of the circuit to be verified without
actually manufacturing the circuit
q Simulators can work on structural or behavioral HDL
48
Recall This “example”
module example (a, b, c, y);
input a;
input b;
input c;
output y;
endmodule
49
Synthesizing the “example”
a AND
AND OR y
b
AND
c
50
Simulating the “example”
AND
a
AND OR y
b
AND
c
1
signals
1
1
0
q At low-level
n Poor readability
n More optimization opportunities (especially for low-level tools)
q At a higher-level of abstraction
n Better readability
n Limited optimization opportunities
53
Comparing Two Numbers
n Defining your own gates as new modules
endmodule endmodule
54
Gate-Level Implementation
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire c0, c1, c2, c3, c01, c23;
endmodule
55
Using Logical Operators
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire c0, c1, c2, c3, c01, c23;
endmodule
56
Eliminating Intermediate Signals
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire c0, c1, c2, c3;
endmodule
57
Multi-Bit Signals (Bus)
module compare (input [3:0] a, input [3:0] b,
output eq);
wire [3:0] c; // bus definition
endmodule
58
Bitwise Operations
module compare (input [3:0] a, input [3:0] b,
output eq);
wire [3:0] c; // bus definition
endmodule
59
Highest Abstraction Level: Comparing Two Numbers
endmodule
60
Writing More Reusable Verilog Code
n We have a module that can compare two 4-bit numbers
61
Parameterized Modules
module mux2
#(parameter width = 8) // name and default value
(input [width-1:0] d0, d1,
input s,
output [width-1:0] y);
assign y = s ? d1 : d0;
endmodule
62
Instantiating Parameterized Modules
module mux2
#(parameter width = 8) // name and default value
(input [width-1:0] d0, d1,
input s,
output [width-1:0] y);
assign y = s ? d1 : d0;
endmodule
63
What About Timing?
n It is possible to define timing relations in Verilog. BUT:
q These are ONLY for simulation
‘timescale 1ns/1ps
module simple (input a, output z1, z2);
endmodule
65
Good Practices
n Develop/use a consistent naming style
67
Implementing Sequential Logic
Using Verilog
68
Combinational + Memory = Sequential
Sequential Circuit
outputs
inputs
Combinational
Circuit
Storage
Element
69
Sequential Logic in Verilog
n Define blocks that have memory
q Flip-Flops, Latches, Finite State Machines
70
The “always” Block
71
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
72
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
73
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
74
Asynchronous and Synchronous Reset
n Reset signals are used to initialize the hardware to a known
state
q Usually activated at system start (on power up)
n Asynchronous Reset
q The reset signal is sampled independent of the clock
q Reset gets the highest priority
q Sensitive to glitches, may have metastability issues
n Will be discussed in Lecture 8
n Synchronous Reset
q The reset signal is sampled with respect to the clock
q The reset should be active long enough to get sampled at the
clock edge
q Results in completely synchronous circuit
75
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
76
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
77
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
78
D Flip-Flop with Synchronous Reset
module flop_sr (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
79
D Flip-Flop with Enable and Reset
module flop_en_ar (input clk,
input reset,
input en,
input [3:0] d,
output reg [3:0] q);
80
Example: D Latch
always @ (clk, d)
if (clk) q <= d; // latch is transparent when
// clock is 1
endmodule
81
Summary: Sequential Statements So Far
n Sequential statements are within an always block
82
Basics of always Blocks
module example (input clk,
input [3:0] d,
output reg [3:0] q);
83
Why Does an always Block Remember?
module flop (input clk,
input [3:0] d,
output reg [3:0] q);
84
An always Block Does NOT Always Remember
module comb (input inv,
input [3:0] data,
output reg [3:0] result);
endmodule
85
always Blocks for Combinational Circuits
n An always block defines combinational logic if:
q All outputs are always (continuously) updated
1. All right-hand side signals are in the sensitivity list
n You can use always @* for short
2. All left-hand side signals get assigned in every possible condition
of if .. else and case blocks
Sequential Sequential
87
The always Block is NOT Always Practical/Nice
reg [31:0] result;
wire [31:0] a, b, comb;
wire sel,
88
always Block for Case Statements (Handy!)
module sevensegment (input [3:0] data,
output reg [6:0] segments);
endmodule
89
Summary: always Block
n if .. else can only be used in always blocks
90
Non-Blocking and Blocking Assignments
Non-blocking (<=) Blocking (=)
always @ (a) always @ (a)
begin begin
a <= 2’b01; a = 2’b01;
b <= a; // a is 2’b01
// all assignments are made here b = a;
// b is not (yet) 2’b01 // b is now 2’b01 as well
end end
91
Example: Blocking Assignment
always @ ( * )
begin
p = a ^ b ; // p = 0 1
g = a & b ; // g = 0 0
s = p ^ cin ; // s = 0 1
cout = g | (p & cin) ; // cout = 0 0
end
n If a changes to ‘1’
q All values are updated in order
93
The Same Example: Non-Blocking Assignment
always @ ( * )
begin
p <= a ^ b ; // p = 0 1
g <= a & b ; // g = 0 0
s <= p ^ cin ; // s = 0 0
cout <= g | (p & cin) ; // cout = 0 0
end
n If a changes to ‘1’
q All assignments are concurrent
q When s is being assigned, p is still 0
94
The Same Example: Non-Blocking Assignment
always @ ( * )
begin
p <= a ^ b ; // p = 1 1
g <= a & b ; // g = 0 0
s <= p ^ cin ; // s = 0 1
cout <= g | (p & cin) ; // cout = 0 0
end
95
Rules for Signal Assignment
n Use always @(posedge clk) and non-blocking
assignments (<=) to model synchronous sequential logic
assign y = a & b;
96
Rules for Signal Assignment (Cont.)
97
Recall: Finite State Machines (FSMs)
n Each FSM consists of three separate parts:
q next state logic
q state register
q output logic
CLK
M next
next k state k output N
inputs state
state
outputs
logic
logic
state register
98
Recall: Finite State Machines (FSMs) Comprise
n Sequential circuits CLK
q State register(s)
n Store the current state and S’ S
Next Current
n Load the next state at the clock edge State State
Output
q Output logic Logic
n Generates the outputs
CL Outputs
99
FSM Example 1: Divide the Clock Frequency by 3
The output Y is HIGH for one clock cycle out of every 3. In other
words, the output divides the frequency of the clock by 3.
100
Implementing FSM Example 1: Definitions
module divideby3FSM (input clk,
input reset,
output q);
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
101
Implementing FSM Example 1: State Register
CLK
S’ S
Next Current
State State
// state register
always @ (posedge clk, posedge reset)
if (reset) state <= S0;
else state <= nextstate;
102
Implementing FSM Example 1: Next State Logic
CLK
M next
next k state k output N
inputs state
state
outputs
logic
logic
103
Implementing FSM Example 1: Output Logic
CLK
M next
next k state k output N
inputs state
state
outputs
logic
logic
// output logic
assign q = (state == S0);
105
Design of Digital Circuits
Lecture 7.2: Hardware Description
Languages and Verilog
Prof. Onur Mutlu
ETH Zurich
Spring 2019
14 March 2019
We did not cover the following.
They are for your preparation.
107
FSM Example 2: Smiling Snail
n Alyssa P. Hacker has a snail that crawls down a paper tape
with 1’s and 0’s on it.
n The snail smiles whenever the last four digits it has crawled
over are 1101.
n Design Moore and Mealy FSMs of the snail’s brain.
Moore
Mealy
108
Implementing FSM Example 2: Definitions
module SmilingSnail (input clk,
input reset,
input number,
output smile);
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
parameter S3 = 2’b11;
number/smile
109
Implementing FSM Example 2: State Register
// state register
always @ (posedge clk, posedge reset)
if (reset) state <= S0;
else state <= nextstate;
110
Implementing FSM Example 2: Next State Logic
// next state logic
always @ (*)
case (state)
S0: if (number) nextstate = S1;
else nextstate = S0;
S1: if (number) nextstate = S2;
else nextstate = S0;
S2: if (number) nextstate = S2;
else nextstate = S3;
S3: if (number) nextstate = S1;
else nextstate = S0;
default: nextstate = S0;
endcase
111
Implementing FSM Example 2: Output Logic
// output logic
assign smile = (number & state == S3);
112
Implementation of FSM Example 2
module SmilingSnail (input clk, always @ (*) // next state logic
input reset, case (state)
input number, S0: if (number)
output smile); nextstate = S1;
else nextstate = S0;
reg [1:0] state, nextstate; S1: if (number)
nextstate = S2;
parameter S0 = 2'b00; else nextstate = S0;
parameter S1 = 2'b01; S2: if (number)
parameter S2 = 2'b10; nextstate = S2;
parameter S3 = 2’b11; else nextstate = S3;
S3: if (number)
// state register nextstate = S1;
always @ (posedge clk, posedge else nextstate = S0;
reset) default: nextstate = S0;
if (reset) state <= S0; endcase
else state <= nextstate; // output logic
assign smile = (number & state==S3);
endmodule
113
What Did We Learn?
n Basics of defining sequential circuits in Verilog
n Writing FSMs
q Next state logic
q State assignment
q Output logic
114
Next Lecture:
Timing and Verification
115