Using Test Benches With HDL and HLS Code Generation
A MATLAB® test bench is a MATLAB script or function that tests a MATLAB design under test (DUT). The test bench varies the input data to the MATLAB DUT to simulate real world conditions. In MATLAB, you use test benches to compare the DUT output against existing data or expected results in order to verify that the output of the DUT meets target design specifications. To learn more about building and using a MATLAB test bench, see Create HDL- or HLS-Compatible MATLAB Algorithms.
HDL Coder™ uses the data from test bench execution to infer fixed-point data types during floating-point to fixed-point conversion, and to generate HDL and High-Level Synthesis (HLS) test bench data for verifying your generated code. For more information on HDL test bench generation, see Test Bench Generation.
Test Bench Requirements
When you use test benches during HDL code generation, follow these requirements:
You can use any MATLAB data type and function in your test bench.
For floating-point to fixed-point conversion, the test bench must be a script or a function with no input arguments. You may define local helper functions in the test bench that accept input arguments.
Maintain a consistent interface for each invocation of the MATLAB DUT in the test bench. Use the same numeric data types, data dimensions, and complexity for each MATLAB DUT inputs and outputs for each invocation of the MATLAB DUT in the test bench.
Test Bench Best Practices
When you use test benches during HDL code generation, consider these best practices:
Design your test bench to cover the full numeric range of data that the MATLAB DUT handles. HDL Coder uses the data from running the test bench to infer fixed-point data types during floating-point to fixed-point conversion of the DUT. For more information on fixed-point conversion for a MATLAB DUT, see Automated Fixed-Point Conversion.
If you call the MATLAB DUT multiple times from your test bench, then HDL Coder uses the accumulated data from each execution of the DUT to infer the fixed-point data types during floating-point to fixed-point conversion of the DUT.
Both the MATLAB DUT and the test bench can call local functions or other functions on the MATLAB path.
The call to the MATLAB DUT can be at any level of your test bench hierarchy.
To handle large input data sets efficiently, modify the test bench to iterate over the entire data set. For each loop iteration in the test bench, pass a subset of the data to the DUT inputs.
Before generating HDL code, run your test bench in MATLAB. If the test bench execution time is slow, generate a MEX function to accelerate your test bench. To learn how to generate a MEX function, see Generate MEX Code to Accelerate Simulation of Bouncing Balls.
If you accelerate the test bench by generating a MEX function, use MATLAB language constructs that are compatible with MEX function generation in your test bench. For more information on compatible MATLAB language constructs for MEX function generation and associated best practices, see Best Practices for Using MEX Functions to Accelerate MATLAB Algorithms.
Before each test bench simulation, use the
clearcommand to reset persistent variables in the MATLAB DUT.
See Also
Apps
Objects
Functions
Topics
- Generate and Synthesize HDL Code for Symmetric FIR Filter Using the HDL Workflow Advisor
- Accelerate MATLAB Algorithms
- Guidelines for Writing MATLAB Code to Generate Efficient HDL and HLS Code
- Functions Supported for HDL and HLS Code Generation
- For-Loop Best Practices for HDL Code Generation
- Supported MATLAB Data Types, Operators, and Control Flow Statements