classdef ExecTimeConfig < handle
    %

    %   Copyright 2011-2013 The MathWorks, Inc.
        
    properties (Constant=true) 
        EmptyTimerSrcName='coder_profile_timer';
    end
    
    methods (Static = true, Access = private)
        
        function lIdentifierDataType = getIdentifierDataType(lIdTypeSize) 
            lIdentifierDataType = sprintf('uint%d', lIdTypeSize);
        end
        
    end
    
    methods (Static = true, Access = public)
        
        function idTypeSize = getIdTypeSize(lTargetWordSize)
            
        % Get the number of bits that must be used for model identifiers and section
        % identifiers
            
            maxIdTypeSize = 32; 
            
            % Apply formula to select the word size for 
            % section and model identifiers
            idTypeSize = min([lTargetWordSize maxIdTypeSize]);
            
        end
        
        
        function maxVal = getIdentifierMaxValueFromTargetWordSize(lTargetWordSize)
            
            lIdTypeSize = coder.profile.ExecTimeConfig.getIdTypeSize(lTargetWordSize);
            lIdentifierDataType = coder.profile.ExecTimeConfig.getIdentifierDataType...
                (lIdTypeSize);
            maxVal = uint64((intmax(lIdentifierDataType)/2)-1);
            
        end
        
        
        function type = getIdentifierCDataType(lTargetWordSize)

            idTypeSize = coder.profile.ExecTimeConfig.getIdTypeSize...
                (lTargetWordSize);
            lIdentifierDataType = ...
                coder.profile.ExecTimeConfig.getIdentifierDataType(idTypeSize);            
            type = sprintf('%s_T', lIdentifierDataType);
        
        end        
        
        function type = getCodeInstrNumElsCDataType(lTargetWordSize)

        % Same as identifier type
            type = coder.profile.ExecTimeConfig.getIdentifierCDataType...
                (lTargetWordSize);
        
        end  
        
        function type = getCodeInstrNumElsTypeSize(lTargetWordSize)

        % Same as identifier type
            type = coder.profile.ExecTimeConfig.getIdTypeSize...
                (lTargetWordSize);
        
        end

        
        function taskInfo = getFunctionInfoForProfiling(lFunction)
            
            timingMode = lFunction.Timing.TimingMode;
            switch timingMode
              case 'PERIODIC'
                samplePeriod = lFunction.Timing.SamplePeriod;
                sampleOffset = lFunction.Timing.SampleOffset;                                    
              case 'INHERITED'
                samplePeriod = -1;
                sampleOffset = 0;  
              case {'ONESHOT', 'ASYNCHRONOUS'}
                samplePeriod = Inf;
                sampleOffset = 0;
              case 'RESET'
                samplePeriod = Inf;
                sampleOffset = lFunction.Timing.SampleOffset;
              otherwise
                assert(false, 'Unknown Task Function TimingMode: %s', timingMode);
            end     
            taskName = lFunction.Prototype.Name;
            
            taskInfo = struct('taskName', taskName,...
                              'samplePeriod', samplePeriod, ...
                              'sampleOffset', sampleOffset);
                              
        end
    end
end
