CSC270 Python simulator for Controllable Sequencer

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 08:04, 1 March 2016 (EST)


Python Version 3


# GYRSequencer.py
# D. Thiebaut
# Simulating a FSM with an input.
# The word problem for this simulator is:
# Design a Moore machine with an input switch.
# When the switch is 1 the machine goes through
# States S0, S1, S2, S0, S1, S2...
# When the switch is 0, the machine goes through
# States S2, S3, S2, S3, ...


# Initialize the flip-flops and the input switch
Q1 = 0
Q0 = 0
S = 0

def NOT( a ):
    return 1 - a

# simulate 20 ticks

for step in range( 20 ):

    # wait for the next clock tick (the user presses Enter)
    # get the value of the switch S from the user.  User
    # must enter 0 or 1.
    while True:
        S = input( "> " ).strip()
        if S != "0" and S != "1":
            print( "Invalid input signal!  Please reenter" )
        else:
            S = int( S )
            break

    # combine switch value with current outputs (present)
    # to compute what the future outputs will be.
    D1 = NOT( S ) | ( Q0 ^ Q1 )
    D0 = NOT( Q1 & Q0 )

    # show the present outputs of the flip-flops
    print( "S Q1Q0 =%d %d%d" % ( S, Q1, Q0 ) )


    # Tick!  Whatever is on the D input of flip-flop
    # becomes the output.
    Q1 = D1
    Q0 = D0