1
\$\begingroup\$

I am familiar to the syntax described in here

net_type [msb:lsb] list_of_net_identifiers;

reg [msb:lsb] list_of_register_identifiers;

For example, to declare the 32bits memory address, I could use the syntax like

reg [31:0] address;

Also to declare the memory comprised of the 16 elements which consist of 32 bits each,

reg [31:0] mem [15:0]; 

However, I could frequently face the syntax like below to declare multidimensional arrays.

reg [31:0] mem [0:15]

I've googled it and found one related article in here. It seems that whether the larger number comes first or not, the first number before the colon (:) is the MSB and the LSB follows. It seems like the difference comes from the preference of coding.

However, I think mixing [31:0] and [0:15] style is little bit count-intuitive and confusing even though it depends on the preference. I could see a lot of mixing especially in the declaration of multi-dimensional arrays. Why is it allowed to be used? Is there any advantage that I couldn't notice?

\$\endgroup\$
2
\$\begingroup\$

In Verilog, the numeric index ordering of a memory is only a matter of preference except when loading the memory from a text file. The first line in the file represents the left index, and like most other languages, starts at 0, so [0:15] is the more common way to declare a memory range. Otherwise there is no advantage one way or the other in how you declare a memory range since you canny only reference one memory element at a time.

The same can be said about a vector if you never plan to access individual bits of the vector. But once you do, how you address each bit becomes significant in position of an integral value.

In SystemVerilog, the ordering of both the vector width and memory dimensions becomes significant because you can stream memories as a whole and well as selects parts just like you do a vector.

\$\endgroup\$
  • \$\begingroup\$ Thanks for a nice answer. I've tried to figure out how the declaration affects the endianness of number stored in the memory by using code in here. To make sure that I understand correctly, the [0:7] or [7:0] declaration doesn't affect the way that synthesized gate store the number in BIG endian or LITTLE endian, but it only determines how to access each bytes with indices ([0:7]=> accessed like BIG_ENDIAN, [7:0]=> accessed like LITTLE_ENDIAN). Is it correct? \$\endgroup\$ – JaeHyuk Lee Feb 5 '18 at 6:17
  • \$\begingroup\$ Correct ------- \$\endgroup\$ – dave_59 Feb 5 '18 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.