design of digital circuits - eth z · e. the lock can now be opened n the state of a system is a...
TRANSCRIPT
![Page 1: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/1.jpg)
Design of Digital CircuitsLecture 7: Sequential Logic Design
Prof. Onur MutluETH ZurichSpring 2018
15 March 2018
![Page 2: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/2.jpg)
Readingsn Please study Slides 102-120 from Lecture 6 on your own
n This weekq Sequential Logic
n P&P Chapter 3.4 until end + H&H Chapter 3 in fullq Hardware Description Languages and Verilog
n H&H Chapter 4 in fullq Timing and Verification
n H&H Chapters 2.9 and 3.5 + Chapter 5
n Next weekq Von Neumann Model, LC3, and MIPS
n P&P Chapter 4-5 + H&H Chapter 6q Digital Building Blocks
n H&H Chapter 52
![Page 3: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/3.jpg)
What We Will Learn Todayn Circuits that can store information
q R-S Latchq Gated D Latchq D Flip-Flopq Register
n Finite State Machines (FSM)q Moore Machineq Mealy Machine
n Verilog implementations of sequential circuits
3
![Page 4: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/4.jpg)
Circuits that Can Store Information
4
![Page 5: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/5.jpg)
Introductionn Combinational circuit output depends only on current inputn We want circuits that produce output depending on
current and past input values – circuits with memoryn How can we design a circuit that stores information?
5
Sequential Circuit
CombinationalCircuitin
puts
outputs
StorageElement
![Page 6: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/6.jpg)
Basic Storage Element:The R-S Latch
6
![Page 7: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/7.jpg)
The R-S (Reset-Set) Latchn The simplest implementation of the R-S Latch
q Two NAND gates with outputs feeding into each other’s inputq Data is stored at Q (inverse at Q’)q S and R inputs are held at 1 in quiescent (idle) state
n S (set): drive S to 0 (keeping R at 1) to change Q to 1n R (reset): drive R to 0 (keeping S at 1) to change Q to 0
n S and R should never both be 0 at the same time
7
S
R Q’
Q Input OutputR S Q1 1 Qprev
1 0 10 1 00 0 Invalid
![Page 8: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/8.jpg)
10
Why not R=S=0?
1. If R=S=0, Q and Q’ will both settle to 1, which breaksour invariant that Q = !Q’
2. If S and R transition back to 1 at the same time, Q and Q’begin to oscillate between 1 and 0 because their final values depend on each other (metastability)
q This eventually settles depending on variation in the circuits (more metastability to come in Lecture 8)
8
S
R Q’
Q Input OutputR S Q1 1 Qprev
1 0 10 1 00 0 Invalid
10
0
01
1
![Page 9: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/9.jpg)
The Gated D Latch
9
![Page 10: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/10.jpg)
The Gated D Latchn How do we guarantee correct operation of an R-S Latch?
10
S
RQ’
Q
![Page 11: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/11.jpg)
The Gated D Latchn How do we guarantee correct operation of an R-S Latch?
q Add two more NAND gates!
q Q takes the value of D, when write enable (WE) is set to 1 q S and R can never be 0 at the same time!
11
S
RQ’
Q
Write Enable
D
![Page 12: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/12.jpg)
The Gated D Latch
12
S
R Q’
Q
Write Enable
D
Input OutputWE D Q0 0 Qprev
0 1 Qprev
1 0 01 1 1
![Page 13: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/13.jpg)
The Register
13
![Page 14: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/14.jpg)
The Register
14
D
Q
How can we use D latches to store more data?• Use more D latches!• A single WE signal for all latches forsimultaneous writes
D2
Q2
D1
Q1
D0
Q0
3
3
Write Enable
Here we have a register, or a structure that stores more than one bit and can be read from and written to
This register holds 4 bits, and its data is referenced as Q[3:0]
![Page 15: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/15.jpg)
The Register
15
How can we use D latches to store more data?• Use more D latches!• A single WE signal for all latches forsimultaneous writes
Register x (Rx)
D3:0
Q3:0
WE
4
4
Here we have a register, or a structure that stores more than one bit and can be read from and written to
This register holds 4 bits, and its data is referenced as Q[3:0]
![Page 16: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/16.jpg)
Memory
16
![Page 17: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/17.jpg)
Memoryn Memory is comprised of locations that can be written to or
read from. An example memory array with 4 locations:
n Every unique location in memory is indexed with a unique address. 4 locations require 2 address bits (log[#locations]).
n Addressability: the number of bits of information stored in each location. This example: addressability is 8 bits.
n The full set of unique locations in memory is referred to as the address space.
n Typical memory is MUCH larger (billions of locations)17
Addr(00):
Addr(10):
Addr(01):
Addr(11):
0100 1001
0010 0010
0100 1011
1100 1001
![Page 18: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/18.jpg)
Addressing Memory
18
Let’s implement a simple memory array with: • 3-bit addressability & address space size of 2 (total of 6 bits)
D QWE
1 Bit
Bit2 Bit1 Bit0
Bit2 Bit1 Bit0
Addr(0)
Addr(1)
6-Bit Memory Array
![Page 19: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/19.jpg)
Reading from Memory
19
How can we select the address to read?• Because there are 2 addresses, address size is log(2)=1 bit
![Page 20: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/20.jpg)
Reading from Memory
20
How can we select the address to read?• Because there are 2 addresses, address size is log(2)=1 bit
D[2] D[1] D[0]
Addr[0]
Wordline
![Page 21: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/21.jpg)
Address Decoder
Reading from Memory
21
How can we select the address to read?• Because there are 2 addresses, address size is log(2)=1 bit
D[2] D[1] D[0]
Addr[0]
Wordline
![Page 22: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/22.jpg)
Address Decoder
Reading from Memory
22
How can we select the address to read?• Because there are 2 addresses, address size is log(2)=1 bit
D[2] D[1] D[0]
Addr[0]
Multiplexer
Wordline
![Page 23: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/23.jpg)
Writing to Memory
23
How can we select the address and write to it?
![Page 24: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/24.jpg)
Writing to Memory
24
How can we select the address and write to it?• Input is indicated with Di
Di[2] Di[1] Di[0]Addr[0]
WE
![Page 25: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/25.jpg)
Putting it all Together
25
Di[2] Di[1] Di[0]
D[2] D[1] D[0]
Addr[0]
WE
Enable reading and writing to a memory array
![Page 26: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/26.jpg)
A Bigger Memory Array
26
Di[2] Di[1] Di[0]
D[2] D[1] D[0]
Addr[1:0]
WE
![Page 27: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/27.jpg)
A Bigger Memory Array
27
Di[2] Di[1] Di[0]
D[2] D[1] D[0]
Addr[1:0]
WE
Address Decoder
Multiplexer
![Page 28: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/28.jpg)
Sequential Logic Circuits
28
![Page 29: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/29.jpg)
Sequential Logic Circuitsn We have looked at designs of circuit elements that can
store informationn Now, we will use these elements to build circuits that
remember past inputs
29https://www.easykeys.com/228_ESP_Combination_Lock.aspxhttps://www.fosmon.com/product/tsa-approved-lock-4-dial-combo
SequentialOpens depending on past inputs
CombinationalOnly depends on current inputs
![Page 30: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/30.jpg)
Staten In order for this lock to work, it has to keep track
(remember) of the past events!n If passcode is R13-L22-R3, sequence of states to unlock:
A. The lock is not open (locked), and no relevant operations have been performed
B. Locked but user has completed R13C. Locked but user has completed L22D. Locked but user has completed R3E. The lock can now be opened
n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot
q To open the lock, states A-E must be completed in orderq If anything else happens (e.g., L5), lock returns to state A
30
![Page 31: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/31.jpg)
Another Simple Example of Staten A standard Swiss traffic light has 4 states
A. GreenB. YellowC. RedD. Red and Yellow
n The sequence of these states are always as follows
31
A B C D
![Page 32: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/32.jpg)
Changing State: The Notion of Clock (I)
n When should the light change from one state to another?n We need a clock to dictate when to change state
q Clock signal alternates between 0 & 1
n At the start of a clock cycle ( ), system state changesq During a clock cycle, the state stays constantq In this traffic light example, we are assuming the traffic light stays in
each state an equal amount of time32
A B C D
CLK: 01
![Page 33: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/33.jpg)
Changing State: The Notion of Clock (II)n Clock is a general mechanism that triggers transition from
one state to another in a sequential circuit
n Clock synchronizes state changes across many sequential circuit elements
n Combinational logic evaluates for the length of the clock cycle
n Clock cycle should be chosen to accommodate maximum combinational circuit delayq More on this later, when we discuss timing
33
![Page 34: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/34.jpg)
Finite State Machines
34
![Page 35: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/35.jpg)
Finite State Machinesn What is a Finite State Machine (FSM)?
q A discrete-time model of a stateful systemq Each state represents a snapshot of the system at a given time
n An FSM pictorially showsq the set of all possible states that a system can be in q how the system transitions from one state to another
n An FSM can model q A traffic light, an elevator, fan speed, a microprocessor, etc.
n An FSM enables us to pictorially think of a statefulsystem using simple diagrams
35
![Page 36: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/36.jpg)
Finite State Machines (FSMs) Consist of:n Five elements:
1. A finite number of states n State: snapshot of all relevant elements of the
system at the time of the snapshot2. A finite number of external inputs3. A finite number of external outputs4. An explicit specification of all state transitions
n How to get from one state to another5. An explicit specification of what determines
each external output value
36
![Page 37: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/37.jpg)
Finite State Machines (FSMs)n Each FSM consists of three separate parts:
q next state logicq state registerq output logic
37
CLKM Nk knext
statelogic
outputlogic
inputs outputsstatenextstate
state register
At the beginning of the clock cycle, next state is latched into the state register
![Page 38: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/38.jpg)
Finite State Machines (FSMs) Consist of:n Sequential circuits
q State register(s)n Store the current state and n Load the next state at the clock edge
n Combinational Circuitsq Next state logic
n Determines what the next state will be
q Output logicn Generates the outputs
38
NextState
CurrentState
S’ S
CLK
CL
Next StateLogic
NextState
CL
OutputLogic
Outputs
![Page 39: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/39.jpg)
Finite State Machines (FSMs) Consist of:n Sequential circuits
q State register(s)n Store the current state and n Load the next state at the clock edge
n Combinational Circuitsq Next state logic
n Determines what the next state will be
q Output logicn Generates the outputs
39
NextState
CurrentState
S’ S
CLK
CL
Next StateLogic
NextState
CL
OutputLogic
Outputs
![Page 40: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/40.jpg)
State Register Implementationn How can we implement a state register? Two properties:
1. We need to store data at the beginning of every clock cycle
2. The data must be available during the entire clock cycle
40
CLK: 01
Input:
Output:
![Page 41: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/41.jpg)
The Problem with Latches
n Currently, we cannot simply wire a clock to WE of a latchq When the clock is high, Q will not take on D’s value ANDq When the clock is low, the latch will propagate D to Q
41
D QCLK = WE
CLK: 01
Input:
Output:
Recall the Gated D Latch
![Page 42: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/42.jpg)
The Problem with Latches
42
D QCLK = WE
CLK: 01
Input:
Output:
Recall the Gated D Latch
n Currently, we cannot simply wire a clock to WE of a latchq When the clock is high, Q will not take on D’s value ANDq When the clock is low, the latch will propagate D to Q
![Page 43: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/43.jpg)
n Currently, we cannot simply wire a clock to WE of a latchq When the clock is high Q will not take on D’s value ANDq When the clock is low the latch will propagate D to Q
The Problem with Latches
43
D QCLK = WE
CLK: 01
Input:
Output:
Recall the Gated D Latch
How can we change the latch, so that
1) D (input) is observable at Q (output) only at the beginning of next clock cycle?
2) Q is available for the full clock cycle
![Page 44: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/44.jpg)
n 1) state change on clock edge, 2) data available for full cycle
D Latch (Slave)D Latch (Master)
The D Flip-Flop
44
DQ
CLK
n When the clock is low, master propagates D to the input of slave (Q unchanged)n Only when the clock is high, slave latches D (Q stores D)
q At the rising edge of clock (clock going from 0->1), Q gets assigned D
CLK:01
![Page 45: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/45.jpg)
The D Flip-Flopn 1) state change on clock edge, 2) data available for full cycle
45
n At the rising edge of clock (clock going from 0->1), Q gets assigned Dn At all other times, Q is unchanged
CLK:01
CLKD Q
Q__
D Flip-Flop
![Page 46: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/46.jpg)
The D Flip-Flopn How do we implement this?
46
n At the rising edge of clock (clock going from 0->1), Q gets assigned Dn At all other times, Q is unchanged
CLK:01
CLKD Q
Q__
D Flip-FlopWe can use these Flip-Flops
to implement the state register!
![Page 47: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/47.jpg)
Rising-Edge Triggered Flip-Flopn Two inputs: CLK, D
n Functionq The flip-flop “samples” D on the rising edgeof CLK (positive edge)
q When CLK rises from 0 to 1, D passes through to Qq Otherwise, Q holds its previous valueq Q changes only on the rising edge of CLK
n A flip-flop is called an edge-triggered device because it is activated on the clock edge
47
D Flip-FlopSymbols
D QQ
CLK
![Page 48: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/48.jpg)
Registern Multiple parallel flip-flops, each of which store 1 bit
48
CLK
D Q
D Q
D Q
D Q
D0
D1
D2
D3
Q0
Q1
Q2
Q3
D3:04 4
CLK
Q3:0
This register stores 4 bits
This line represents 4 wires
![Page 49: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/49.jpg)
Finite State Machines (FSMs)n Next state is determined by the current state and the inputsn Two types of finite state machines differ in the output
logic:q Moore FSM: outputs depend only on the current state
49
CLKM Nk knext
statelogic
outputlogic
Moore FSM
CLKM Nk knext
statelogic
outputlogic
inputs
inputs
outputs
outputsstate
statenextstate
nextstate
Mealy FSM
![Page 50: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/50.jpg)
Finite State Machines (FSMs)n Next state is determined by the current state and the inputsn Two types of finite state machines differ in the output
logic:q Moore FSM: outputs depend only on the current stateq Mealy FSM: outputs depend on the current state and the
inputs
50
CLKM Nk knext
statelogic
outputlogic
Moore FSM
CLKM Nk knext
statelogic
outputlogic
inputs
inputs
outputs
outputsstate
statenextstate
nextstate
Mealy FSM
![Page 51: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/51.jpg)
Finite State Machine Examplen “Smart” traffic light controller
q 2 inputs: n Traffic sensors: TA , TB (TRUE when there’s traffic)
q 2 outputs: n Lights: LA , LB (Red, Yellow, Green)
51
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
![Page 52: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/52.jpg)
Finite State Machine Black Boxn Inputs: CLK, Reset, TA , TBn Outputs: LA , LB
52
TA
TB
LA
LB
CLK
Reset
TrafficLight
Controller
![Page 53: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/53.jpg)
Finite State Machine Transition Diagramn Moore FSM: outputs labeled in each state
q States: Circlesq Transitions: Arcs
53
S0LA: greenLB: red
Reset
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
![Page 54: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/54.jpg)
Finite State Machine Transition Diagramn Moore FSM: outputs labeled in each state
q States: Circlesq Transitions: Arcs
54
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
![Page 55: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/55.jpg)
Finite State Machine Transition Diagramn Moore FSM: outputs labeled in each state
q States: Circlesq Transitions: Arcs
55
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
![Page 56: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/56.jpg)
Finite State Machine Transition Diagramn Moore FSM: outputs labeled in each state
q States: Circlesq Transitions: Arcs
56
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
![Page 57: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/57.jpg)
Finite State Machine Transition Diagramn Moore FSM: outputs labeled in each state
q States: Circlesq Transitions: Arcs
57
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
![Page 58: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/58.jpg)
Finite State MachinesState Transition Table
58
![Page 59: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/59.jpg)
FSM State Transition TableCurrentState Inputs NextState
S TA TB S'S0 0 XS0 1 XS1 X XS2 X 0S2 X 1S3 X X
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
59
![Page 60: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/60.jpg)
FSM State Transition TableCurrentState Inputs NextState
S TA TB S'S0 0 X S1S0 1 X S0S1 X X S2S2 X 0 S3S2 X 1 S2S3 X X S0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
60
![Page 61: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/61.jpg)
FSM State Transition TableCurrentState Inputs NextState
S TA TB S'S0 0 X S1S0 1 X S0S1 X X S2S2 X 0 S3S2 X 1 S2S3 X X S0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
61
![Page 62: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/62.jpg)
FSM State Transition TableCurrentState Inputs NextStateS1 S0 TA TB S’1 S’00 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
62
![Page 63: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/63.jpg)
FSM State Transition TableCurrentState Inputs NextStateS1 S0 TA TB S’1 S’00 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
S’1 =?
63
![Page 64: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/64.jpg)
FSM State Transition TableCurrentState Inputs NextStateS1 S0 TA TB S’1 S’00 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
S’1 =(S1 ∙S0)+(S1 ∙S0 ∙TB)+(S1 ∙S0 ∙TB)
64
![Page 65: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/65.jpg)
FSM State Transition TableCurrentState Inputs NextStateS1 S0 TA TB S’1 S’00 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
S’1 =(S1 ∙S0)+(S1 ∙S0 ∙TB)+(S1 ∙S0 ∙TB)
S’0 =?65
![Page 66: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/66.jpg)
FSM State Transition TableCurrentState Inputs NextStateS1 S0 TA TB S’1 S’00 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
S’1 =(S1 ∙S0)+(S1 ∙S0 ∙TB)+(S1 ∙S0 ∙TB)
S’0 =(S1 ∙S0 ∙TA)+(S1 ∙S0 ∙TB)66
![Page 67: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/67.jpg)
FSM State Transition TableCurrentState Inputs NextStateS1 S0 TA TB S’1 S’00 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
State Encoding
S0 00
S1 01
S2 10
S3 11
S’1 =S1 xor S0(Simplified)
S’0 =(S1 ∙S0 ∙TA)+(S1 ∙S0 ∙TB)67
![Page 68: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/68.jpg)
Finite State MachinesOutput Table
68
![Page 69: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/69.jpg)
FSM Output Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset CurrentState OutputsS1 S0 LA LB0 0 green red0 1 yellow red1 0 red green1 1 red yellow
69
![Page 70: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/70.jpg)
FSM Output Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset CurrentState OutputsS1 S0 LA LB0 0 green red0 1 yellow red1 0 red green1 1 red yellow
Output Encoding
green 00
yellow 01
red 10
70
![Page 71: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/71.jpg)
FSM Output Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset CurrentState OutputsS1 S0 LA1 LA0 LB1 LB00 0 0 0 1 00 1 0 1 1 01 0 1 0 0 01 1 1 0 0 1
Output Encoding
green 00
yellow 01
red 10
LA1 =S1
71
![Page 72: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/72.jpg)
FSM Output Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset CurrentState OutputsS1 S0 LA1 LA0 LB1 LB00 0 0 0 1 00 1 0 1 1 01 0 1 0 0 01 1 1 0 0 1
Output Encoding
green 00
yellow 01
red 10
LA1 =S1LA0 =S1 ∙S0
72
![Page 73: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/73.jpg)
FSM Output Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset CurrentState OutputsS1 S0 LA1 LA0 LB1 LB00 0 0 0 1 00 1 0 1 1 01 0 1 0 0 01 1 1 0 0 1
Output Encoding
green 00
yellow 01
red 10
LA1 =S1LA0 =S1 ∙S0LB1 =S1
73
![Page 74: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/74.jpg)
FSM Output Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset CurrentState OutputsS1 S0 LA1 LA0 LB1 LB00 0 0 0 1 00 1 0 1 1 01 0 1 0 0 01 1 1 0 0 1
Output Encoding
green 00
yellow 01
red 10
LA1 =S1LA0 =S1 ∙S0LB1 =S1LB0 =S1 ∙S0
74
![Page 75: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/75.jpg)
Finite State MachinesSchematic
75
![Page 76: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/76.jpg)
FSM Schematic: State Register
76
![Page 77: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/77.jpg)
77
FSM Schematic: State Register
S1
S0
S'1
S'0
CLK
state register
Resetr
![Page 78: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/78.jpg)
78
FSM Schematic: Next State Logic
S1
S0
S'1
S'0
CLK
next state logic state register
Reset
TA
TB
inputs
S1 S0
r
S’1 =S1 xor S0
S’0 =(S1 ∙S0 ∙TA)+(S1 ∙S0 ∙TB)
![Page 79: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/79.jpg)
79
FSM Schematic: Output Logic
S1
S0
S'1
S'0
CLK
next state logic output logicstate register
Reset
LA1
LB1
LB0
LA0
TA
TB
inputs outputs
S1 S0
r
LA1 =S1LA0 =S1 ∙S0LB1 =S1LB0 =S1 ∙S0
![Page 80: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/80.jpg)
80
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 81: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/81.jpg)
81
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 82: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/82.jpg)
82
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 83: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/83.jpg)
83
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 84: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/84.jpg)
84
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 85: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/85.jpg)
85
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 86: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/86.jpg)
86
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 87: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/87.jpg)
87
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 88: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/88.jpg)
88
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 89: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/89.jpg)
89
FSM Timing Diagram
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: yellow
LB: red
S1LA: yellow
LB: red
S2LA: red
LB: green
S3LA: red
LB: yellow
Reset TA
TA__
__TB
TB
![Page 90: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/90.jpg)
FSM State Encodingn 3 common state binary encodings with different tradeoffs
1. Fully Encoded2. 1-Hot Encoded3. Output Encoded
n Let’s see an example Swiss traffic light with 4 statesq Green, Yellow, Red, Yellow+Red
90
![Page 91: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/91.jpg)
FSM State Encoding1. Fully Encoded:
q Minimizes # flip-flops, but not necessarily output logic or next state logic
q Use log2(num_states) bits to represent the statesq Example states: 00, 01, 10, 11
2. 1-Hot Encoded:q Maximizes # flip-flops, minimizes next state logicq Simplest design process – very automatableq Use num_states bits to represent the statesq Example states: 0001, 0010, 0100, 1000
91
![Page 92: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/92.jpg)
FSM State Encoding3. Output Encoded:
q Minimizes output logic q Only works for Moore Machinesq Each state has to be encoded uniquely, but the outputs
must be directly accessible in the state encodingq For example, since we have 3 outputs (light color),
encode state with 3 bits, where each bit represents a color
q Example states: 001, 010, 100, 110n Bit0 encodes green light output, n Bit1 encodes yellow light outputn Bit2 encodes red light output
92
![Page 93: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/93.jpg)
FSM State Encoding3. Output Encoded:
q Minimizes output logic q Only works for Moore Machinesq Each state has to be encoded uniquely, but the outputs
must be directly accessible in the state encodingq For example, since we have 3 outputs (light color),
encode state with 3 bits, where each bit represents a color
q Example states: 001, 010, 100, 110n Bit0 encodes green light output, n Bit1 encodes yellow light outputn Bit2 encodes red light output
93
The designer must carefully choosean encoding scheme to optimize the design
under given constraints
![Page 94: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/94.jpg)
Moore vs. Mealy Machines
94
![Page 95: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/95.jpg)
Moore vs. Mealy FSMn 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.
95
CLKM Nk knext
statelogic
outputlogic
Moore FSM
CLKM Nk knext
statelogic
outputlogic
inputs
inputs
outputs
outputsstate
statenextstate
nextstate
Mealy FSM
![Page 96: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/96.jpg)
Moore vs. Mealy FSMn 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.
96
CLKM Nk knext
statelogic
outputlogic
Moore FSM
CLKM Nk knext
statelogic
outputlogic
inputs
inputs
outputs
outputsstate
statenextstate
nextstate
Mealy FSM
![Page 97: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/97.jpg)
State Transition Diagrams
97
reset
Moore FSM
S00
S10
S20
S30
S41
0
1 1 0 1
1
01 00
reset
S0 S1 S2 S30/0
1/0 1/0 0/01/1
0/01/0
0/0
Mealy FSMWhat are the tradeoffs?
![Page 98: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/98.jpg)
FSM Design Proceduren Determine all possible states of your machinen Develop a state transition diagram
q Generally this is done from a textual descriptionq You need to 1) determine the inputs and outputs for each state and
2) figure out how to get from one state to anothern Approach
q Start by defining the reset state and what happens from it – this is typically an easy point to start from
q Then continue to add transitions and statesq Picking good state names is very importantq Building an FSM is like programming (but it is not programming!)
n An FSM has a sequential “control-flow” like a program with conditionals and goto’sn The if-then-else construct is controlled by one or more inputsn The outputs are controlled by the state or the inputs
q In hardware, we typically have many concurrent FSMs
98
![Page 99: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/99.jpg)
Implementing Sequential Logic Using Verilog
99
![Page 100: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/100.jpg)
Verilog: Last Week vs. This Weekn We have seen an overview of Verilog
n Discussed structural and behavioral modeling
n Showed combinational logic constructs
This weekn Sequential logic constructs in Verilog
n Developing testbenches for simulation
100
![Page 101: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/101.jpg)
Combinational + Memory = Sequential
101
Sequential Circuit
CombinationalCircuitinpu
ts
outputs
StorageElement
![Page 102: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/102.jpg)
Sequential Logic in Verilogn Define blocks that have memory
q Flip-Flops, Latches, Finite State Machines
n Sequential Logic state transition is triggered by a ‘CLOCK’ eventq Latches are sensitive to level of the signalq Flip-flops are sensitive to the transitioning of clock
n Combinational constructs are not sufficientq We need new constructs:
n alwaysn posedge/negedge
102
![Page 103: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/103.jpg)
103
The “always” Block
always @ (sensitivity list)statement;
Whenever the event in the sensitivity list occurs, the statement is executed
![Page 104: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/104.jpg)
104
Example: D Flip-Flopmodule flop(input clk,
input [3:0] d, output reg [3:0] q);
always @ (posedge clk)q <= d; // pronounced “q gets d”
endmodule
n posedge defines a rising edge (transition from 0 to 1).
n Statement executed when the clk signal rises (posedge of clk)
n Once the clk signal rises: the value of d is copied to q
![Page 105: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/105.jpg)
105
Example: D Flip-Flop
module flop(input clk, input [3:0] d, output reg [3:0] q);
always @ (posedge clk)q <= d; // pronounced “q gets d”
endmodule
n assign statement is not used within an always blockn <= describes a non-blocking assignment
q We will see the difference between blocking assignment and non-blocking assignment soon
![Page 106: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/106.jpg)
106
Example: D Flip-Flop
module flop(input clk, input [3:0] d, output reg [3:0] q);
always @ (posedge clk)q <= d; // pronounced “q gets d”
endmodule
n Assigned variables need to be declared as regn The name reg does not necessarily mean that the value is a
register (It could be, but it does not have to be)n We will see examples later
![Page 107: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/107.jpg)
Asynchronous and Synchronous Resetn Reset signals are used to initialize the hardware to a known
stateq Usually activated at system start (on power up)
n Asynchronous Resetq The reset signal is sampled independent of the clockq Reset gets the highest priorityq Sensitive to glitches, may have metastability issues
n Will be discussed in Lecture 8
n Synchronous Resetq The reset signal is sampled with respect to the clockq The reset should be active long enough to get sampled at the
clock edgeq Results in completely synchronous circuit
107
![Page 108: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/108.jpg)
108
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,input reset, input [3:0] d, output reg [3:0] q);
always @ (posedge clk, negedge reset)begin
if (reset == 0) q <= 0; // when resetelse q <= d; // when clk
endendmodule
n In this example: two events can trigger the process:q A rising edge on clkq A falling edge on reset
![Page 109: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/109.jpg)
109
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,input reset, input [3:0] d, output reg [3:0] q);
always @ (posedge clk, negedge reset)begin
if (reset == 0) q <= 0; // when resetelse q <= d; // when clk
endendmodule
n For longer statements, a begin-end pair can be usedq To improve readabilityq In this example, it was not necessary, but it is a good idea
![Page 110: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/110.jpg)
110
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,input reset, input [3:0] d, output reg [3:0] q);
always @ (posedge clk, negedge reset)begin
if (reset == 0) q <= 0; // when resetelse q <= d; // when clk
endendmodule
n First reset is checked: if reset is 0, q is set to 0.q This is an asynchronous reset as the reset can happen
independently of the clock (on the negative edge of reset signal)n If there is no reset, then regular assignment takes effect
![Page 111: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/111.jpg)
111
D Flip-Flop with Synchronous Reset
module flop_sr (input clk,input reset, input [3:0] d, output reg [3:0] q);
always @ (posedge clk)begin
if (reset == ‘0’) q <= 0; // when resetelse q <= d; // when clk
endendmodule
n The process is only sensitive to clockq Reset happens only when the clock rises. This is a
synchronous reset
![Page 112: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/112.jpg)
112
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);
always @ (posedge clk, negedge reset)begin
if (reset == ‘0’) q <= 0; // when resetelse if (en) q <= d; // when en AND clk
endendmodule
n A flip-flop with enable and resetq Note that the en signal is not in the sensitivity list
n q gets d only when clk is rising and en is 1
![Page 113: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/113.jpg)
113
Example: D Latch
module latch (input clk, input [3:0] d, output reg [3:0] q);
always @ (clk, d)if (clk) q <= d; // latch is transparent when
// clock is 1endmodule
![Page 114: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/114.jpg)
Summary: Sequential Statements So Farn Sequential statements are within an always block
n The sequential block is triggered with a change in the sensitivity list
n Signals assigned within an always must be declared as reg
n We use <= for (non-blocking) assignments and do not use assign within the always block.
114
![Page 115: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/115.jpg)
115
Basics of always Blocksmodule example (input clk,
input [3:0] d, output reg [3:0] q);
wire [3:0] normal; // standard wirereg [3:0] special; // assigned in always
always @ (posedge clk)special <= d; // first FF array
assign normal = ~ special; // simple assignment
always @ (posedge clk)q <= normal; // second FF array
endmodule
You can have as many always blocks as neededAssignment to the same signal in different always blocks is not allowed!
![Page 116: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/116.jpg)
116
Why Does an always Block Memorize?
module flop (input clk,input [3:0] d, output reg [3:0] q);
always @ (posedge clk)begin
q <= d; // when clk rises copy d to qend
endmodule
n This statement describes what happens to signal qn … but what happens when the clock is not rising?n The value of q is preserved (memorized)
![Page 117: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/117.jpg)
117
An always Block Does NOT Always Memorize
module comb (input inv,input [3:0] data, output reg [3:0] result);
always @ (inv, data) // trigger with inv, dataif (inv) result <= ~data;// result is inverted dataelse result <= data; // result is data
endmodule
n This statement describes what happens to signal resultq When inv is 1, result is ~dataq When inv is not 1, result is data
n The circuit is combinational (no memory)q result is assigned a value in all cases of the if .. else block, always
![Page 118: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/118.jpg)
always Blocks for Combinational Circuitsn An always block defines combinational logic if:
q All outputs are always (continuously) updated1. All right-hand side signals are in the sensitivity list
n You can use always @* for short2. All left-hand side signals get assigned in every possible condition of if .. else and case blocks
n It is easy to make mistakes and unintentionally describe memorizing elements (latches)q Vivado will most likely warn you. Make sure you check the
warning messages
n Always blocks allow powerful combinational logic statementsq if .. elseq case
118
![Page 119: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/119.jpg)
119
Sequential or Combinational?
wire enable, data;reg out_a, out_b;
always @ (*) beginout_a = 1’b0;if(enable) begin
out_a = data;out_b = data;
endend
Sequential
wire enable, data;reg out_a, out_b;
always @ (data) beginout_a = 1’b0;out_b = 1’b0;if(enable) begin
out_a = data;out_b = data;
endend
Sequential
No assignment for ~enable Not in the sensitivity list
![Page 120: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/120.jpg)
120
The always Block is NOT Always Practical/Nice
reg [31:0] result;wire [31:0] a, b, comb;wire sel,
always @ (a, b, sel) // trigger with a, b, selif (sel) result <= a; // result is aelse result <= b; // result is b
assign comb = sel ? a : b;
n Both statements describe the same multiplexer
n In this case, the always block is more work
![Page 121: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/121.jpg)
121
always Block for Case Statements (Handy!)module sevensegment (input [3:0] data,
output reg [6:0] segments);
always @ ( * ) // * is short for all signalscase (data) // case statement
4'd0: segments = 7'b111_1110; // when data is 04'd1: segments = 7'b011_0000; // when data is 1 4'd2: segments = 7'b110_1101;4'd3: segments = 7'b111_1001;4'd4: segments = 7'b011_0011;4'd5: segments = 7'b101_1011;// etc etcdefault: segments = 7'b000_0000; // required
endcase
endmodule
![Page 122: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/122.jpg)
Summary: always Blockn If .. else can only be used in always blocks
n The always block is combinational only if all regs within the block are always assigned to a signalq Use the default case to make sure you do not forget an
unimplemented case, which may otherwise result in a latch
n Use casex statement to be able to check for don’t cares
122
![Page 123: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/123.jpg)
123
Non-Blocking and Blocking Assignments
always @ (a)begin
a <= 2’b01;b <= a;
// all assignments are made here// b is not (yet) 2’b01end
always @ (a)begin
a = 2’b01;// a is 2’b01
b = a;// b is now 2’b01 as wellend
Non-blocking (<=) Blocking (=)
n All assignments are made at the end of the block
n All assignments are made in parallel, process flow isnot-blocked
n Each assignment is made immediately
n Process waits until the first assignment is complete, it blocks progress
![Page 124: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/124.jpg)
Why use (Non)-Blocking Statements
124
n There are technical reasons why both are requiredq It is out of the scope of this course to discuss these
n Blocking statements allow sequential descriptionsq More like a programming language
n If the sensitivity list is correct, blocks with non-blocking statements will always evaluate to the same resultq This may require some additional iterations
![Page 125: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/125.jpg)
125
Example: Blocking Assignment
always @ ( * )begin
p = a ^ b ; // p = 0 g = a & b ; // g = 0s = p ^ cin ; // s = 0 cout = g | (p & cin) ; // cout = 0
end
n Assume all inputs are initially ‘0’
n If a changes to ‘1’q All values are updated in order
1010
![Page 126: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/126.jpg)
126
The Same Example: Non-Blocking Assignment
always @ ( * )begin
p <= a ^ b ; // p = 0 g <= a & b ; // g = 0s <= p ^ cin ; // s = 0 cout <= g | (p & cin) ; // cout = 0
end
n Assume all inputs are initially ‘0’
n If a changes to ‘1’q All assignments are concurrentq When s is being assigned, p is still 0
1000
![Page 127: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/127.jpg)
127
The Same Example: Non-Blocking Assignment
always @ ( * )begin
p <= a ^ b ; // p = 1 g <= a & b ; // g = 0s <= p ^ cin ; // s = 0 cout <= g | (p & cin) ; // cout = 0
end
n After the first iteration, p has changed to ‘1’ as well
n Since there is a change in p, the process triggers againn This time s is calculated with p=1
1010
![Page 128: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/128.jpg)
128
Rules for Signal Assignment
n Use always @(posedge clk) and non-blockingassignments (<=) to model synchronous sequential logic
n Use continuous assignments (assign) to model simple combinational logic.
always @ (posedge clk)q <= d; // non-blocking
assign y = a & b;
![Page 129: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/129.jpg)
Rules for Signal Assignment (Cont.)
n Use always @ (*) and blocking assignments (=) to model more complicated combinational logic.
n You cannot make assignments to the same signal in more than one always block or in a continuous assignment
129
always @ (*)a = b;
always @ (*)a = c;
always @ (*)a = b;
assign a = c;
![Page 130: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/130.jpg)
Recall: Finite State Machines (FSMs)n Each FSM consists of three separate parts:
q next state logicq state registerq output logic
130
CLKM Nk knext
statelogic
outputlogic
inputs outputsstatenextstate
state register
![Page 131: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/131.jpg)
Recall: Finite State Machines (FSMs) Comprisen Sequential circuits
q State register(s)n Store the current state and n Load the next state at the clock edge
n Combinational Circuitsq Next state logic
n Determines what the next state will be
q Output logicn Generates the outputs
131
NextState
CurrentState
S’ S
CLK
CL
Next StateLogic
NextState
CL
OutputLogic
Outputs
![Page 132: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/132.jpg)
FSM Example 1: Divide the Clock Frequency by 3
132
TheoutputYisHIGHforoneclockcycleoutofevery3.Inotherwords,theoutputdividesthefrequencyoftheclockby3.
![Page 133: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/133.jpg)
133
Implementing FSM Example 1: Definitions
module divideby3FSM (input clk, input reset, output q);
reg [1:0] state, nextstate;
parameter S0 = 2'b00;parameter S1 = 2'b01;parameter S2 = 2'b10;
n We define state and nextstate as 2-bit regn The parameter descriptions are optional, it makes reading
easier
![Page 134: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/134.jpg)
134
Implementing FSM Example 1: State Register
// state registeralways @ (posedge clk, posedge reset)
if (reset) state <= S0;else state <= nextstate;
n This part defines the state register (memorizing process)n Sensitive to only clk, resetn In this example, reset is active when it is ‘1’ (active-high)
NextState
CurrentState
S’ S
CLK
![Page 135: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/135.jpg)
135
Implementing FSM Example 1: Next State Logic
// next state logicalways @ (*)
case (state)S0: nextstate = S1;S1: nextstate = S2;S2: nextstate = S0;default: nextstate = S0;
endcase
CLKM Nk knext
statelogic
outputlogic
inputs outputsstatenextstate
![Page 136: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/136.jpg)
136
Implementing FSM Example 1: Output Logic
// output logicassign q = (state == S0);
n In this example, output depends only on stateq Moore type FSM
CLKM Nk knext
statelogic
outputlogic
inputs outputsstatenextstate
![Page 137: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/137.jpg)
137
Implementation of FSM Example 1module divideby3FSM (input clk, input reset, output q);
reg [1:0] state, nextstate;
parameter S0 = 2'b00; parameter S1 = 2'b01; parameter S2 = 2'b10;
always @ (posedge clk, posedge reset) // state registerif (reset) state <= S0;else state <= nextstate;
always @ (*) // next state logiccase (state)
S0: nextstate = S1;S1: nextstate = S2;S2: nextstate = S0;default: nextstate = S0;
endcaseassign q = (state == S0); // output logic
endmodule
![Page 138: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/138.jpg)
FSM Example 2: Smiling Snailn 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.
138
Moore
Mealy
![Page 139: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/139.jpg)
We did not cover the following.They are for your preparation.
139
![Page 140: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/140.jpg)
140
Implementing FSM Example 2: Definitionsmodule SmilingSnail (input clk,
input reset,input number,output smile);
reg [1:0] state, nextstate;
parameter S0 = 2'b00;parameter S1 = 2'b01;parameter S2 = 2'b10;parameter S3 = 2’b11;
number/smile
![Page 141: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/141.jpg)
141
Implementing FSM Example 2: State Register
// state registeralways @ (posedge clk, posedge reset)
if (reset) state <= S0;else state <= nextstate;
n This part defines the state register (memorizing process)
n Sensitive to only clk, reset
n In this example reset is active when ‘1’ (active-high)
![Page 142: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/142.jpg)
142
Implementing FSM Example 2: Next State Logic
// next state logicalways @ (*)
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
![Page 143: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/143.jpg)
143
Implementing FSM Example 2: Output Logic
// output logicassign smile = (number & state == S3);
n In this example, output depends on state and inputq Mealy type FSM
n We used a simple combinational assignment
![Page 144: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/144.jpg)
144
Implementation of FSM Example 2module SmilingSnail (input clk,
input reset,input number,output smile);
reg [1:0] state, nextstate;
parameter S0 = 2'b00;parameter S1 = 2'b01;parameter S2 = 2'b10;parameter S3 = 2’b11;
// state registeralways @ (posedge clk, posedge
reset)if (reset) state <= S0;else state <= nextstate;
always @ (*) // next state logiccase (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
// output logicassign smile = (number & state==S3);
endmodule
![Page 145: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/145.jpg)
What Did We Learn?n Basics of defining sequential circuits in Verilog
n The always statementq Needed for defining memorizing elements (flip-flops, latches)q Can also be used to define combinational circuits
n Blocking vs Non-blocking statementsq = assigns the value immediatelyq <= assigns the value at the end of the block
n Writing FSMsq Next state logicq State assignmentq Output logic
145
![Page 146: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/146.jpg)
Next Lecture: Timing and Verification
146
![Page 147: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/147.jpg)
Design of Digital CircuitsLecture 7: Sequential Logic Design
Prof. Onur MutluETH ZurichSpring 2018
15 March 2018
![Page 148: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/148.jpg)
Backup Slidesn Different types of flip flops
148
![Page 149: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/149.jpg)
The D Flip-Flop
149
![Page 150: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/150.jpg)
Enabled Flip-Flopsn Inputs: CLK, D, EN
q The enable input (EN) controls when new data (D) is storedn Function:
q EN = 1: D passes through to Q on the clock edge q EN = 0: the flip-flop retains its previous state
150
InternalCircuit
D Q
CLKEN
DQ
0
1D QEN
Symbol
![Page 151: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/151.jpg)
Resettable Flip-Flopn Inputs: CLK, D, Reset
q The Reset is used to set the output to 0.n Function:
q Reset = 1: Q is forced to 0 q Reset = 0: the flip-flop behaves like an ordinary D flip-flop
151
Symbols
D QReset
r
![Page 152: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/152.jpg)
Resettable Flip-Flopsn Two types:
q Synchronous: resets at the clock edge onlyq Asynchronous: resets immediately when Reset = 1
n Asynchronously resettable flip-flop requires changing the internal circuitry of the flip-flop (see Exercise 3.10)
n Synchronously resettable flip-flop?
152
InternalCircuit
D Q
CLK
D QReset
![Page 153: Design of Digital Circuits - ETH Z · E. The lock can now be opened n The state of a system is a snapshot of all relevant elements of the system at the moment of the snapshot q To](https://reader034.vdocument.in/reader034/viewer/2022050205/5f58d7f889dbb30aab69b84b/html5/thumbnails/153.jpg)
Settable Flip-Flopn Inputs: CLK, D, Setn Function:
q Set = 1: Q is set to 1 q Set = 0: the flip-flop behaves like an ordinary D flip-flop
153
Symbols
D QSet
s