基于DPI-C接口的systemverilog和C之间的动态数组传递

testbench实现

import "DPI-C" function void modulate_my_frame (input byte unsigned data[], int len1, int len2, inout byte unsigned d[]);

module harness;
    byte unsigned c[];
    byte unsigned d[];
    int  len1 = 10;
    int  len2 = 20;

	initial begin
        c = new[len1];
        d = new[len2];

        c[0] = 100;
        c[1] = 100;
        c[2] = 100;
        c[3] = 100;
        c[4] = 100;
        c[5] = 100;

        $display("c=%p", c);
        modulate_my_frame(c, len1, len2, d);
        $display("d=%p", d);

		$finish;
	end
    
    initial begin
        $fsdbDumpfile("top.fsdb");
        $fsdbDumpvars(0);
    end

endmodule

C函数实现

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

void modulate_my_frame (const svOpenArrayHandle p, int len1, int len2, svOpenArrayHandle q)
{
    int i, j;
    unsigned char p1, *q1;
    
    printf("len1=%0d\r\n", len1);
    printf("len2=%0d\r\n", len2);

    for (i = 0; i < len1; i++) {
        p1 = *(unsigned char *)svGetArrElemPtr1(p, i);
        printf("p[%d]=%2x\r\n", i, p1);
    }

    for (j = 0; j < len2; j++) {
        *(unsigned char *)svGetArrElemPtr1(q, j) = 0x55;
    }
}

Makefile脚本

all:
	@make clean && make comp && make run

comp:
	@vcs -sverilog -full64 -kdb -lca -debug_access+all +libext.sv+.v *.c *.sv

run:
	@./simv
verdi:
	@verdi -ssf top.fsdb &
clean:
	@rm -rf `ls -1 | grep -E -v "Makefile|makefile|harness.sv|*.c"`
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 SystemVerilog DPI 中,可以通过使用 C/C++ 代码来改变 SystemVerilog 动态数组的大小。下面是一个示例: 首先,在 SystemVerilog 模块中声明一个 DPI 导出函数,该函数用于改变动态数组的大小: ```systemverilog module my_module; import "DPI-C" function void resize_array(input int new_size); int my_array[]; // 动态数组 initial begin my_array = new[10]; // 初始化数组大小为 10 $display("原始数组大小: %0d", my_array.size()); resize_array(20); // 调用 DPI 函数改变数组大小 $display("改变后的数组大小: %0d", my_array.size()); // 可以继续操作改变后的数组 end // ... endmodule ``` 然后,在 C/C++ 环境中实现 DPI 函数的定义,该函数通过调用 SystemVerilog 的 `new[]` 运算符来改变数组的大小: ```c #include <stdio.h> #include <svdpi.h> extern "C" void resize_array(int new_size) { svBitVecVal* array_ptr; svBitVecVal* resized_array_ptr; // 获取 SystemVerilog 动态数组的句柄 svGetScope()->getVar("my_module.my_array", &array_ptr); // 创建新的调整大小后的数组 resized_array_ptr = svNewBitVecArray(new_size); // 复制原始数组内容到新的调整大小后的数组 for (int i = 0; i < array_ptr->size; i++) { resized_array_ptr[i] = array_ptr[i]; } // 更新 SystemVerilog 动态数组的句柄 svPutScope()->setVar("my_module.my_array", resized_array_ptr); } ``` 在这个示例中,SystemVerilog 模块使用 DPI 导出函数 `resize_array()` 来改变动态数组 `my_array` 的大小。在 C/C++ 中,通过获取动态数组的句柄,并使用 `svNewBitVecArray()` 创建新的调整大小后的数组。然后,将原始数组的内容复制到新数组中,并通过 `svPutScope()->setVar()` 更新 SystemVerilog 动态数组的句柄。 请注意,在使用 DPI 进行动态数组大小调整时,需要小心处理内存分配和释放,以避免内存泄漏和访问错误。确保在调整大小后,原始数组的内容正确复制到新的调整大小后的数组中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunvally

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值