Professional Documents
Culture Documents
Nonblocking Assignments
Verilog supports two types of assignments within always blocks, with subtly different behaviors. Blocking assignment: evaluation and assignment are immediate
always @ (a or b or c) begin x = a | b; y = a ^ b ^ c; z = b & ~c; end 1. Evaluate a | b, assign result to x 2. Evaluate a^b^c, assign result to y 3. Evaluate b&(~c), assign result to z
Nonblocking assignment: all assignments deferred until all right-hand sides have been evaluated (end of simulation timestep)
always begin x <= y <= z <= end @ (a or b or c) a | b; a ^ b ^ c; b & ~c; 1. Evaluate a | b but defer assignment of x 2. Evaluate a^b^c but defer assignment of y 3. Evaluate b&(~c) but defer assignment of z 4. Assign x, y, and z with their new values
a b c
x y
Blocking: Evaluation and assignment are immediate Non-Blocking: Assignment is postponed until all r.h.s. evaluations are done When to use:
( only in always blocks! )
a = b b = a a <= b b <= a
Sequential Circuits
Will nonblocking and blocking assignments both produce the desired result?
module nonblocking(in, clk, out); input in, clk; output out; reg q1, q2, out; always @ (posedge clk) begin q1 <= in; q2 <= q1; out <= q2; end endmodule
6.111 Fall 2007
module blocking(in, clk, out); input in, clk; output out; reg q1, q2, out; always @ (posedge clk) begin q1 = in; q2 = q1; out = q2; end endmodule
Lecture 6, Slide 3
in
q1 q2 D Q
out
Blocking assignments do not reflect the intrinsic behavior of multi-stage sequential logic
x = a & b; y = x | c;
x y
Nonblocking Behavior
(Given) Initial Condition a changes; always block triggered
Deferred
Nonblocking assignments do not reflect the intrinsic behavior of multi-stage combinational logic While nonblocking assignments can be hacked to simulate correctly (expand the sensitivity list), its not elegant
BUTTON CLK
SINGLE GLOBAL CLOCK
6.111 Fall 2007
D Q
LIGHT
LOAD-ENABLED REGISTER
Lecture 6, Slide 7
Clock
Idea: ensure that external signals directly feed exactly one flip-flop
Clocked Synchronous System Async Input D Q Q0
D Q
Sequential System
Clock D Q Q1
Clock
Clock
This prevents the possibility of I and II occurring in different places in the circuit, but what about metastability?
Lecture 6, Slide 9
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
Can be metastable right after sampling Very unlikely to be metastable for >1 clock cycle Extremely unlikely to be metastable for >2 clock cycle
D Q
D Q D Q
Clock
How many registers are necessary? Depends on many design parameters(clock speed, device speeds, ) In 6.111, a pair of synchronization registers is sufficient
6.111 Fall 2007 Lecture 6, Slide 10
Combinational Logic
Q
CLK
6.111 Fall 2007
FlipFlops
Lecture 6, Slide 11
Logic diagram
Combinational logic
0 1
BUTTON CLK
6.111 Fall 2007
D Q
LIGHT
Register
Lecture 6, Slide 12
+1
count
Verilog # 4-bit counter module counter(clk, count); input clk; output [3:0] count; reg [3:0] count; always @ (posedge clk) begin count <= count+1; end endmodule
6.111 Fall 2007
clk
Lecture 6, Slide 13
+1
4 1 0
count
Verilog
enb
clk
# 4-bit counter with enable module counter(clk,enb,count); input clk,enb; output [3:0] count; Could I use the following instead? if (enb) count <= count+1; reg [3:0] count; always @ (posedge clk) begin count <= enb ? count+1 : count; end endmodule
6.111 Fall 2007 Lecture 6, Slide 14
+1
0 1 0
1 4 0 clr clk
count
Verilog
enb
# 4-bit counter with enable and synchronous clear module counter(clk,enb,clr,count); Isnt this a lot like Exercise 1 in Lab 2? input clk,enb,clr; output [3:0] count; reg [3:0] count; always @ (posedge clk) begin count <= clr ? 4b0 : (enb ? count+1 : count); end endmodule
6.111 Fall 2007 Lecture 6, Slide 15
inputs
x0...xn
Flip- Q Flops
n
Comb. Logic
outputs yk = f k ( S )
present state S
Mealy FSM:
direct combinational path!
inputs
x0...xn
Comb. Logic
S+
n CLK
FlipFlops
S
6.111 Fall 2007 Lecture 6, Slide 16
Lecture 6, Slide 17
D Q
D Q
L=1 01
Edge Detected!
L=1 11
High input, Waiting for fall
L=0
00
Low input, Waiting for rise
P=0
if L=0 at the clock edge, then stay in state 00.
L=1
P=0
This is the output that results from this state. (Moore or Mealy?)
Lecture 6, Slide 18
Curren In t State S1 0 0 0 0 1 1 S0 0 0 1 1 1 1 L 0 1 0 1 0 1
Out
P 0 0 1 1 0 0
00
Low input, Waiting for rise
01
Edge Detected!
11
High input, Waiting for fall
P=0
P=1
L=0
P=0 L=0
0 0 0 0 X 1 0 1 1 X
+:
S+ Comb. Logic
n CLK D Flip- Q
S1S0 for S0 00 01 11 10 L
Flops
n
Comb. Logic
P S0 S1
for P:
0 0 0 0 X 1 1 1 1 X
S1+ = LS0 S0 + = L
P = S1S0
0 1 0 0 X 1 1 0
Lecture 6, Slide 19
Flip- Q Flops
n
Comb. Logic
outputs yk = f k ( S )
S1 = LS0 S0 + = L
+
Q Q
S0
S1 +
Q Q
S1
Lecture 6, Slide 20
S+ Comb. Logic
n CLK
D Flip- Q
Flops
n
Comb. Logic
Since outputs are determined by state and inputs, Mealy FSMs may need fewer states than Moore FSM implementations
1. When L=1 and S=0, this output is asserted immediately and until the state transition occurs (or L changes).
L P Clock State
0
Input is low L=0 | P=0
1
Input is high
L=1 | P=0
2. While in state S=1 and as long as L remains at 1, this output is asserted.
6.111 Fall 2007
Lecture 6, Slide 21
0
Input is low L=0 | P=0 L=0 | P=0
1
Input is high L=1 | P=0
Pres. State S 0 0 1 1
In L 0 1 0 1
Q Q
FSMs state simply remembers the previous value of L Circuit benefits from the Mealy FSMs implicit singlecycle assertion of outputs during state transitions
6.111 Fall 2007 Lecture 6, Slide 22
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: Moore: delayed assertion of P
L P Clock State[0]
0 1
D Q
LIGHT
BUTTON
D Q Q
Lecture 6, Slide 24