Verilog Synthesis & FSMs EECS150 Spring 2007 – Lab Lecture #3 Brent Mochizuki Greg Gibeling 2/2/07 EECS150 Lab Lecture #3

Download Report

Transcript Verilog Synthesis & FSMs EECS150 Spring 2007 – Lab Lecture #3 Brent Mochizuki Greg Gibeling 2/2/07 EECS150 Lab Lecture #3

Verilog Synthesis & FSMs
EECS150 Spring 2007 – Lab Lecture #3
Brent Mochizuki
Greg Gibeling
2/2/07
EECS150 Lab Lecture #3
1
Today







2/2/07
Designing Digital Logic
Efficient Hardware Design
HDL Simulation
Blocking vs. Non-Blocking
Administrative Info
Lab #3: The Combo Lock
FSMs in Verilog
EECS150 Lab Lecture #3
2
Designing Digital Logic (1)

High Level Design



Implementing the Design




2/2/07
Top-Down Design
Different from testing!
Follow the flow of data
Start with Inputs
Determine State
Generate Outputs
EECS150 Lab Lecture #3
3
Designing Digital Logic (2)

Start with Inputs

What are they?



Process Them



2/2/07
Possible Values and Don’t Cares
Timing
Raw inputs are often not what you need
Might need delay/timing change
Might look for a specific value/range
EECS150 Lab Lecture #3
4
Designing Digital Logic (3)

Determine State

What does the module need to remember?



Design Memory for State




2/2/07
Has it seen a particular input?
How many cycles have passed?
Generalized FSM
Standard D Register
Counter
Shift Register
EECS150 Lab Lecture #3
5
Designing Digital Logic (4)

Generate Outputs

What are they?



Possible Values
Timing
Create the outputs


Don’t set them, they’re not variables
Compute them from state (and inputs)


2/2/07
Learn to think in Boolean equations
assign is helpful
EECS150 Lab Lecture #3
6
Efficient Hardware Design (1)
B
always @ (*) begin
if (a) Z = A + B;
else Z =
A + C;
end
A
C
11
0
a
Z
always @ (*) begin
if (a) aux =B;
else aux = C;
Z =
A + aux;
end
B
C
1
0
A
a
aux
Z
2/2/07
EECS150 Lab Lecture #3
7
Efficient Hardware Design (2)
A
B
assign B = 3;
assign Z = A * B;
Z
A
assign Z = A + (2 * A);
<<
assign Z = A + (A << 1);
assign Z = A + {A, 1’b0};
n+1 bit adder
Z
2/2/07
EECS150 Lab Lecture #3
8
Efficient Hardware Design (3)
assign aux = A + {1’b0, A[n-1:1]};
assign Z = {aux, A[0]};
A[n-1:1]
A
A[0]
n bit adder
aux
Z
2/2/07
EECS150 Lab Lecture #3
9
HDL Simulation (1)

Software Based Simulation




Drawbacks



2/2/07
Fast, simple and accurate
Allows for simulation at any precision
Easy to see any signal - perfect Visibility
Simulator Dependant
Deadlocks are Possible!
Simulation != Synthesis
EECS150 Lab Lecture #3
10
HDL Simulation (2)

Implications

Verilog is not executed!


2/2/07
Things don’t necessarily happen in order
Verilog is SIMULATED
EECS150 Lab Lecture #3
11
Blocking vs. Non-Blocking (1)
Verilog Fragment
2/2/07
Result
always @ (a) begin
b = a;
c = b;
end
C=B=A
always @ (posedge Clock) begin
b <= a;
c <= b;
end
B=A
C = Old B
EECS150 Lab Lecture #3
12
Blocking vs. Non-Blocking (2)

Use Non-Blocking for FlipFlop Inference



posedge/negedge require Non-Blocking
Else simulation and synthesis wont match
Use #1 to show causality
always @ (posedge Clock) begin
b <= #1 a;
c <= #1 b;
end
2/2/07
EECS150 Lab Lecture #3
13
Administrative Info

Don’t expect to be checked off during
any lab times other than your own

2/2/07
You should get checked off during your lab
or during your lab TA’s office hours
EECS150 Lab Lecture #3
14
Administrative Info (2)

Partners - 1 week warning

You MUST have one for Lab4 and later…



If you do not have a partner:



2/2/07
Try to keep the same one for the project
You must have one in your lab section
Find one now!!
Post to the newsgroup
E-mail Jeff
EECS150 Lab Lecture #3
15
Lab #3: The Combo Lock (1)
DIPSwitches
Code[1] Code[0]
1
0
1
0
Your Verilog
Lab4Compare
Open
ResetCombo
Reset
Lab4Top
(Lab4Lock)
Enter
Prog1
Prog2
Buttons

