verilog blocking and nonblocking assignments are explained
TRANSCRIPT
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
1/24
6.111 Fall 2007 Lecture 6, Slide 1
1. Evaluate a| bbut defer assignment ofx
2. Evaluate a^b^c but defer assignment of y
3. Evaluate b&(~c) but defer assignment of z
1. Evaluate a| b, assign result tox
2. Evaluate a^b^c, assign result to y
3. Evaluate b&(~c), assign result to z
I. Blocking vs. Nonblocking Assignments
Verilog supports two types of assignments within alwaysblocks, with subtly different behaviors.
Blocking assignment: evaluation and assignment are immediate
Nonblocking assignment: all assignments deferred until allright-hand sides have been evaluated (end of simulationtimestep)
Sometimes, as above, both produce the same result.Sometimes, not!
always @ (a or b or c)
begin
x = a | b;
y = a ^ b ^ c;
z = b & ~c;end
always @ (a or b or c)
begin
x
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
2/24
6.111 Fall 2007 Lecture 6, Slide 2
Why two ways of assigning values?
Conceptual need for two kindsof assignment (in alwaysblocks):
ab
c
x
y
a
b
a =b
b =a
x =a & b
y =x | c
Blocking:Evaluation and assignmentare immediate
a
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
3/24
6.111 Fall 2007 Lecture 6, Slide 3
Assignment Styles for Sequential Logic
Will nonblocking and blocking assignments bothproduce the desired result?
module nonblocking(in, clk, out); inputin, clk; output out; reg q1, q2, out;
always @ (posedge clk) begin q1
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
4/24
6.111 Fall 2007 Lecture 6, Slide 4
Use Nonblocking for Sequential Logic
D Q D Q D Qin outq1 q2
clk
always @ (posedge clk)
begin q1
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
5/24
6.111 Fall 2007 Lecture 6, Slide 5
x
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
6/24
6.111 Fall 2007 Lecture 6, Slide 6
II. Single-clock Synchronous Circuits
Single-clock Synchronous Discipline:
No combinational cycles
Only care about value ofcombinational circuits justbefore rising edge of clock
Period greater than every combinational delay
Change saved state after noise-inducing logic transitions havestopped!
Well use Flip Flops and Registers groups of FFs sharing a clock input in ahighly constrained way to build digital systems.
Single clock signal shared amongall clocked devices
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
7/24
6.111 Fall 2007 Lecture 6, Slide 7
Clocked circuit for on/off button
moduleonoff(clk,button,light);
inputclk,button;
outputlight;
reglight;
always@ (posedge clk)
begin if(button) light
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
8/24
6.111 Fall 2007 Lecture 6, Slide 8
Asynchronous Inputs in Sequential Systems
What about external signals?
Sequential System
Clock
Cant guaranteesetup and holdtimes will be met!
When an asynchronous signal causes a setup/holdviolation...
Clock
Q
D
I
Transition is missed on
first clock cycle, but
caught on next clock
cycle.
II
Transition is caught on
first clock cycle.
?
III
Output is metastable
for an indeterminate
amount of time.
Q: Which cases are problematic?
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
9/24
6.111 Fall 2007 Lecture 6, Slide 9
Asynchronous Inputs in Sequential Systems
All of them can be, if more than one happenssimultaneously within the same circuit.
Idea: ensure that external signals directly feedexactly oneflip-flop
D QSequential System
Clock
This prevents the possibility of I and II occurring in different placesin the circuit, but what about metastability?
D Q
D Q
Q0
Clock
Clock
Q1
Async
Input
Clocked
Synchronous
System
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
10/24
6.111 Fall 2007 Lecture 6, Slide 10
Handling Metastability
Preventing metastability turns out to be an impossible problem High gain of digital devices makes it likely that metastable
conditions will resolve themselves quickly
Solution to metastability: allow time for signals to stabilize
How many registers are necessary? Depends on many design parameters(clock speed, device speeds, ) In 6.111, a pair of synchronization registers is sufficient
D QComplicated
Sequential Logic
System
Clock
D Q D Q
Canbe
metastable
right aftersampling
Veryunlikely to be
metastable for >1
clock cycle
Extremelyunlikely to
be metastable for >2
clock cycle
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
11/24
6.111 Fall 2007 Lecture 6, Slide 11
III. Finite State Machines
Finite State Machines (FSMs) are a useful abstraction forsequential circuitswith centralized states of operation At each clock edge, combinational logic computes outputsand
next stateas a function of inputsandpresent state
CombinationalLogic
Flip-
Flops
Q D
CLK
inputs
+
presentstate
outputs
+
nextstate
n
n
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
12/24
6.111 Fall 2007 Lecture 6, Slide 12
Example 1: Light Switch
LIGHT= 0
LIGHT= 1
BUTTON=1
BUTTON=1
BUTTON=0 BUTTON=0
State transition diagram
D QBUTTON
LIGHT
CLK
0
1
Combinational logic
Register
Logic diagram
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
13/24
6.111 Fall 2007 Lecture 6, Slide 13
Example 2: 4-bit Counter
+1
clk
count44
Logic diagram
# 4-bit counter
modulecounter(clk, count);
inputclk;
output[3:0] count;
reg[3:0] count;
always@ (posedgeclk)begin
count
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
14/24
6.111 Fall 2007 Lecture 6, Slide 14
Example 2: 4-bit Counter
1
0
+1
enb clk
count44
Logic diagram
# 4-bit counter with enable
modulecounter(clk,enb,count);
inputclk,enb;
output[3:0] count;
reg[3:0] count;
always@ (posedgeclk)begin
count
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
15/24
6.111 Fall 2007 Lecture 6, Slide 15
Example 2: 4-bit Counter
0 1
01
0
+1
enb clr clk
count44
Isnt this a lot likeExercise 1 in Lab 2?
Logic diagram
# 4-bit counter with enable and synchronous clear
modulecounter(clk,enb,clr,count);
inputclk,enb,clr;
output[3:0] count;
reg[3:0] count;
always@ (posedgeclk)begin
count
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
16/24
6.111 Fall 2007 Lecture 6, Slide 16
Two Types of FSMs
Mooreand MealyFSMs : different output generation
outputsyk= fk(S)
inputsx0...xn
Moore FSM:
Comb.
Logic
CLK n
Flip-
Flops
Comb.
Logic
D Q
present state S
n
nextstate
S+
inputsx0...xn
Mealy FSM:
S
Comb.
Logic
CLK
Flip-
Flops
Comb.
LogicDQ
n
S+
n
outputs
yk= fk(S, x0...xn)
direct combinational path!
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
17/24
6.111 Fall 2007 Lecture 6, Slide 17
Design Example: Level-to-Pulse
A level-to-pulse converterproduces asingle-cycle pulse each time its input goeshigh.
Its a synchronous rising-edge detector.
Sample uses:
Buttons and switches pressed by humans forarbitrary periods of time
Single-cycle enable signals for counters
Level to
PulseConverterL P
CLK
Whenever input L goes
from low to high...
...output P produces a
single pulse, one clock
period wide.
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
18/24
6.111 Fall 2007 Lecture 6, Slide 18
High input,Waiting for fall
11
P = 0
L=1
L=0
00
Low input,Waiting for rise
P = 0
01Edge Detected!
P = 1
L=1
L=0L=0
L=1
State transition diagramis a useful FSM representation and design aid:
Step 1: State Transition Diagram
Block diagram of desired system:
D QLevel to
Pulse
FSM
L Punsynchronized
user input
Synchronizer Edge Detector
This is the output that results fromthis state. (Moore or Mealy?)
P = 0
11
Binary values of states
L=0
if L=0 at the clock edge,then stay in state 00.
L=1if L=1 at the clock edge,then jump to state 01.
D Q
CLK
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
19/24
6.111 Fall 2007 Lecture 6, Slide 19
Step 2: Logic Derivation
00Low input,
Waiting for rise
P = 0
01Edge Detected!
P = 1
11High input,
Waiting for fall
P = 0
L=1 L=1
L=0
L=0
L=1L=0
10
1
0
1
0
L
In
00
1
1
0
0
P
Out
10
1
0
1
0
S0+
10
1
0
0
0
S1+
11
0
0
0
0
S1
Next
State
Curren
t State
1
1
11
0
0
S0
Combinational logic may be derived using Karnaugh maps
X1101
X000010110100
X1111X000010110100
S1S0L
S1S0L
for S1+:
for S0+:
011X0010
S1for P:
S0
Comb.
LogicCLK
n
Flip-
Flops
Comb.
Logic
D Q
S
n
S+
L P
S1+= LS0
S0+= L
P = S1S0
Transition diagram is readily converted to astate transition table (just a truth table)
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
20/24
6.111 Fall 2007 Lecture 6, Slide 20
Moore Level-to-Pulse Converter
Moore FSM circuit implementation of level-to-pulse converter:
outputsyk= fk(S)
inputsx0...xn
Comb.
Logic
CLKn
Flip-
Flops
Comb.
Logic
D Q
present state S
n
next
stateS+
D Q
S1
+= LS0S0
+= L P = S1S0
D Q
S0
S1
CLK
S0+
S1+
L P
Q
Q
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
21/24
6.111 Fall 2007 Lecture 6, Slide 21
1. When L=1 and S=0, this output isasserted immediatelyand untilthe
state transition occurs (or Lchanges).
2. While in stateS=1 and as long as Lremains at 1, thisoutput is asserted.
L=1 | P=0
L=1 | P=1
P=0
0Input is low
1Input is high
L=0 | P=0
L=0 | P=0
Design of a Mealy Level-to-Pulse
Since outputs are determined by state andinputs, Mealy FSMs
may need fewer states than Moore FSM implementations
S
Comb.
LogicCLK
Flip-
Flops
Comb.
LogicD Q
n
S+
n
direct combinational path!
P
L
State
Clock
Output transitions immediately.
State transitions at the clock edge.
12
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
22/24
6.111 Fall 2007 Lecture 6, Slide 22
Mealy Level-to-Pulse Converter
Mealy FSM circuit implementation of level-to-pulse converter:
1
0
1
0
L
In
0
0
1
0
P
Out
1
0
1
0
S+
Next
State
Pres.
State
1
1
0
0
S
D QS
CLK
S+
L
P
Q
S
FSMs state simply remembers the previous value of L
Circuit benefits from the Mealy FSMs implicit single-cycle assertion of outputs during state transitions
0Input is low
1Input is high
L=1 | P=1
L=0 | P=0
L=1 | P=0L=0 | P=0
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
23/24
6.111 Fall 2007 Lecture 6, Slide 23
Moore/Mealy Trade-Offs
How are they different? Moore: outputs = f( state )only Mealy outputs = f( state and input) Mealy outputs generally occur one cycle earlier than a Moore:
Compared to a Moore FSM, a Mealy FSM might... Be more difficult to conceptualize and design
Have fewer states
P
L
State
Clock
Mealy:immediate assertion of P
P
L
State[0]
Clock
Moore:delayed assertion of P
-
8/12/2019 Verilog Blocking and Nonblocking assignments are explained
24/24
6.111 Fall 2007 Lecture 6, Slide 24
Light Switch Revisited
D Q
BUTTON
LIGHT
CLK
0
1
D Q
Q
Level-to-PulseFSM
Light SwitchFSM