——缘起——
翻出之前的工作记录,有些当时已整理成文(比如此篇),有些也貌似可以整理成文。决定以后抽时间分享到CSDN,每周至少一篇。目的呢?一是通过整理提高自己的认识,二是与大家共同交流。
——2020年9月5日
……
提示:以下是本篇文章正文内容,下面案例可供参考
一、硬件协仿概述
作为Xilinx公司System Generator的利器之一,硬件协仿的核心目的只有一个:加快仿真速度。官方的描述如下:
System Generator provides hardware co-simulation, making it possible to incorporate a design running in an FPGA directly into a Simulink simulation. This allows all (or a portion) of the System Generator design that had been simulating in Simulink as sequential software to be executed in parallel on the FPGA, and can speed up simulation dramatically.
硬件协仿的方式有两种:
- 突发(burst )
- 非突发(non-burst (standard))
突发模式具有更高的性能(更快)。但是突发模式只能使用基于脚本的硬件协仿,无法在Simulink环境使用。
However, burst mode is only supported through MATLAB script-based hardware co-simulation of the Hardware Co-Simulation target and is not used within Simulink.
硬件协仿的两种物理接口:
- JTAG
- Ethernet
Ethernet接口具有更快的协仿速度,在sysgen2016.2中支持官方开发板KC705和VC707。硬件协仿编译器自动生成具有相应接口功能的目标bit文件。
二、硬件协仿编译
很简单的几个步骤:
- 选目标板
- 编译目标:JTAG/Ethernet,Burst/Non-burst
- 选HDL,目录…
- 选生成:接口文档/测试台(testbench)
- 点击Generate
如下图所示,真的很傻瓜式。
生成的配置文件包含了我们所需要的一切,官方描述请看:
The configuration bitstream contains the hardware associated with your model, and also contains additional interfacing logic that allows System Generator to communicate with your design using a physical interface between the board and the PC. This logic includes a memory map interface over which System Generator can read and write values to the input and output ports on your design. It also includes any board-specific circuitry that is required for the target FPGA board to function correctly.
我的目标是最快的仿真速度,所以选择了Ethernet+Burst,意味着我接下来要去研究脚本。当然,编译器已经在目标目录下准备好了一个(生成的testbench):
<design_name>_hwcosim_test.m
三、突发模式硬件协仿:基于脚本
在上一步编译完成的基础上,按照如下清单执行协仿。
- 硬件连接准备:
- JTAG - 用于对FPGA进行配置编程;
- Ethernet - 用于硬件协仿通信。
- Ethernet连接准备(PC设置):
- 网卡本地连接属性中仅仅选中“TCP/IPv4”。
- 本地IP设置:192.168.1.11;掩码255.255.255.0。
- 网卡配置高级属性中:流控制(Flow Control)- Rx & Tx 开启(Rx and Tx Enabled)。
- 网卡配置高级属性中:开启Ethernet的巨型帧(jumbo frames,帧长超过1500字节)- 9KB MTU
- 运行
<design_name>_hwcosim_test.m
:
- 产生Xilinx模块(FPGA)输入端口的激励数据(stimulus data):Simulink源模块或Matlab变量;
- 下载bit文件,建立计算机与FPGA之间的网络连接;
- 运行FPGA协仿模块:激励数据送入FPGA,激活协仿模块的时钟运行;
- 获取Xilinx模块(FPGA)的输出数据:
<design_name>_<sub_system>_<port_name>.dat
;- 实际输出与期望值相比较:
<design_name>_<sub_system>_hwcosim_test.result
。– 注意到testbench生成过程中实际是把仿真运行了一遍,可能很耗时间。
四、硬件协仿的突发数据传输
硬件协仿的本质和实现原理
本质是一种加快仿真运行的方法论:
将sysgen模型中最耗计算机资源的部分或模型整体卸载(offload)到目标FPGA平台上。
实现原理可以用一张图表示如下。
- 通信接口(JTAG或Ethernet):与主机通信,接收指令消息,发送响应。
- 指令处理器:指令消息的分析与执行(parsed and executed)。
- Memory-mapped AXI4-Lite register bank: 写指令在寄存器中建立激励数据,驱动DUT的输入端口;读指令对DUT的输出进行队列处理;run(x)或run(inf)+run(0)指令控制DUT的输入时钟。
突发数据传输
突发模式下,M-HWCosim调度器(scheduler)将包含n个样点的时间序列突发写(burst write)入输入FIFO中,运行一定量的时钟周期(取决于端口速度和FIFO深度),并将DUT结果送到入输出FIFO中;而后,调度器将输出FIFO中的数据突发读(burst read)到MATLAB变量中。
Testbench脚本
Testbench脚本的主要工作:
- 准备要写入到输入FIFO中的激励数据;
- 可以读取生成Testbench时自动存储下来的激励数据;
- 也可以动态生成激励数据(最灵活方便)。
- 配置FPGA、建立网络连接的准备:
h = Hwcosim(project);
% rx.hwc is the data structure containing the Hardware Co-Simulation % design information returned after netlisting the Simulink / System % Generator model. % Hwcosim(project) instantiates and returns a handle to the API shared library object. project = 'rx.hwc'; h = Hwcosim(project);
- 运行协仿:
run(h, ncycles);
try %% Open the Hardware Co-Simulation target and co-simulate the design open(h); h('sim_reset') = sim_reset; % 输入激励 run(h, ncycles); % result_dbg_payload = h('dbg_payload', ncycles); % 输出结果 release(h); %% Release Target on Error catch err release(h); rethrow(err); error('Error running hardware co-simulation testbench. Please refer to hwcosim.log for details.'); end
- 输出结果的处理:标准的MATLAB操作
inData is the data to be written to the port. Normal single writes are performed if inData is a scalar value. If burst mode is enabled and inData is a 1xn array, it will be interpreted as a timeseries and written to the port via burst data transfer.
五、总结
- 一个验证无误的sysgen工程,一块支持Ethernet硬件协仿的开发板,利用硬件协仿可极大提高工作效率。
- 一些纯靠计算机仿真无法完成的sysgen设计和验证工作可以实施了。