Skip to content
asicguru.tech@gmail.com
REGISTER LOGIN
AsicGuru Ventures
  • All Courses
  • CONTACT
  • GALLERY
  • HOME
  • Tutorials

0

Verilog Tutorial

17
  • Introduction
  • Lexical Tokens
  • Gate-Level Modelling
  • Data Types
  • Operators
  • Operands
  • Modules
  • Behavioral Modeling
  • Timing Controls
  • Procedures: Always and Initial Blocks
  • Functions
  • Tasks
  • Component Inference
  • Finite State Machines.
  • Compiler Directives
  • System Tasks and Functions
  • Test Benches

System Verilog

23
  • System Verilog Introduction
  • Data Types
  • Type Casting
  • Arrays
  • Associative Arrays
  • Dynamic Arrays
  • Queue
  • Operators
  • Procedural statements and Control flow
  • Classes
  • Inheritance
  • Encapsulation
  • This and Super operator
  • In-Line Constraints
  • Class Constraints
  • Virtual Classes
  • Parameterized Classes
  • Classes Summary
  • Singleton Classes
  • Functional Coverage Introduction
  • Covergroup
  • Tools
  • Books

Scripting

15
  • Introduction
  • Perl Tutorial
  • What is Perl
  • Perl: Syntax And Variable
  • Perl Strings
  • Perl Arrays
  • Perl Associative Arrays
  • If/While Syntax
  • File Input
  • Print Output
  • String Processing with Regular Expressions
  • Subroutines
  • Running External Programs
  • References
  • Terse Perl

Makefile

1
  • Makefile Tutorial

Interview Questions

2
  • SystemVerilog Interview Questions
  • Verilog Interview Questions

Handwritten Notes

3
  • Perl (Scripting)
  • Linting
  • CMOS Logic Design
View Categories
  • Home
  • Tutorials
  • System Verilog
  • Associative Arrays

Associative Arrays

2 min read

What is an Associative Array? #

An Associative Array is a one-dimensional unpacked array in SystemVerilog where elements are indexed using non-consecutive values such as integers, strings, or even objects. Unlike fixed or dynamic arrays, storage is allocated only when an element is used, making associative arrays ideal for sparse data structures.

Syntax:

data_type array_name [index_type];

Example:

bit [31:0] mem[int];           // Integer-indexed associative array
string str_arr[string];        // String-indexed associative array
class C;                       // Class-based index
C obj;
bit [7:0] class_arr[C];        // Associative array indexed by class object

Differences from Regular Arrays

FeatureFixed/Dynamic ArraysAssociative Arrays
Index TypeInteger (sequential)Any scalar (int, string, class handle)
Memory AllocationPre-allocated / on newAllocated only on use
Index ContinuityContiguousNon-contiguous (sparse)
Element OrderDeterministicRandom / Implementation-defined
UsageKnown size / dense memoryUnknown size / sparse memory

Built-in Methods

SystemVerilog provides special methods for traversing and querying associative arrays.

MethodDescription
.num()Returns the number of entries
.exists(idx)Checks if an entry exists at the given index
.first(idx)Assigns the first index used to the variable idx
.last(idx)Assigns the last index used to the variable idx
.next(idx)Assigns the next index after idx to idx
.prev(idx)Assigns the previous index before idx to idx
.delete()Deletes all elements (or only the one at a given index)

Example: Using Associative Array with Integer Index

module assoc;
  bit [31:0] mem[*];      // Associative array with integer index
  bit [31:0] i;

  initial begin
    // Store values at non-contiguous indices
    mem[0] = 32'hFFAAFFAA;
    mem[3] = 32'hFFAAFFAA;
    mem[5] = 32'hFFAAFFAA;
    mem[7] = 32'hFFAAFFAA;

    $display("Size of array: %0d", mem.num());

    // First index
    mem.first(i);
    $display("First index used: %0d", i);

    // Next index
    mem.next(i);
    $display("Next index: %0d", i);

    // Last index
    mem.last(i);
    $display("Last index used: %0d", i);

    // Previous index
    mem.prev(i);
    $display("Previous index: %0d", i);

    // Delete all elements
    mem.delete();
    $display("Size after delete: %0d", mem.num());

    // Check existence
    $display("Element exists? %0d", mem.exists(i));
  end
endmodule

Output:

Size of array:     4
First index used:  0
Next index:        3
Last index used:   7
Previous index:    5
Size after delete: 0
Element exists?    0

Summary

FeatureDetails
TypeUnpacked, one-dimensional
Index typesint, string, class
Memory usageSparse — memory allocated only when used
Use caseModeling sparse memories, scoreboards, variable tables
Best methods.exists(), .num(), .first(), .next(), .delete()

Updated on August 10, 2025

What are your Feelings

  • Happy
  • Normal
  • Sad
Share This Article :
  • Facebook
  • X
  • LinkedIn
  • Pinterest
ArraysDynamic Arrays

Leave a Reply Cancel reply

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

Table of Contents
  • What is an Associative Array?

Archives

  • May 2025

Categories

  • Slider Post
  • Uncategorized

Copyright @ Asicguru Ventures by Misbah WP | Proudly powered by WordPress