Simple question
If I need to use 4 8-bit numbers, I would declare the following reg.
reg [7:0] numbers [3:0]
I'm quite confused about the difference between the first and second declaration ([7:0] and [3:0]). In what order should they come? Does first one stay for the size of a number while the second is for the number of numbers or vice versa? And is [7:0] or [0:7] give the right order?
Thanks in advance.
EDIT:
Ordinary arrays of numbers look like this, for example
0000
0110
0001
There are three 4-bit numbers (0000, 0110, 0001). We can access them by using array indices. So, accessing the first digit of the second number is done by something like this
a[0][1]
assuming that this array is stored in a variable a.
Returning to Verilog, how would accessing elements change if I would swap values in reg or declare them in reverse order ([0:7]), for example?
Yes, that syntax can be used to declare 4 8-bit numbers, however it is more conventional for 0 to be left of the colon for the number of words:
reg[7:0]
is an 8-bit "register", or variablereg[7:0] numbers[3:0]
is a 1-D array with 4 elements, namednumbers
, each of which is an 8-bit registernumbers
is accessed asnumbers[index]
numbers[i][j]
is a bit-select ofnumbers[i]
. It accesses bitj
in thei
th element ofnumbers
[lsb:msb]
, but there's no good reason for this.When assigning two objects, bits are copied left-to-right, as for VHDL.
Verilog has (very) poor checking of bit and part selects and array indexes. See the code below.
The dimensions to the right are called unpacked dimensions and those to the left are packed. I found this over the net as an easy way to remember for accessing elements. "Left to right, starting with right". So you start with unpacked dimensions first. For accessing 8th bit of 3rd number in your declaration I would use numbers numbers[2][7]. This post has proper explanation.
If you use same convention for declaring and assigning values, I think it should be fine. Actually it is far simpler to eliminate the [:] and declare it as
reg [7:0] numbers [4];
giving four, 8 bit numbers.