T Flip Flop Design Using Verilog With Testbbench

The T Flip Flop (Toggle Flip Flop) is a simplified sequential logic circuit derived from the JK Flip Flop by tying both J and K inputs together. It has a single data input T along with a clock signal CLK and an optional reset RST. When T is 1, the output toggles on every active clock edge; when T is 0, the output retains its previous state. In this article, we’ll explore the T Flip Flop in depth, covering its block diagram, circuit design, truth table, Verilog implementation, and testbench simulation.


Example: T Flip Flop in Verilog

module tFlipFlop(T, CLK, RST, Q);
  input T, CLK, RST;
  output reg Q = 1;

  always @(posedge CLK)
    begin
      if(RST == 1)
        Q = 0;          // Synchronous Reset
      else
        begin
          if(T == 1)
            Q = ~Q;     // Toggle
          else
            Q = Q;      // Memory: no change
        end
    end

endmodule
        

Output:

 0 T=1, Q=0, CLK=1
 2 T=1, Q=0, CLK=0
 4 T=1, Q=1, CLK=1
 6 T=0, Q=1, CLK=0
 8 T=1, Q=0, CLK=1
10 T=0, Q=0, CLK=0
        

Note: The T Flip Flop includes a synchronous reset (RST). When RST=1, Q is forced to 0 on the next positive clock edge, regardless of the T input.


What Is a T Flip Flop?

A T Flip Flop is a clocked sequential logic element derived from the JK Flip Flop by permanently connecting both J and K inputs together and treating them as a single input called T (Toggle). It is one of the simplest flip flop types, with only two possible behaviors: hold the current state or toggle it.

Key characteristics of the T Flip Flop include:

  • Single Data Input: Only one input T determines whether the output toggles or stays the same.
  • Toggle Behavior: When T=1, the output Q complements itself on every active clock edge — making it ideal for counters.
  • Memory Behavior: When T=0, the output Q retains its previous value — no state change occurs.
  • Synchronous Reset: An RST input allows the flip flop to be cleared to 0 synchronously with the clock.
  • Derived from JK: A T Flip Flop is equivalent to a JK Flip Flop with J=K=T at all times.

T Flip Flops are extensively used in binary counters, frequency dividers, and shift registers due to their simple toggle behavior.


Block Diagram and Circuit Diagram

The block diagram of a T Flip Flop shows a single data input T, a clock input CLK, a reset RST, and complementary outputs Q and Q’. The circuit is internally identical to a JK Flip Flop with J and K shorted together.

Block Diagram

Block-Diagram-T-Flip-Flop

Logic Circuit Diagram

The logic circuit of a T Flip Flop is built by connecting the J and K inputs of a JK Flip Flop together. The single T input drives both, so when T=1 the circuit toggles and when T=0 it holds — exactly matching the JK Flip Flop’s behavior for J=K.

t-flip-flop-circuit-diagram

Note: The T Flip Flop has no dedicated Set mode. If you need Set functionality, you must use the complement — i.e., toggle from a known reset state.


Truth Table

The truth table of a T Flip Flop describes the next state of Q for every combination of the clock and T input. The symbol X denotes a “don’t care” condition, and Q’ represents the toggled (complemented) output.

CLOCK T Q (Next State) Description
0 X Q Memory (No Change)
1 0 Q Memory (No Change)
1 1 Q’ Toggle

Note: When the clock is LOW (0), the flip flop ignores T entirely and maintains its current state — state changes only occur on the active (positive) clock edge.


Core Concepts of T Flip Flop

The T Flip Flop involves several key concepts essential for understanding its behavior and implementing it effectively in digital systems. We’ll cover these in detail.

1. Operating Modes

The T Flip Flop has two distinct operating modes based on the T input value when the clock pulse is active, plus a synchronous reset:

  • Memory Mode (T=0): The output Q retains its previous value. No state change occurs on the clock edge.
  • Toggle Mode (T=1): The output Q flips to its complement on every rising clock edge. If Q=1, it becomes 0, and vice versa.
  • Synchronous Reset (RST=1): Forces Q to 0 on the next positive clock edge, overriding the T input.

2. Verilog Implementation

The T Flip Flop is modeled in Verilog using an always block sensitive to the positive edge of CLK. The module takes T, CLK, and RST as inputs and produces Q as a registered output. The reset is checked first with the highest priority.

