HDL Coder™ supports several optimizations, block implementations, and options that introduce discrete delays into the model, with the goal of more efficient hardware usage or achieving higher clock rates. Examples include:
Optimizations: Optimizations such as output pipelining, streaming, or resource sharing can introduce delays.
Cascading: Some blocks support cascade implementations, which introduce a cycle of delay in the generated code.
Block implementations: Some block implementations such as the Newton-Raphson 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 because equivalent delays are not introduced on other, parallel signal paths. Manual insertion of compensating delays along the other paths is possible, but is error prone and does not scale well to large models with many signal paths or multiple sample rates.
To help you solve this problem, HDL Coder supports 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 delay balancing is enabled, 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 numerical 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.
You can set delay balancing for an entire model. For finer control, you can also set delay balancing for subsystems within the top-level DUT subsystem.
Use the following
makehdl properties to set delay
balancing for a model:
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, the following commands generate HDL code with delay balancing and generate a validation model.
dut = 'ex_rsqrt_delaybalancing/Subsystem'; makehdl(dut,'BalanceDelays','on','GenerateValidationModel','on');
You can disable delay balancing for an entire model or disable 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 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.
To disable delay balancing for a subsystem within the top-level DUT subsystem:
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. To learn how to set delay balancing for a subsystem, see Set Delay Balancing For a Subsystem.
If delay balancing is unsuccessful,
cannot optimize the generated HDL code.
The following blocks and subsystems do not support delay balancing:
Data Type Duplicate
Decrement To Zero
FFT HDL Optimized
To VCD File
Magnitude-Angle to Complex
Blocks that have
NoHDL architecture do not support
HDL Coder does not support delay balancing, if:
There are multiple instances of an Atomic Subsystem in different conditional subsystems.
In the Block Parameters dialog box of the Atomic
Subsystem, you can set Function
The BalanceDelays block property for all instances of an Atomic Subsystem or Model Reference resolves to a different value.
To fix this error, disable
all instances of the Atomic
Subsystem or Model Reference.
The block is inside a conditional subsystem and has pipeline delays.
A subsystem with
Architecture has the
ImplementationLatency block property set to a
To fix this error, for ImplementationLatency, enter a nonnegative integer.
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, and 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.
When you have Constant blocks as inputs inside the DUT
Subsystem for which delay balancing is enabled, you see an initial simulation
mismatch in the validation model. Consider this model inside a DUT Subsystem.
The Constant block that outputs a value of
has the HDL block property OutputPipeline set to
This figure displays the generated validation model. You see that delay
balancing added a matching delay to the input port to balance the pipeline
register inserted for the Delay block. The code generator does
not insert a matching delay on the parallel path containing the
Constant block with the value
the output value of the block is a constant. This delay not inserted results in
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 with the value
To see the delay balancing information in the report, before you generate code for each subsystem or model reference, enable the optimization report. To enable this report, in the HDL Code tab, select Report Options, 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. In the report, select the Delay Balancing section of the report.
The Delay Balancing Report shows latency changes, pipeline delay 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 mentions the criteria that was violated and displays the link to any block or subsystem that caused delay balancing to fail.