function soc_hwsw_stream_set_parameters (caseNumber)
mdlName = 'soc_hwsw_stream_top';
procMdlName = 'soc_hwsw_stream_proc';
switch caseNumber
    case 2
        FrameSize = 100;
        NumBuffers = 99;
        BurstLength = 50;
        taskDurationData(1).percent = 90;
        taskDurationData(1).dev = 2.9623e-05;
        taskDurationData(1).min = 9.6580e-04;
        taskDurationData(1).mean = 1.0583e-3;
        taskDurationData(1).max = 1.262e-3;
        %
        taskDurationData(2).percent = 10;
        taskDurationData(2).dev = 2.9623e-05;
        taskDurationData(2).min = 7.5900e-04;
        taskDurationData(2).mean = 1.434e-3;
        taskDurationData(2).max = 2.86e-3;
    case 3
        FrameSize = 800;
        NumBuffers = 11;
        BurstLength = 200;
        taskDurationData(1).percent = 90;
        taskDurationData(1).dev = 2.1975e-04;
        taskDurationData(1).min = 7.164e-3;
        taskDurationData(1).mean = 7.858e-3;
        taskDurationData(1).max = 9.367e-3;
        %
        taskDurationData(2).percent = 10;
        taskDurationData(2).dev = 2.1975e-04;
        taskDurationData(2).min = 5.64e-3;
        taskDurationData(2).mean = 10.644e-3;
        taskDurationData(2).max = 21.216e-3;
    case 4
        FrameSize = 1000;
        NumBuffers = 9;
        BurstLength = 250;
        taskDurationData(1).percent = 90;
        taskDurationData(1).dev = 2.6876e-04;
        taskDurationData(1).min = 8.762e-3;
        taskDurationData(1).mean =  9.61e-3;
        taskDurationData(1).max =  11.457e-3;
        %
        taskDurationData(2).percent = 10;
        taskDurationData(2).dev = 2.6876e-04;
        taskDurationData(2).min = 6.886e-3;
        taskDurationData(2).mean = 13.019e-3;
        taskDurationData(2).max = 25.948e-3;
    case 5
        FrameSize = 1600;
        NumBuffers = 5;
        BurstLength = 200;
        taskDurationData(1).percent = 90;
        taskDurationData(1).dev = 4.2786e-04;
        taskDurationData(1).min = 13.95e-3;
        taskDurationData(1).mean = 15.3e-3;
        taskDurationData(1).max = 18.239e-3;
        %
        taskDurationData(2).percent = 10;
        taskDurationData(2).dev = 4.2786e-04;
        taskDurationData(2).min = 10.963e-3;
        taskDurationData(2).mean = 20.726e-3;
        taskDurationData(2).max = 41.309e-3;
    case 6
        FrameSize = 2000;
        NumBuffers = 4;
        BurstLength = 250;
        taskDurationData(1).percent = 90;
        taskDurationData(1).dev = 5.3321e-04;
        taskDurationData(1).min = 17.384e-3;
        taskDurationData(1).mean = 19.067e-3;
        taskDurationData(1).max = 22.73e-3;
        %
        taskDurationData(2).percent = 10;
        taskDurationData(2).dev = 5.3321e-04;
        taskDurationData(2).min = 13.662e-3;
        taskDurationData(2).mean = 25.829e-3;
        taskDurationData(2).max = 51.48e-3;
    case 7
        FrameSize = 2400;
        NumBuffers = 3;
        BurstLength = 300;
        taskDurationData(1).percent = 90;
        taskDurationData(1).dev = 6.3792e-04;
        taskDurationData(1).min = 20.798e-3;
        taskDurationData(1).mean = 22.812e-3;
        taskDurationData(1).max = 27.194e-3;
        %
        taskDurationData(2).percent = 10;
        taskDurationData(2).dev = 6.3792e-04;
        taskDurationData(2).min = 16.345e-3;
        taskDurationData(2).mean = 30.901e-3;
        taskDurationData(2).max = 61.589e-3;
    otherwise
        assert(false,'Only cases 2-7 can be simulated using this script.');
