The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL RAM Basics Anselmo Lastra.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.
-
Upload
barrie-morrison -
Category
Documents
-
view
220 -
download
0
Transcript of The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.
![Page 1: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/1.jpg)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
State Machines
Anselmo Lastra
![Page 2: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/2.jpg)
2 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Topics
• How to design machines that go through a sequence of events
• Basically close this loop
![Page 3: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/3.jpg)
3 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Lab Preview
• Digital lock• You’ll need clock• Will provide code for slowing
clock♦ Next slide♦ There are better ways to change clock
speed. Will discuss later.
![Page 4: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/4.jpg)
4 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Counter
module cntr(output out, input clk);
reg [31:0] count;
always @ (posedge clk) count <= count + 1;
assign out = count[22];
endmodule
What does this do?
![Page 5: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/5.jpg)
5 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Button and Debouncing
• Button normally high• Mechanical switches can
“bounce”♦ Go H and L a number of times
• We’ll want to♦ debounce♦ synchronize
![Page 6: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/6.jpg)
6 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Flip-Flop for pushbutton
module button_test( output q, input btn, input clk );
reg q;
always @ (posedge clk)begin
if(btn == 1)q <= 1;
elseq <= 0;
end
endmodule
What is this?
![Page 7: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/7.jpg)
7 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Simple Module to Begin With
module led_on(output s6, input button, input clk);
wire clkb; //opt
cntr C1(clkb, clk);button_test B1(s6, ~button, clkb);
endmodule
• clk to board clock, P88• button to pushbutton, P93
• Why ~button?• s6 to one of LED segments
![Page 8: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/8.jpg)
8 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Things to Think About
• Can I press button and not light LED?• What happens if I hold button down
for a long time?• What effect will changing period of
clkb have?♦ On LED♦ On button debouncing
• What does it mean to “press the button”?♦ Think carefully about this
![Page 9: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/9.jpg)
9 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Analysis of Sequential Circuits
• Earlier we learned how to analyze combinational circuits
• Now extend to synchronous sequential♦ Include time
• We’ll use state tables and state diagrams
![Page 10: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/10.jpg)
10 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Input Equations
• Can describe inputs to FF with logic equations
)( BXAXDA
XADB
XBAY )(
![Page 11: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/11.jpg)
11 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Time is Implied
• Note that last circuit used the♦ Previous state to determine next state♦ State and inputs to determine outputs
• Synchronous circuit• When are transitions?
• So timing is discrete
![Page 12: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/12.jpg)
12 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
State Table
• Just truth table with state added
)( BXAXDA XADB XBAY )(
![Page 13: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/13.jpg)
13 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Another Table
• Same info, different layout style
![Page 14: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/14.jpg)
14 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Sequential Circuit Types
• Moore model – outputs depend on states
• Mealy model – outputs also depend on inputs
![Page 15: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/15.jpg)
15 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
State Diagram
• Alternative representation for state table
• Moore-> State/OutputInputs
![Page 16: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/16.jpg)
16 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Mealy Model
• Output depends on input and state Input/Output
![Page 17: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/17.jpg)
17 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
State Table vs. Diagram
• Same information• Table is perhaps easier to fill in
from description• Diagram is perhaps easier to
understand♦ You can label states with English
description
![Page 18: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/18.jpg)
18 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Design Procedure
• Take problem description and refine it into a state table or diagram
• Assign codes to the states• Write Verilog♦ See example in a moment♦ Designing with gates and FFs more
involved because you have to derive input and output functions
![Page 19: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/19.jpg)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Good Place to go off on a Tangent
![Page 20: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/20.jpg)
20 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Example – Sequence Recognizer
• Circuit has input, X, and output, Z
• Recognizes sequence 1101 on X♦ Specifically, if X has been 110 and next
bit is 1, make Z high
![Page 21: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/21.jpg)
21 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
How to Design States
• States remember past history• Clearly must remember we’ve
seen 110 when next 1 comes along
• Tell me one necessary state
![Page 22: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/22.jpg)
22 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Beginning State
• Some state, A• If 1 appears, move to next state
BInput / Output
![Page 23: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/23.jpg)
23 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Second 1
• New state, C• To reach C, must have seen 11
![Page 24: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/24.jpg)
24 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Next a 0
• If 110 has been received, go to D
• Next 1 will generate a 1 on output Z
![Page 25: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/25.jpg)
25 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
What else?
• What happens to arrow on right?
• Must go to some state.• Where?
![Page 26: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/26.jpg)
26 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
What Sequence?
• Here we have to interpret problem
• We’ve just seen 01♦ Is this beginning of new 1101?♦ Or do we need to start over w/ another 1?
• They decide that it’s beginning (01…)
![Page 27: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/27.jpg)
27 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Cover every possibility
• Well, must have every possibility out of every state
• In this case, just two: X = 0 or 1• You fill in other cases
![Page 28: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/28.jpg)
28 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Fill in
![Page 29: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/29.jpg)
29 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Answer From Book
![Page 30: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/30.jpg)
30 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
State Minimization
• When we make state diagram, do we need all those states?
• Some may be redundant• State minimization procedures
can be used♦ We won’t cover now
![Page 31: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/31.jpg)
31 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
How to code in Verilog
• Instead of learning how to hand design (Sections 4-6 and 4-7)
• Learn how to code this in Verilog
![Page 32: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/32.jpg)
32 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Verilog Case Statement
• Similar to sequence of if/then/else case (expression)
case: statements; other case: statements; default: statements; // optional
endcase
• Example in a moment
![Page 33: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/33.jpg)
33 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Parameter – Just Shorthand
module seq_rec_v(CLK, RESET, X, Z);input CLK, RESET, X;output Z;reg [1:0] state, next_state;
parameter A = 2'b00, B = 2'b01, C = 2 'b10, D = 2'b11;
Notice that we’ve assigned codes to the states – more later
![Page 34: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/34.jpg)
34 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Next State
always @(X or state)begin
case (state) A: if (X == 1)
next_state <= B; else
next_state <= A; B: if(X) next_state <= C;else next_state <=
A; C: if(X) next_state <= C;else next_state <=
D; D: if(X) next_state <= B;else next_state <=
A;endcase
end
The last 3 cases do same thing.Just sparse syntax.
![Page 35: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/35.jpg)
35 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
On Reset or CLK
always @(posedge CLK or posedge RESET)beginif (RESET == 1)
state <= A;else
state <= next_state;end
Notice that state only gets updatedon posedge of clock (or on reset)
![Page 36: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/36.jpg)
36 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Output
always @(X or state)begincase(state)
A: Z <= 0;B: Z <= 0;C: Z <= 0;D: Z <= X ? 1 : 0;
endcaseend
![Page 37: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/37.jpg)
37 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Synthesis of Latches
• Sometimes unexpected latches created
• always will try to synthesize FFif (select) out <= A;♦ To save old value if select != 1
• If cover all possibilities, no FFif (select) out <= A;
else out <= B;
![Page 38: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/38.jpg)
38 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Comment on Book Code
• Could shorten• Don’t need next_state, for example
♦ Can just set state on clock♦ Note that the two are a little different in function
• Don’t need three always clauses♦ Although it’s easier to have combinational code
to set output be separate
• Template helps synthesizer♦ Check to see whether your state machines were
recognized
![Page 39: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/39.jpg)
39 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Read
• 7-1 and 7-11• Lab♦ I’d suggest spending time thinking
about the lock
![Page 40: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/40.jpg)
40 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Today
• Simple state machines♦ How to code them in Verilog
• Next Week♦ More on state machine styles♦ Registers♦ Counters♦ Info for next lab
• VGA timing
![Page 41: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/41.jpg)
41 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
![Page 42: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/42.jpg)
42 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
BACKUP
![Page 43: The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.](https://reader036.fdocuments.us/reader036/viewer/2022062305/5697c0071a28abf838cc5cb7/html5/thumbnails/43.jpg)
43 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
One Shot
• Help me analyze this one
• What does it do?