Difference between revisions of "CSC270 Homework 5 Solution"
(→Problems #3 & #4) |
(→Problems 3 & 4) |
||
Line 53: | Line 53: | ||
def JK( J, K, Q ): | def JK( J, K, Q ): | ||
if J==K and J==0: return 0 | if J==K and J==0: return 0 | ||
− | if J==K and J==1: return | + | if J==K and J==1: return not Q |
return J | return J | ||
Revision as of 05:49, 25 March 2009
Problems 1 & 2
Everybody knew how to deal with this problem, but didn't see the shortcut that could be taken: that of xoring the two switches together, and simplifying the sequencer from one with 2 inputs to one with only 1 input. The input is simply Switch1 XOR Switch2. When the two switches are the same, the input is 0, when they are different (somebody is crossing) the input is 1.
Your design should have shown you that D1 and D2 were functions of (Switch 1 XOR Switch2).
D1 = not Q1 and Q0 or Q1 and (Switch1 XOR Switch2 ) D0 = not Q1 and not Q0 or Q1 and ( Switch1 XOR Switch2 ) G = not Q1 and not Q0 R = Q1 Y = not ( G or R )
A simple trick using two lists for the two switches can help write a simple Python simulator/tester:
D2 = None
D0 = None
list1 = [0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0]
list2 = [0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
for i in range( len( list1 ) ):
s1 = list1[i]
s2 = list2[i]
# the Q1 and Q2 outputs go through combinational logic to generate the new values
# of D1, D2, and the outputs G, Y, R...
D1 = not Q1 and Q0 or Q1 and ( s1 ^ s2 )
D0 = (not Q1 and not Q0) or Q1 and ( s1 ^ s2)
G = not Q1 and not Q0
R = Q1
Y = not G and not R
# show the stable circuit signals
print "S1S2 = %d %d Q1Q0 = %d %d | GYR = %d %d %d" \
% ( s1, s2, Q1, Q0, G, Y, R )
# wait for the next clock tick (the user presses Enter)
#raw_input( "> " )
Q1 = D1
Q0 = D0
Problems 3 & 4
J2 = not Q2 and not Q1 and not Q0 K2 = Q2 and not Q1 and not Q0 J1 = not Q1 and not Q0 K1 = Q1 and not Q0 J0 = K0 = 1
The trick for the JK is to use a function that sets Q depending on J and K:
def JK( J, K, Q ): if J==K and J==0: return 0 if J==K and J==1: return not Q return J
Then the program becomes:
def JK( J, K, Q ):
if J==K and J==0: return 0
if J==K and J==1: return 1
return J
Q0 = Q1 = Q2 = 0
for i in range( 20 ):
J2 = (not Q2 ) and ( not Q1 ) and ( not Q0 )
K2 = Q2 and ( not Q1 ) and ( not Q0 )
J1 = ( not Q1 ) and (not Q0)
K1 = Q1 and ( not Q0 )
J0 = K0 = 1
print " Q2 Q1 Q0 = ", Q2, Q1, Q0
Q0 = JK( J0, K0, Q0 )
Q1 = JK( J1, K1, Q1 )
Q2 = JK( J2, K2, Q2 )