Generate HDL and HLS Code from a Bisection Square Root Algorithm
R2026bThis example shows how to generate HDL and HLS code from a MATLAB® function that computes the square root of an unsigned fixed-point number using a pipelined bisection algorithm.
The bisection method finds the square root by guessing bits of the solution one at a time, from most significant to least significant. Each stage sets one bit and keeps it only if the squared candidate does not exceed the input. The design pipelines these stages so that it produces one result per clock cycle after an initial latency equal to the input word length. Because the design operates directly on fixed-point data, it does not require floating-point to fixed-point conversion.
Examine the MATLAB Design and Test Bench
Set up the MATLAB function and test bench for this example. In the MATLAB Command Window, enter:
mlhdlc_demo_setup("mlhdlc_sqrt");MATLAB function
Test bench
<model>_runme.mscript, which includes the commands to generate HDL code<model>_runme_hls.mscript, which includes the commands to generate HLS code
The MATLAB function, mlhdlc_sqrt, computes the square root of
unsigned fixed-point inputs by pipelining the bisection algorithm. The function uses
persistent variables to maintain pipeline state and produces one result per clock cycle
after an initial filling latency.
To view the function, enter:
open mlhdlc_sqrt.m;The test bench file, mlhdlc_sqrt_tb, verifies the behavior of the
MATLAB function by feeding random unsigned fixed-point values through the pipeline and
comparing the output against the floating-point square root. It plots the absolute error to
confirm numerical accuracy. To view the test bench,
enter:
open mlhdlc_sqrt_tb.m;Simulate the Design
To check for run-time errors, simulate the design by running the test bench. In the MATLAB Command Window, enter:
mlhdlc_sqrt_tb;
Generate HDL Code
The mlhdlc_sqrt_runme script specifies the target files,
enables the settings required for this example, and generates HDL code.
The script specifies the MATLAB function and test bench, then creates a fixed-point
configuration object and an HDL configuration object by using the
coder.config function. It associates the test bench with both
configuration
objects.
designName = "mlhdlc_sqrt"; designTB = "mlhdlc_sqrt_tb"; fixptCfg = coder.config("fixpt"); fixptCfg.TestBenchName = designTB; cfg = coder.config("hdl"); cfg.TestBenchName = designTB;
The script then generates code:
codegen("-float2fixed", "fixptCfg", "-config", "cfg", designName, ... "-launchreport");
Run the Script
First, modify the mlhdlc_sqrt_runme script. Synthesis
is disabled by default. Change the value for the
SynthesizeGeneratedCode
property:
cfg.SynthesizeGeneratedCode = true;
Update the settings for your synthesis tool:
cfg.SynthesisTool = "Xilinx Vivado"; cfg.SynthesisToolChipFamily = "Artix7"; cfg.SynthesisToolDeviceName = "xa7a100t"; cfg.SynthesisToolPackageName = "csg324"; cfg.SynthesisToolSpeedValue = "-1I";
Then, generate code by running the script:
mlhdlc_sqrt_runme
After code generation completes, the report opens. Examine the generated HDL code in the report.
Generate HLS Code
The mlhdlc_sqrt_runme_hls script specifies the target
files, enables the settings required for this example, and generates HLS code.
The script specifies the MATLAB function and test bench, then creates a fixed-point
configuration object and an HLS configuration object by using the
coder.config function. It associates the test bench with both
configuration
objects.
designName = "mlhdlc_sqrt"; designTB = "mlhdlc_sqrt_tb"; fixptCfg = coder.config("fixpt"); fixptCfg.TestBenchName = designTB;
The script creates an HLS configuration object by using the
coder.config function, associates the test bench, and enables
simulation.
cfg = coder.config("hls");
cfg.TestBenchName = designTB;
cfg.GenerateHLSTestBench = true;
cfg.SimulateGeneratedCode = true;The script then generates code:
codegen("-float2fixed", "fixptCfg", "-config", "cfg", designName, ... "-launchreport");
Run the Script
First, modify the mlhdlc_sqrt_runme_hls script. Synthesis is
disabled by default. Set the value for the SynthesizeGeneratedCode
property to
true:
cfg.SynthesizeGeneratedCode = true;
Depending on your synthesis tool, set one of these flags to
true:
isCodingForStratusHLS = false; isCodingForVitisHLS = true;
Depending on your synthesis tool, update these synthesis tool settings:
if isCodingForStratusHLS cfg.SynthesisTool = "Cadence Stratus HLS"; elseif isCodingForVitisHLS cfg.SynthesisTool = "Xilinx Vitis HLS"; cfg.SynthesisToolChipFamily = "Artix7"; cfg.SynthesisToolDeviceName = "xa7a100t"; cfg.SynthesisToolPackageName = "csg324"; cfg.SynthesisToolSpeedValue = "-1I"; end
Then, generate code by running the script:
mlhdlc_sqrt_runme_hls
After code generation completes, the report opens. Examine the generated HLS code in the report.