Showing posts with label Sequential Logic. Show all posts
Showing posts with label Sequential Logic. Show all posts

April 5, 2026

Mastering Verilog: Implementing a SR Flip Flop

Welcome to another edition of our Verilog series! In this blog post, we’ll explore the implementation of an SR Flip-Flop in Verilog. The SR (Set-Reset) Flip-Flop is one of the simplest sequential circuits used to store a single bit of data.

It has two inputs, S (Set) and R (Reset), which control the output state. This flip-flop forms the basic foundation for more advanced flip-flops like JK and D Flip-Flops.

Below is the Verilog code for an SR Flip-Flop, implemented using a Behavioral Modeling approach:

๐Ÿ“Š Block Diagram

In the behavioral modeling approach, we use conditional logic inside a clock-triggered block to define the output behavior.

module sr_flip_flop(input clk, input S, input R, output reg Q);

always @(posedge clk)
begin
  case ({S, R})
    2'b00: Q <= Q;     // Hold
    2'b01: Q <= 1'b0;  // Reset
    2'b10: Q <= 1'b1;  // Set
    2'b11: Q <= 1'bx;  // Invalid state
  endcase
end

endmodule

๐Ÿงช Testbench

module tb_top;
  reg clk, S, R;
  wire Q;
  
  sr_flip_flop srff(clk, S, R, Q);
  
  initial begin
    clk = 0;
    forever #5 clk = ~clk;
  end
  
  initial begin
    $monitor("At time %0t: S=%b R=%b Q=%b", $time, S, R, Q);
    
    S = 0; R = 0; #10;
    S = 1; R = 0; #10;
    S = 0; R = 1; #10;
    S = 1; R = 1; #10;
    
    #10 $finish;
  end
endmodule

Explanation:

  • When S = 0 and R = 0, the output remains unchanged (hold condition).
  • When S = 1 and R = 0, the flip-flop sets (Q = 1).
  • When S = 0 and R = 1, the flip-flop resets (Q = 0).
  • When S = 1 and R = 1, it results in an invalid state.
  • The always @(posedge clk) block ensures edge-triggered operation.

Conclusion

This Verilog implementation of an SR Flip-Flop demonstrates the basic storage mechanism in sequential circuits. While simple, it highlights the importance of avoiding invalid input conditions in digital design.

What’s Next?

Now that you’ve explored SR, D, JK, and T Flip-Flops, try comparing their behavior and applications. In the next post, we’ll move toward registers and counters.

Happy Coding! ๐Ÿš€

April 4, 2026

Mastering Verilog: Implementing a JK Flip Flop

Welcome to another edition of our Verilog series! In this blog post, we’ll explore the implementation of a JK Flip-Flop in Verilog. The JK Flip-Flop is an advanced version of the SR Flip-Flop that eliminates the invalid state and provides more flexibility in sequential circuit design.

Depending on the inputs J and K, the output can set, reset, hold, or toggle, making it highly useful in counters and control circuits.

Below is the Verilog code for a JK Flip-Flop, implemented using a Behavioral Modeling approach:

๐Ÿ“Š Block Diagram

In the behavioral modeling approach, we use conditional statements inside a clock-triggered block to define the flip-flop behavior.

module jk_flip_flop(input clk, input J, input K, output reg Q);

always @(posedge clk)
begin
  case ({J, K})
    2'b00: Q <= Q;     // No change
    2'b01: Q <= 1'b0;  // Reset
    2'b10: Q <= 1'b1;  // Set
    2'b11: Q <= ~Q;    // Toggle
  endcase
end

endmodule

๐Ÿงช Testbench

module tb_top;
  reg clk, J, K;
  wire Q;
  
  jk_flip_flop jkff(clk, J, K, Q);
  
  initial begin
    clk = 0;
    forever #5 clk = ~clk;
  end
  
  initial begin
    $monitor("At time %0t: J=%b K=%b Q=%b", $time, J, K, Q);
    
    J = 0; K = 0; #10;
    J = 0; K = 1; #10;
    J = 1; K = 0; #10;
    J = 1; K = 1; #10;
    J = 1; K = 1; #10;
    
    #10 $finish;
  end
endmodule

Explanation:

  • When J = 0 and K = 0, the output remains unchanged.
  • When J = 0 and K = 1, the flip-flop resets (Q = 0).
  • When J = 1 and K = 0, the flip-flop sets (Q = 1).
  • When J = 1 and K = 1, the output toggles.
  • The always @(posedge clk) block ensures edge-triggered operation.

Conclusion

This Verilog implementation of a JK Flip-Flop demonstrates how multiple operations like set, reset, hold, and toggle can be achieved within a single sequential circuit. It is widely used in designing counters and control logic.

What’s Next?

Try implementing counters using JK Flip-Flops and observe state transitions. In the next post, we’ll explore more advanced sequential circuits and their Verilog implementations.

Happy Coding! ๐Ÿš€

Mastering Verilog: Implementing a T Flip Flop

Welcome to another edition of our Verilog series! In this blog post, we’ll explore the implementation of a T Flip-Flop in Verilog. A T Flip-Flop (Toggle Flip-Flop) is a sequential circuit that changes its state whenever the input T is high and a clock edge occurs.

