====== Modelling D flip-flop with WTG guards ======
[[wp>Flip-flop_(electronics)#D_flip-flop | D Flip-flop]] is a very popular circuit with digital electronics that implements a basic memory element. This tutorial will introduce the use of guards in WTGs by using a flip-flop as an example. Check [[tutorial:design:basic_buck-wtg:start|Design of Waveform Transition Graph for basic buck controller]] before attempting to complete this tutorial.
A D flip-flop is composed of four signals, two inputs and two outputs. It captures the value of its data input signal ''D'' at a specific change of its clock input signal ''CLK''. For example, at every rising transition of ''CLK''. The captured value becomes the output signal ''Q'' until a different value is captured. The remaining output signal ''Q''' corresponds to the complementary of signal ''Q''.
In this example, the value for signal ''D'' is irrelevant most of the time and must be ignored until the rising edge of ''CLK''. This makes it a natural fit for design with WTG.
===== Modelling =====
Often the first step in WTG design is defining the initialization of the circuit. In this case, we will assume that all the inputs start at ''low'', with the outputs ''Q'' and ''_Q'' at ''low'' and ''high'' respectively. The only initialization needed for the flip-flop is for signal ''D''. It starts at ''low'', but afterwards it might arbitrarily change value. Our circuit should ignore any changes that ''D'' performs before a rising edge. This can be specified by an ''unstable'' transition. Note that, since ''D'' needs to become stable before the next transition of ''CLK'', the ''unstable'' transition must be followed by a ''stable'' transition.
++++ Detailed instructions|
* Create the initialisation structure at the state level:
* Place a new state and double click on it to make it initial.
* Add a new waveform and rename it to //initialisation//.
* Connect the initial state to the initialisation waveform.
* Complete the initialisation waveform:
* Double click on the waveform to move into the waveform level.
* Use the signal tool to create 4 new signals. Hold Shift while placing two of them to make them into inputs. Rename the signals with the property editor into ''D'', ''CLK'', ''Q'' and ''_Q''.
* Set the initial state for ''_Q'' to ''high'' by using the property editor.
* Double click on ''D'' while holding Ctrl and Shift to add a ''destabilise'' transition.
* Double click on ''D'' to add a ''stabilise'' transition.
++++
The following figure shows a possible implementation of this initialisation.
{{ wtg_flipflop-initialisation.svg?44%,nolink }}
In the previous figure we show all four signals, even though the waveform only has transitions for ''D''. The WTG model would be correct if we didn't use signals in waveforms that do not have transitions for them((There is an exception for waveforms with guards. A signal guarded has to be present in any waveform that guards it, even if there are no transitions for that signal.)). Yet, it is sometimes helpful to keep them for improved readability. Decide if you want to show unused signals or not according to your needs.
Regardless of your choice, we have now initialised the flip-flop and ''D'' should be set at ''stable'' state. This means that ''D'' might have had an arbitrary number of transitions (including none), but now is guaranteed to remain stable at some unknown value. WTGs allows us to specify how the model should behave depending on the value of a ''stable'' signal by using guards.
Add two new waveforms and specify a guard for ''D'' in them: one for ''low'' and one for ''high''.
++++ Detailed instructions|
* Add a new state and two waveforms.
* Connect the //initialisation// waveform to the new state and the new state to the new waveforms.
* Set guards for ''D'':
* Click on one of the waveforms. In the property editor, write ''D'' in the //Guard// field.
* Edit the //Guard// field for the other waveform and write ''!D''.
++++
{{ wtg_flipflop-guards1.svg?44%,nolink }}
You can now edit every waveform and model their behavior assuming the value of ''D''. Let us start with the waveform that guards for ''low''. The first transition has to be for ''CLK''. Since it is a ''raise'' transition, the value of ''D'' is captured and propagated to the outputs. Note that, since these outputs were already capturing a value of ''low'', their value remains the same.
But the transition for ''CLK'' has an additional effect: the transitions for ''D'' must again be ignored. This signal should now ''destabilise'' and ''stabilise'' before the end of the waveform. Add a ''fall'' transition for ''CLK'' in order to make this waveform a self-contained scenario. Rename this waveform into //d_low_q_same//.
Finally, since the state of all signals matches that of the initialisation, add a connection between the waveform and the previous state. If ''D'' has stabilised to ''low'' again, this will prompt this waveform to repeat. If the value has changed to ''high'', then the other scenario will play out.
++++ Detailed instructions|
* Click on the waveform with a guard for ''!D'' and rename it with the property editor into //d_low_q_same//.
* Double click on the waveform to go into the waveform level.
* Declare the signals:
* Use the property editor to declare the four signals by clicking in the //declared// checkbox for each signal.
* The raising edge of ''CLK'' triggers ''D'' into unstable:
* Double click on ''CLK'' to add a ''rise'' transition.
* Double click on ''D'' while holding Ctrl and Shift to add a ''destabilise'' transition.
* Add a connection between the transition of ''CLK'' and the transition of ''D''.
* Return the inputs to their previous state to finish the scenario.
* Double click on ''CLK'' to add a ''fall'' transition.
* Double click on ''D'' to add a ''stabilise'' transition.
* After this scenario is played out, the model goes back to its state after initialisation:
* Double click outside of the waveform to move into the state level.
* Add a connection between //d_low_q_same// and its previous state.
++++
The following figure shows a possible implementation of this model. Note that, as with the initialisation, the signals ''Q'' and ''_Q'' could be omitted from this waveform.
{{ wtg_flipflop-d_low_q_same.svg?44%,nolink }}
Let us now model the waveform for when ''D'' is stabilised at ''high''. We will call this scenario //d_high_q_rise//. This case is similar to the previous scenario, but this time the transition for ''CLK'' will also trigger transitions for ''Q'' and ''_Q''.
++++ Detailed instructions|
* Click on the waveform with a guard for ''D'' and rename it with the property editor into //d_high_q_rise//.
* Double click on the waveform to go into the waveform level.
* Declare the signals:
* Use the property editor to declare the four signals by clicking in the //declared// checkbox for each signal.
* The raising edge of ''CLK'' triggers ''rise'' and ''fall'' transitions for ''Q'' and ''Q_'' respectively. Additionally, it also triggers a ''destabilise'' transition for ''D'':
* Double click on ''CLK'' to add a ''rise'' transition.
* Double click on ''Q'' and ''Q_'' to add a ''rise'' and ''fall'' transition.
* Double click on ''D'' while holding Ctrl and Shift to add a ''destabilise'' transition.
* Add a connection between the transition of ''CLK'' and the transitions of ''Q'', ''Q_'' and ''D''.
* Return the inputs to their previous state to finish the scenario.
* Double click on ''CLK'' to add a ''fall'' transition.
* Double click on ''D'' to add a ''stabilise'' transition.
++++
The WTG should look similar to the following figure.
{{ wtg_flipflop-d_high_q_rise.svg?44%,nolink }}
At this point it might be tempting to add a connection between the last waveform and the previous state. Yet this is not possible, since the states for signals ''Q'' and ''_Q'' do not match the initial states. We have to add two new waveforms with guards for signal ''D''.
Add two new waveforms, including the state and the guards and connections. Rename the waveforms into //d_high_q_same// and //d_low_q_fall//. Start by designing the scenario from //d_high_q_same//. Note that, since this scenario returns the signals to their previous states you can add a connection back from the waveform to the state.
++++ Detailed instructions|
* Place a new state and two waveforms.
* Add a connection between the waveform //d_high_q_rise// and the new state, and a connection between the new state and the two new waveforms.
* Edit the waveform names into //d_high_q_same// and //d_low_q_fall//.
* Set the guard field to ''D'' in the property editor for the waveform //d_high_q_same//.
* Set the guard field to ''!D'' in the property editor for the waveform //d_low_q_fall//.
* Double click the waveform //d_high_q_same// to go into waveform level.
* Declare the signals:
* Use the property editor to declare the four signals by clicking in the //declared// checkbox for each signal.
* The raising edge of ''CLK'' triggers ''D'' into unstable:
* Double click on ''CLK'' to add a ''rise'' transition.
* Double click on ''D'' while holding Ctrl and Shift to add a ''destabilise'' transition.
* Add a connection between the transition of ''CLK'' and the transition of ''D''.
* Return the inputs to their previous state to finish the scenario.
* Double click on ''CLK'' to add a ''fall'' transition.
* Double click on ''D'' to add a ''stabilise'' transition.
* After this scenario is played out, the model goes back to its previous state:
* Double click outside of the waveform to move into the state level.
* Add a connection between //d_high_q_rise// and its previous state.
++++
Check that your WTG looks like this.
{{ wtg_flipflop-d_high_q_same.svg?44%,nolink }}
Finally, you can complete the last scenario, //d_low_q_fall//. The state for all the signals at the end of this waveform matches the state after the initialisation, so you can close the loop with a connection between the waveform and the state succeeding the initialisation waveform.
++++ Detailed instructions|
* Double click on the waveform //d_low_q_fall// to go into the waveform level.
* Declare the signals:
* Use the property editor to declare the four signals by clicking in the //declared// checkbox for each signal.
* The raising edge of ''CLK'' triggers ''fall'' and ''rise'' transitions for ''Q'' and ''Q_'' respectively. Additionally, it also triggers a ''destabilise'' transition for ''D'':
* Double click on ''CLK'' to add a ''rise'' transition.
* Double click on ''Q'' and ''Q_'' to add a ''fall'' and ''rise'' transition.
* Double click on ''D'' while holding Ctrl and Shift to add a ''destabilise'' transition.
* Add a connection between the transition of ''CLK'' and the transitions of ''Q'', ''Q_'' and ''D''.
* Return the inputs to their previous state to finish the scenario.
* Double click on ''CLK'' to add a ''fall'' transition.
* Double click on ''D'' to add a ''stabilise'' transition.
* This scenario returns the state of the model to its state after initialisation:
* Double click outside of the waveform to move into the state level.
* Add a connection between //d_low_q_fall// and the state that follows the //initialisation// waveform.
++++
You can see the final model in the next figure.
{{ wtg_flipflop-final.svg?44%,nolink }}