Traffic Light System Clock

Our First Real System
Review
On our way to building a computer…
Logic
0’s
Circuits:
&
1’s Universal
Method
Represent
Info with
0’s & 1’s
(aka bits)
Memory
Circuits
We are here
Computers
The System Clock
• “500 Mhz Pentium III computer…”
• What does “500 Mhz” mean?
• It refers to the speed of the
System Clock.
(actually this is only one of the clocks…)
• All digital systems have such “Clocks,” even
traffic lights and elevator control systems.
Clocks: Some Terminology
• Think of System Clock as a heart.
• How fast it beats is measured in
Hertz (Hz) which means
“cycles per second”
• Prefixes:
• Kilo = 1,000
• Mega = 1,000,000
• Giga = 1,000,000,000
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
0
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
1
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
0
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
1
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
0
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
1
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
0
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
1
The System Clock (cont.)
• Rough idea: System takes a “step” every
time the System Clock beats.
• We’ll see how this works in an example.
• In real life, Clock is a Quartz crystal,
which sends out 0’s and 1’s on a wire.
• In every cycle, it changes
from 0 to 1 and back to 0
0
Our System:
A Traffic Light
A Traffic Light
• Intersection of two one-way roads
A Traffic Light
• Intersection of two one-way roads
A
B
Car
Sensors
Traffic Light
System Clock
• Traffic Lights don’t have to act very fast.
• Let’s say the System Clock only beats
once every 4 seconds.
• This is 0.25 Hz.
(a few Billion times slower than your PC)
Traffic Light Design
• How do we go about designing the Traffic
Light?
• First, let’s think about all the possible
States of the traffic light.
Traffic Light
• Intersection of two one-way roads
A
B
Car
Sensors
Light A
Light B
Light A
Possible States
Light B
Light A
Possible States
Light B
Light A
Possible States
Light B
Light A
Possible States
Light B
Light A
Actually Only 4 Possibilities
Light B
Traffic Light Design
• We figured out some possible States of the
Traffic Light.
• Next, we should think logically about how
the Traffic Light should behave.
• For now, we will try to keep it simple.
• (Suppose these are infrequently used
country roads.)
Light A
How should the Light behave?
A
B
Light B
Car
Sensors
Traffic Light Behavior
IF A=1
Light A
AND B=0
Light B
Traffic Light Behavior
Otherwise
IF A=1
Light A
AND B=0
Light B
Traffic Light Behavior
Otherwise
IF A=1
Light A
AND B=0
Always
Light B
Traffic Light Behavior
Otherwise
IF A=1
Light A
AND B=0
Always
IF A=0
AND B=1
Light B
Otherwise
Traffic Light Behavior
Otherwise
IF A=1
Light A
AND B=0
Always
Always
IF A=0
AND B=1
Light B
Otherwise
Traffic Light Behavior
Otherwise
IF A=1
Light A
AND B=0
Note:
Clock beats
every 4 sec.
So Light is
Yellow for
4 sec.
Always
Always
IF A=0
AND B=1
Light B
Otherwise
Traffic Light State Machine
Otherwise
IF A=1
Light A
AND B=0
Note:
Clock beats
every 4 sec.
So Light is
Yellow for
4 sec.
Always
Always
IF A=0
AND B=1
Light B
Otherwise
Traffic Light Design
• We’ve figured out the logical behavior of
the Traffic Light in terms of a
State Machine:
• We know what the states are.
• For each state, given any input,
we know which state to go to next.
• How do we build it?
Traffic Light Design
• Thinking this through:
• Our machine needs to:
Traffic Light Design
• Thinking this through:
• Our machine needs to:
• Remember which state it is in (Memory)
Traffic Light Design
• Thinking this through:
• Our machine needs to:
• Remember which state it is in (Memory)
• Given the state it is in and input values,
it must determine which state to go to
next (Logic)
Traffic Light Design
• Thinking this through:
• Our machine needs to:
• Remember which state it is in (Memory)
• Given the state it is in and input values,
it must determine which state to go to
next (Logic)
• And that’s all !
Traffic Light Design
• Problem 1:
• Remember which state it is in (Memory)
• How do represent the states?
• We could keep one bit of memory for
whether Light A is Green or not,
whether it is Yellow or not, …
• This would take 6 bits of memory.
• There is a much simpler way!
Traffic Light Design
• Problem 1:
• Remember which state it is in (Memory)
• How do represent the states?
• Answer:
• Just number them (using binary numbers).
• We have 4 states.
• Call them 00, 01, 10, 11.
• We (human designers) will keep track of
what these names mean.
Traffic Light States
Otherwise
IF A=1
Light A
AND B=0
Always
Always
IF A=0
AND B=1
Light B
Otherwise
Traffic Light States
Otherwise
Light A
00
IF A=1
AND B=0
Always
Always
11
01
IF A=0
AND B=1
Light B
10
Otherwise
Traffic Light Design
• Problem 2:
• Given the state it is in and input values,
it must determine which state to go to
next (Logic)
• Answer:
Traffic Light Design
• Problem 2:
• Given the state it is in and input values,
it must determine which state to go to
next (Logic)
• Answer:
• Build a Truth Table
• Use Universal Method!
Traffic Light Behavior
Build A Truth Table
for next state / Output
M1 M2 A B
D1 D2 Light A Red
Light B Red …
Traffic Light Behavior
Otherwise
Light A
00
IF A=1
AND B=0
Always
Always
11
01
IF A=0
AND B=1
Light B
10
Otherwise
Traffic Light Behavior
Build A Truth Table
for next state / Output
M1 M2 A B
D1 D2 Light A Red
Light B Red …
0
0 1
0
0
1
0
1
Traffic Light Behavior
Otherwise
Light A
00
IF A=1
AND B=0
Always
Always
11
01
IF A=0
AND B=1
Light B
10
Otherwise
Traffic Light Behavior
Build A Truth Table
for next state / Output
M1 M2 A B
D1 D2 Light A Red
Light B Red …
0
0
1
0 1
1
0
0
1
* *
1
1
0
0
0
Traffic Light Behavior
Otherwise
Light A
00
IF A=1
AND B=0
Always
Always
11
01
IF A=0
AND B=1
Light B
10
Otherwise
Traffic Light Behavior
Build A Truth Table
for next state / Output
M1 M2 A B
D1 D2 Light A Red
Light B Red …
0
0
1
0 1
1
0
0
1
* *
1
0
1
0
1
0
0 1
1
1
0
1
0
Traffic Light Behavior
Otherwise
Light A
00
IF A=1
AND B=0
Always
Always
11
01
IF A=0
AND B=1
Light B
10
Otherwise
Traffic Light Behavior
Build A Truth Table
for next state / Output
M1 M2 A B
D1 D2 Light A Red
Light B Red …
0
0
1
0 1
1
0
0
1
* *
1
0
1
0
1
0
0 1
1
1
0
1
1
1
* *
0 0
0
1
0
Traffic Light Behavior
Otherwise
Light A
00
IF A=1
AND B=0
Always
Always
11
01
IF A=0
AND B=1
Light B
10
Otherwise
Traffic Light Behavior
Build A Truth Table
for next state / Output
M1 M2 A B
D1 D2 Light A Red
Light B Red …
0
0
1
0 1
1
0
0
1
* *
1
0
1
0
1
0
0 1
1
1
0
1
1
1
* *
0 0
0
1
0
0
0 0
0 0
1
0
…
…
… …
… …
…
0
Traffic Light Design
• We understand how to represent which
state the system is in (so we can store it in
Memory)
• We understand how the system can
determine which state to go to next (Logic)
• Let’s put it together…
Traffic Light Design
Input: Sensor A
Input: Sensor B
Traffic Light Design
D1
Current State
D2
Write
Sensor A
Sensor B
2-bit
Memory
Register
M1
M2
Traffic Light Design
Current State
Write
Sensor A
Sensor B
2-bit
Memory
Register
Logic
For
Next
State
&
Output
Next
State
6 Outputs:
for each
Light
Traffic Light Design
Current State
Write
Sensor A
Sensor B
2-bit
Memory
Register
Logic
For
Next
State
&
Output
6 Outputs:
for each
Light
Traffic Light Design
Current State
Clock
Sensor A
Sensor B
2-bit
Memory
Register
Logic
For
Next
State
&
Output
6 Outputs:
for each
Light
Design for Any State Machine
Many
bits
Current State
Clock
Inputs
Memory
Register
Logic
For
Next
State
&
Output
Outputs
State Machines
in Real Life
• Elevator control systems
• Car control systems
• VCR’s
• Alarm Clocks
• Personal Computers
• … Just about everything!
More Sophisticated Designs
• Just add more states, more inputs, more
outputs, more rules…
• Example: Add a longer delay before Traffic
Light changes from Yellow to Red.
(Say 8 seconds instead of 4.)
Traffic Light Behavior
Otherwise
IF A=1
Light A
AND B=0
Always
Always
Always
Always
IF A=0
AND B=1
Light B
Otherwise
More Sophisticated Designs
• Note that the new “delay” states we added
correspond to the SAME combinations of
lights as other states (Red/Yellow).
• The purpose of the new states is only
to add delays.
• Logical states need not correspond to
different observable states of the machine!
More Sophisticated Designs
• Can also add other State Machines!
More Sophisticated Designs
• Example (Fairness):
• We might want a separate Timer machine
for the Traffic Light:
• If cars keep coming, Light should decide
after 3 minutes to switch lights.
• A Timer is just a state machine that keeps
counting beats of the Clock
More Sophisticated Designs
Inputs
Main State
Machine
Outputs
Timer
State
Machine
Next Time:
Computers!
State Machines that you can program