SystemVerilog Fixed Array codes

Fixed Array Codes in SystemVerilog

 

1. Fixed-Size Array of Integers

module fixed_array1;
   // Declare and initialize a fixed-size array of integers
   int myArray [4] = '{10, 20, 30, 40};
   
   initial begin
      // Accessing and displaying elements of the array
      foreach (myArray[i]) begin
         $display("Element %0d: %0d", i, myArray[i]);
      end
   end
endmodule

Output:

Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
           V C S   S i m u l a t i o n   R e p o r t

2. Fixed-Size Array of Strings

module fixed_array2;
   // Declare and initialize a fixed-size array of strings
   string fruits [3] = {"Apple", "Orange", "Banana"};
   
   initial begin
      // Accessing and displaying elements of the array
      foreach (fruits[i]) begin
         $display("Fruit %0d: %s", i, fruits[i]);
      end
   end
endmodule

Output:

Fruit 0: Apple
Fruit 1: Orange
Fruit 2: Banana
           V C S   S i m u l a t i o n   R e p o r t

3. Fixed-Size Array of Custom Structures

typedef struct {
    logic [7:0] id;
    string name;
} Person;

module fixed_array3;
   // Declare and initialize a fixed-size array of custom structures
   Person people [2] = '{'{8'h01, "Alice"}, '{8'h02, "Bob"}};
   
   initial begin
      // Accessing and displaying elements of the array
      foreach (people[i]) begin
         $display("Person %0d: ID=%h, Name=%s", i, people[i].id, people[i].name);
      end
   end
endmodule

Output:

Person 0: ID=01, Name=Alice
Person 1: ID=02, Name=Bob
           V C S   S i m u l a t i o n   R e p o r t

4. Fixed-Size Array of 2D Integer Arrays


module fixed_array4;
   // Declare and initialize a fixed-size array of 2D integer arrays
   int matrix [2][3] = '{'{1, 2, 3}, '{4, 5, 6}};
   
   initial begin
      // Accessing and displaying elements of the array
      for (int i = 0; i < 2; i++) begin
         for (int j = 0; j < 3; j++) begin
            $display("Element [%0d][%0d]: %0d", i, j, matrix[i][j]);
         end
      end
   end
endmodule

Output:

Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
           V C S   S i m u l a t i o n   R e p o r t

5. Fixed-Size Array of Packed Arrays

module fixed_array5;
   // Declare and initialize a fixed-size array of packed arrays
   logic [7:0] data [4] = '{8'h12, 8'h34, 8'h56, 8'h78};
   
   initial begin
      // Accessing and displaying elements of the array
      foreach (data[i]) begin
         $display("Data[%0d]: %h", i, data[i]);
      end
   end
endmodule

Output:

Data[0]: 12
Data[1]: 34
Data[2]: 56
Data[3]: 78
           V C S   S i m u l a t i o n   R e p o r t

6. Fixed-Size Array of Enums

typedef enum logic [1:0] {RED, GREEN, BLUE} Color;

module fixed_array6;
   // Declare and initialize a fixed-size array of enums
   Color colors [3] = '{RED, GREEN, BLUE};
   
   initial begin
      // Accessing and displaying elements of the array
      foreach (colors[i]) begin
         $display("Color %0d: %s", i, colors[i]);
      end
   end
endmodule

Output:

Color 0: RED
Color 1: GREEN
Color 2: BLUE
           V C S   S i m u l a t i o n   R e p o r t 
Scroll to Top