Generate Code for User-Defined System Objects
How To Create A User-Defined System object
To create a user-defined System object™ and generate code:
Create a class that subclasses from
matlab.System.Define one of the following sets of methods:
setupImplandstepImplsetupImpl,outputImpl, andupdateImpl
Optionally, if your System object has private state properties, define the
resetImplmethod to initialize them to zero.Write a top-level design function that creates an instance of your System object and calls the object, or calls the
outputandupdatemethods.Note
The
resetImplmethod runs automatically during System object initialization. For HDL code generation, you cannot call the publicresetmethod.Write a test bench function that exercises the top-level design function.
Generate HDL code.
User-Defined System object Example
This example shows how to generate HDL code for a user-defined System object that implements the setupImpl and
stepImpl methods.
In a writable folder, create a System object,
CounterSysObj, which subclasses frommatlab.System. Save the code asCounterSysObj.m.Theclassdef CounterSysObj < matlab.System properties (Nontunable) Threshold = int32(1) end properties (Access=private) State Count end methods function obj = CounterSysObj(varargin) setProperties(obj,nargin,varargin{:}); end end methods (Access=protected) function setupImpl(obj, ~) % Initialize states obj.Count = int32(0); obj.State = int32(0); end function y = stepImpl(obj, u) if obj.Threshold > u(1) obj.Count(:) = obj.Count + int32(1); % Increment count end y = obj.State; % Delay output obj.State = obj.Count; % Put new value in state end end endstepImplmethod implements the System object functionality. ThesetupImplmethod defines the initial values for the persistent variables in the System object.Write a function that uses this System object and save it as
myDesign.m. This function is your DUT.function y = myDesign(u) persistent obj if isempty(obj) obj = CounterSysObj('Threshold',5); end y = obj(u); endWrite a test bench that calls the DUT function and save it as
myDesign_tb.m.clear myDesign for ii=1:10 y = myDesign(int32(ii)); endGenerate HDL code for the DUT function as you would for any other MATLAB® code, but skip fixed-point conversion.