|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



ARRAYS


Open Arrays



The size of the packed dimension, the unpacked dimension, or both dimensions can remain unspecified,such cases are referred to as open arrays (or unsized arrays). Open arrays allow the use of generic code to handle different sizes. Formal arguments in SystemVerilog can be specified as open arrays solely in import declarations, exported. SystemVerilog functions cannot have formal arguments specified as open arrays.

OpenArrays are good for generic programming, since C language doesn't have concept of parameterizable arguments. Standared query and library functions are provided to determine array information to acess array elements.



EXAMPLE: open arrays

CODE:SV_file.sv

program main;

int fxd_arr_1[8:3];
int fxd_arr_2[12:1];

import "DPI-C" context function void pass_array(input int dyn_arr[] );

initial
begin
for (int i = 3; i<=8 ; i++)
begin
fxd_arr_1[i] = $random() ;
$display("SV:fxd_arr_1 %0d %d ",i, fxd_arr_1[i] );
end

$display("\n Passing fxd_arr_1 to C \n");
pass_array( fxd_arr_1 );

for (int i = 1; i<=12 ; i++)
begin
fxd_arr_2[i] = $random() ;
$display("SV: fxd_arr_2 %0d %d ",i, fxd_arr_2[i] );
end

$display("\n Passing fxd_arr_2 to C \n");
pass_array( fxd_arr_2 );
end
endprogram

CODE: C_file.c

#include <stdio.h>
#include <svdpi.h>

void pass_array(const svOpenArrayHandle dyn_arr ) {
int i;

printf("Array Left %d, Array Right %d \n\n", svLeft(dyn_arr,1), svRight(dyn_arr, 1) );
for (i= svRight(dyn_arr,1); i <= svLeft(dyn_arr,1); i++) {
printf("C: %d %d \n", i, *(int*)svGetArrElemPtr1(dyn_arr, i) );
}
printf("\n\n");

}

RESULTS:


SV:fxd_arr_1 3 303379748
SV:fxd_arr_1 4 -1064739199
SV:fxd_arr_1 5 -2071669239
SV:fxd_arr_1 6 -1309649309
SV:fxd_arr_1 7 112818957
SV:fxd_arr_1 8 1189058957

Passing fxd_arr_1 to C

Array Left 8, Array Right 3

C: 3 303379748
C: 4 -1064739199
C: 5 -2071669239
C: 6 -1309649309
C: 7 112818957
C: 8 1189058957

SV: fxd_arr_2 1 -1295874971
SV: fxd_arr_2 2 -1992863214
SV: fxd_arr_2 3 15983361
SV: fxd_arr_2 4 114806029
SV: fxd_arr_2 5 992211318
SV: fxd_arr_2 6 512609597
SV: fxd_arr_2 7 1993627629
SV: fxd_arr_2 8 1177417612
SV: fxd_arr_2 9 2097015289
SV: fxd_arr_2 10 -482925370
SV: fxd_arr_2 11 -487095099
SV: fxd_arr_2 12 -720121174

Passing fxd_arr_2 to C

Array Left 12, Array Right 1

C: 1 -1295874971
C: 2 -1992863214
C: 3 15983361
C: 4 114806029
C: 5 992211318
C: 6 512609597
C: 7 1993627629
C: 8 1177417612
C: 9 2097015289
C: 10 -482925370
C: 11 -487095099
C: 12 -720121174



Packed Arrays



A packed array is represented as an array of one or more elements (of type svBitVecVal for 2-state values and svLogicVecVal for 4-state values), each element representing a group of 32 bits.



CODE:SV_file.sv
program main;

import "DPI-C" function void get_nums(output logic [15:0] nums[10]);

logic [15:0] nums[10];

initial begin
get_nums(nums);
foreach (nums[i]) $display(i,nums[i]);
end
endprogram

CODE:C_file.c
#include "svdpi.h"

void fib(svLogicVecVal nums[10]) {
int i;
for (i=0; i<10; i++) {
nums[i] = i ;
}
}

RESULTS:

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9




Linearized And Normalized

Arrays use normalized ranges for the packed [n-1:0] and the unpacked part [0:n-1]

For example, if SV code defines an array as follows:

logic [2:3][1:3][2:0] b [1:10][31:0];

Then C code would see it as defined like this:

logic [17:0] b [0:9][0:31];

Array Querying Functions




svLeft() shall return the left bound (MSB) of the dimension.

svRight() shall return the right bound (LSB) of the dimension.

svLow() shall return the minimum of left index and right index of the dimension.

svHigh() shall return the maximum of left index and right index of the dimension.

svIncrement() shall return 1 if left index is greater than or equal to right index and -1 if left index is less than right index.

svLength() shall return the number of elements in the dimension, which is equivalent to high index - low index + 1.

svDimensions() shell return total number of dimensions in the array



CODE: SV_file.sv
program main;

int fxd_arr_1[8:3];
int fxd_arr_2[1:13];

import "DPI-C" context function void pass_array(input int dyn_arr[] );

initial
begin
$display("\n Passing fxd_arr_1 to C \n");
pass_array( fxd_arr_1 );
$display("\n Passing fxd_arr_2 to C \n");
pass_array( fxd_arr_2 );
end

endprogram
CODE: C_file.c
#include <stdio.h>
#include <svdpi.h>

void pass_array(const svOpenArrayHandle dyn_arr ) {
printf("Array Pointer is %x \n", svGetArrayPtr(dyn_arr) );
printf(" Lower index %d \n", svLow(dyn_arr,1));
printf(" Higher index %d \n", svHigh(dyn_arr, 1) );
printf(" Left index %d \n", svLeft(dyn_arr,1), svRight(dyn_arr, 1) );
printf(" Right index %d \n", svRight(dyn_arr, 1) );
printf(" Length of array %d \n", svLength(dyn_arr,1) );
printf(" Incremental %d \n",svIncrement(dyn_arr,1));
printf("Dimentions of Array %d \n", svDimensions(dyn_arr ));
printf("Size of Array in bytes %d \n", svSizeOfArray(dyn_arr) );
}

RESULTS:

Passing fxd_arr_1 to C

Array Pointer is 80fdc58
Lower index 3
Higher index 8
Left index 8
Right index 3
Length of array 6
Incremental 1
Dimentions of Array 1
Size of Array in bytes 24

Passing fxd_arr_2 to C

Array Pointer is 80fdc70
Lower index 1
Higher index 13
Left index 1
Right index 13
Length of array 13
Incremental -1
Dimentions of Array 1
Size of Array in bytes 52


Index
Introductions
Layers
Import
Naming
Export
Pure And Context
Data Types
Arrays
Passing Structs And Unions
Arguments Type
Disablie

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright © 2007-2017 :: all rights reserved www.testbench.in::Disclaimer