%%============================================================================
%%
%%
%%
%%
%%Abstract:
%%
%%Thissystemfilegeneratesthemodel_targ_data_map.mfilecontaining
%%themappinginfofromtheglobalmemorymapsothatSLcancreatethe
%%datatypetransitiontables.
%%
%%Copyright1994-2010TheMathWorks,Inc.
%%
%if EXISTS("_EXTMODEMAPLIB_") == 0
%assign _EXTMODEMAPLIB_ = 1
 
 
%%Function:FcnDumpExtModeHostMapBody=========================================
%%Abstract:
%%Dumpsthedatatypetransitioninformationforsignalsorparametersas
%%MATLABcode.
%%
%function FcnDumpExtModeHostMapBody(type) Output
  %with GlobalMemoryMap
    %assign structuredData = SLibGetMapStructData(type)
    %assign nTotSects = SLibGetMapNumDataSections(type)
    %switch(type)
      %case "parameter"
    %assign map = "paramMap"
    %assign structureName = "%<LibGetParametersStruct()>"
    %assign sectIdxOffset = 0
    %break
      %case "signal"
    %assign map = "sigMap"
    %assign structureName = "%<LibGetBlockIOStruct()>"
    %assign sectIdxOffset = 0
    %break
      %case "dwork"
    %assign map = "dworkMap"
    %assign structureName = "%<LibGetDWorkStruct()>"
    %assign sectIdxOffset = SLibGetMapNumDataSections("signal")
    %break
      %default
    %assert TLC_FALSE
    %break
    %endswitch
    %assign globalSectIdx = 1
     
    nTotData = 0; %add to this count as we go
    nTotSects = %<nTotSects>;
    sectIdxOffset = %<sectIdxOffset>;
     
    ;%
    ;% Define dummy sections & preallocate arrays
    ;%
    dumSection.nData = -1;
    dumSection.data = [];
     
    dumData.logicalSrcIdx = -1;
    dumData.dtTransOffset = -1;
     
    ;%
    ;% Init/prealloc %<map>
    ;%
    %<map>.nSections = nTotSects;
    %<map>.sectIdxOffset = sectIdxOffset;
    %if (%<nTotSects> > 0)
      %<map>.sections(nTotSects) = dumSection; %prealloc
    %endif
    %<map>.nTotData = -1;
     
    ;%
    ;% Auto data (%<structureName>)
    ;%
    %if !EXISTS(structuredData.NumSections)
      %assign numSections = 0
    %else
      %assign numSections = structuredData.NumSections
    %endif
    %foreach sectionIdx = numSections
      %assign section = structuredData.Section[sectionIdx]
      section.nData = %<section.NumData>;
      section.data(%<section.NumData>) = dumData; %prealloc
       
      %foreach dataIdx = section.NumData
    %assign data = section.Data[dataIdx]
    %if ISFIELD(data,"SL_LogicalSrc")
      %assign mIdx = dataIdx + 1 %% '1' based indexing for M
      ;% %<data.Name>
      %assign varName = "data.SL_LogicalSrc"
      %assign var = %<varName>
      %assign dtTransOffset = CAST("Number",data.OffsetFromBaseAddr)
      %if section.IsComplex
        %assign dtTransOffset = dtTransOffset/2
      %endif
      section.data(%<mIdx>).logicalSrcIdx = %<var>;
      section.data(%<mIdx>).dtTransOffset = %<dtTransOffset>;
    %endif
     
      %endforeach
      nTotData = nTotData + section.nData;
      %<map>.sections(%<globalSectIdx>) = section;
      clear section
       
      %assign globalSectIdx = globalSectIdx + 1
    %endforeach
     
    %if ISFIELD(GlobalMemoryMap,"UnstructuredData")
      ;%
      ;% Non-auto Data (%<type>)
      ;%
      %assign nData = UnstructuredData.NumData
      %foreach dataIdx = nData
    %assign data = UnstructuredData.Data[dataIdx]
    %if data.SL_ExtModeType == type
      %% Any data record without the SL_LogicalSrc field does not
      %% correspond to an element of the Simulink BIO or Aggregate
      %% Parameter tables and can not be included in the data type
      %% tranistion table or the target data map file. An example
      %% is a signal declared as an "exported global". The function
      %% SLibGetMapNumDataSections() in globalmaplib.tlc performs
      %% the same check to return the correct number of data sections.
      %if ISFIELD(data,"SL_LogicalSrc")
           
        ;% %<data.Name>
        section.nData = 1;
        %assign varName = "data.SL_LogicalSrc"
        %assign var = %<varName>
        section.data(1).logicalSrcIdx = %<var>;
        section.data(1).dtTransOffset = 0;
       
        nTotData = nTotData + section.nData;
        %<map>.sections(%<globalSectIdx>) = section;
        clear section
       
        %assign globalSectIdx = globalSectIdx + 1
      %endif
    %endif
      %endforeach
    %endif
     
    %if ISFIELD(GlobalMemoryMap,"CustomData")
      ;%
      ;% Custom Data (%<type>)
      ;%
      %assign nDataRecs = NumCustomData
      %foreach dataRecIdx = nDataRecs
    %assign customDataRec = CustomData[dataRecIdx]
    %assign nData = customDataRec.NumData
     
      %foreach dataIdx = nData
      %if nData == 1
        %assign data = customDataRec.Data
      %else
        %assign data = customDataRec.Data[dataIdx]
      %endif
          %if SLibIsGlobalMapDataWithNoExternalLinkage(data)
            %continue
          %endif
      %if (data.SL_ExtModeType == type)
        %% Any data record without the SL_LogicalSrc field does not
        %% correspond to an element of the Simulink BIO or Aggregate
        %% Parameter tables and can not be included in the data type
        %% transition table or the target data map file. An example
        %% is a signal declared as an "exported global". The function
        %% SLibGetMapNumDataSections() in globalmaplib.tlc performs
        %% the same check to return the correct number of data sections.
        %if ISFIELD(data,"SL_LogicalSrc")
       
          ;% %<data.Name>
          section.nData = 1;
          %assign varName = "data.SL_LogicalSrc"
          %assign var = %<varName>
          section.data(1).logicalSrcIdx = %<var>;
          section.data(1).dtTransOffset = 0;
       
          nTotData = nTotData + section.nData;
          %<map>.sections(%<globalSectIdx>) = section;
          clear section
       
          %assign globalSectIdx = globalSectIdx + 1
        %endif
      %endif
    %endforeach
      %endforeach
    %endif
 
    ;%
    ;% Add final counts to struct.
    ;%
    %<map>.nTotData = nTotData;
     
  %endwith
