How to Build a Sequence Counter in Verilog

Written by

in

To build a sequence counter in Verilog, you must determine whether you want to implement a standard sequential counter (like an up/down binary counter) or a custom non-binary sequence counter (like an arbitrary sequence of numbers or a Finite State Machine). The implementation requires a clocked always block, an explicit reset mechanism, and non-blocking assignments (<=) to accurately model hardware flip-flops. Scenario 1: Standard Binary Sequence Counter

A standard binary sequence counter increments (or decrements) by a fixed step on every active clock edge.

Here is the implementation of a 4-bit synchronous up/down counter with an asynchronous active-high reset:

module binary_sequence_counter ( input wire clk, // Clock signal input wire reset, // Asynchronous active-high reset input wire up_down, // Control signal: 1 for up, 0 for down output reg [3:0] count // 4-bit output sequence (0 to 15) ); // Clock-driven block triggered on positive edges always @(posedge clk or posedge reset) begin if (reset) begin count <= 4’b0000; // Reset condition forces sequence back to 0 end else begin if (up_down) begin count <= count + 1’b1; // Counts up: 0, 1, 2, …, 15, 0 end else begin count <= count - 1’b1; // Counts down: 15, 14, 13, …, 0, 15 end end end endmodule Use code with caution. Scenario 2: Custom / Arbitrary Sequence Counter

If your design requires a custom, non-sequential sequence (for example, cycling exclusively through values 1 -> 5 -> 3 -> 7 -> 0 -> repeat), you should implement a two-block Finite State Machine (FSM) style design. This separates state tracking logic from the output mapping.

module custom_sequence_counter ( input wire clk, input wire reset, output reg [2:0] out_sequence // 3-bit output holding the custom numbers ); // Define internal state variables to track position in the sequence reg [2:0] state; // Sequential block: Tracks the state machine steps always @(posedge clk or posedge reset) begin if (reset) begin state <= 3’d0; end else begin if (state >= 3’d4) begin state <= 3’d0; // Loop back to the start of the sequence end else begin state <= state + 1’b1; // Advance to next position end end end // Combinational block: Maps the sequence index to custom values always @(*) begin case (state) 3’d0: out_sequence = 3’d1; // First number in sequence 3’d1: out_sequence = 3’d5; // Second number 3’d2: out_sequence = 3’d3; // Third number 3’d3: out_sequence = 3’d7; // Fourth number 3’d4: out_sequence = 3’d0; // Fifth number default: out_sequence = 3’d0; endcase end endmodule Use code with caution. Critical Guidelines for Reliable Hardware Design

Use Non-Blocking Assignments (<=): Always use <=, not =, inside your edge-triggered always @(posedge clk) sequential blocks to prevent race conditions and correctly infer registers.

Implement a Reset State: Registers power up in undefined states on real hardware chips like FPGAs; an explicit reset ensures a predictable initial sequence window.

Define Vector Sizes Explicitly: Ensure numbers are correctly defined with their width (e.g., 4’b0000 or 3’d5) to prevent unexpected bit truncation and compiler warnings. Verification: Writing a Testbench

To confirm your sequence logic executes correctly, create a testbench to inject a clock stream and verify output transitions.

`timescale 1ns / 1ps module tb_sequence_counter; reg clk; reg reset; wire [2:0] out_sequence; // Instantiate your sequence counter unit under test (UUT) custom_sequence_counter uut ( .clk(clk), .reset(reset), .out_sequence(out_sequence) ); // Generate a continuous clock signal with a 10ns period always #5 clk = ~clk; initial begin // Initialize lines clk = 0; reset = 1; #15; // Release reset to start the sequence counter reset = 0; // Let the sequence cycle multiple times #100; $finish; // End the simulation run end endmodule Use code with caution. If you want to tailor this further, please tell me:

What specific sequence of numbers your counter needs to output (e.g., binary up, Gray code, or pseudo-random)?

Whether your design prefers a synchronous or asynchronous reset architecture? Counter with repeated states – verilog – Stack Overflow

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *