1

During parameter overriding, the parameter my_secret is getting overridden by 2.3.4.5. I want to impose a condition that overrides my_secret to 2 for count = 0 to 10, my_secret to 3 for count = 10 to 20, my_secret to 4 for count = 20 to 30, my_secret to 5 for count = 40 to 50 ?

Test bench:

module tb_def_param;

    // Inputs
    reg CLK;
    reg RST;
    reg [4:0] a;
    reg [4:0] b;
   reg  [5:0] count;
    parameter my_secret = 4'd0;
wire [6:0] sum;

    initial 
    begin
        // Initialize Inputs
        a = 0;
        b = 0;
        CLK = 1;
        RST = 1; 

        // Wait 100 ns for global reset to finish
        #100 RST = 1;
      #100 RST = 0; 

        // Add stimulus here
        a = 5'd2;
        b = 5'd3;
    end

always @ (posedge CLK)
   begin
    if(RST)
        count <= 6'd0;
        else 
        count <= count + 1;
    end 

secret_number #(2) U0(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum));
secret_number #(3) U1(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum));
secret_number #(4) U2(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum));
secret_number #(5) U3(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum));

always
    begin
         #10 CLK <= ~CLK;
    end 

endmodule

main module is:

 module secret_number(CLK,RST,a,b,sum); 
 input CLK, RST;
 input [4:0]a,b;
 output [6:0]sum;
 reg [6:0] sum;
 parameter my_secret = 0;
 always@(posedge CLK)    
   sum = a + b + my_secret;
     initial begin
     $display("%d", my_secret);
            end
     endmodule 

Is that possible ???

1 Answer 1

1

If I understand you correctly, you want to use count to determine which value of my_secret is used in computing sum. You have most of what you need in your testbench module now, you are just missing is some logic to select between the resulting values from your various secret_number modules. You currently have all of those modules driving the same vector sum but they should really be driving separate vectors and then using combinational logic to select the desired result:

wire [6:0] sum2, sum3, sum4, sum5;
reg [6:0] sum;
secret_number #(2) U0(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum2));
secret_number #(3) U1(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum3));
secret_number #(4) U2(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum4));
secret_number #(5) U3(.CLK(CLK), .RST(RST), .a(a),.b(b),.sum(sum5));

always @(*) begin
  if (count <= 10) begin // Not sure if you want inclusive of 10, if not, do <
    sum = sum2;
  end
  else if (count <= 20) begin
    sum = sum3;
  end
  else if (count <= 30) begin
    sum = sum4;
  end
  else
    sum = sum5;
  end
end

However, you can also have my_secret be a input to the module rather than a parameter unless you need to use the my_secret = 2 block when count is greater than 10 for something else. It will save a good amount of area if you do it as an input instead.

(Now also, inside the module it should be sum <= a + b + my_secret, use NBA (<=) for clocked blocks)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your comment. Sum is of "wire" type, it cannot be declared under always block. Any suggestions to over come this issue ?
Youre right; sorry; change sum to be a reg and it will work (Ill edit the above)
That's the issue there ... sum should be an output, i.e it should be a wire type.
And why is that a problem? Outputs can be declared type reg
Thanks a lot. It was greatly helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.