Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:56:26+00:00 2026-06-11T02:56:26+00:00

I am coding in Verilog a typical count-to-n-then-reset-to-0 counter. My module has the logic

  • 0

I am coding in Verilog a typical count-to-n-then-reset-to-0 counter. My module has the logic to increment and reset the counter.

My issue is that I don’t know where the counter itself should be defined.

I could pass the counter (as inout?) to the module. That’s ok, but the counter still has to be defined somewhere so it this doesn’t do me any good.

Nothing else except this module should touch the counter, so I’d like to have the counter created within this module, and not passed in or out.

Is this reasonably standard, and if so, will someone point to a reference please on how to instantiate the counter?

Here’s the code. As far as I can tell, it works. I haven’t implemented DIR == REVERSE yet.

Couple of interesting gotchas. The (now commented out) STEPPER=0 line was causing an error in a schematic; it thought that STEPPER was tied to ground as well as other logic.

Also, I use = instead of <= in some places involving counter – I was getting timing problems (I suppose.) The procedural assignment removed (hid?) the problem.

module cam(
    input [7:0] DIVISOR,
    input DIR,
    input SPINDLE,
    output reg STEPPER
    );

     parameter FORWARD = 1'b1;
     parameter REVERSE = !FORWARD;
     
     reg[7:0] counter = 0;
        
    always @(posedge SPINDLE) begin
    //  STEPPER = 0;
        if (DIR == FORWARD) begin
            counter = counter + 1;
            if (counter == DIVISOR) counter = 0;
            end
        else begin
        //  counter <= counter - 1;
        //  if (counter == (-1)) counter <= DIVISOR;
            end
    end
    
    always @(negedge SPINDLE) begin
        STEPPER = (counter == 0) ? 1 : 0;
    end

endmodule
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:56:27+00:00Added an answer on June 11, 2026 at 2:56 am

    It should just be defined as a register within the module. Here’s an example from some of my code.

    module trigger(clk, rxReady, rxData, txBusy, txStart, txData);
    input clk;
    input [7:0] rxData;
    input rxReady;
    input txBusy;
    output reg txStart;
    output reg[7:0] txData;
    
    integer count81; // Number of cells received over serial (start solving after 81)
    reg[8:0] data[0:8][0:8];
    
    integer state;
    
    always @(posedge clk) begin
        case (state)
            read:
                if (rxReady) begin
                    data[count81 % 9][count81 / 9] = rxData ? 1<<(rxData-1) : -1;
                    if (count81 < 80) count81 <= count81 + 1;
                    else begin
                        count81 <= 0;
                        state <= solving;
                    end
                end
            // etc....                 
        endcase
    end
    endmodule
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read Nonblocking Assignments in Verilog Synthesis, Coding Styles that Kill! by Clifford
I have read Nonblocking Assignments in Verilog Synthesis, Coding Styles that Kill! by Clifford
I coding a PHP script that has to check if exists a GitHub repo
Coding Problem: Converting a Asp.Net GridView that has controls back into a data table
Coding HTML5 page for use in iPad Mobile Safari that has tag for embedding
Coding footer naively, if there's not enough content, then there will be empty space
The coding like decimal prodprice = Convert.ToDecimal(NavigationContext.QueryString[Price]); then what should i put for Binary
Im coding in Java.. Does anyone know how i can get the content of
A coding style presentation that I attended lately in office advocated that variables should
Iam coding a dating application for facebook. The application has to have a standalone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.