0

I have an input logic sequence and I would like to convert it to a parameter in order to add it elsewhere in my program.

For example,

module myModule(input logic[7:0] SW, output logic[7:0] LEDR);

     parameter shift = SW;
     assign LEDR = SW[shift + 1: shift];

endmodule

I know that's not correct syntax, I just wanted to get the main idea.

Developer
  • 341
  • 3
  • 19

2 Answers2

2

Parameters are by definition compile time constants. That means you can not change their value based on an expression that can change over time.

What you can do is change the way you model so it does not require a parameter. For example , you could write your code as

module myModule(input logic[7:0] SW, output logic[7:0] LEDR);
     assign LEDR = SW[SW +: 2];
endmodule
dave_59
  • 39,096
  • 3
  • 24
  • 63
0

You cannot convert a variable to parameter. The value of the parameter will be locked after elaborations. A variable will not have a value until simulation.

Part-select (sometimes called range-slice) should do what you need. See Indexing vectors and arrays with +: for more info.

Having SW slice itself does make sense since the resulting value would always be 0. Here is better example:

module myModule(input [8:0] IN, input [2:0] SW, output [1:0] LEDR);
     assign LEDR = IN[SW +: 2];
endmodule
Community
  • 1
  • 1
Greg
  • 18,111
  • 5
  • 46
  • 68
  • Thank you, it solved my problem. A question though, how come this is allowed? I thought that whatever was in the index bracket `$IN[SW+:2];` had to be a constant? And SW can change at runtime. – Developer Dec 02 '16 at 22:16
  • Both sides need to be a constant when using `:`. `+:` and `-:` are different, the left case can be a variable (starting index), the right side must still be a constant (width and direction). It is explained in the IEEE documentation, citation and link to the documentation are in the link I put in my answers – Greg Dec 03 '16 at 01:13
  • Ohhhhh okay I see, so the overall selection must be constant, thank you. – Developer Dec 04 '16 at 02:07