%% %% dsplib.tlc - Helper functions for DSP Blockset code generation %% %% $Revision: 1.29 $ $Date: 2000/08/11 21:11:21 $ %% %if EXISTS("_DSPLIB_") == 0 %assign _DSPLIB_ = 1 %% =========================================================================== %% Include code-gen framework path: %% %addincludepath "../../dsprtw/util_c" %include "dsp_cgfc.tlc" %% Include the standard DSP Blockset C header file in all generated code: %% %assign MATLAB_ROOT_TMP = FEVAL("matlabroot") %assign MATLAB_ROOT = FEVAL("strrep", MATLAB_ROOT_TMP, "\\", "/") %% %openfile buffer #include "%/toolbox/dspblks/src/rt/dsp_rt.h" /* DSP Blockset */ %closefile buffer % %% =========================================================================== %% Function: JustCreatedCompiledModelRecord ================================== %% Abstract: %% Checks for CompiledModel.recordName %% If record already exists, return with a 0 (false) %% If record does not exist, %% create recordName under CompiledModel %% return with 1 (true) %% %% Example usage: %% %% %if JustCreatedCompiledModelRecord("my_cache") %% %% This is the first time the record was created %% %% %% %else %% %% 2nd or subsequent call (record already exists!) %% %% %% %endif %% %function JustCreatedCompiledModelRecord(recordName) void %assign fullRecordName = "::CompiledModel." + recordName %% %if !EXISTS("%") %assign retval = 1 %% First call %% Create record since it does not yet exist: %assign % = 1 %assign ::CompiledModel = ::CompiledModel + % %undef % %% Remove from current scope %else %assign retval = 0 %% Subsequent call %endif %return retval %endfunction %% JustCreatedCompiledModelRecord %% Function: MAX ============================================================= %% %% Abstract: Return the maxof two variables %% %function MAX(a,b) %return ((a > b) ? a : b) %endfunction %% Function: MIN ============================================================= %% %% Abstract: Return the min of two variables %function MIN(a,b) %return ((a < b) ? a : b) %endfunction %% Function: IsInputPortContiguous =========================================== %% %% Abstract: Determine if an input port's data is contiguous in memory. %% %function IsInputPortContiguous(block,port) %return (STRING(block.Connections.InputPortContiguous[port]) == "yes") %endfunction %% Function: LibGetNumberOfElements ========================================== %% %% Abstract: Get the number of elements in a parameter. %% Equivalent to mxGetNumberOf Elements %% %function LibGetNumberOfElements(X) void %return SIZE(X,0)*SIZE(X,1) %endfunction %% LibGetNumberOfElements %% Function: LibBlockInputSignalIsFullMatrix ================================== %% %% Abstract: Return the max of two variables %% %function LibBlockInputSignalIsFullMatrix(port) Output %assign numDims = LibBlockInputSignalNumDimensions(port) %assign dims = LibBlockInputSignalDimensions(port) %% %return ((numDims > 1) && (dims[0] != 1) && (dims[1] != 1)) %endfunction %% LibBlockInputSignalIsFullMatrix %% Function: IsModelMultiTasking ============================================= %% %% Abstract: Determine if the model is in multitasking mod %% %function IsModelMultiTasking() void %if CompiledModel.SolverType == "FixedStep" %assign isMultiTasking = (CompiledModel.FixedStepOpts.SolverMode == "MultiTasking") %else %assign isMultiTasking = 0 %endif %return isMultiTasking %endfunction %% IsModelMultiTasking %% Function: LibDiscontiguousInputError ====================================== %% %% Abstract: Error message and exit fucntion for blocks that do not support %% codegen for discontiguous inputs. %% %function LibDiscontiguousInputError(block) %assign dspErrorMsg = ("Cannot generate code for discontiguous inputs for " \ "block % (%). Consider inserting the " \ "Contiguous Copy Block before the input to this block. The Contiguous Copy "\ "block is in the DSP Blockset/Signal Management/Signal Attributes library.") %exit % %endfunction %% Function: ErrorIfDiscontiguous %% %% Issues an error if the input port has discontiguous data %% Otherwise, no errors are produced %% %function ErrorIfDiscontiguous(block,port) void %if !IsInputPortContiguous(block,port) % %endif %endfunction %% ErrorIfDiscontiguous %% Function: LibClipRollRegions ============================================= %% %% Abstract: %% Clip the RollRegions vector so that it starts with FIRST index %% and ends with LAST index, where FIRST and LAST are specified %% using 1-based indices. %% %% Synopsis: %% rollRegions = LibClipRollRegions(RollRegions, firstIdx, lastIdx) %% %% Example: Remove first entry from roll region %% %assign rollRegions1 = LibClipRollRegions(RollRegions, 2, -1) %% %% Example: Truncate roll regions %% %assign truncRollRegions = LibClipRollRegions(RollRegions, 1, 5) %function LibClipRollRegions(origRoll, first, last) void %assign strRoll = FEVAL("cliprollregions", "%", first, last) %return % %endfunction %% Function: CreateParamVarStr ================================================== %function CreateParamVarStr(var, name, DType) void %% %% For complex data, creates an "interleaved" data structure %% %% Create a variable from a block parameter. Maximum number %% of values to be rendered per line is hard coded here, so %% we will be consistent. Note that the parameter data type %% has to be the same as the input, but the complextiy can differ. %% %% VAR -- TLC variable which contains the values to be rendered %% NAME -- Name of the C variable to be created %% DTYPE -- Simulink data type (real_T, creal_T, uint8_T, cuint8_T, etc...) %% %assign cmplx = LibIsComplex(var) %assign rows = % %assign cols = % %assign len = rows * cols %assign astr = "" %assign count = 0 %assign maxlen = 6 %% %assign astr = "% %[%] = {\n" %% %foreach Col = cols %foreach Row = rows %if !cmplx %assign astr = astr + "%" %else %assign astr = astr + "{%,%}" %endif %% %assign count = count + 1 %if (count < len) %assign astr = astr + "," %else %assign astr = astr + "};" %endif %% %if (count % maxlen == 0 || count == len) %assign astr = astr + "\n" %endif %% %endforeach %endforeach %return astr+"\n" %endfunction %% CreateParamVarStr %% Function: CreateParamVar ================================================== %function CreateParamVar(var, name, DType) Output %% % %endfunction %% CreateParamVar(var, name) %% Function: CreateParamVarCplxPartsStr ================================================== %function CreateParamVarCplxPartsStr(var, name, DType) void %% %% For complex data, returns two separate declarations, one for the real, %% and one for the imaginary. %% %% Create a variable from a block parameter. Maximum number %% of values to be rendered per line is hard coded here, so %% we will be consistent. Note that the parameter data type %% has to be the same as the input, but the complextiy can differ. %% %% VAR -- TLC variable which contains the values to be rendered %% NAME -- Name of the C variable to be created %% DTYPE -- Simulink data type (real_T, creal_T, uint8_T, cuint8_T, etc...) %% %assign cmplx = LibIsComplex(var) %assign rows = % %assign cols = % %assign len = rows * cols %assign astr = "" %assign count = 0 %assign maxlen = 6 %% %assign re_str = "% %_re[%] = {\n" %if !cmplx %assign im_str = "" %else %assign im_str = "% %_im[%] = {\n" %endif %% %foreach Col = cols %foreach Row = rows %assign re_str = re_str + "%" %if cmplx %assign im_str = im_str + "%" %endif %% %assign count = count + 1 %if (count < len) %assign re_str = re_str + "," %if cmplx %assign im_str = im_str + "," %endif %else %assign re_str = re_str + "};" %if cmplx %assign im_str = im_str + "};" %endif %endif %% %if (count % maxlen == 0 || count == len) %assign re_str = re_str + "\n" %if cmplx %assign im_str = im_str + "\n" %endif %endif %% %endforeach %endforeach %assign retStr = re_str %if cmplx %assign retStr = retStr + "\n" + im_str %endif %return retStr + "\n" %endfunction %% CreateParamVarCplxPartsStr %% Function: getRealOrComplexDataType ============================================= %% Abstract: %% Returns string with data type name (real64_T, creal32_T, etc) %% Handles both real and complex data types. %% %function getRealOrComplexDataType(DTypeId, Cplx) %return (Cplx) ? LibGetDataTypeComplexNameFromId(DTypeId) \ : LibGetDataTypeNameFromId(DTypeId) %endfunction %% getRealOrComplexDataType %% Function: LibTaskSFcnComment ============================================== %% Abstract: %% Generate a C-style comment indicating the sample- and offset- %% times for the S-Function TID. The TID must be any of the %% valid TID inputs that LibGetGlobalTIDFromLocalSFcnTID accepts. %% String includes the comment delimiters (both /* and */) %% %% Example output: %% /* Sample time: [1.0, 0.0] */ %% %% Example calls: %% For port-based sample times: %% LibTaskSFcnComment("InputPortIdx0") %% LibTaskSFcnComment("OutputPortIdx0") %% %% For block-based sample times: %% LibTaskSFcnComment(0) %% %function LibTaskSFcnComment(sfcnTID) Output %% %\ %% %endfunction %% LibTaskSFcnComment %% Function: DSPAddToFile ================================================== %% Abstract: %% Write out the function code cache for outputs, updates, etc. %% %function DSPAddToFile(fcnbuffer) void %assign fileroot = "%<::CompiledModel.Name>_dsp" %assign fileDotC = "%.c" %assign fileDotH = "%.h" %if % %openfile topbuffer /* * % * * DSP Blockset function calls %with CompiledModel %\ %endwith */ #include "%" %closefile topbuffer % %% This adds file to list of sources to build % %endif % %endfunction %% DSPAddToFile %% Function: DSPAddToFileHeader ================================================= %% Abstract: %% Write out the function code cache for outputs, updates, etc. %% %function DSPAddToFileHeader(buffer) void %assign fileDotH = "%<::CompiledModel.Name>_dsp.h" %if % %openfile topbuffer /* * % * * DSP Blockset function calls %with CompiledModel %\ %endwith */ %assign mat_root = FEVAL("matlabroot") #include "%/toolbox/dspblks/src/rt/dsp_rt.h" %% %% Embedded C-format uses different simstruct %% definition and different macros to access it. %% %if CodeFormat == "Embedded-C" #include "ertformat.h" #include "%" %else #include "simstruc.h" extern SimStruct *const %; %endif %closefile topbuffer % %% Add include line to model_common.h file %openfile includebuffer /* DSP Blockset header for extern functions */ #include "%" %closefile includebuffer % %endif % %endfunction %% DSPAddToFileHeader %endif %% _DSPLIB_ %% [EOF] dsplib.tlc