%% Resolve Numerical Mismatch with Delay Balancing
% This example shows how to use delay balancing to resolve a numerical mismatch
% between the generated model and original model after HDL code generation.
%% Problem
% The issue is that simulating the validation model results in a numerical
% mismatch between the original model and the generated model after HDL
% code generation. To illustrate this issue:
%%
% 1. Open the |hdlcoder_resolve_delaybalancing| model. The DUT is a simple multirate design. 
modelname = 'hdlcoder_resolve_delaybalancing';
dutname = 'hdlcoder_resolve_delaybalancing/Subsystem';
load_system(modelname)
open_system(dutname)
set_param(modelname, 'SimulationCommand', 'update');
%% 
% 2. Generate HDL code and validation model for the DUT.
makehdl(dutname,'GenerateValidationModel', 'on', ...
                    'TargetDirectory','C:/Temp/hdlsrc')
%%
% 3. View the validation model. The validation model compares the generated model 
% with the original model. The generated model displays the effect of
% optimizations and block-specific architectures that you specify. Use the
% validation model to verify that the DUT in the generated model is bit-true to the
% numerical results produced by the original DUT.
valmodelname = 'gm_hdlcoder_resolve_delaybalancing_vnl';
valmodelsubsys = 'gm_hdlcoder_resolve_delaybalancing_vnl/Subsystem';
load_system(valmodelname)
open_system(valmodelsubsys)
set_param(valmodelname, 'SimulationCommand', 'update');
%%
% 4. Simulate the validation model. HDL Coder(TM) generates warnings that
% indicate an assertion detected at various time stamps. If you navigate
% through the validation model by double-clicking the |Compare| Subsystem and then the
% |Assert_Out1| Subsystem, you see a |compare: Out1| Scope block. This
% Scope block compares the output of the original model DUT with the generated model
% DUT and displays numerical differences as an error signal. When you
% double-click the Scope block, you see a nonzero error, which indicates a
% numerical mismatch.
%
% <<../compare_error_validationmodel.png>>
%% Cause
% To diagnose this issue:
%%
% 1. Observe the parameters saved on the original model. You see that |BalanceDelays|
% is set to |off| on the model.
hdlsaveparams(modelname)
%%
% 2. Inspect the validation model. Inside the DUT
% Subsystem, you see that the code generator implemented the reciprocal
% square root operation as a Subsystem. If you double-click the Sqrt
% Subsystem, you see that the implementation has a latency. This latency
% arises due to the Newton-Raphson implementation of reciprocal square
% root. 
open_system('gm_hdlcoder_resolve_delaybalancing_vnl/Subsystem/Sqrt')
%%
% The simulation mismatch occurred because the Newton-Raphson choice for implementing
% the Reciprocal Sqrt block results in a latency difference between the original model
% and the generated model. In addition, the downsampling introduced by the Rate Transition
% block drops samples. As delay balancing is disabled on the model, the
% code generator did not add matching delays to account for this latency.
%% Solution
% To fix this issue, enable delay balancing on the model. In the original model, set 
% |BalanceDelays| to |on|. When you enable delay balancing, the code generator detects
% introduction of delays along one path and adds matching delays on other, parallel
% signal paths. 
%%
% 1. Enable |BalanceDelays| on the model and generate HDL code and validation model.
load_system(modelname)
makehdl(dutname,'BalanceDelays','on', ...
                'GenerateValidationModel', 'on', ...
                    'TargetDirectory','C:/Temp/hdlsrc')
%%
% 2. Open the validation model. You see that the code generator
% introduced matching delays to balance the latency introduced by the Sqrt 
% block and to offset the effect of downsampling. The additional delays
% account for the latency difference.
load_system(valmodelname)
open_system(valmodelsubsys)
set_param(valmodelname, 'SimulationCommand', 'update');
%%
% 3. Simulate the validation model and open the |compare: Out1| Scope
% block. You see that the numerical mismatch has been resolved.
%
% <<../compare_zeroerror_validationmodel.png>>

             