Error
Outputs
Used to control entry to a locked room
2bit, 2 digit combo (By Default 11, 01)
Set code to 11, Press Enter
Set code to 01, Press Enter
Lock Opens (Open = 1)
2/2/07
EECS150 Lab Lecture #3
16
Lab #3: The Combo Lock (2)
2/2/07
Signal
Width
Dir
Description
Code
2
I
Code from the dipswitches
Enter
1
I
Enter button (examine the code)
ResetCombo
1
I
Reset to the default combination
Clock
1
I
System Clock
Reset
1
I
System Reset, doesn’t affect the combo
Open
1
O
Indicates the lock is open
Error
1
O
Indicates a bad combination
Prog1
1
O
Reprogramming the first digit
Prog2
1
O
Reprogramming the second digit
LED
8
O
Use these for debugging
EECS150 Lab Lecture #3
17
Lab #3: The Combo Lock (3)

Example 1:







2/2/07
1:
2:
3:
4:
5:
6:
7:
Press ResetCombo, Combo: 2’b11, 2’b01
Set 2’b11, Press Enter
Set 2’b01, Press Enter, LEDs: “OPEN”
Press Enter, LEDs: “Prog1”
Set 2’b00, Press Enter, LEDs: “Prog2”
Set 2’b10, Press Enter, LEDs: “OPEN”
Combo: 2’b00, 2’b10
EECS150 Lab Lecture #3
18
Lab #3: The Combo Lock (4)

Example 2:




2/2/07
1: Press ResetCombo, Combo: 2’b11, 2’b01
2: Set 2’b01, Press Enter
3: Set 2’b01, Press Enter, LEDs: “Error”
Why doesn’t “Error” show until step 3?
EECS150 Lab Lecture #3
19
Lab #3: The Combo Lock (5)
Code1 &
Enter
Init
~Code1 &
Enter
OK1
BAD1
Code2 &
Enter
~Code2 &
Enter
OK2
Enter
BAD2
Enter
[Open]
[Error]
Enter
Prog1
[Prog1]
2/2/07
Enter
Prog2
[Prog2]
EECS150 Lab Lecture #3
20
Lab #3: The Combo Lock (6)
Code
0
1
0
Prog1/Prog2
1
Enter
Code[1] Code[0]
DIPSwitches
Code
2b
Code1Reg
2b
Reset
Combo
ResetCombo
Enter
Code2Reg
2b 2b
2b
==
==
2b
Lab4Compare
Enter
2b
Decode1
Decode2
Reset
Decode1
Decode2
Code1 &
Enter
Init
~Code1 &
Enter
Lab4Top
OK1
Prog1
Prog2
BAD1
Code2 &
Enter
~Code2 &
Enter
Enter
Enter
OK2
[Open]
BAD2
[Error]
Error
Prog1
Prog2
Outputs
Enter
Prog1
[Prog1]
2/2/07
Open
Lab4Lock
Enter
Enter
EECS150 Lab Lecture #3
Prog2
[Prog2]
21
Lab #3: The Combo Lock (7)

Debugging with LEDs

A powerful way to debug




Easy to understand
Lower overhead than other debugging tools
A great way to see NextState/CurrentState
Drawbacks



Slow, can’t see fast events
No timing information, no waveform
Limited number

2/2/07
Dipswitches!
EECS150 Lab Lecture #3
22
FSMs in Verilog (1)

Mealy Machines



Output based on input
and current state
Can have major timing
problems
Moore Machines



Mealy Machine
Output based on current
state
Easier to work with
Slightly harder to build
Moore Machine
2/2/07
EECS150 Lab Lecture #3
23
FSMs in Verilog (2)

Two or Three always blocks

1st: CurrentState Register




2nd: Generates NextState (+ Outputs in Mealy)

Uses CurrentState and Inputs

Combinational
3rd: Generates Outputs (Optional)


2/2/07
Clocked
Handles Reset
Uses CurrentState only (for Moore Machines)
Might be replaced with a few assigns
EECS150 Lab Lecture #3
24
FSMs in Verilog (3)
module MyFSM(In, Out, Clock, Reset);
input
In, Clock, Reset;
output
Out;
parameter
STATE_Idle =
STATE_Run =
STATE_X =
1’b0,
1’b1,
1’bx;
reg
CurrentState, NextState, Out;
always @ (posedge Clock) begin
if (Reset) CurrentState <= STATE_Idle;
else CurrentState <=
NextState;
end
…
2/2/07
EECS150 Lab Lecture #3
25
FSMs in Verilog (4)
…
always @ (CurrentState or In) begin
NextState = CurrentState;
Out =
1’b0;
// The case block goes here
// Its on the next slide…
end
endmodule
2/2/07
EECS150 Lab Lecture #3
26
FSMs in Verilog (5)
case (CurrentState)
STATE_Idle: begin
if (In) NextState = STATE_Run;
Out =
1’b0;
end
STATE_Run: begin
if (In) NextState = STATE_Idle;
Out =
1’b1;
end
default: begin
NextState =
STATE_X;
Out =
1’bX;
end
endcase
2/2/07
EECS150 Lab Lecture #3
27