Arrays - muneeb-mbytes/verilator GitHub Wiki
Dynamic Array:
// module declaration for dynamic array example
module dynamic_array;
// creating dynamic array of type int
int abc [];
initial begin
// give size of dynamic array for eg. take 7
$display("");
$display("// setting array size to seven");
abc = new[7];
// initialize array elements with 7 values
abc = '{11,12,13,14,15,16,17};
// iterate through array and print values
$display("");
$display("// dynamic array values");
foreach(abc[i])begin
$display("value of abc[%0d]=%0d",i,abc[i]);
end
$display("");
end
endmodule : dynamic_array
Output:
Associative array:
Error code:
// module declaration for associative array
module associative_method();
// Declare an associative array abc of type int and index type string
int abc[string];
string variable;
string val1;
initial begin
$display("");
$display("// associative array given below");
// initialize each dynamic array with values
abc = '{ "vadodara" : 10 , "ahmedabad" : 25 , "surendranagar" : 38 , "rajkot" : 55 ,
"surat":48};
// printing both array values
$display("abc = %p",abc);
// function num(): this function prints the number of items in array
$display("");
$display("// abc.num() - gives number of elements inside array");
$display("// output of abc.num()");
$display("%0d",abc.num());
//function size(): this works same as num()
$display("");
$display("// abc.size() - returns size of array");
$display("// output of abc.size()");
$display("%0d",abc.size());
// function exists(index): this function checks if the particular key exist in array or not
$display("");
$display("// abc.exists(index) - returns whether the particular index exists inside array or not");
$display("// output of abc.exist(vadodara)");
if(abc.exists ("vadodara")) begin
$display("index vadodara exists in array");
end
else begin
$display("index vadodara not exists in array");
end
// function first(variable): it will assign value of first index to variable
$display("");
$display("// abc.first(index) - returns the first index value in array");
$display("// output of abc.first(variable)");
if(abc.first(variable)) begin
$display("abc[%s]=%0d",variable,abc[variable]);
end
// function last(variable): it will assign value of last index to variable
$display("");
$display("// abc.last(index) - returns the last index value in array");
$display("// output of abc.last(variable)");
if(abc.last(variable)) begin
$display("abc[%s]=%0d",variable,abc[variable]);
end
// function next(index): it will give next index value which is greater than current index the
// in array
$display("");
$display("// abc.next(index) - gives next index value which is greater than current index");
$display("// and it's corresponding value in array");
$display("// output after abc.next(val1)");
if(abc.next(val1)) begin
$display("abc[%s]=%0d",val1,abc[val1]);
end
// function delete(index): it will delete index from the array
$display("");
$display("// abc.delete(index) - deletes the index and it's corresponding value in array");
$display("// output after deleting surendranagar index in array");
abc.delete("surendranagar");
$display("%p",abc);
$display("");
end
endmodule : associative_method
On running the above code we get the errors as methods of associative array returns int type which is 32 bit whereas “if” condition expects 1 bit, verilator does not support truncating and appending additional bits when there is a change in data size.
This error can be cleared by performing static casting to bit within the logical operator “if”. i.e., if(bit'(abc.exists ("vadodara")))
Error:
Without error code:
// module declaration for associative array
module associative_method();
// Declare an associative array abc of type int and index type string
int abc[string];
string variable;
string val1;
initial begin
$display("");
$display("// associative array given below");
// initialize each dynamic array with values
abc = '{ "vadodara" : 10 , "ahmedabad" : 25 , "surendranagar" : 38 , "rajkot" : 55 ,
"surat":48};
// printing both array values
$display("abc = %p",abc);
// function num(): this function prints the number of items in array
$display("");
$display("// abc.num() - gives number of elements inside array");
$display("// output of abc.num()");
$display("%0d",abc.num());
//function size(): this works same as num()
$display("");
$display("// abc.size() - returns size of array");
$display("// output of abc.size()");
$display("%0d",abc.size());
// function exists(index): this function checks if the particular key exist in array or not
$display("");
$display("// abc.exists(index) - returns whether the particular index exists inside array or not");
$display("// output of abc.exist(vadodara)");
if(bit'(abc.exists ("vadodara"))) begin
$display("index vadodara exists in array");
end
else begin
$display("index vadodara not exists in array");
end
// function first(variable): it will assign value of first index to variable
$display("");
$display("// abc.first(index) - returns the first index value in array");
$display("// output of abc.first(variable)");
if(bit'(abc.first(variable))) begin
$display("abc[%s]=%0d",variable,abc[variable]);
end
// function last(variable): it will assign value of last index to variable
$display("");
$display("// abc.last(index) - returns the last index value in array");
$display("// output of abc.last(variable)");
if(bit'(abc.last(variable))) begin
$display("abc[%s]=%0d",variable,abc[variable]);
end
// function next(index): it will give next index value which is greater than current index the
// in array
$display("");
$display("// abc.next(index) - gives next index value which is greater than current index");
$display("// and it's corresponding value in array");
$display("// output after abc.next(val1)");
if(bit'(abc.next(val1))) begin
$display("abc[%s]=%0d",val1,abc[val1]);
end
// function delete(index): it will delete index from the array
$display("");
$display("// abc.delete(index) - deletes the index and it's corresponding value in array");
$display("// output after deleting surendranagar index in array");
abc.delete("surendranagar");
$display("%p",abc);
$display("");
end
endmodule : associative_method
Output:
Queue:
module queue_data;
string queue1[$];
//int queue2[$];
initial
begin
queue1 = {"manipal", "banglaore", "udupi"};
$display("\nqueue1 = ['manipal', 'banglaore', 'udupi']");
$display("\n OUTPUT : ");
//queue1.pop_front();
$display("\n // pop_front()-pop front the array element at first index position of queue1: \n\t %p",queue1.pop_front());
$display("\n // After pop front the 'manipal' is removed from queue1 is : \n\t %p", queue1);
//queue1.pop_back();
$display("\n // pop_back()-pop back the array element at last index position of queue1: \n\t %p", queue1.pop_back());
$display("\n // After pop back the 'udupi' is removed from queue1 is :\n\t %p", queue1);
queue1.push_front("Yelahanka");
$display("\n // push front the array element at first index position of queue1:");
$display(" // push_front()-After push front the elements of the queue1 is :\n\t %p", queue1);
queue1.push_back("udupi");
$display("\n // push back the array element at last index position of queue1:");
$display(" // push_back()-After push back the elements of the queue1 is :\n\t %p", queue1);
$display("");
end
endmodule:queue_data