6a Hdl Verilog Afterlecture
6a Hdl Verilog Afterlecture
ETH Zürich
Spring 2023
10 March 2023
Agenda for Today
n Hardware Description Languages
2
Why Specialized Languages for Hardware?
n HDLs enable easy description of hardware structures
q Wires, gates, registers, flip-flops, clock, rising/falling edge, …
q Combinational and sequential logic elements
3
Hardware Design Using HDL
4
Structural (Gate-Level) HDL
5
Behavioral HDL
6
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 those modules
q Describes a hierarchy of modules defined as gates
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
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
q Simulation is essential for functional and timing verification
8
Implementing Sequential Logic
Using Verilog
9
Sequential = Combinational + Memory
Sequential Circuit
outputs
inputs
Combinational
Circuit
Storage
Element
10
Sequential Logic in Verilog
n We can describe hardware that has memory
q Flip-Flops, Latches, Finite State Machines
11
The “always” Block
12
Recall: The D Flip-Flop
n 1) state change on clock edge, 2) data available for full cycle
D Latch (First)
D D Latch (Second)
CLK
1
CLK:
0
n When the clock is low, 1st latch propagates D to the input of the 2nd (Q unchanged)
n Only when the clock is high, 2nd latch latches D (Q stores D)
q At the rising edge of clock (clock going from 0->1), Q gets assigned D
13
Recall: The D Flip-Flop
n 1) state change on clock edge, 2) data available for full cycle
D CLK Q
__
Q
D Flip-Flop
1
CLK:
0
n At the rising edge of clock (clock going from 0->1), Q gets assigned D
n At all other times, Q is unchanged
14
Recall: The D Flip-Flop
n 1) state change on clock edge, 2) data available for full cycle
D CLK Q
__
Q
We can use D Flip-Flops
D Flip-Flop
to implement the state register
1
CLK:
0
n At the rising edge of clock (clock going from 0->1), Q gets assigned D
n At all other times, Q is unchanged
15
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
16
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
17
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
18
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 the Timing & Verification Lecture
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
19
Recall: Asynchronous vs. Synchronous State Changes
n Sequential lock we saw is an asynchronous “machine”
q State transitions occur when they occur
q There is nothing that synchronizes when each state transition
must occur
21
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
22
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
23
D Flip-Flop with Synchronous Reset
module flop_sr (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
24
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);
25
Example: D Latch
always @ (clk, d)
if (clk) q <= d; // latch is transparent when
// clock is 1
endmodule
26
Summary: Sequential Statements So Far
n Sequential statements are within an always block
27
Basics of always Blocks
module example (input clk,
input [3:0] d,
output reg [3:0] q);
28
Why Does an always Block Remember?
module flop (input clk,
input [3:0] d,
output reg [3:0] q);
29
An always Block Does NOT Always Remember
module comb (input inv,
input [3:0] data,
output reg [3:0] result);
endmodule
Sequential Sequential
32
The always Block is NOT Always Practical/Nice
reg [31:0] result;
wire [31:0] a, b, comb;
wire sel,
33
always Block for Case Statements (Handy!)
module sevensegment (input [3:0] data,
output reg [6:0] segments);
endmodule
34
Summary: always Block
n if .. else can only be used in always blocks
35
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
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
38
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
39
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
40
Rules for Signal Assignment
n Use always @(posedge clk) and non-blocking
assignments (<=) to model synchronous sequential logic
assign y = a & b;
41
Rules for Signal Assignment (Cont.)
42
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
At the beginning of the clock cycle, next state is latched into the state register
43
Recall: Finite State Machines (FSMs) Consist of:
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
44
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.
45
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;
46
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;
47
Implementing FSM Example 1: Next State Logic
CLK
M next
next k state k output N
inputs state
state
outputs
logic
logic
48
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);
50
FSM Example 2: Recall the 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
51
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;
digit/smile
52
Implementing FSM Example 2: State Register
// state register
always @ (posedge clk, posedge reset)
if (reset) state <= S0;
else state <= nextstate;
53
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
54
Implementing FSM Example 2: Output Logic
// output logic
assign smile = (number & state == S3);
55
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
56
What Did We Learn?
n Basics of describing sequential circuits in Verilog
58
Digital Design & Computer Arch.
Lecture 6a: Hardware Description
Languages and Verilog II
ETH Zürich
Spring 2023
10 March 2023