Assignment 3
Assignment 3
Q1:
Working Principle of Basic Flip-Flops Using FSM Modelling
Finite State Machines (FSMs) are a mathematical way to model the behaviour of flip-
flops. A flip-flop is a bistable device, meaning it has two stable states (0 and 1), and it
changes state based on input and a clock signal.
FSM Modelling:
Working Principle:
• The T flip-flop toggles its output state Q whenever the T input is 1, on the triggering
clock edge.
• If T=0, the output Q remains unchanged.
FSM Modelling:
State diagram:
FSM Modelling:
State diagram:
3. Transitions:
State table:
Current state Input J Input k Next state Qn Output Q
0 0 0 0 0
0 0 1 0 0
0 1 0 1 1
0 1 1 1 1
1 0 0 1 1
1 0 1 0 0
1 1 0 1 1
1 1 1 0 0
Q2:
D Flip-Flop:
Design code:
module d_flip_flop (
);
if (rst)
else
end
endmodule
Testbench code:
module tb_d_flip_flop;
d_flip_flop DUT (
.D(D),
.clk(clk),
.rst(rst),
.Q(Q)
);
// Clock generation
initial begin
clk = 0;
end
// Stimulus
initial begin
rst = 1; D = 0;
#10 rst = 0; D = 1;
#10 D = 0;
#10 D = 1;
#10 $finish;
end
// Monitor signals
initial begin
end
endmodule
Output:
Explanation:
The output Q follows the input D at every rising edge of the clock signal (clk), as long as the
reset (rst) is not active. If reset is active, Q is set to 0.
If reset is not active, and clock is high the Output Q equals input D.
b. T flip-flop
design code:
module t_flip_flop (
);
if (rst)
else if (T)
end
endmodule
`timescale 1ns / 1ps
module tb_t_flip_flop;
t_flip_flop DUT (
.T(T),
.clk(clk),
.rst(rst),
.Q(Q)
);
initial begin
clk = 0;
end
initial begin
rst = 1; T = 0;
#10
rst = 0; T = 1;
#10 T = 0;
#10 T = 1;
#10 T = 1;
#10
$finish;
end
initial begin
end
endmodule
Fig1. (monitor output output)
Explanation:
The output Q toggles its state at each rising clock edge when T=1. If T=0, Q remains unchanged.
Reset (rst) forces Q to 0 when active.
If reset is low and clock is high and T = 1 the output toggles, if output Q = 0 it toggles to 1 and if
the output Q = 1 it toggles to 0.
c. JK Flip-flop:
design code:
module jk_flip_flop (
);
if (rst)
else begin
endcase
end
end
endmodule
testbench code:
module tb_jk_flip_flop;
jk_flip_flop DUT (
.J(J),
.K(K),
.clk(clk),
.rst(rst),
.Q(Q)
);
initial begin
clk = 0;
end
initial begin
rst = 1; J = 0; K = 0;
#10 J = 0; K = 1; // Reset
#10 J = 1; K = 1; // Toggle
#10 J = 0; K = 0; // No change
#10 $finish;
end
initial begin
end
endmodule
Output:
Explanation:
When both J=1 and K=1, Q toggles its state on the rising clock edge. For other J and K
combinations, Q either sets, resets, or holds its previous state. If reset (rst) is active, Qs reset to
0.
If clock is high, reset is low and j = 1, k = 0, the output Q=1, if j = 0, k=1, the output is Q=0.