module tFlipFlop(T, CLK, RST, Q);
  input T, CLK, RST;
  output reg Q = 1;

  always @(posedge CLK)
    begin
      if(RST == 1)
        Q = 0;        // Synchronous Reset: force Q to 0
      else
        begin
          if(T == 1)
            Q = ~Q;   // Toggle: invert current output
          else
            Q = Q;    // Memory: retain current state
        end
    end

endmodule
        

Output:

 0 T=1, Q=0, CLK=1
 2 T=1, Q=0, CLK=0
 4 T=1, Q=1, CLK=1
 6 T=0, Q=1, CLK=0
 8 T=1, Q=0, CLK=1
10 T=0, Q=0, CLK=0
        

Note: The reset in this implementation is synchronous — it only takes effect on the positive clock edge. For an asynchronous reset, add RST to the sensitivity list: always @(posedge CLK or posedge RST).

3. Writing the Testbench

The testbench instantiates the T Flip Flop as a DUT (Device Under Test), generates a clock using an always block with a period of 4 time units, and applies different T values to observe toggle and memory behavior through $monitor.

module tFlipFlopTB;
  reg T0 = 1, RST0 = 0, CLK0 = 0;
  reg Q0;

  // Instantiate the DUT
  tFlipFlop DUT(.Q(Q0), .T(T0), .CLK(CLK0), .RST(RST0));

  // Clock generation: period = 4 time units (toggle every 2)
  always
    begin
      CLK0 = ~CLK0;
      #2;
    end

  // Stimulus: apply different T values
  initial
    begin
      #2 T0 = 1;   // Toggle
      #2 T0 = 1;   // Toggle again
      #2 T0 = 0;   // Memory: no change
      #2 T0 = 1;   // Toggle
      #2 T0 = 0;   // Memory: no change
      $finish;
    end

  // Monitor output at each signal change
  initial
    begin
      $monitor($time, " T=%b, Q=%B, CLK=%b", T0, Q0, CLK0);
    end

endmodule
        

Output:

 0 T=1, Q=0, CLK=1
 2 T=1, Q=0, CLK=0
 4 T=1, Q=1, CLK=1
 6 T=0, Q=1, CLK=0
 8 T=1, Q=0, CLK=1
10 T=0, Q=0, CLK=0
        

Note: The $monitor task prints values whenever any monitored signal changes. %B is used in place of %b for Q to match the original testbench format; both display binary values.

4. Characteristic Equation

The characteristic equation of a T Flip Flop expresses the next state Q(t+1) in terms of the current state Q(t) and the input T:

  • Q(t+1) = T ⊕ Q(t) (XOR operation)
  • When T=0: Q(t+1) = Q(t) — Memory (no change)
  • When T=1: Q(t+1) = Q'(t) — Toggle (complement)

The XOR relationship makes the T Flip Flop especially straightforward to use in counter and frequency divider circuits, where toggling on every clock edge is the primary requirement.

5. T Flip Flop vs JK Flip Flop

The T Flip Flop is a special case of the JK Flip Flop. Understanding their differences helps in choosing the right element for a given digital design.

Feature T Flip Flop JK Flip Flop
Number of Inputs 1 (T) 2 (J, K)
Set Mode Not available directly Available (J=1, K=0)
Reset Mode Via RST pin only Available (J=0, K=1)
Toggle Mode T=1 J=1, K=1
Best Use Case Counters, frequency dividers General-purpose sequential logic

6. Simulation Output Explained

Reading the simulation output step by step confirms that the Verilog model behaves correctly across all T input combinations:

  • Time 0: CLK=1, T=1, Q initializes to 0 (reset overrides initial value of 1 at first posedge).
  • Time 2: CLK falls to 0, no posedge — Q remains 0.
  • Time 4: CLK rises to 1, T=1 → Toggle mode, Q flips from 0 to 1.
  • Time 6: CLK falls to 0, T changes to 0 — no posedge, Q stays 1.
  • Time 8: CLK rises to 1, T=1 → Toggle mode, Q flips from 1 to 0.
  • Time 10: CLK falls to 0, T=0 — no posedge, Q remains 0.

Both Toggle (T=1) and Memory (T=0) modes are verified correctly across the simulation run.


Why Is the T Flip Flop Important?

The T Flip Flop is a fundamental component in digital design for the following reasons:

  1. Binary Counters: A chain of T Flip Flops with T permanently tied to 1 forms a ripple counter — each stage divides the clock frequency by 2.
  2. Frequency Division: A single T Flip Flop in toggle mode divides the input clock by 2, making it essential in digital clocks, timers, and PLLs.
  3. Simplicity: With only one data input, T Flip Flops are simpler to use and reason about compared to JK or SR Flip Flops.
  4. Synchronous Design: The synchronous reset allows clean initialization of counter chains without glitches.
  5. State Machine Building Block: Used as storage elements in state machines where toggling behavior is required on specific transitions.

