function lsramStruct = claLSRAMMemoryConfig(hCS)
% claLSRAMMemoryConfig Returns a structure of start addresses and length of
% data and program memory of CPU AND CLA

% Copyright 2021-22 The MathWorks, Inc.
lsramStruct = '';
targetData = codertarget.targethardware.getTargetHardware(hCS);
claData = codertarget.data.getParameterValue(hCS,'CLA');
if ~claData.LSConfigEnable && ismember(targetData.SubFamily,{'28004x','28003x'})
    % keep default same for f28004x and f28003x
    % If memory configuration is off, keep start address and
    % length same as previous releases for f28004x
    cpuProgStart = '0xB000';
    cpuProgLength = '0x1000';
    claProgStart = '0xA800';
    claProgLength = '0x800';
    claDataStart = '0x8000';
    claDataLength = '0x1000';
    cpuDataStart = '0x9000';
    cpuDataLength = '0x1800';
else
    % Get start and length from config set if memory config
    % is on for processors other than f28004x.
    % Note: For othe processors, it will result in same
    % default address as previous releases
    % Note: For f28004x and f28003x, if memory config is on, default start addresses change
    cpuLength = claData.CPULSLength;
    claProgLength = claData.PMLength;
    claDataLength = claData.DMLength;
    % Keeping start address of CPU memory as 0x8000
    cpuProgStart = '0x8000';
    if ismember(targetData.SubFamily,{'28004x','28003x'})
        % Divide CPU LSxRAM memory between CPU Program and CPU Data
        % note Configset sends data in string format and
        % the value represents memory 1 LSRAM in KW i.e. 2
        cpuLength = str2num(cpuLength)/2;
        if mod(cpuLength,2) == 0
            cpuProgLength = cpuLength;
            cpuDataLength = cpuLength;
        else
            cpuDataLength = cpuLength+1;
            cpuProgLength = cpuLength*2 - cpuDataLength;
        end
        % convert memory length to hex numbers
        cpuProgLength = ['0x', dec2hex(cpuProgLength*0x400)];
        cpuDataLength = ['0x', dec2hex(cpuDataLength*0x400)];
        claProgLength = ['0x', dec2hex(str2num(claProgLength)*0x400)];
        claDataLength = ['0x', dec2hex(str2num(claDataLength)*0x400)];
        % CPU prog -> CPU data -> CLA data -> CLA Prog
        cpuDataStart = dec2hex(hex2dec(cpuProgStart) + hex2dec(cpuProgLength));
        cpuDataStart = ['0x', cpuDataStart];
        claDataStart = dec2hex(hex2dec(cpuDataStart) + hex2dec(cpuDataLength));
        claDataStart = ['0x', claDataStart];
        claProgStart = dec2hex(hex2dec(claDataStart) + hex2dec(claDataLength));
        claProgStart = ['0x', claProgStart];
    else
        % Keeping entire CPU LSxRAM memory as CPU Program
        % for processors other than f28004x and f28003x
        cpuDataLength = '';
        cpuDataStart = '';
        cpuProgLength  = cpuLength;
        % convert memory length to hex numbers
        cpuProgLength = ['0x',dec2hex(str2num(cpuProgLength)*0x400)];
        claProgLength = ['0x', dec2hex(str2num(claProgLength)*0x400)];
        claDataLength = ['0x', dec2hex(str2num(claDataLength)*0x400)];
        % Get Start Addresses for CLA Data and Program memory
        % CPU LSRAM Start Address = 0x8000
        % CPU -> CLA Data -> CLA Program
        claDataStart = dec2hex(hex2dec(cpuProgStart) + hex2dec(cpuProgLength));
        claDataStart =['0x',claDataStart];
        claProgStart= dec2hex(hex2dec(claDataStart) + hex2dec(claDataLength));
        claProgStart = ['0x',claProgStart];
    end
end
lsramStruct.cpuProgStart = cpuProgStart;
lsramStruct.cpuProgLength = cpuProgLength;
lsramStruct.cpuDataStart = cpuDataStart;
lsramStruct.cpuDataLength = cpuDataLength;
lsramStruct.claProgStart = claProgStart;
lsramStruct.claProgLength = claProgLength;
lsramStruct.claDataStart = claDataStart;
lsramStruct.claDataLength = claDataLength;
end
