% This script is a modified version of the auto-generated aximaster script
% for the soc_image_rotation example.

% NOTE: You must program the FPGA with the generated bitstream file before
% running this script.
%       You can use the provided, pre-generated bitstream files:
%       soc_image_rotation-zc706.bit /
%       soc_image_rotation-kc705.bit /
%       soc_image_rotation-arty.bit/
%       soc_image_rotation-zcu102.bit/
%       soc_image_rotation-XilinxZynqUltraScale_RFSoCZCU111EvaluationKit.bit/
%       soc_image_rotation-a10soc.sof

% Copyright 2018-2019 The MathWorks, Inc.

%%
% Load saved address info for generated IP cores.
% This file contains register address information for IP cores used by this script. 
% Example commands to read/write these registers are provided below.
load('soc_image_rotation_zc706_aximaster.mat');
% Replace 'zc706' with 'kc705', 'arty', 'zcu102', 'XilinxZynqUltraScale_RFSoCZCU111EvaluationKit' or 'a10soc' as appropriate.

%% Create hardware object
hwObj = socHardwareBoard('Xilinx Zynq ZC706 evaluation kit','Connect',false);

%% Create socAXIMaster object
AXIMasterObj = socAXIMaster(hwObj);

%% User settings
% Initialize memory contents
% load the existing input figure data 
load('soc_image_rotation_inputdata.mat');
inputFigure = smallImage;
[x, y] = size(inputFigure);
% reformat 
inputImage = uint32(reshape(inputFigure', 1, x*y));
writememory(AXIMasterObj,memRegions.AXI4MasterMemRegion1, inputImage);
% clean the memory region
writememory(AXIMasterObj,memRegions.AXI4MasterMemRegion2, uint32(zeros(1,x*y)));

%% Initialize user IP core(s)
init_imagerotation_ip(AXIMasterObj, imagerotation_ip);

%% Run Testbench
% start the rotation operation
writememory(AXIMasterObj,imagerotation_ip.start, uint32(1));
writememory(AXIMasterObj,imagerotation_ip.start, uint32(0));
% read the processed data from external memory 
outputData = readmemory(AXIMasterObj,memRegions.AXI4MasterMemRegion2, x*y);
% reformat it as figure 
outputImage = char(reshape(outputData,y,x)');
% plot it into ASCII ART 
figure('Name', 'Output ASCII ART', 'NumberTitle', 'off', 'MenuBar', 'None'); 
annotation('textbox', [0 0 1 1], 'String', outputImage, 'FitBoxToText', 'on', ...
    'FontName', 'Courier','FontSize', 5, 'HorizontalAlignment', 'center', ...
    'VerticalAlignment', 'middle', 'FontWeight', 'Bold');


%% Release MATLAB as AXI Master object
AXIMasterObj.release;

%%
% -------------------------------------------------------------------------------
%
% SUPPORT FUNCTIONS
%
% -------------------------------------------------------------------------------
%%

function init_imagerotation_ip(AXIMasterObj, imagerotation_ip)

%% Register space for flipfigure_ip IP core.
writememory(AXIMasterObj,imagerotation_ip.IPCore_Reset, uint32(1));

% AXI4-Lite Memory-Mapped register access
% AXI4-Lite register write (Default value from Register Channel)
writememory(AXIMasterObj,imagerotation_ip.start, uint32(0));
end
