<jupyter_start><jupyter_text>Finite State Machine GeneratorThis notebook will show how to use the Finite State Machine (FSM) Generator to generate a state machine. The FSM we will build is a Gray code counter.  The counter has three state bits and can count up or down through eight states.  The counter outputs are Gray coded, meaning that there is only a single-bit transition between the output vector of any state and its next states.   Step 1: Download the `logictools` overlay<jupyter_code>from pynq.overlays.logictools import LogicToolsOverlay
from pynq.lib.logictools import FSMGenerator

logictools_olay = LogicToolsOverlay('logictools.bit')<jupyter_output><empty_output><jupyter_text>Step 2: Specify the FSM<jupyter_code>fsm_spec = {'inputs': [('reset','D0'), ('direction','D1')],
            'outputs': [('bit2','D3'), ('bit1','D4'), ('bit0','D5')],
            'states': ['S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7'],
            'transitions': [['01', 'S0', 'S1', '000'],
                            ['00', 'S0', 'S7', '000'],
                            ['01', 'S1', 'S2', '001'],
                            ['00', 'S1', 'S0', '001'],
                            ['01', 'S2', 'S3', '011'],
                            ['00', 'S2', 'S1', '011'],
                            ['01', 'S3', 'S4', '010'],
                            ['00', 'S3', 'S2', '010'],
                            ['01', 'S4', 'S5', '110'],
                            ['00', 'S4', 'S3', '110'],
                            ['01', 'S5', 'S6', '111'],
                            ['00', 'S5', 'S4', '111'],
                            ['01', 'S6', 'S7', '101'],
                            ['00', 'S6', 'S5', '101'],
                            ['01', 'S7', 'S0', '100'],
                            ['00', 'S7', 'S6', '100'],
                            ['1-', '*',  'S0', '']]}<jupyter_output><empty_output><jupyter_text>__Notes on the FSM specification format__![](./images/fsm_spec_format.png)  Step 3: Instantiate the FSM generator object<jupyter_code>fsm_generator = logictools_olay.fsm_generator<jupyter_output><empty_output><jupyter_text>__Setup to use trace analyzer__   In this notebook, the trace analyzer is used to check if the inputs and outputs of the FSM.Users can choose whether to use the trace analyzer by calling the `trace()` method.<jupyter_code>fsm_generator.trace()<jupyter_output><empty_output><jupyter_text>Step 5: Setup the FSM generatorThe FSM generator will work at the default frequency of 10MHz. This can be modified using a `frequency` argument in the `setup()` method.<jupyter_code>fsm_generator.setup(fsm_spec)<jupyter_output><empty_output><jupyter_text>__Display the FSM state diagram__   This method should only be called after the generator has been properly set up.<jupyter_code>fsm_generator.show_state_diagram()<jupyter_output><empty_output><jupyter_text>__Set up the FSM inputs on the PYNQ board__* Check that the reset and direction inputs are correctly wired on the PYNQ board, as shown below:|  | PYNQ-Z1 | PYNQ-Z2 ||--|---------|---------|| GND | D0 | AR0 || 3.3V | D1 | AR1 |![](./images/fsm_wiring.png) __Notes:__    * The 3-bit Gray code counter is an up-down, wrap-around counter that will count from states 000 to 100 in either ascending or descending order    * The reset input is connected to pin D0/AR0 of the Arduino connector    * Connect the reset input to GND for normal operation     * When the reset input is set to logic 1 (3.3V), the counter resets to state 000    * The direction input is connected to pin D1/AR1 of the Arduino connector    * When the direction is set to logic 0, the counter counts down    * Conversely, when the direction input is set to logic 1, the counter counts up  Step 6: Run and display waveformThe ` run()` method will execute all the samples, `show_waveform()` method is used to display the waveforms<jupyter_code>fsm_generator.run()
fsm_generator.show_waveform()<jupyter_output><empty_output><jupyter_text>Verify the trace output against the expected Gray code count sequence| State | FSM output bits: bit2, bit1, bit0        ||:-----:|:----------------------------------------:|| s0    | 000                                      || s1    | 001                                      || s2    | 011                                      || s3    | 010                                      || s4    | 110                                      || s5    | 111                                      || s6    | 101                                      || s7    | 100                                      |  Step 7: Stop the FSM generatorCalling `stop()` will clear the logic values on output pins; however, the waveform will be recorded locally in the FSM instance.<jupyter_code>fsm_generator.stop()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13198498748957466, 'fraction_numerical': 0.041701417848206836, 'mean_word_length': 2.256619144602851, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '119112', 'license': 'MIT', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1384, 'n_words': 482, 'path': 'zynq-old/PYNQ_Workshop/Session_3/4_fsm_generator.ipynb', 'repo_name': 'chiro2001/zynqs', 'size': '4796'}