Delay Balancing
Why Use Delay Balancing
To achieve more efficient hardware usage and higher clock rates in your model, HDL Coder™ has several optimizations, block implementations, and options that introduce discrete delays into your model. Optimizations such as output pipelining, streaming, or resource sharing can introduce delays. Some block implementations, such as the Newton-Raphson and CORDIC architecture, inherently introduce delays in the generated code.
When optimizations or block implementation options introduce delays along the critical path in a model, the numerics of the original model and generated model or HDL code can differ from each other because equivalent delays are not introduced on other, parallel signal paths. Manual insertion of compensating delays along other paths is possible, but is prone to error and does not scale well to large models that have many signal paths or multiple sample rates.
To avoid this issue, HDL Coder provides delay balancing. By default, delay balancing is enabled on the model. The code generator detects introduction of new delays along one path, and then inserts matching delays on the other paths. When you enable delay balancing, the generated model is functionally equivalent to the original model. It is not recommended that you disable delay balancing on the model. If you disable this setting, HDL Coder generates a warning that numeric differences can occur in the validation model. To fix this warning, enable Balance delays on the model or run the model check Check delay balancing setting.
Specify Delay Balancing
You can set delay balancing for an entire model. For finer control, you can also set delay balancing for subsystems within the top-level device under test (DUT) subsystem.
Set Delay Balancing for a Model
Use these makehdl
options:
BalanceDelays
: By default, model-level delay balancing is enabled, and subsystems within the model inherit the model-level setting. To learn how to set delay balancing for a model, see Balance delays.GenerateValidationModel
: By default, validation model generation is disabled. When you enable delay balancing, generate a validation model to view delays and other differences between your original model and the generated model. To learn how to enable validation model generation, see the Generate validation model section in Model Generation Parameters for HDL Code.
For example, these commands generate HDL code that has delay balancing and generates a validation model.
dut = 'ex_rsqrt_delaybalancing/Subsystem'; makehdl(dut,'BalanceDelays','on','GenerateValidationModel','on');
Disable Delay Balancing for a Subsystem
You can disable delay balancing for an entire model or disable it for a subsystem within the top-level DUT subsystem. For example, if you do not want to balance delays for a control path, you can put the control path in a subsystem, and then disable delay balancing for that subsystem.
To disable delay balancing for a subsystem within the top-level DUT subsystem, disable delay balancing at the model level. When you disable delay balancing for the model, the validation model does not compensate for latency inserted in the generated model due to optimizations or block implementations. The validation model can therefore show mismatches between the original model and generated model.
The workflow for disabling delay balancing for a subsystem within the top-level DUT subsystem is:
Disable delay balancing for the model.
Enable delay balancing for the top-level DUT subsystem.
Disable delay balancing for a subsystem within the DUT subsystem.
When delay balancing is enabled on the model, the delay balancing setting on individual subsystems is ignored. See Set Delay Balancing For a Subsystem.
Delay Balancing Considerations
When you generate HDL code, delay balancing is an essential part of code
optimizations. If any optimizations are enabled, you
must balance pipelines introduced as a part of those optimizations. Failure to
balance automatically inserted pipeline delays causes issues in generated code
deployed in hardware. The recommended way to balance these delays is to keep
BalanceDelays
on for the model (its default).
When you disable delay balancing for the model, or at some specific subsystems, HDL Coder does not compensate for latency inserted due to optimizations or block implementations. Unbalanced delays can lead to a mismatch in simulation results between HDL code and your Simulink model and unexpected and untested behavior of hardware implementation. It is recommended that you generate the validation model and observe effects of such unbalanced delays. For examples of delay balancing and the validation model, see Delay Balancing and Validation Model Workflow in HDL Coder and Resolve Numeric Mismatch with Delay Balancing.
For some multirate models, HDL Coder might generate a large number of pipeline registers that can prevent the HDL design from fitting into an FPGA. Before you disable delay balancing for the model to try to solve this issue, see Delay Balancing on Multirate Designs and Optimize Generated HDL Code for Multirate Designs with Large Rate Differentials.
Best Practices for Disabling Global Delay Balancing
If delay balancing is disabled for your model:
When turning off
BalanceDelays
, also turn off features that generate pipelines or add latency, such as optimizations like resource sharing, streaming, pipelining, or enabling theMapToRAM
HDL block property of a lookup table (LUT) block. Manually balance the delays instead. For more information, see Resource Sharing for Area Optimization, Streaming: Area Optimization, Distributed Pipelining: Speed Optimization, and MapToRAM.Use local delay balancing where possible in your model to save time on manually balancing delays for your entire DUT. For more information on how to apply local delay balancing to specific parts of your model, see Control the Scope of Delay Balancing and Set Delay Balancing For a Subsystem.
Check for the command-line messages about additional latency, such as
The code generation and optimization options you have chosen have introduced additional pipeline delays
, and remove optimization settings that insert latency. For an example, see Delay Balancing and Validation Model Workflow in HDL Coder.Run the
Check for blocks that have nonzero output latency
Model Advisor check. For more information, see Check for blocks that have nonzero output latency.
Generate HDL Code Without Global Delay Balancing
There might be some use cases where you must turn off global delay balancing. To disable delay balancing for the model:
Disable the
BalanceDelays
option for your model at the command line. See Command-Line Information. Starting in R2021b, theBalanceDelays
option is no longer a global option in the model configuration parameters.Change the diagnostic option
TreatBalanceDelaysOffAs
toWarning
orNone
. This option highlights whenBalanceDelays
is set to off and there are unbalanced delays in the model as a result. For more information, see Check for presence of unbalanced delays in generated HDL code.
Disable Delay Balancing for Constant Sources
A stable path is a path where the initial source to the
path is constant. If your model contains a stable path outside the DUT connected
to the top-level DUT subsystem and you want to reduce the amount of resources
needed for your design, you can disable delay
balancing for the stable path by disabling the HDL block property
BalanceDelays on the DUT-level Inport
block. BalanceDelays affects only DUT-level Inport blocks.
For example, in this model, you can disable BalanceDelays
for the DUT_Subsystem
Inport block h_in1
because the input to the
Inport block is a Constant block.
To disable the HDL block property BalanceDelays for an Inport block using the HDL Block Properties dialog box:
Right-click the Inport block.
Select HDL Code > HDL Block Properties.
Set BalanceDelays to
off
.
To disable delay balancing for an Inport block
from the command line, use the hdlset_param
function.
If you have a subsystem hierarchy in your DUT and you disable BalanceDelays on the DUT-level Inport block, delay balancing on the stable path does not occur at lower-level hierarchy if the stable path propagates to the lower-level hierarchy. For an example, see Constrain Delay Balancing to the Data Path in Control the Scope of Delay Balancing.
When you disable delay balancing for a subsystem, that setting takes
precedence over the on
setting for the
BalanceDelays property of Inport blocks
in the subsystem. For example, if you disable delay balancing for a model or
subsystem that contains an Inport block with
BalanceDelays set to on
, you do not
need to disable delay balancing for that Inport block.
Delay Balancing Limitations
If delay balancing is unsuccessful, hdlcoder.optimizeDesign
cannot optimize the generated HDL code.
Unsupported Blocks
These blocks and subsystems do not support delay balancing:
Triggered Subsystem
HDL Cosimulation
Data Type Duplicate
Decrement To Zero
Ground
LMS Filter
Model Reference
To VCD File
Magnitude-Angle to Complex
Block Mode Restrictions
Blocks that have NoHDL
architecture do not support
delay balancing.
Subsystem-Level Restrictions
HDL Coder does not support delay balancing if:
The BalanceDelays block property for all instances of an Atomic Subsystem or Model Reference resolves to a different value.
To fix this error, disable
BalanceDelays
for all instances of the Atomic Subsystem or Model Reference.The block is inside a conditional subsystem and has pipeline delays.
A subsystem with
BlackBox
Architecture has the ImplementationLatency block property set to a negative value.To fix this error, for ImplementationLatency, enter a nonnegative integer.
Other Restrictions
HDL Coder does not support delay balancing if:
Delays are introduced in a feedback loop and HDL Coder cannot balance the path delays. For example, if you apply clock-rate pipelining inside a feedback loop, HDL Coder introduces a delay at the clock-rate, which can cause delay balancing to fail.
To reduce the number of clock-rate delays, increase the Oversampling factor.
The sample time is not discrete or the ratio of sample times of the fastest to the slowest rate is too large.
Delay Balancing for Constant Blocks
When you have Constant blocks as inputs inside the DUT
subsystem for which delay balancing is enabled, you can see an initial
simulation mismatch in the validation model. Consider this model inside a DUT
subsystem. The Constant block that outputs a value of
1
has the HDL block property
OutputPipeline set to 1
.
This figure displays the generated validation model. Delay balancing added a
matching delay to the input port to balance the pipeline register inserted for
the Delay block. HDL Coder does not insert a matching delay on the parallel path containing
the Constant block that has the value 2
because the output value of the block is a constant. This delay that is not
inserted results in less resources needed for the
design, but also an initial simulation mismatch.
To resolve the simulation mismatch, in the validation model, manually add a
matching delay at the output of the Constant block that has the
value 2
.
Delay Balancing Report
To see the delay balancing information in the report, before you generate code for each subsystem or model reference, enable the optimization report. In the HDL Code Generation tab, select Report, and then select Generate optimization report.
When you generate code for each subsystem, model reference, or MATLAB Function block, HDL Coder produces the optimization report. Select the Delay Balancing section of the report.
The Delay Balancing Report shows latency changes, pipeline and phase delay at the output ports, and the number of pipelines added at the output ports to match the delays. If delay balancing fails, the report displays the criteria that was violated and the link to any block or subsystem that caused delay balancing to fail.
Related Examples
- Delay Balancing and Validation Model Workflow in HDL Coder
- Control the Scope of Delay Balancing
- Delay Balancing on Multirate Designs