# Unable to understand Verilog syntax

I found an example Verilog code as following:

module test #(parameter p=1) ();
localparam [1:0] lp = ~(p)'(1'b0);
endmodule


I'm unable to undestand the localparam lp assignment. Can you please explain the code?

Very strange example, not sure what this example was intended to do. There's only the module declaration and a localparam declaration, nothing else.

Defines a module which is named test, which has no input nor output ports (because the port list is an empty set of parenthesis). However it does take a single parameter named p (this is inside the hash-parenthesis list #(p) part of the module declaration ). The default value of the parameter p is 1 unless otherwise specified.

Inside the definition of the module, there is another parameter declared, which is named lp, which is defined as the constant expression ~(p)'(1'b0).

The unary ~ is the bitwise negation operator, and the literal integer expression (p)'(1'b0) is a constant that is "p" number of bits wide, and all of the bits are 0. So ~(p)'(1'b0) is all bits 1. Note in verilog, we always care exactly about the bit width of every constant, wire, and net; literal constants use that infix apostrophe ' to indicate the bit width.

This is a very strange example, because with no ports and nothing making any use of the parameters, there's nothing for a simulator or an HDL compiler to do.

Usually verilog modules have input and output ports, with the exception of a test bench that is used to simulate and test other modules. But that's not the case here, because this module doesn't instantiate any other modules -- it literally does nothing.

• I have just given a code snippet. Now, I have the clear understanding of the localparam assignment from your explanation. It's to set 'p' number of bits of lp to 1. – codedoc Aug 24 '15 at 8:06
• BTW, this code will not work if P==0 because it is illegal to have a 0-width expression. See section 6.24.1 Casting Operator in the 1800-2012 LRM. A better way to write this is lp = (2**p)-1 – dave_59 Aug 24 '15 at 14:39
• Also, I believe that some verilog parsers do not allow expressions in front of the apostrophe and are limited to literal constants. – mattgately Aug 26 '15 at 0:31