%% Approximate a Function Call by Using Edge-Triggered Events
% If you cannot use a function-call output event, such as for HDL code
% generation, you can approximate a function call by using:
%
% * An edge-triggered output event
% * An enabled subsystem
% * Two consecutive event broadcasts
%
% The queuing behavior of consecutive edge-triggered output events enables
% you to approximate a function call with an enabled subsystem.

% Copyright 2018 The MathWorks, Inc.

%%
% For example, in this model, the edge-triggered output event |output_cmd|
% activates the enabled subsystem.

model = 'sf_edgetrigger_approx_fcncall.slx';
open_system(model)

%%
% The Caller chart broadcasts the edge-triggered output event by using the
% |send| operator.

open_system('sf_edgetrigger_approx_fcncall/Caller');

%%
% When simulation starts, the value of the trigger signal is 0. At time _t_
% = 20, the chart dispatches |output_cmd|, changing the value of the
% trigger signal to 1. The enabled subsystem becomes active and executes
% during that time step. Because no other event broadcasts occur, the
% enabled subsystem continues to execute at every time step until
% simulation ends at _t_= 40. The Display block shows a final value of 40.

sim(model);
open_system('sf_edgetrigger_approx_fcncall/Scope');

%%
% To approximate a function call, add a second event broadcast in the same
% action.

model = 'sf_edgetrigger_approx_fcncall_modified.slx';
open_system(model);
open_system('sf_edgetrigger_approx_fcncall_modified/Caller');

%%
% When simulation starts, the value of the trigger signal is 0. At time _t_
% = 20, the chart dispatches |output_cmd|, changing the value of the
% trigger signal to 1. The enabled subsystem becomes active and executes
% during that time step. The chart queues up the second event for dispatch
% at the next time step. At time _t_= 21, the chart dispatches the second
% output event, which changes the value of the trigger signal back to 0.
% The enabled subsystem stops executing and the Display block shows a final
% value of 20.

sim(model);
open_system('sf_edgetrigger_approx_fcncall_modified/Scope');

%%
% Although you can approximate a function call, there is a subtle
% difference in execution behavior. Execution of a function-call subsystem
% occurs _during_ execution of the chart action that provides the trigger.
% Execution of an enabled subsystem occurs _after_ execution of the chart
% action is complete.