end
showDistr(taskDurationData);
FIFODepth = FrameSize/BurstLength;
set_param([mdlName,'/Processor/Task Manager'],'AllTaskData',['{"taskName":"dataReadTask","taskType":"Event-driven","taskEvent":"dataReadTaskEvent","taskPeriod":0.018,"taskPriority":50,"coreSelection":"Specified core","coreNum":1,"dropOverranTasks":false,"playbackRecorded":false,"diagnosticsFile":"dataReadTask.csv","taskDurationSource":"Dialog","taskDuration":0.003,"taskDurationDeviation":0,"logExecutionData":true,"logDroppedTasks":false,"version":"2.0","taskDurationData":[{"percent":',num2str(taskDurationData(1).percent),',"mean":',num2str(taskDurationData(1).mean) ,',"dev":',num2str(taskDurationData(1).dev) ,',"min":',num2str(taskDurationData(1).min),',"max":',num2str(taskDurationData(1).max),'},{"percent":',num2str(taskDurationData(2).percent),',"mean":',num2str(taskDurationData(2).mean),',"dev":',num2str(taskDurationData(2).dev),',"min":',num2str(taskDurationData(2).min),',"max":',num2str(taskDurationData(2).max),'}]}']);
load_system(procMdlName);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait'],'durpercent',['[' num2str([taskDurationData(1).percent taskDurationData(2).percent]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait'],'durstd',['[' num2str([taskDurationData(1).dev taskDurationData(2).dev]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait'],'durmin',['[' num2str([taskDurationData(1).min taskDurationData(2).min]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait'],'durmax',['[' num2str([taskDurationData(1).max taskDurationData(2).max]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait'],'durmean',['[' num2str([taskDurationData(1).mean taskDurationData(2).mean]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait1'],'durpercent',['[' num2str([taskDurationData(1).percent taskDurationData(2).percent]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait1'],'durstd',['[' num2str([taskDurationData(1).dev taskDurationData(2).dev]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait1'],'durmin',['[' num2str([taskDurationData(1).min taskDurationData(2).min]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait1'],'durmax',['[' num2str([taskDurationData(1).max taskDurationData(2).max]) ' 0 0 0]']);
set_param([procMdlName,'/Processor Algorithm Wrapper/Busy Wait1'],'durmean',['[' num2str([taskDurationData(1).mean taskDurationData(2).mean]) ' 0 0 0]']);
assignin('base','FrameSize',FrameSize);
assignin('base','NumBuffers',NumBuffers);
set_param([mdlName,'/FPGA'],'simmode',get_param([mdlName,'/FPGA'],'simmode'));

currFIFOAFullDepth = hsb.blkcb2.cbutils('GetCSParam', mdlName, 'AXIMemoryInterconnectFIFOAFullDepth');

if FIFODepth < currFIFOAFullDepth
    hsb.blkcb2.cbutils('SetCSParam', mdlName, {'AXIMemoryInterconnectFIFOAFullDepth',num2str(FIFODepth-1)});
    hsb.blkcb2.cbutils('SetCSParam', mdlName, {'AXIMemoryInterconnectFIFODepth',num2str(FIFODepth)});
else
    hsb.blkcb2.cbutils('SetCSParam', mdlName, {'AXIMemoryInterconnectFIFODepth',num2str(FIFODepth)});
    hsb.blkcb2.cbutils('SetCSParam', mdlName, {'AXIMemoryInterconnectFIFOAFullDepth',num2str(FIFODepth-1)});
end

assignin('base','BurstLength',BurstLength);
end

function showDistr(durData)

nLen = 10000;
vals = zeros(1,nLen);
for i=1:nLen
    vals(i) = genCompositeRnd(durData);
end
figure('name','Histogram of Task Execution times');
histogram (vals, 100, 'Normalization', 'probability');
title('Histogram of Task Execution times');
xlabel('Task Execution in seconds');
hold on;
end


% -------------------------------------------------------------------------
function val = genCompositeRnd(durData)

y =  [durData(:).percent];
[yprime, idx] = sort(y, 'ascend');
ysecond = [0 yprime];
limits = [];
for i = 1:numel(ysecond)-1
    val = ysecond(i) + ysecond(i+1);
    limits = [limits val]; %#ok<AGROW>
end
limits = limits/100;
coinFlip = rand(1);
for j=1:numel(yprime)
    if coinFlip < limits(j)
        val = getNormal(durData(idx(j)));
        break
    end
end
end


% -------------------------------------------------------------------------
function val = getNormal(elem)
val = elem.mean + elem.dev * randn(1, 1);
if (val < elem.min)
    val = elem.min;
elseif (val > elem.max)
    val = elem.max;
end
end


