%% Resettable Subsystem Support in HDL Coder(TM)
% This example shows how to use Resettable Subsystems in HDL Coder.

%   Copyright 2016 The MathWorks, Inc.

%% Introduction to Resettable Subsystems
% A Resettable Subsystem is a subsystem that will reset all states within the
% subsystem hierarchy based on a boolean control signal. It does this without
% requiring wiring the reset signal to each stateful block in Simulink. This
% feature allows resetting blocks such as the MATLAB Function Block, which does
% not have an available reset port. For support in HDL Coder, a Resettable
% Subsystem is supported only within a |Synchronous| *StateControl* region.

load_system('hdlcoder_resettable_subsystem');
open_system('hdlcoder_resettable_subsystem/DUT/Resettable Subsystem');
set_param('hdlcoder_resettable_subsystem', 'SimulationCommand', 'update');

%% The Reset Block
% A Resettable Subsystem looks similar to an Enabled Subsystem or any other
% Simulink conditionally executed subsystem in that it has a specialized Reset
% Port block inside it. This control port block has several trigger types
% available. HDL Coder supports the "level hold" trigger type.

open_system('hdlcoder_resettable_subsystem/DUT/Resettable Subsystem/LocalReset');

%% Resettable Subsystem Effects on Generated HDL code
% Resettable Subsystems allow resetting the state of all blocks with state
% inside the subsystem to their initial value. In the generated HDL code, each
% design delay--a delay modeled explicitly in Simulink--will have a reset
% added. Hardware implementation delays such as pipeline delays are not
% reset. The reset signal is a synchronous signal and is entirely independent
% from the global reset signal.

close_system('hdlcoder_resettable_subsystem/DUT/Resettable Subsystem/LocalReset');
makehdl('hdlcoder_resettable_subsystem/DUT');
type hdlsrc/hdlcoder_resettable_subsystem/DUT.vhd

%%
% The MATLAB Function Block does not have support for an explicit reset
% port. When placed in a Resettable Subsystem, HDL Coder will generate a
% synchronous external reset signal to control the resetting of persistent
% variables inside the function.
%
%    function y = fcn(u)
%
%    persistent state;
%
%    if isempty(state)
%        state = fi(0, 1, 33, 20);
%    end
%
%    y = state;
%    state = u;
%    end

type hdlsrc/hdlcoder_resettable_subsystem/MATLAB_Delay_Fcn.vhd

%%
% A synchronous delay signal named "LocalDelay" has been added to the VHDL code
% generated for the delay implemented in the MATLAB Function block.

displayEndOfDemoMessage(mfilename)
