主要内容

Create HDL- or HLS-Compatible MATLAB Algorithms

In this example you create a MATLAB® algorithm that is compatible with and ready for HDL code generation. To create a Simulink model for HDL code generation, see Create HDL-Compatible Simulink Model.

To generate HDL code from a compatible MATLAB algorithm, see Generate HDL Code from MATLAB Algorithms.

Develop a MATLAB Algorithm

The MATLAB function mlhdlc_counter is a model of a 4-bit synchronous up counter.

The function demonstrates these best practices for writing MATLAB code for HDL and HLS code generation:

  • Use only supported functions for HDL and HLS code generation. For more information, see Functions Supported for HDL and HLS Code Generation.

  • Initialize persistent variables to a specific value. In this example, an if statement and the isempty function initialize the persistent variable. If you do not initialize the persistent variable, then you cannot generate HDL and HLS code.

  • Inside a function, read persistent variables before they are modified so that the persistent variables are inferred as registers.

  1. Create a new MATLAB function in your current working directory named mlhdlc_counter.m. Copy the MATLAB algorithm into the function:

     mlhdlc_counter.m

  2. Review the MATLAB function. The persistent variable count_val has an initial value of zero that represents the current value of the count. The function uses two if statements to determine the value of the count based on the input argument enable_ctr. When enable_ctr is a nonzero value, the function increments the count register, count_val, by one. The function increases the counter until the count reaches 15, or 4 bits. Then, the function resets the counter to zero.

    %#codegen
    function count = mlhdlc_counter(enable_ctr)
    
    persistent count_val;
    
    if isempty(count_val)
        count_val = 0;
    end
    
    if enable_ctr
        count_val = count_val + 1;
        if count_val > 15
            count_val = 0;
        end
    end
    
    count = count_val;
    
    end

Create a MATLAB Test Bench

A MATLAB test bench provides input data to your MATLAB algorithm and checks the algorithm output against an expected result. For more information on test bench requirements and best practices, see Using Test Benches With HDL and HLS Code Generation.

In this example, you create a test bench to test whether the mlhdlc_counter function is suitable for code generation. The test bench generates an expected counter output using a MATLAB for loop, and the actual counter algorithm output using the mlhdlc_counter function, and then compares the outputs graphically.

  1. Create a new MATLAB script in your current working directory named mlhdlc_counter_tb.m. Copy the MATLAB code into the MATLAB script:

     mlhdlc_counter_tb.m

  2. Review the variable-initialization portion of the test bench. The test bench initializes the expected_output and actual_output arrays with zeros. Each array has a length of 40. The script then clears the persistent variables associated with mlhdlc_counter.

    expected_output = zeros(1, 40);
    actual_output = zeros(1, 40);
    clear mlhdlc_counter

    Review the portion of the test bench that generates the expected output of the counter algorithm using a for loop. A for loop iterates 40 times, incrementing counter_value unless the loop index i is a multiple of 5. If counter_value exceeds 15, it is reset to 0. The value of counter_value for each iteration is stored in the expected_output array.

    counter_value = 0;
    for i = 1:40
        if mod(i, 5) ~= 0
            counter_value = counter_value + 1;
            if counter_value > 15
                counter_value = 0;
            end
        end
        expected_output(i) = counter_value;
    end

    Review the portion of the test bench that generates the actual output by calling the MATLAB algorithm for HDL code generation. Another for loop iterates 40 times, calling mlhdlc_counter with a true or false argument based on whether the loop index i is a multiple of 5. The result of each iteration is stored in the actual_output array.

    for i = 1:40
        if mod(i, 5) == 0
            actual_output(i) = mlhdlc_counter(false);
        else
            actual_output(i) = mlhdlc_counter(true);
        end
    end

    Review the portion of the test bench that plots the outputs. The code creates a tiled layout with two plots: one for actual_output and one for expected_output. Then, the code compares expected_output with actual_output to check for equivalency.

    t = tiledlayout(2,1);
    nexttile
    plot(1:40,actual_output,LineWidth=2)
    title("Actual Output")
    
    nexttile
    plot(1:40,expected_output,LineWidth=2,Color="#D95319")
    title("Expected Output")
    
    ylabel(t,"Count Value")
    xlabel(t,"Input")
    
    if isequal(expected_output, actual_output)
        disp("Test passed. The actual output matches the expected output.");
    else
        disp("Test failed. The actual output does not match the expected output.");
        disp("Expected Output:");
        disp(expected_output);
        disp("Actual Output:");
        disp(actual_output);
    end

Run and Verify Algorithm Functionality

To test the mlhdlc_counter function, run the mlhdlc_counter_tb test bench.

Comparison of the mlhdlc_counter function output (top) with the simulated output (bottom) using stacked plots.

The test bench results show that the output of mlhdlc_counter matches the simulated output, which verifies that the mlhdlc_counter function logic matches the test bench counter algorithm.

After you validate the MATLAB algorithm, you can generate HDL code from the function. For more information about generating HDL or HLS code from a compatible MATLAB algorithm, see Generate HDL Code from MATLAB Algorithms.

See Also

Topics