# Latches¶

Latches have an internal Boolean state that can be modified using Boolean inputs. In digital logic, there are many different types of latch that vary in the effects their inputs produce on the internal Boolean state variable. All latches provide the value of the internal state as their output, traditionally named Q.

In electronics terminology, there is a technical distinction between latches and flip-flops related to their interaction with clocks. However, this distinction is meaningless in CertSAFE, and this example project uses the terms interchangeably.

In the latches included in this example library, the starting value of Q before the first time the latch is executed is always false. However, it is easy to add an IC (initial condition) pin to the components if it is desired that the initial state be user-specifiable.

## SR Latch (Set Priority), SR Latch (Reset Priority), E Latch, and JK Latch¶

These four components have two Boolean inputs, named S and R for set and reset. (The inputs to the JK Latch are instead named J and K respectively, to match the convention from traditional circuit diagrams.) Every frame, one of four actions is taken, depending on the values of the S and R inputs:

• If S is true and R is false, the internal state Q is set to true.

• If S is false and R is true, Q is set to false.

• If both S and R are false, Q retains its value from the previous frame.

• If both S and R are true, the behavior differs between the four components:

• The SR Latch (Set Priority) sets Q to true.
• The SR Latch (Reset Priority) sets Q to false.
• The E Latch retains the value of Q from the previous frame.
• The JK Latch toggles the value of Q relative to the previous frame.

All four latches are implemented internally by using Boolean logic to compute the new Q value for the current frame as a function of the Q value from the previous frame (obtained with a One Frame Delay primitive) and the current values of the S and R inputs.

## T Flip-Flop¶

The T Flip-Flop has a single Boolean input labeled T. Every frame where the T input is true, the internal state toggles. The example implementation of a T Flip-Flop uses an XOR gate combining the T input with the previous value of Q. One way of thinking of an XOR gate is as a controlled NOT gate that flips the value of Q if T is true, but leaves Q at its previous value if T is false.