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:
Q2:
D Flip-Flop:
Design code:
module d_flip_flop (
input wire D, // Data input
input wire clk, // Clock input
input wire rst, // Reset input (active high)
output reg Q // Output
);
always @(posedge clk or posedge rst) begin
if (rst)
Q <= 1'b0; // Reset output to 0
else
Q <= D; // Capture D on clock edge
end
endmodule
Testbench code:
module tb_d_flip_flop;
reg D, clk, rst; // Testbench signals
wire Q; // Output wire
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 tb_t_flip_flop;
module t_flip_flop (
reg T, clk, rst; // Testbench signals
input wire T, // Toggle input
wire Q; // Output wire
input wire clk, // Clock input
input wire rst, // Reset input (active high)
t_flip_flop DUT (
output reg Q // Output
.T(T),
);
.clk(clk),
always @(posedge clk or posedge rst) begin
.rst(rst),
if (rst)
.Q(Q)
Q <= 1'b0; // Reset output to 0
);
else if (T)
initial begin
Q <= ~Q; // Toggle output
clk = 0;
end
forever #5 clk = ~clk;
endmodule
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
$monitor("clk = %b | T = %b | rst = %b | Q = %b", clk, T, rst, Q);
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 (
input wire J, // J input
input wire K, // K input
testbench code: