Introduction To C And HDL Code Generation From MATLAB

Transcription

Introduction to C and HDL CodeGeneration from MATLAB이웅재 차장Senior Application Engineer 2012 The MathWorks, Inc.1

Algorithm Development ProcessRequirementsResearch & Design Explore and discover Gain insight into problemDesignTestTest & VerificationElaborate Evaluate options, , CC .exeVHDL / Verilog.c,C, .cppC Structured TextTestElaborate2

The Algorithm Design Challenge How can we:MATLABAlgorithm Design– Implement designsfaster?– Reuse designs on avariety of hardware?FPGAMCUASICDSPFPGAASIC3

Solution: C and HDL Code Generation Automatically generate C orHDL code Deploy generated code onhardwareMATLABAlgorithm DesignMATLAB CoderFPGAMCUHDL CoderGenerateDesign, execute, and verifyalgorithms in MATLABGenerate CVHDL/VerilogASICDSPFPGAASIC4

Code Generation Products for C/C Embedded CoderSimulinkCoderMATLAB CoderEmbedded Coder Automatically generate C and C optimized for embedded systemsSimulink Coder Automatically generate C and C fromSimulink models and Stateflow chartsMATLAB Coder Automatically generate C and C fromMATLAB code5

Code Generation Products for VHDL/VerilogHDL CoderMATLAB CoderHDL Coder Automatically generate VHDL or Verilogfrom MATLAB code and Simulink ModelMATLAB Coder Automatically generate C and C fromMATLAB code6

MATLAB Coder Capabilities 2012 The MathWorks, Inc.7

Why Engineers translate MATLAB to C today?Integrate MATLAB algorithms w/ existing C environmentusing source code and static/dynamic librariesPrototype MATLAB algorithms on desktops asstandalone executablesAccelerate user-written MATLAB algorithmsImplement C code on processors or hand-off tosoftware engineers8

Challenges with Manual Translationfrom MATLAB to CiterateRe-code inC/C Algorithm Designin MATLABverify / ac celerateSeparate functional and implementation specification– Leads to multiple implementations that are inconsistent– Hard to modify requirements during development– Difficult to keep reference MATLAB code and C code in-sync Manual coding errorsTime consuming and expensive9

i terateAutomatic Translation of MATLAB to CAlgorithm Design andCode Generation inMATLABverify/a ccelerateWith MATLAB Coder, design engineers can Maintain one design in MATLABDesign faster and get to C quicklyTest more systematically and frequentlySpend more time improving algorithms in MATLAB10

MATLAB Coder Key Features Code generation for MATLAB– Portable ANSI/ISO C code– Floating-point or Fixed-point(requires Fixed-Point Toolbox)– Processors specific optimizations(requires Embedded Coder)MATLAB CoderVerificationGenerate MATLABAlgorithm Design– Generates MEX test-benches Design and Build– Automated compile and build fordesktop execution– Ability to cross-compile and build forexecution on other platformsHDLCFPGAMCUASICDSP11

Using MATLAB Coder: 3-Step WorkflowPrepare your MATLAB algorithm for code generation Make implementation choices Use supported language featuresTest if your MATLAB code is compliant Validate that MATLAB program generates code Accelerate execution of user-written algorithmGenerate source code or MEX for final use Iterate your MATLAB code to optimize Implement as source, executable or library12

Examplec a*b Coder UITestbenchCode Generation optionsGenerate codeBrowse through report Demo13

Demo: Newton/Rhapson Algorithmworkflow examplePreparing your MATLAB code 𝑓(𝑥0 )𝑥1 𝑥0 𝑓′(𝑥0 )Pre-allocateIdentify more efficientconstructsBuilding optionsTest & Generate Demo14

MATLAB Language Support forCode Generationvisualizationvariable-sized dataJavastructglobalsparsecomplexnested functionsmallocfunctionsnumericSystem objectsfixed-point arraysclassespersistentgraphics15

Embedded Coder for Optimized CodeEmbedded Coder extendsMATLAB Coder with: Processor-specific codegeneration– Built-in support for selectprocessors– Open APIs for use withany processor Speed, memory, andcode appearanceadvanced features16

HDL Coder Capabilities 2012 The MathWorks, Inc.17

HDL CoderGenerate VHDL and Verilog Code for FPGA and ASIC designsNew: MATLAB to HDLMATLABSimulinkHDLCoderVerilog and VHDL Automatic floating-point tofixed-point conversion HDL resource optimizationsand reports Algorithm-to-HDL traceability Integration with simulation &synthesis tools18

Which benefits Engineers get from MATLAB toHDL?Automate for implementing HDL without direct RTLhand-codingLess LOC by using MATLAB TestBench and Reusing itthrough Entire Design ProcessEasy conversion from Floating-Point to Fixed-PointFast to simulate compare with using HDL DUT/TB inthe HDL simulator environment19

HDL Coder Key Features Code Generation for MATLAB andSimulink– Target-independent RTL level HDLCode– IEEE 1376 compliant VHDL– IEEE 1364-2001 compliant Verilog– Automate Fixed-Point conversionHDL CoderGenerate MATLABAlgorithm DesignVerification– Generate HDL test-bench– Generate Scripts for Synthesis tool &HDL simulator Design automation– Synthesize and P&R using integratedXilinx and Altera synthesis tool interface– Optimize for area or speedVHDL/VerilogHDLFPGAASIC20