It is commonly used in counters and frequency division circuits, where toggling behavior is required.

Below is the Verilog code for a T Flip-Flop, implemented using a Behavioral Modeling approach:

๐Ÿ“Š Block Diagram

In the behavioral modeling approach, we use clock-triggered logic to toggle the output based on the input condition.

module t_flip_flop(input clk, input T, output reg Q);

always @(posedge clk)
begin
  if (T)
    Q <= ~Q;
end

endmodule

๐Ÿงช Testbench

module tb_top;
  reg clk, T;
  wire Q;
  
  t_flip_flop tff(clk, T, Q);
  
  initial begin
    clk = 0;
    forever #5 clk = ~clk;
  end
  
  initial begin
    $monitor("At time %0t: T=%b Q=%b", $time, T, Q);
    
    T = 0; #10;
    T = 1; #10;
    T = 1; #10;
    T = 0; #10;
    
    #10 $finish;
  end
endmodule

Explanation:

  • The T Flip-Flop toggles its output when T = 1 at the rising edge of the clock.
  • If T = 0, the output remains unchanged.
  • The always @(posedge clk) block ensures edge-triggered behavior.
  • Non-blocking assignment (<=) is used for sequential logic.
  • The testbench verifies the toggling behavior under different input conditions.

Conclusion

This Verilog implementation of a T Flip-Flop demonstrates how toggling behavior can be achieved using sequential logic. It is widely used in designing counters and frequency dividers in digital systems.

What’s Next?

Try implementing a JK Flip-Flop or counters using this T Flip-Flop. In the next post, we’ll explore more sequential circuits and their Verilog implementations.

Happy Coding! ๐Ÿš€

April 3, 2026

Mastering Verilog: Implementing a D Flip-Flop

Welcome to another edition of our Verilog series! In this blog post, we’ll explore the implementation of a D Flip-Flop in Verilog. A D Flip-Flop is a fundamental sequential circuit used to store a single bit of data and is widely used in registers, memory elements, and synchronous systems.

It captures the input value (D) on the rising edge of the clock and holds it until the next clock event.

Below is the Verilog code for a D Flip-Flop, implemented using a Behavioral Modeling approach:

๐Ÿ“Š Block Diagram

In the behavioral modeling approach, we use clock-triggered always blocks to define how data is stored.

module d_flip_flop(input clk, input D, output reg Q);

always @(posedge clk)
begin
  Q <= D;
end

endmodule

๐Ÿงช Testbench

module tb_top;
  reg clk, D;
  wire Q;
  
  d_flip_flop dff(clk, D, Q);
  
  initial begin
    clk = 0;
    forever #5 clk = ~clk;
  end
  
  initial begin
    $monitor("At time %0t: D=%b Q=%b", $time, D, Q);
    
    D = 0; #10;
    D = 1; #10;
    D = 0; #10;
    D = 1; #10;
    
    #10 $finish;
  end
endmodule

Explanation:

  • The D Flip-Flop stores the input value (D) on the rising edge of the clock.
  • The always @(posedge clk) block ensures edge-triggered behavior.
  • Non-blocking assignment (<=) is used for sequential logic.
  • The output Q updates only on the clock edge, not immediately with input changes.
  • The testbench generates a clock signal and verifies different input conditions.

Conclusion

This Verilog implementation of a D Flip-Flop demonstrates how sequential circuits store and synchronize data using a clock signal. It is a key building block in digital design and forms the basis of registers and memory systems.

What’s Next?

Try adding features like reset or enable to this flip-flop to explore more advanced sequential designs. In the next post, we’ll dive deeper into sequential circuits and their Verilog implementations.

Happy Coding! ๐Ÿš€

March 24, 2024

Mastering Verilog: Part 4 — Understanding ‘assign’, ‘always’ and ‘initial’ keywords.

 In our journey through Verilog, we’ve delved into various aspects like operators, modules, and data types. Now, let’s explore three fundamental keywords in Verilog that play crucial roles in describing behavior, initializing values, and creating continuous assignments: assign, always, and initial.

1. assign: Continuous Assignments

  • The assign keyword in Verilog is used to create continuous assignments. These assignments describe combinational logic, where the output value is continuously updated based on the input values without any notion of time or sequence.
  • Unlike procedural constructs like always and initial blocks, continuous assignments operate without a sensitivity list, specifically catering to the needs of combinational logic rather than sequential logic.
  • Here’s how the assign keyword works:

Syntax:

assign <output_variable> = <expression>;

  1. <output_variable>: The output or net that the assignment drives.
  2. <expression>: The logic expression or value assigned to the output.
  • Here’s a simple example to illustrate:

module ContinuousAssignmentExample(
input wire a,
input wire b,
output wire y
);
assign y = a & b; // Continuous assignment: y is the AND of a and b
endmodule

  • In this example, y is assigned the result of the AND operation between inputs a and b continuously. Any change in a or b will immediately reflect in y.

Example:

module CombinationalLogicExamples(
input wire a, b, enable, sel,
input wire [3:0] inputs,
input wire data_in,
output reg y1, y2, y3, y4, y5, y6, y7, y8,
);

