MATLAB Test Bench Requirements and Best Practices for Code Generation
What Is a MATLAB Test Bench?
A test bench is a MATLAB® script or function that you write to test the algorithm in your MATLAB design function. The test bench varies the input data to the design to simulate real world conditions. It can also can check that the output data meets design specifications.
HDL Coder™ uses the data it gathers from running your test bench with your design to infer fixed-point data types for floating-point to fixed-point conversion. The coder also uses the data to generate HDL and SystemC test data for verifying your generated code. For more information on how to write your test bench for the best results, see MATLAB Test Bench Requirements and Best Practices for Code Generation.
MATLAB Test Bench Requirements
You can use any MATLAB data type and function in your test bench.
A MATLAB test bench has the following requirements:
For floating-point to fixed-point conversion, the test bench must be a script or a function with no inputs. The test bench can have local helper functions that take inputs.
The inputs and outputs in your MATLAB design interface must use the same data types, sizes, and complexity in each call site in your test bench.
If you enable the Accelerate test bench for faster simulation option in the Float-to-Fixed Workflow, the MATLAB constructs in your test bench loop must be compilable.
MATLAB Test Bench Best Practices
Use the following MATLAB test bench best practices:
Design your test bench to cover the full numeric range of data that the design must handle. HDL Coder uses the data that it accumulates from running the test bench to infer fixed-point data types during floating-point to fixed-point conversion.
If you call the design function multiple times from your test bench, the coder uses the accumulated data from each instance to infer fixed-point types. Both the design and the test bench can call local functions within the file or other functions on the MATLAB path. The call to the design function can be at any level of your test bench hierarchy.
Before trying to generate code, run your test bench in MATLAB. If simulation is slow, accelerate your test bench. To learn how to accelerate your simulation, see Accelerate MATLAB Algorithms.
If you have a loop that calls your design function, use only compilable MATLAB constructs within the loop and enable the Accelerate test bench for faster simulation option.
Before each test bench simulation run, use the
clear variablescommand to reset your persistent variables.
To see an example of a test bench, enter this command: