%

% Copyright 2017 The MathWorks, Inc.

%% parameter and data initialization
Matrix_Size = 64;
hdlcoder_external_memory_init;

if ~Matrix_Multiplication_On && Matrix_Size > 255
error('The maximum Matrix_Size of the loop back mode is 255, this is due the size constraint of the internal RAM');
end

%% simulate the model
sim('hdlcoder_external_memory');

%% verify DDR data initialization
% read logged DDR initialization data
ddr3ReadData = reshape( DDR_WR_DATA(:,:,1:Vector_Matrix_Length),1,Vector_Matrix_Length);

% verify that the DDR is initialized correctly
cmp_IO = (ddrInitData(1:Vector_Matrix_Length)) - ddr3ReadData(1:Vector_Matrix_Length) ;
CountNonZeroValues=find(cmp_IO~=0);
if isempty(CountNonZeroValues)
disp('PASSED: DDR initialization data matches.');
else
disp('FAILED: DDR initialization data mismatches.');
end

%% result comparison
if Matrix_Multiplication_On

% compute expected matrix vector multiplication result
vectorIn = reshape(ddrInitData(1:Matrix_Size), Matrix_Size, 1);
matrixIn = reshape(ddrInitData(Matrix_Size+1 : Matrix_Size+Matrix_Size^2), Matrix_Size, Matrix_Size);
matrixOut = double(matrixIn') * double(vectorIn);

% matrix vector multiplication result
ddr3MatMul = reshape( DDR_WR_DATA(:,:,Vector_Matrix_Length+1:Vector_Matrix_Length+Matrix_Size),Matrix_Size,1);

% verify matrix vector multiplication result
cmp_MatMul = (matrixOut) - double(ddr3MatMul);
CountNonZeroMatMul = find(cmp_MatMul>Single_Tolerance);
if isempty(CountNonZeroMatMul)
disp('PASSED: Matrix vector multiplication output matches with the expected data')
else
disp('FAILED: Matrix vector multiplication output mismatches.');
end

else
% data loop back mode result
ddr3WriteData = reshape( DDR_WR_DATA(:,:,Vector_Matrix_Length+1:Vector_Matrix_Length*2),1,Vector_Matrix_Length);

% verify DDR write result
cmp_IO = (ddrInitData(1:Vector_Matrix_Length)) - ddr3WriteData(1:Vector_Matrix_Length) ;
CountNonZeroValues=find(cmp_IO~=0);
if isempty(CountNonZeroValues)
disp('PASSED: DDR data loop back result matches.');
else
disp('FAILED: DDR data loop back result mismatches.');
end

end

% LocalWords: DDR