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

0

Verilog Tutorial

17
  • Verilog 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
View Categories
  • Home
  • Tutorials
  • System Verilog
  • Associative Arrays

Associative Arrays

3 min read

#

An Associative array is a better option when the size of the collection is unknown or the data space is sparse. So the associative arrays are mainly used to model the sparse memories. In the associative arrays the storage is allocated only when we use it not initially like in dynamic arrays. Associative arrays can be assigned only to another Associative array of a compatible
type and with the same index type.

Another main difference between Associative array and normal arrays is in that in assoc arrays the arrays subscript can be any scalar value.

So to summarize :

  1. Dynamically allocated, non-contiguous elements
  2. Accessed with integer, or string index, single dimension
  3. Great for sparse arrays with wide ranging index
  4. Array functions: exists, first, last, next, prev

eg :

view source

print?

1.mem[address];     // where address is a integer

2. 

3.str_arr["str"];             // string index is used

4. 

5.class_arr[class_obj]   // index is class object

* Another important thing about assoc arrays is that they are stored in random order. Associative arrays are unpacked arrays.

Build in methods : #

  • num() — returns the number of entries in the Associative array                      Eg:sram_model.num()
  • first() — assigns the value of the first index in the Associative array to the given index variable Eg:sram_model.first(i);
  • last() — assigns the value of the last index in the Associative array to the given index variable Eg:sram_model.last(i);
  • next() — assigns the value of the next index in the Associative array to the given index variable Eg:sram_model.next(i);
  • prev() — assigns the value of the previous index in the Associative array to the given index variable Eg:sram_model.prev(i);
  • delete() — removes all the elements in the Associative array.Eg:sram_model.delete(); If the index is specified, then the element at the specified index is deleted
  • exists() — checks if element exists at the specified index in the Associative array Eg:sram_model.delete();

Example : #

view source

print?

01.module assoc;

02.bit [31:0] mem[*];

03.int assoc[string];

04.bit [31:0] i;

05.initial

06.begin

07.mem[ 0 ] = 32'hFFAAFFAA;

08.mem[ 3 ] = 32'hFFAAFFAA;

09.mem[ 5 ] = 32'hFFAAFFAA;

10.mem[ 7 ] = 32'hFFAAFFAA;

11.$display("size of array: %d\n", mem.num());

12. 

13.// find first element

14.mem.first(i);

15.$display ("the first index of the array is %d", i);

16. 

17.// find the next element, which is now BASE_ADDR +3:

18.mem.next(i);

19.$display ("the next index used of the array is %d", i);

20. 

21.// find the last element

22.mem.last(i);

23.$display ("the last index used of the array is %d", i);

24. 

25.mem.prev(i);

26.$display ("the previous index used of the array is %d", i);

27. 

28.mem.delete();

29.$display("size of array: %d\n", mem.num());

30.$display(" Existence of array: %d\n", mem.exists(i));

31.end

32.endmodule

Output : #

view source

print?

01.# size of array:           4

02.#

03.# the first index of the array is          0

04.# the next index used of the array is          3

05.# the last index used of the array is          7

06.# the previous index used of the array is          5

07.# size of array:           0

08.#

09.#  Existence of array:           0

10.#

Updated on May 4, 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
  • Build in methods :
  • Example :
  • Output :

Archives

  • May 2025

Categories

  • Slider Post
  • Uncategorized

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