Systemverilog Open Arrays

原文链接:http://www.testbench.in/DP_08_ARRAYS.html

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


```css
#include "svdpi.h"

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

### RESULTS:

```c
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
### Basys3 IP Core Resources and Information For FPGA design using the Basys3 board, obtaining suitable IP cores is essential for efficient development. The Xilinx Vivado Design Suite provides comprehensive support for creating and integrating IP cores into designs targeted at Artix-7 FPGAs like those found in the Basys3 platform[^1]. Xilinx offers a wide range of pre-built IP cores that can significantly accelerate project timelines by providing ready-to-use functionalities such as UARTs, SPI controllers, I2C masters/slaves, Ethernet MACs, DDR memory interfaces, and more[^2]. Moreover, third-party vendors also supply specialized IP cores tailored for specific applications or industries, expanding options beyond what official sources provide. Open-source communities contribute additional resources through platforms like GitHub where developers share their work under permissive licenses. Learning how to effectively utilize these tools requires familiarity with hardware description languages (HDL), particularly VHDL or SystemVerilog, alongside mastery over synthesis and simulation flows within the chosen EDA toolchain environment[^3]. To facilitate easier integration between custom logic blocks written directly in HDL code versus imported IPs, it's beneficial to explore tutorials specifically addressing this topic area along with exploring example projects provided either officially from manufacturer documentation sites or unofficially via enthusiast forums dedicated towards similar interests surrounding programmable devices based around Field Programmable Gate Arrays technology. ```verilog // Example instantiation template for an AXI GPIO IP core in Verilog module top_level ( input wire clk, input wire reset_n, // ... other signals ... ); axi_gpio your_instance_name ( .s_axi_aclk(clk), .s_axi_aresetn(reset_n), // ... connect remaining ports according to interface specifications ... ); endmodule ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值