Ilan Mermelstein
Ilan Mermelstein

Reputation: 27

Is it a bad practice to reset a variable in one model using a variable from another model?

I'm building a vending machine. I want the pay sum to maintain its value until the candy is out or canceled.

To simplify, is it ok to build a reset this way:

module m1(
  input clk,
  input m2_reset,
  output enough_payment);
   
  always@(posedge clk, posedge m2_reset) begin
    if (m2_reset) begin
        payment<=0;
        enough_payment<=0; end
    else if(payment<=CANDY_PRICE) begin //collect money while the payment is not enough
            payment<= coin_5*5 + coin_10*10; 
            enough_payment<=0; end 
         else // payment will be equal to CANDY_PRICE and maybe a little extra
            enough_payment<=1;  
  end
endmodule

module m2(
  input clk, enough_payment,
  output m2_reset);

  always@(posedge clk) begin
    if(enough_payment)
        if(counter==RANDOM_NUM)
           m2_reset<=1;
        else begin
           m2_reset<=0;
           counter<=counter+1; 
  end
endmodule

Upvotes: 1

Views: 46

Answers (1)

toolic
toolic

Reputation: 62163

Assuming you directly connect the m2_reset signals to each other at the upper level of the design hierarchy, this should be fine.

In m1, m2_reset is used as an asynchronous reset. Since it is a registered output of m2, there will be no glitches on it. That is good design practice.

Upvotes: 2

Related Questions