Variable-size arrays- Dynamic arrays and queues in SystemVerilog

Variable-sized arrays have dynamic sizes that are set, extended, or shrunk during run time. They are not synthesizable and are used for verification.

Dynamic array:

Dynamic arrays are declared with variable names followed by []. No memory is allocated when declared. Memory is allocated only when it is instantiated with new[size], where size is a numerical value representing the size of the array. Can be resized by new[new_size], where new_size will become the size of the array. If we need to retain old elements, then use syntax

array_name = new[size](array_name)

delete() function empties the array and size() returns the size of the array.

module zoo_example;

  // Declare a dynamic array to store zoo animals
  string animals[];

  initial begin

    animals = new[4];
    animals[1] = "dog";
    animals[2] = "cat";

    // Display the information of all zoo animals
    $display("Zoo Animals:Ex 1");
    foreach (animals[i]) begin
      $display("Name: %s", animals[i]);
    end

    animals = new[6] (animals);  // resize and maintaining the elements from previous array

    // Display the information of all zoo animals
    $display("Zoo Animals: Ex 2");
    foreach (animals[i]) begin
      $display("Name: %s", animals[i]);
    end

    animals = new[3];  //resize to new array

    // Display the information of all zoo animals
    $display("Zoo Animals: Ex 3");
    foreach (animals[i]) begin
      $display("Name: %s", animals[i]);
    end

    //display size of array
    $display("Size: %d", animals.size());

    // delete array 
    animals.delete();

    //display size of array
    $display("Size after delete() command: %d", animals.size());
  end
endmodule

Log message:

oo Animals:Ex 1
Name:
Name: dog
Name: cat
Name:
Zoo Animals: Ex 2
Name:
Name: dog
Name: cat
Name:
Name:
Name:
Zoo Animals: Ex 3
Name:
Name:
Name:
Size: 3
Size after delete() command: 0

Size: 3
Size: 0

Queues:

Queues are declared with the variable name followed by [$]. They have multiple functions which makes the variable act as a queue.

Operation
Descriptions
push_backAdds an element to the end (back)
push_frontAdds an element to the beginning (front)
pop_backRemoves and returns the last element from the end (back)
pop_frontRemoves and returns the first element from the beginning (front)
Common functions in Queue
module zoo_ex_queue;

  // Declare a queue to store zoo animal names
  string animals_q[$];
  int queue_size;

  initial begin
    // queue some animal names to the back of the queue
    animals_q.push_back("dog");
    animals_q.push_back("cat");

    // Display the information of all zoo animals
    $display("Zoo Animals:");
    foreach (animals_q[i]) begin
      $display("Name: %s at queue index %0d", animals_q[i], i);
    end

    // queue more animal names to the front of the queue
    animals_q.push_front("elephant");
    animals_q.push_front("lion");

    // Display the information of all zoo animals after enqueuing more to the front
    $display("Zoo Animals (After queuing More to the Front):");
    foreach (animals_q[i]) begin
      $display("Name: %s at queue index %0d", animals_q[i], i);
    end

    // Insert an animal name at a specific position
    animals_q.insert(2, "tiger");

    // Display the information of all zoo animals after inserting "tiger"
    $display("Zoo Animals (After Inserting Tiger):");
    foreach (animals_q[i]) begin
      $display("Name: %s at queue index %0d", animals_q[i], i);
    end

    // Delete an animal name at a specific position
    animals_q.delete(1);

    // Display the information of all zoo animals after deleting an element
    $display("Zoo Animals (After Deleting an Element):");
    foreach (animals_q[i]) begin
      $display("Name: %s at queue index %0d", animals_q[i], i);
    end

    // Get the size of the queue
    queue_size = animals_q.size();

    // Display the size of the queue
    $display("Queue Size: %d", queue_size);
  end
endmodule

Log message:

Zoo Animals:
Name: dog at queue index 0
Name: cat at queue index 1
Zoo Animals (After queuing More to the Front):
Name: lion at queue index 0
Name: elephant at queue index 1
Name: dog at queue index 2
Name: cat at queue index 3
Zoo Animals (After Inserting Tiger):
Name: lion at queue index 0
Name: elephant at queue index 1
Name: tiger at queue index 2
Name: dog at queue index 3
Name: cat at queue index 4
Zoo Animals (After Deleting an Element):
Name: lion at queue index 0
Name: tiger at queue index 1
Name: dog at queue index 2
Name: cat at queue index 3
Queue Size: 4


by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *