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:
setupImpl
and stepImpl
setupImpl
, outputImpl
,
and updateImpl
To use the outputImpl
and
updateImpl
methods, your System object must also inherit from the matlab.system.mixin.Nondirect
class.
Optionally, if your System object has private state properties, define the
resetImpl
method to initialize them to zero.
Write a top-level design function that creates an instance of your
System object and calls the step
method, or the
output
and update
methods.
Note
The resetImpl
method runs automatically during
System object initialization. For HDL code generation, you cannot
call the public reset
method.
Write a test bench function that exercises the top-level design function.
Generate HDL code.
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 from
matlab.System
. Save the code as
CounterSysObj.m
.
classdef 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 end
stepImpl
method implements the System object functionality. The setupImpl
method
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 = step(obj, u); end
Write 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)); end
Generate HDL code for the DUT function as you would for any other MATLAB® code, but skip fixed-point conversion.