// Example 1: AND gate using assign
assign y1 = a & b;

// Example 2: Multiplexer using assign
assign y2 = (sel) ? b : a;

// Example 3: Bitwise XOR using assign
assign y3 = a ^ b;

// Example 4: Bitwise NOT using assign
assign y4 = ~a;

// Example 5: Conditional assignment using assign
assign y5 = (enable) ? data_in : 0;

// Example 6: OR gate using assign
assign y6 = a | b;

// Example 7: NAND gate using assign
assign y7 = ~(a & b);

// Example 8: Tri-state buffer using assign
assign y8 = (enable) ? data_in : 1'bz;

endmodule

This module CombinationalLogicExamples includes examples of various combinational logic circuits implemented using the assign keyword in Verilog. It showcases AND gates, multiplexers, bitwise operations, conditional assignments, logic gates, and a tri-state buffer, all driven by continuous assignments.

2. always: Procedural Continuous Behavior

  • In Verilog, an always block is used to describe behavior that is sensitive to certain events or conditions.
  • It is commonly used for modeling sequential logic elements such as flip-flops, registers, state machines, and other sequential circuits.
  • These blocks describe behavior that occurs continuously in a simulation.
  • There are different types of always blocks based on sensitivity lists:
  1. always @(*): This is used for combinational logic, where the block is executed whenever any of its inputs change.
  2. always @(posedge clk): This is used for sequential logic triggered by the positive edge of a clock signal.
  • Here’s an example of how an always block is used in Verilog:

module SequentialLogicExample(
input wire clk, reset,
input wire [3:0] data_in,
output reg [3:0] data_out
);

// Register declaration
reg [3:0] reg_data;

// Sequential logic using always block
always @(posedge clk or posedge reset) begin
if (reset) begin
// Reset condition: initialize register to 0
reg_data <= 4'b0000;
end else begin
// Positive edge of clock: update register with input data
reg_data <= data_in;
end
end

// Output assignment
assign data_out = reg_data;

endmodule

Explanation:

  • The always block is triggered by the positive edge of the clk signal or the positive edge of the reset signal.
  • Inside the always block, there is an if-else statement that handles two conditions:
    1. When reset is high, the register reg_data is initialized to 4'b0000.
    2. When the positive edge of clk occurs (and reset is not high), the register reg_data is updated with the input data_in.
  • The assign statement outside the always block assigns the value of reg_data to the output data_out.

This example demonstrates how an always block is used to model sequential logic in Verilog. The sensitivity list (posedge clk or posedge reset) specifies the events that trigger the execution of the block.

3. initial: Initializing Values

  • In Verilog, an initial block is used to initialize variables, registers, or memory elements at the start of simulation. It executes only once when the simulation begins or when the module is instantiated.
  • It is particularly useful for setting up initial conditions before simulation begins.
  • Here’s an example showcasing the initial block:

module InitialExample(
input wire clk,
output reg q
);
reg [3:0] counter; // Declare a register for counting

initial
begin
counter = 4'b0000; // Initialize counter to 0 at simulation start
end

always @(posedge clk)
begin
if (counter == 4'b1010)
counter <= 4'b0000; // Reset counter at specific value
else
counter <= counter + 1; // Increment counter otherwise
end

always @(posedge clk)
q <= counter[2]; // Output the third bit of the counter
endmodule

Explanation:

  • The module InitialExample has an input clock clk and an output q representing the third bit of a counter.
  • Inside the module, a reg type variable counter of size 4 bits is declared to count from 0 to 9. The initial block is used to initialize counter to 4'b0000 (0) at the start of simulation.
  • The first always block triggers on the positive edge of the clock clk. It checks if counter reaches the value 4'b1010 (10 in decimal) and resets counter to 4'b0000 if so. Otherwise, it increments counter by 1.
    The second always block also triggers on the positive edge of clk and assigns the value of the third bit (counter[2]) of the counter to the output q.

This example demonstrates the use of the initial block for setting initial values and always blocks for sequential logic and output generation in Verilog.

Conclusion:

In this journey through Verilog, we’ve explored essential keywords that form the backbone of behavioral modeling and initialization in Verilog HDL. The assign keyword enables continuous assignments, perfect for describing combinational logic without time dependencies. On the other hand, the always block embodies procedural continuous behavior, making it ideal for modeling sequential logic elements such as flip-flops and registers. Lastly, the initial block serves as the foundation for initializing variables, registers, or memory elements at the simulation’s outset, ensuring the system starts with the desired initial conditions.

Understanding these keywords not only enhances our Verilog coding skills but also equips us to design and simulate complex digital systems effectively. Whether it’s creating continuous logic, modeling sequential circuits, or setting up initial states, mastering these Verilog keywords is essential for FPGA design and digital circuit implementation.

Happy coding and exploring the vast world of Verilog!

Like, Share and Follow me if you like my content.
Thank You.

Explore Our Topics!

Check out the extensive list of topics we discuss:  Tech and AI Blogs Communication Protocols: -  USB   - RS232   -  Ethernet   -  AMBA Prot...