Tips for Using T Flip Flops

  • Tie T=1 for Pure Toggle: If you only need a divide-by-2 frequency divider, connect T permanently to logic HIGH and let the clock drive the toggling — no additional logic needed.
  • Synchronous vs Asynchronous Reset: Use a synchronous reset (RST in the always @(posedge CLK) block) for glitch-free behavior; use asynchronous reset (RST in the sensitivity list) when immediate clearing is needed regardless of the clock.
  • Initialize Output Register: Assign an initial value to Q (e.g., output reg Q = 0) to avoid unknown (x) states at the start of simulation.
  • Use Non-Blocking Assignments in RTL: Prefer <= (non-blocking) over = (blocking) inside always @(posedge CLK) for proper RTL synthesis and to prevent race conditions.
  • Verify Both Modes in Testbench: Always test both T=0 (memory) and T=1 (toggle) in your testbench to confirm correct behavior before synthesizing to hardware.
  • Convert from JK When Needed: If your target device only provides JK Flip Flops, implement a T Flip Flop by simply connecting J=K=T — no extra gates required.

Example: Using T Flip Flop in a Program

The following is a complete, well-commented Verilog implementation of the T Flip Flop along with its testbench, demonstrating both Toggle and Memory modes with a synchronous reset in a single simulation run.

// ============================================
// T Flip Flop - Verilog RTL Implementation
// ============================================
module tFlipFlop(T, CLK, RST, Q);
  input T, CLK, RST;
  output reg Q = 1;  // Initialize Q to 1

  // Trigger on positive clock edge only
  always @(posedge CLK)
    begin
      if(RST == 1)
        Q = 0;        // Synchronous Reset: clear Q to 0
      else
        begin
          if(T == 1)
            Q = ~Q;   // Toggle: invert current output
          else
            Q = Q;    // Memory: retain current state
        end
    end

endmodule

// ============================================
// T Flip Flop Testbench
// ============================================
module tFlipFlopTB;
  reg T0 = 1, RST0 = 0, CLK0 = 0;
  reg Q0;

  // Instantiate Device Under Test (DUT)
  tFlipFlop DUT(.Q(Q0), .T(T0), .CLK(CLK0), .RST(RST0));

  // Clock generation: period = 4 time units (toggle every 2)
  always
    begin
      CLK0 = ~CLK0;
      #2;
    end

  // Apply stimulus: test Toggle and Memory modes
  initial
    begin
      #2 T0 = 1;   // Toggle mode
      #2 T0 = 1;   // Toggle again
      #2 T0 = 0;   // Memory mode: no state change
      #2 T0 = 1;   // Toggle mode
      #2 T0 = 0;   // Memory mode: no state change
      $finish;
    end

  // Monitor and display output at each signal change
  initial
    begin
      $monitor($time, " T=%b, Q=%B, CLK=%b", T0, Q0, CLK0);
    end

endmodule
        

Output:

 0 T=1, Q=0, CLK=1
 2 T=1, Q=0, CLK=0
 4 T=1, Q=1, CLK=1
 6 T=0, Q=1, CLK=0
 8 T=1, Q=0, CLK=1
10 T=0, Q=0, CLK=0
        

This complete example demonstrates both Toggle (T=1) and Memory (T=0) modes of the T Flip Flop with a synchronous reset, verified through a structured testbench with clock generation and $monitor for real-time signal tracking.


Did You Know?

  • The T Flip Flop gets its name from its defining behavior — “T” stands for Toggle, reflecting the fact that it flips its output on every active clock edge when T=1.
  • A ripple counter built from N T Flip Flops (each with T=1) can count from 0 to 2ⁿ−1, dividing the input clock frequency by 2ⁿ at the final stage — a technique used in every digital clock chip.
  • In FPGAs, T Flip Flops are not always available as native primitives. Synthesis tools automatically implement them using the D Flip Flops present in the FPGA fabric by adding an XOR gate: D = T XOR Q.
  • The T Flip Flop is the simplest possible 1-bit memory element capable of indefinitely dividing a clock signal — a property exploited heavily in frequency synthesizers and phase-locked loops (PLLs).

Leave a comment