Using HDL Coder: 5-Step WorkflowPrepare your MATLAB algorithm for code generation Use supported language features Make implementation ,P&R* FixPt.m* wrapper FixPt.m* tb FixPt.m*.vhd, *.v*.do, *.tcl*.wlf, *.txtFixed-Point MATLAB code generation from yourfloating-point design using your MATLABTestBench Accelerate TestBench for fast simulationAutomatically propose Fixed-Point typeIterate data-type customization to optimizeVerify Fixed-Point code against original Floating-Pointcode.Generate synthesizable RTL & TestBench code fromFixed-Point MATLAB code for final use Iterate your MATLAB code to optimize Implement as source, executable or librarySimulation the generated HDL code with test vectorsfrom the test bench using the specifiedsimulation toolSynthesis, Place and Route the generated RTL codeby creating project with ISE/Quartus II*.edf, *.edn*.bit Check timing analysis report to optimize21

Workflow AdvisorAutomatic HDL codegeneration with built-inoptimizationsAutomatically convertfloating point to fixedpointAutomatic HDLVerificationIntegration with FPGAimplementationworkflows22

Example: Symmetric FIR Filter Demo23

DemosGenerate Modular Code for FunctionsFixed-Point Type Conversion and RefinementDistributed Pipelining: Clock Speed OptimizationResource Sharing of Multiplier : Area Optimization24

MATLAB Language Support forHDL Code GenerationstructvisualizationArithmetic OperatorsJavacomplexsparseRational OperatorsLogical OperatorspersistentControl Flow Statementmatrixnested functionsvectorSystem objects (limited)fixed-pointgraphicsclasses25

Customer Successes 2012 The MathWorks, Inc.26

Ono Sokki Reduces DevelopmentTime for Speed Measurement DeviceUsing MATLAB Generated CodeChallengeDevelop a high-precision speedometer for prototypevehicles within a tight scheduleLC-8100 GPS Speedometer System.SolutionUse MathWorks tools for Model-Based Design todevelop algorithms, generate production code for anembedded processor, and perform processor-in-theloop verificationResults Development time cut significantly Code base reduced dramatically System model reused as a test bench“With MathWorks tools we have aseamless environment fordevelopment, simulation, codegeneration, and processor-in-theloop verification. The advantages ofModel-Based Design over handcoding in C can’t be overestimated.”Kazuhiro IchikawaOno Sokki27

FLIR Accelerates Development of ThermalImaging FPGAChallengeAccelerate the implementation of advanced thermalimaging filters and algorithms on FPGA hardwareimages after applying filter developed with HDL Coder.SolutionUse MATLAB to develop, simulate, and evaluatealgorithms, and use HDL Coder to implement the bestalgorithms on FPGAs“With MATLAB and HDL Coder we areResultsmarketplace needs. We now embrace Time from concept to field-testable prototypereduced by 60% Enhancements completed in hours, not weeks Code reuse increased from zero to 30%much more responsive tochange, because we can take a newidea to a real-time-capable hardwareprototype in just a few weeks. Thereis more joy in engineering, so we’veincreased job satisfaction as well ascustomer satisfaction.”Nicholas HogastenFLIR SystemsLink to user story28

Summary Generate C and HDL Deploy code to Processorsand Hardware Accelerate simulation speed Easy conversion fromfloating-point to fixed-point Iterate your designs fasterMATLABAlgorithm DesignMATLAB CoderFPGAMCUHDL CoderGenerateDesign using MATLABGenerate HDLCVHDL/VerilogHDLASICDSPFPGAASIC29

Additional Information MATLAB Coder– Choosing the Right Deployment Solution– Working with Simulink HDL Coder– Working with Simulink– Simulink Design for HDL Code Generation30

Choosing the Right Deployment SolutionMATLAB Coder and MATLAB CompilerMATLAB Compiler.exeMATLAB Coder.dll.exe.lib.dll31

Choosing the Right Deployment SolutionMATLAB Coder and MATLAB CompilerOutputMATLAB supportRuntime requirementLicense modelMATLAB CoderMATLAB CompilerPortable and readableC source codeExecutable or softwarecomponent/librarySubset of languageSome toolboxesFull languageMost toolboxesGraphicsNoneMATLAB CompilerRuntime (MCR)Royalty-freeRoyalty-free32

Working With Simulinkfor C code generation MATLAB Function block in SimulinkC33

Working With Simulinkfor HDL code generation MATLAB Function block in SimulinkHDL34

Simulink Library Support for HDLHDL Supported Blocks Almost 170 blocks supported Core Simulink Blocks– Signal Processing Blocks– Basic and Array Arithmetic, Look-Up Tables,Signal Routing (Mux/Demux, Delays,Selectors), Logic & Bit Operations, Dual andsingle port RAMs, FIFOs, CORDICsNCOs, FFTs, Digital Filters (FIR, IIR, Multirate, Adaptive), Rate Changes (Up &DownSample), Statistics (Min/Max)Communications Blocks–Psuedo-random Sequence Generators,Modulators / Demodulators, Interleavers /Deinterleavers, Viterbi Decoders35

MATLAB & Stateflow for HDLHDL Supported Blocks MATLAB– Relevant subset of the MATLABlanguage for modeling and generatingHDL implementations– eml hdl design patterns:Useful MATLAB Function Block DesignPatterns for HDL Stateflow– Graphical tool for modeling Mealyand Moore Finite State Machines36

Model-Based Design flow using Simulinkfrom Algorithm to FPGA ImplementationMATLAB and Simulink Algorithm and System DesignDESIGNHDL CoderRTL CreationRTLImplement DesignSynthesisMapPlace & RouteHDL VerifierHDL teflowBack AnnotationVerificationFunctional SimulationStatic Timing AnalysisTiming SimulationHDL VerifierFPGA in the Loop37

MATLAB Coder MCU DSP C erate. 12 Using MATLAB Coder: 3-Step Workflow Prepare your MATLAB algorithm for code generation Make implementation choices Use supported language features Test if your MATLAB code is compliant Val