Title here
Summary here
Arrays in Verilog are called “vectors” or “registers” depending on their use. They are commonly used in digital design for storing and manipulating data.
module arrays;
// Here we create a register 'a' that will hold exactly 5 4-bit values.
// The type of elements and length are both part of the register's declaration.
// By default, a register is initialized to 'x' (unknown) values.
reg [3:0] a [0:4];
integer i;
initial begin
// We can set a value at an index using the array[index] = value syntax
a[4] = 4'd10; // Set the 5th element (index 4) to decimal 10
// To print the contents of the array, we need to use a loop
$display("Array contents:");
for (i = 0; i < 5; i = i + 1)
$display("a[%0d] = %0d", i, a[i]);
// The number of elements in an array is fixed at declaration
$display("Array size: %0d", 5);
// Use this syntax to declare and initialize an array in one line
reg [3:0] b [0:4] = '{4'd1, 4'd2, 4'd3, 4'd4, 4'd5};
$display("Initialized array b:");
for (i = 0; i < 5; i = i + 1)
$display("b[%0d] = %0d", i, b[i]);
// In Verilog, we don't have a direct equivalent to Go's [...] syntax
// However, we can initialize arrays with specific values at specific indices
reg [3:0] c [0:4] = '{4'd10, default:4'd0, 3:4'd40, 4:4'd50};
$display("Array c with specific initializations:");
for (i = 0; i < 5; i = i + 1)
$display("c[%0d] = %0d", i, c[i]);
// Multidimensional arrays are supported in Verilog
reg [3:0] twoD [0:1][0:2];
for (i = 0; i < 2; i = i + 1)
for (integer j = 0; j < 3; j = j + 1)
twoD[i][j] = i + j;
$display("2D array:");
for (i = 0; i < 2; i = i + 1)
for (integer j = 0; j < 3; j = j + 1)
$display("twoD[%0d][%0d] = %0d", i, j, twoD[i][j]);
end
endmodule
In Verilog, arrays are typically used to represent memory or registers in digital circuits. Here are some key differences and similarities:
len()
. The size is known from the declaration.When you simulate this Verilog code, it will display the contents of the arrays, demonstrating how to work with arrays in Verilog for digital design and hardware description.