%implements "ModelReference" "C"
%function BlockInstanceSetup(block,system) void
%<LibBlockSetCustomStorageComplianceLevel(block,2)>
%endfunction
%function LibMarkNonFiniteAccessed(block) void
%assign blockInterface = GetModelrefInterface(block)
%assign nonFinite= blockInterface.NonFiniteInfo
%assign infIdx = 0
%assign minusInfIdx = 1
%assign nanIdx = 2
%if nonFinite[infIdx]
%assign dummyInf = LibRealNonFinite("inf")
%endif
%if nonFinite[minusInfIdx]
%assign dummyMInf = LibRealNonFinite("-inf")
%endif
%if nonFinite[nanIdx]
%assign dummyNan = LibRealNonFinite("nan")
%endif
%endfunction
%function ModelBlockCallOutputUpdateWithInitStatusGuard( ...
block, system, sysFcn, tidInChildModel, tidInParentModel, forSingleTasking) Output
%assert(sysFcn == "Output" || sysFcn == "Update" || sysFcn == "OutputUpdate")
%if GetModelrefHasMdlPeriodicPort(block) && ...
!SLibParameterChangeEventTID(tidInParentModel)
%return
%endif
%assign useInitStatusGuard = ...
block.ParamSettings.UseInitStatusGuardForPeriodicOutputUpdate && ...
(forSingleTasking || !SLibNonInlinedModelWideEventTID(tidInParentModel))
%if useInitStatusGuard
%assign initStatDWorkIdx = block.ParamSettings.InitStatusDWorkIdx
%assert(initStatDWorkIdx != -1)
if (%<LibBlockDWork(block.DWork[initStatDWorkIdx],"","",0)> == 1U) {
%endif
%<ModelBlockCallFunction(block, system, sysFcn, tidInChildModel)>
%if useInitStatusGuard
}
%endif
%endfunction
%function ModelRefTidGuard()
%assign tidGuard = ""
%assign orOperator = ""
%assign noGuardNeeded = TLC_FALSE
%assign multTIDs = TLC_FALSE
%if LibTriggeredTID(TID) || ISEQUAL(TID, "constant") || ...
LibAsynchronousTriggeredTID(TID) || ...
SLibIsPeriodicRateGrouping() || SIZE(TID,1) == 1
%assign noGuardNeeded = TLC_TRUE
%else
%assert(TYPE(TID) == "Vector")
%foreach tidIdx = SIZE(TID, 1)
%if TID[tidIdx] == -2
%continue
%endif
%if SLibModelWideEventTID(TID[tidIdx])
%continue
%endif
%assign tidString = LibIsSampleHit(TID[tidIdx])
%if tidString == "1"
%assign noGuardNeeded = TLC_TRUE
%endif
%assign multTIDs = !ISEMPTY(tidGuard)
%assign tidGuard = tidGuard + "%<orOperator>%<tidString>"
%assign orOperator = " || "
%endforeach
%if !multTIDs
%assign noGuardNeeded = TLC_TRUE
%endif
%endif
%return (noGuardNeeded) ? "" : tidGuard
%endfunction
%function GenerateModelRefTIDInfo(block, sysFcn)
%assign res = ["","",""]
%assign tidGuard = ModelRefTidGuard()
%assign needsTID = !Accelerator && GetModelRefFcnNeedsTID(block, sysFcn)
%assign tidOpen = ""
%if (tidGuard != "")
%assign tidOpen = "if (%<tidGuard>) {"
%elseif needsTID
%assign tidOpen = "{"
%endif
%assign tidMap = ""
%if needsTID
%openfile TIDBuffer
%assign tid = LibTID()
%assign else = ""
int tmpTID = %<tid>;
%foreach tidIdx = SIZE(TID,1)
%<else> if (% == tmpTID) {
%<tid> = %<tidIdx>;
}
%assign else = "else"
%endforeach
%closefile TIDBuffer
%assign tidMap = TIDBuffer
%endif
%assign tidClose = ""
%if needsTID
%openfile tidCloseBuffer
%<tid> = tmpTID;
}
%closefile tidCloseBuffer
%assign tidClose = tidCloseBuffer
%elseif (tidGuard != "")
%assign tidClose = "}"
%endif
%assign res[0] = tidOpen
%assign res[1] = tidMap
%assign res[2] = tidClose
%return res
%endfunction
%function mdlrefIsPortConstantSampleTime(block, port)
%if(ISEQUAL(block.TID, "constant"))
%return TLC_TRUE
%elseif(ISEQUAL(block.TID, "triggered"))
%assign retValue = ((! ISEMPTY(block.TriggeredBlockConstantSampleTimePorts)) && ...
(block.TriggeredBlockConstantSampleTimePorts[port] == 1))
%return retValue
%else
%if block.PortBasedSampleTimes == "yes"
%assign retValue = (LibBlockOutputSignalSampleTimeIndex(port) == -2)
%else
%assign retValue = TLC_FALSE
%endif
%return retValue
%endif
%endfunction
%function SetupRuntimeResources(block, system) Output
%assign sysFcn = "SetupRuntimeResources"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%if ISFIELD(block.ParamSettings, "InitStatusDWorkIdx")
%assign initStatDWorkIdx = block.ParamSettings.InitStatusDWorkIdx
%assert(initStatDWorkIdx != -1)
%<LibBlockDWork(block.DWork[initStatDWorkIdx],"","",0)> = 0U;
%endif
%if ISFIELD(block.ParamSettings, "InitRestoreDataDWorkIdx")
%assign restoreDataDWorkIdx = block.ParamSettings.InitRestoreDataDWorkIdx
%assert(restoreDataDWorkIdx != -1)
%if SLibModelBlockRTMAllocateInParent(block, blockInterface)
%assign rtmArg = block.Identifier
memcpy((void *) &(%<LibBlockDWork(block.DWork[restoreDataDWorkIdx],"","",0)>.rtm),
(void *) &(%<rtmArg>), sizeof(%<rtmArg>));
%else
memcpy((void *) &(%<LibBlockDWork(block.DWork[restoreDataDWorkIdx],"","",0)>),
(void *) &(%<LibBlockDWork(block.DWork[0],"","",0)>),
sizeof(%<LibBlockDWork(block.DWork[0],"","",0)>));
%endif
%endif
%if blockInterface.HasPrivateDWork
%assign mdlName = block.ParamSettings.ReferencedModelName
%<SLibGetCreateInitRestoreDataFcnNameForModel(mdlName)>();
%endif
%endfunction
%function CleanupRuntimeResources(block, system) Output
%assign sysFcn = "CleanupRuntimeResources"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%if blockInterface.HasPrivateDWork
%assign mdlName = block.ParamSettings.ReferencedModelName
%<SLibGetDestroyInitRestoreDataFcnNameForModel(mdlName)>();
%endif
%endfunction
%function Start(block, system) Output
%if Accelerator
%assert(0)
%elseif !ISFIELD(block.ParamSettings, "InitStatusDWorkIdx")
%<LibMarkNonFiniteAccessed(block)>
%assign sysFcn = "Start"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<GetFunctionStr(block, sysFcn, 0, "", 0, "")>
%endif
%foreach portIdx = block.NumDataOutputPorts
%if (mdlrefIsPortConstantSampleTime(block, portIdx))
%assign storageClass = LibBlockOutputSignalStorageClass(portIdx)
%if (ISEQUAL(storageClass, "ImportedExtern") || ...
ISEQUAL(storageClass, "ImportedExternPointer"))
%assign op = FcnGetOutputPortRecord(portIdx)
%assign bo = SLibGetSourceRecord(op, 0)
%if ! ISEMPTY(bo.InitialValue)
%assign id = LibGetRecordIdentifier(bo)
%<FcnInitDataInitialValue(bo, id)>
%endif
%endif
%endif
%endforeach
%endif
%endfunction
%function InitializeConditions(block, system) Output
%assign sysFcn = "Initialize"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function SystemInitialize(block, system) Output
%assign sysFcn = "SystemInitialize"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function SystemReset(block, system) Output
%assign sysFcn = "SystemReset"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function Enable(block, system) Output
%assign sysFcn = "Enable"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function Disable(block, system) Output
%assign sysFcn = "Disable"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function ModelRefBlockHasFcn(block, fcnType, tid)
%assign blockInterface = GetModelrefInterface(block)
%assign retVal = TLC_FALSE
%with block
%if !blockInterface.RateGrouped
%if TYPE(tid) == "Number"
%foreach tidIdx = NumSampleTimes
%assign localTid = MapParentTID2ChildTID(block, tidIdx)
%if (localTid == tid) && SLibNonInlinedModelWideEventTID(tidIdx)
%return ISFIELD(blockInterface, "%<fcnType>TID%<localTid>Fcn")
%endif
%endforeach
%endif
%assign retVal = ISFIELD(blockInterface, "%<fcnType>Fcn")
%else
%if TYPE(tid) == "Number"
%assign retVal = ISFIELD(blockInterface, "%<fcnType>TID%<tid>Fcn")
%else
%foreach tidIdx = NumSampleTimes
%if SLibSynchronousTID(tidIdx) || SLibNonInlinedModelWideEventTID(tidIdx)
%assign localTid = MapParentTID2ChildTID(block, tidIdx)
%if localTid >= 0
%if ISFIELD(blockInterface, "%<fcnType>TID%<localTid>Fcn")
%assign retVal = TLC_TRUE
%break
%endif
%endif
%endif
%endforeach
%endif
%endif
%endwith
%return retVal
%endfunction
%function ModelBlockCallFunction(block, system, sysFcn, localTid) Output
%if Accelerator
/* Call into Simulink */
%if sysFcn == "SystemInitialize"
%<SLibCallMdlRefSystemInitializeInSimulink(system, block)>
%elseif sysFcn == "SystemReset"
%<SLibCallMdlRefSystemResetInSimulink(system, block)>
%else
%assign accFunction = GetAcceleratorStringForFunction(sysFcn)
%<SLibCallBlockInSimulink(system, block, accFunction)>
%endif
%else
%<GetFunctionStr(block, sysFcn, 0, localTid, 0, "")>
%endif
%endfunction
%function Outputs(block, system) Output
%assign blockInterface = GetModelrefInterface(block)
%with block
%if !blockInterface.RateGrouped || Accelerator
%if ModelRefBlockHasFcn(block,"Output","")
%assign sysFcn = "Output"
%elseif ModelRefBlockHasFcn(block,"OutputUpdate","")
%assign sysFcn = "OutputUpdate"
%else
%return
%endif
%assign res = GenerateModelRefTIDInfo(block, "%<sysFcn>Fcn")
%assign tidOpen = res[0]
%assign tidMap = res[1]
%assign tidClose = res[2]
%<tidOpen>
%<tidMap>
%<ModelBlockCallOutputUpdateWithInitStatusGuard( ...
block, system, sysFcn, "", "", TLC_TRUE)>
%<tidClose>
%else
%foreach tidIdx = NumSampleTimes
%if SLibSynchronousTID(tidIdx)
%assign tidString = LibIsSampleHit(tidIdx)
%if tidString == "1"
%<OutputsForTID(block,system,tidIdx)>
%else
%openfile tmpBuf
%<OutputsForTID(block,system,tidIdx)>
%closefile tmpBuf
%if !WHITE_SPACE(tmpBuf)
if (%<tidString>) {
%<tmpBuf>
}
%endif
%endif
%endif
%endforeach
%endif
%endwith
%endfunction
%function OutputsForTID(block, system, tid) Output
%if (SIZE(TID,1) == 1 && ISEQUAL(TID, tid) && SLibParameterChangeEventTID(tid))
%<Outputs(block, system)>
%return
%endif
%assign blockInterface = GetModelrefInterface(block)
%assign tidInChildModel = MapParentTID2ChildTID(block, tid)
%assert !ModelBlockHasAsyncInput(tid, block) || SLibInitResetTermEventTID(tid)
%if tidInChildModel >= 0
%if ModelRefBlockHasFcn(block,"Output",tidInChildModel)
%assign sysFcn = "Output"
%elseif ModelRefBlockHasFcn(block,"OutputUpdate",tidInChildModel)
%assign sysFcn = "OutputUpdate"
%else
%return
%endif
%else
%return
%endif
%<ModelBlockCallOutputUpdateWithInitStatusGuard( ...
block, system, sysFcn, tidInChildModel, tid, TLC_FALSE)>
%endfunction
%function Update(block, system) Output
%assign blockInterface = GetModelrefInterface(block)
%with block
%if !blockInterface.RateGrouped || Accelerator
%if ModelRefBlockHasFcn(block,"Update","")
%assign res = GenerateModelRefTIDInfo(block, "UpdateFcn")
%assign tidOpen = res[0]
%assign tidMap = res[1]
%assign tidClose = res[2]
%<tidOpen>
%<tidMap>
%<ModelBlockCallOutputUpdateWithInitStatusGuard( ...
block, system, "Update", "", "", TLC_TRUE)>
%<tidClose>
%endif
%else
%foreach tidIdx = NumSampleTimes
%if SLibSynchronousTID(tidIdx) || SLibNonInlinedModelWideEventTID(tidIdx)
%assign tidString = LibIsSampleHit(tidIdx)
%if tidString == "1"
%<UpdateForTID(block,system,tidIdx)>
%else
%openfile tmpBuf
%<UpdateForTID(block,system,tidIdx)>
%closefile tmpBuf
%if !WHITE_SPACE(tmpBuf)
if (%<tidString>) {
%<tmpBuf>
}
%endif
%endif
%endif
%endforeach
%endif
%endwith
%endfunction
%function UpdateForTID(block, system, tid) Output
%if (SIZE(TID,1) == 1 && ISEQUAL(TID, tid) && SLibParameterChangeEventTID(tid))
%<Update(block, system)>
%return
%endif
%assign blockInterface = GetModelrefInterface(block)
%assign localTid = MapParentTID2ChildTID(block, tid)
%assert !ModelBlockHasAsyncInput(tid, block)
%if localTid < 0 || !ModelRefBlockHasFcn(block,"Update",localTid)
%return
%endif
%<ModelBlockCallOutputUpdateWithInitStatusGuard( ...
block, system, "Update", localTid, tid, TLC_FALSE)>
%endfunction
%function SetDims(block, system, oIdx, rIdx) Output
%assign sysFcn = "SetDims"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%if Accelerator
/* Call into Simulink for MEX-version of S-function */
%<SLibCallSFcnSetDimsInSimulink(system, block, oIdx, rIdx)>
%else
%assign outAndRuleIdx = ["%<oIdx>", "%<rIdx>"]
%<GetSetDimsFunctionStr(outAndRuleIdx, block, sysFcn, 0, "", 0, "")>
%endif
%endif
%endfunction
%function FinalizeAllDims(block, system) Output
%assign sysFcn = "FinalizeDims"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function ZeroCrossings(block, system) Output
%assign sysFcn = "ZeroCrossing"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function Projection(block, system) Output
%assign sysFcn = "Projection"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function Derivatives(block, system) Output
%assign sysFcn = "Derivative"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function ForcingFunction(block, system) Output
%assign sysFcn = "ForcingFunction"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%else
%<Derivatives(block,system)>
%endif
%endfunction
%function MassMatrix(block, system) Output
%assign sysFcn = "MassMatrix"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(blockInterface, "%<sysFcn>Fcn")
%assert (::CompiledModel.ModelIsLinearlyImplicit == "yes")
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction
%function Terminate(block, system) Output
%assign sysFcn = "Terminate"
%assign blockInterface = GetModelrefInterface(block)
%if ISFIELD(block.ParamSettings, "InitStatusDWorkIdx")
%assign initStatDWorkIdx = block.ParamSettings.InitStatusDWorkIdx
%assert(initStatDWorkIdx != -1)
if (%<LibBlockDWork(block.DWork[initStatDWorkIdx],"","",0)> == 1U) {
%<CallModelBlockTerminatePortFunction(block, blockInterface, "")>
}
%elseif ISFIELD(blockInterface, "%<sysFcn>Fcn")
%assign termInfo = GetModelWideEventFcnInfo(blockInterface, "PowerDownEvent", "")
%if termInfo.eventTid != -1 && !ISEMPTY(termInfo.fcnName) && ::isRAccel
%<SLibCallModelBlockFunction([], block, ...
"OutputFcn", blockInterface, ...
GETFIELD(blockInterface, termInfo.fcnName), 0, termInfo.eventTid, 0, "")>
%endif
%<ModelBlockCallFunction(block, system, sysFcn, "")>
%endif
%endfunction