为 MATLAB 函数生成 SystemVerilog 代码
此示例说明如何为 MATLAB® 函数生成 SystemVerilog HDL 代码。与 Verilog® 代码相比,SystemVerilog 代码具有更高级的功能,例如使用数组端口、包声明、结构体端口等。
检查算法和测试平台
此示例使用一个小型算法,该算法将类型为 uint8 的两个 2×2 矩阵相加。
design_name = 'systemverilog_example'; testbench_name = 'systemverilog_example_tb';
打开 MATLAB 算法和测试平台。
open(design_name);
function out = systemverilog_example(in1, in2) out = in1 + in2; end
open(testbench_name);
function systemverilog_example_tb() in1 = uint8([[1 2]; [3 4]]); in2 = uint8([[5 6]; [7 8]]); i = 1; while i < 3 out = systemverilog_example(in1, in2); in1 = out; end end
新建 HDL Coder 工程
要创建一个新工程,请输入以下命令:
coder -hdlcoder -new systemverilog_example_prj
在 MATLAB Function 下,点击添加 MATLAB Function 并选择 systemverilog_example.m 文件。在 MATLAB Test Bench 下,点击添加文件并选择 systemverilog_example_tb.m。
有关创建和填充 MATLAB HDL Coder 工程的更完整教程,请参阅 Get Started with MATLAB to HDL Workflow。
运行定点转换和 HDL 代码生成
点击工作流顾问以在 MATLAB 中启动工作流顾问。
点击 HDL 代码生成,将
Language属性设置为SystemVerilog。右键点击 HDL 代码生成任务,然后点击运行到选定任务以从头开始运行所有步骤直到 HDL 代码生成。
通过点击日志窗口中的链接来检查生成的 HDL 代码。
使用 MATLAB 命令行生成 SystemVerilog 代码
您也可以通过在 MATLAB 命令行窗口中输入命令为 MATLAB 设计生成 SystemVerilog 代码。
首先,创建一个 coder.config 对象 hdlcfg。
hdlcfg = coder.config('hdl'); hdlcfg.TestBenchName = 'systemverilog_example_tb';
将 TargetLanguage 属性设置为 SystemVerilog。
hdlcfg.TargetLanguage = 'SystemVerilog';
3.运行代码生成。
codegen -config hdlcfg systemverilog_example
### Begin MATLAB to HDL Code Generation...
### Working on DUT: systemverilog_example.
### Using TestBench: systemverilog_example_tb.
### Begin SystemVerilog Code Generation
### Working on systemverilog_example as <a href="matlab:edit('/tmp/Bdoc25b_2988451_710120/tp11ca1a46/hdlcoder-ex49202891/codegen/systemverilog_example/hdlsrc/systemverilog_example.sv')">systemverilog_example.sv</a>.
### Generating package file <a href="matlab:edit('/tmp/Bdoc25b_2988451_710120/tp11ca1a46/hdlcoder-ex49202891/codegen/systemverilog_example/hdlsrc/systemverilog_example_pkg.sv')">systemverilog_example_pkg.sv</a>.
### Generating Resource Utilization Report <a href="matlab:hdlcoder.report.openDdg('/tmp/Bdoc25b_2988451_710120/tp11ca1a46/hdlcoder-ex49202891/codegen/systemverilog_example/hdlsrc/resource_report.html')">resource_report.html</a>.
### Generating Optimization report
### To rerun codegen evaluate the following commands...
---------------------
cgi = load('/tmp/Bdoc25b_2988451_710120/tp11ca1a46/hdlcoder-ex49202891/codegen/systemverilog_example/hdlsrc/codegen_info.mat');
inVals = cgi.CodeGenInfo.inVals;
cfg = cgi.CodeGenInfo.codegenSettings;
codegen -config cfg -args inVals -report
---------------------
### Generating HDL Conformance Report <a href="matlab:web('/tmp/Bdoc25b_2988451_710120/tp11ca1a46/hdlcoder-ex49202891/codegen/systemverilog_example/hdlsrc/systemverilog_example_hdl_conformance_report.html')">systemverilog_example_hdl_conformance_report.html</a>.
### HDL Conformance check complete with 0 errors, 0 warnings, and 0 messages.
### Code generation successful: To view the report, open('codegen/systemverilog_example/hdlsrc/html/report.mldatx')