4

Is it possible to parameterize a bit-field in verilog? Essentially I want to use a parameter or alternative to define a bit-range. The only way I can think of doing this is with a `define as shown below but it seems like there should be a better way.

`define BITFIELD_SELECT 31:28
foo = bar[BITFIELD_SELECT]

2 Answers 2

6

Parameters are nicer(safer) than defines since the namespace is not global to the project. You should be able to do this with two parameters.

parameter BITFIELD_HIGH = 31;
parameter BITFIELD_LOW = 28;

assign foo = bar[BITFIELD_HIGH:BITFIELD_LOW];

Alternatively

parameter BITFIELD_HIGH = 31;
localparam BITFIELD_LOW = BITFIELD_HIGH-3;

assign foo = bar[BITFIELD_HIGH:BITFIELD_LOW];
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I wasn't really a fan of the `define method, hence the question. Is this something you use in practice or is it just an answer to the questions asked?
I forget if I have ever defined the upper and lower bounds. Many times I have used a parameter to define the width of a vector. Ie: foo[(WIDTH-1):0]. It works great in practice and is very powerful combined with generate statements.
For some reason many developers are not aware of a convenient constant part select in Verilog-2001: assign foo = bar[HIGH -: SIZE];
0

If you use macros (define) include the "`" when call the macro

`define BITFIELD_SELECT 31:28
foo = bar[`BITFIELD_SELECT]; // `BITFIELD_SELECT

1 Comment

This is not incorrect, however macros are not parameters. Would it not be easier to have a parameter WIDTH and then something like: paramter WIDTH = 8; /*...*/ foo = bar[(WIDTH-1):0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.