T flip-flop behavior in Verilog - Time & Space Complexity
We want to understand how the time it takes for a T flip-flop to update changes as inputs change.
How does the number of operations grow when the input toggles repeatedly?
Analyze the time complexity of the following code snippet.
module t_flip_flop(
input wire clk,
input wire t,
output reg q
);
always @(posedge clk) begin
if (t) q <= ~q;
end
endmodule
This code models a T flip-flop that toggles its output on each clock rising edge if the input t is high.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The flip-flop toggles output on every clock rising edge when t is 1.
- How many times: This happens once per clock cycle, repeating as many times as clock cycles occur.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 clock cycles | 10 toggles if t=1 each cycle |
| 100 clock cycles | 100 toggles if t=1 each cycle |
| 1000 clock cycles | 1000 toggles if t=1 each cycle |
Pattern observation: The number of toggles grows linearly with the number of clock cycles when t stays high.
Time Complexity: O(n)
This means the number of toggles grows directly in proportion to the number of clock cycles.
[X] Wrong: "The flip-flop toggles multiple times within one clock cycle."
[OK] Correct: The flip-flop only changes state once per clock rising edge, so toggling happens at most once per cycle.
Understanding how hardware elements like flip-flops behave over time helps you reason about timing and performance in digital designs.
"What if the input 't' changes randomly every clock cycle? How would the time complexity of toggling change?"
Practice
Q of a T flip-flop when the input T is 1 at the rising edge of the clock?Solution
Step 1: Understand T flip-flop behavior
A T flip-flop toggles its outputQwhenTis 1 at the clock's rising edge.Step 2: Apply the toggle rule
SinceTis 1,Qchanges from 0 to 1 or 1 to 0.Final Answer:
The output Q toggles (changes from 0 to 1 or 1 to 0). -> Option AQuick Check:
T=1 causes toggle = A [OK]
- Thinking output resets when T=1
- Assuming output stays the same always
- Confusing T flip-flop with D flip-flop
rst and clock clk?Solution
Step 1: Identify asynchronous reset and clock edge
Asynchronous reset triggers on positive edge ofrst, clock triggers on positive edge ofclk.Step 2: Write sensitivity list accordingly
Sensitivity list must includeposedge clkandposedge rstfor asynchronous reset.Final Answer:
always @(posedge clk or posedge rst) -> Option AQuick Check:
Async reset and posedge clock = A [OK]
- Using level-sensitive instead of edge-sensitive triggers
- Mixing posedge and negedge incorrectly
- Omitting reset from sensitivity list
Q after two rising edges of clk if T=1 and rst=0 initially?module t_ff(input clk, rst, T, output reg Q);
always @(posedge clk or posedge rst) begin
if (rst) Q <= 0;
else if (T) Q <= ~Q;
end
endmoduleSolution
Step 1: Analyze behavior on each clock edge
At each rising edge, sinceT=1andrst=0,Qtoggles.Step 2: Calculate after two toggles
Two toggles meanQchanges twice, returning to its initial value.Final Answer:
Q toggles twice, so final Q = initial Q -> Option BQuick Check:
Two toggles return Q to initial = B [OK]
- Assuming Q toggles only once after two clocks
- Forgetting reset is zero so no reset occurs
- Confusing toggle with set or reset
always @(posedge clk or posedge rst) begin
if (rst == 1'b1)
Q = 0;
else if (T == 1'b1)
Q = ~Q;
endSolution
Step 1: Check assignment types in sequential always block
Sequential logic inside always @(posedge clk ...) must use non-blocking assignments<=.Step 2: Identify the error
The code uses blocking assignments=, which can cause simulation mismatches.Final Answer:
Using blocking assignments = instead of non-blocking <= inside always block -> Option CQuick Check:
Sequential logic needs non-blocking <= = D [OK]
- Using blocking = instead of non-blocking <=
- Adding negedge clk unnecessarily
- Miswriting reset condition syntax
T for each flip-flop to achieve this?Solution
Step 1: Calculate number of flip-flops for divide by 4
Divide by 4 requires 2^n = 4, so n = 2 flip-flops. A chain of 2 toggling flip-flops achieves frequency divide by 4.Step 2: Consider toggle input for frequency division
Each T flip-flop must toggle (T=1) on its clock input. The first flip-flop toggles on main clock, the next toggles on previous flip-flop output.Step 3: Confirm the configuration
2 T flip-flops chained with T=1 for each achieves divide-by-4.Final Answer:
2 T flip-flops, each with T=1 to toggle on every clock edge -> Option DQuick Check:
Divide by 4 needs 2 toggling T flip-flops = C [OK]
- Using wrong number of flip-flops for division
- Setting T=0 so flip-flops don't toggle
- Confusing divide by 4 with divide by 8
Ready to prove it?
Pass the test and this pattern moves unknown
