Main Content

使用命令行接口从MATLAB代码生成HDL代码

This example shows how to use the HDL Coder™ command line interface to generate HDL code from MATLAB® code, including floating-point to fixed-point conversion and FPGA programming file generation.

概述

HDL code generation with the command-line interface has the following basic steps:

  1. 创建一个FIXPTcoder config object. (Optional)

  2. 创建一个HDLcoder config object.

  3. Set config object parameters. (Optional)

  4. 运行代码命令以生成代码。

这HDL Coder command-line interface can use two coder config objects with the codegen command. The optionalFIXPT编码器配置对象将浮点数配置为MATLAB代码的定点转换。这HDLcoder config object configures HDL code generation and FPGA programming options.

在此示例中,我们探索了不同的方式,您可以将浮点配置为定点转换和代码生成。

示例代码实现了离散时间集成器及其测试工作台。

将设计和测试基准文件复制到临时文件夹中

Execute the following code to copy the design and test bench files into a temporary folder:

all;design_name ='mlhdlc_dti';testbench_name ='mlhdlc_dti_tb';mlhdlc_demo_dir = fullfile(matlabroot,'toolbox',,,,'hdlcoder',,,,'hdlcoderdemos',,,,'matlabhdlcoderdemos');mlhdlc_temp_dir = [tempdir'mlhdlc_dti']; cd(tempdir); [~, ~, ~] = rmdir(mlhdlc_temp_dir,');mkdir(mlhdlc_temp_dir); cd(mlhdlc_temp_dir); copyfile(fullfile(mlhdlc_demo_dir, [design_name,'.m*']),mlhdlc_temp_dir);copyfile(fullfile(mlhdlc_demo_dir,[testbench_name,'.m*']),mlhdlc_temp_dir);

基本代码生成,浮点转换为浮点

您可以生成HDL代码,并使用默认设置将设计从浮点点转换为定点。

您只需要设计名称,mlhdlc_dti,,,,and test bench name,mlhdlc_dti_tb

all;% Create a 'fixpt' config with default settingsfixptcfg = coder.config(“ fixpt');fixptcfg.testbenchname ='mlhdlc_dti_tb';% Create an 'hdl' config with default settingshdlcfg = coder.config('hdl');%#ok

设置后FIXPTandHDLconfig objects, run the following codegen command to perform floating-point to fixed-point conversion, and generate HDL code.

代码根-float2fixedFIXPTcfg-configHDLCFGmlhdlc_dti

If your design already uses fixed-point types and functions, you can skip fixed-point conversion:

hdlcfg = coder.config('hdl');% Create an 'hdl' config with default settingshdlcfg.testbenchname ='mlhdlc_dti_tb';代码根-configHDLCFGmlhdlc_dti

这rest of this example describes how to configure code generation using theHDLandFIXPT对象。

为定点转换配置对象创建一个浮点

要执行定点转换的浮点,您需要一个FIXPT配置对象。

创建一个FIXPTconfig object and specify your test bench name:

all;fixptcfg = coder.config(“ fixpt');fixptcfg.testbenchname ='mlhdlc_dti_tb';

Set Fixed-Point Conversion Type Proposal Options

代码生成器可以根据您选择的单词长度或分数长度提出定点类型。这两个选项是互斥的。

将提出类型的单词长度基础24

FIXPTcfg.DefaultWordLength = 24; fixptcfg.ProposeFractionLengthsForDefaultWordLength = true;

另外,您可以将建议的定点类型基于分数长度。以下代码将编码器配置为基于分数长度的类型10

FIXPTcfg.DefaultFractionLength = 10; fixptcfg.ProposeWordLengthsForDefaultFractionLength = true;

设置安全保证金

代码生成器增加了将其固定点类型建议基于安全保证金百分比的模拟数据范围。例如,默认的安全保证金为4,,,,which increases the simulation data range used for fixed-point type proposal by4%

将Safetymargin设置为10%

FIXPTcfg.SafetyMargin = 10;

启用数据记录

代码生成器在浮点转换之前和之后使用设计工作台运行测试工作台。您可以启用仿真数据记录以绘制新的定点数据类型的量化效果。

Enable data logging in theFIXPT配置对象:

FIXPTcfg.LogIOForComparisonPlotting = true;

View the Numeric Type Proposal Report

一旦提出了固定点类型,配置代码生成器以启动类型提案报告:

fixptcfg.launchnumerictypesreport = true;

创建一个HDL Code Generation Config Object

要生成代码,您必须创建一个HDLconfig object and set your test bench name:

hdlcfg = coder.config('hdl');hdlcfg.testbenchname ='mlhdlc_dti_tb';

Set the Target Language

您可以生成VHDL或Verilog代码。HDL编码器默认情况下生成VHDL代码。生成Verilog代码:

HDLCFG。TargetLanguage ='Verilog';

生成HDL测试基准代码

Generate an HDL test bench from your MATLAB® test bench:

HDLCFG。GenerateHDLTestBench = true;

使用HDL模拟器模拟生成的HDL代码

如果要使用HDL模拟器模拟生成的HDL代码,则还必须生成HDL测试工作台。

Enable HDL simulation and use the ModelSim simulator:

HDLCFG。SimulateGeneratedCode = true; hdlcfg.SimulationTool =“模型”;% or 'ISIM'

Generate an FPGA Programming File

如果您设置了合成工具,则可以生成FPGA编程文件。启用合成,指定合成工具,并指定FPGA:

%启用合成。hdlcfg.synthesizegeneratedCode = true;% Configure Synthesis tool.hdlcfg.synthesistool ='Xilinx ISE';%或“ Altera Quartus II”;HDLCFG。SynthesisToolChipFamily ='virtex7';hdlcfg.synthesistooldevicename ='XC7VH580T';hdlcfg.synthesistoolpackagename ='HCG1155';hdlcfg.synthesistoolspeedvalue ='-2G';

运行代码生成

Now that you have yourFIXPTandHDL配置对象设置,运行CodeGen命令以执行定点转换的浮点,生成HDL代码并生成FPGA编程文件:

代码根-float2fixedFIXPTcfg-configHDLCFGmlhdlc_dti