%endfunction
 
 
%%Function:SLibWriteExtModeHostMapFile=======================================
%%Abstract:
%%Theoutputofthisfileistheinterfacetosimulinkfordatamapping.
%%
%%Thisprototypecontainsthefollowingassumptions.TheERTteam
%%wouldneedtomaintainthisandgeneralizethistocoverKai'schanges.
%%
%%oalldatadefinedinSimulinkappearsinthemap(butdoesn'tmatter
%%whereaslongasthisfunctioniswrittencorrectly)
%%oallstructuredparamdataisinrtP
%%oallstructuredblocki/odataisinrtB
%%odatatypesspecifiedinSimulinkarerespectedbyERT
%%
%%Notes
%%oToviewthegeneratedMATLABcodewithproperindentation,tryloadingit
%%intotheMATLABeditororemacswiththemeditingmode.
%%
%function SLibWriteExtModeHostMapFile() void
 
  %assign targDataMapFileName = ""
  %assign modelName = "%<Name>"
  %assign modelNameLen = SIZE(modelName,1)
  %assign targDataMapFileNameSuffix = "_targ_data_map"
  %assign targDataMapFileNameSuffixLen = SIZE(targDataMapFileNameSuffix,1)
  %assign targDataMapFileNameMaxLen = 63
  %assign modelNameAllowableLen = targDataMapFileNameMaxLen - targDataMapFileNameSuffixLen
  %if modelNameLen < modelNameAllowableLen
    %assign modelNameAllowableLen = modelNameLen
  %endif
  %foreach idx = modelNameAllowableLen
    %assign targDataMapFileName = targDataMapFileName + modelName[idx]
  %endforeach
  %assign targDataMapFileName = targDataMapFileName + targDataMapFileNameSuffix + ".m"
   
  %openfile f = targDataMapFileName
  function targMap = targDataMap(),
 
  ;%***********************
  ;% Create Parameter Map *
  ;%***********************
  %<FcnDumpExtModeHostMapBody("parameter")>
 
  ;%**************************
  ;% Create Block Output Map *
  ;%**************************
  %<FcnDumpExtModeHostMapBody("signal")>
 
  ;%*******************
  ;% Create DWork Map *
  ;%*******************
  %<FcnDumpExtModeHostMapBody("dwork")>
 
  ;%
  ;% Add individual maps to base struct.
  ;%
 
  targMap.paramMap = paramMap;
  targMap.signalMap = sigMap;
  targMap.dworkMap = dworkMap;
   
  ;%
  ;% Add checksums to base struct.
  ;%
 
  %assign ModelChecksum0 = SPRINTF("%u", %)
  %assign ModelChecksum1 = SPRINTF("%u", %)
  %assign ModelChecksum2 = SPRINTF("%u", %)
  %assign ModelChecksum3 = SPRINTF("%u", %)
 
  targMap.checksum0 = %<ModelChecksum0>;
  targMap.checksum1 = %<ModelChecksum1>;
  targMap.checksum2 = %<ModelChecksum2>;
  targMap.checksum3 = %<ModelChecksum3>;
 
  %closefile f
 
%endfunction %% SLibWriteExtModeHostMapFile
 
 
%endif %% _EXTMODEMAPLIB_
 
%%[EOF]extmodemaplib.tlc