% This script is a modified version of auto-generated aximaster script for
% soc_memory_traffic_generator 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_memory_traffic_generator-zc706.bit /
%       soc_memory_traffic_generator-kc705.bit
%

% Copyright 2018-2019 The MathWorks, Inc.

%% Load saved AXI master info
% This file contains information used by this script
% Available structs in this file: perf_mon, atg, memRegions
load('soc_memory_traffic_generator_zc706_aximaster.mat');
% Replace 'zc706' with 'kc705' as appropriate.

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

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

%% User settings

% AXI Traffic Generator parameters
% Generated from soc_memory_traffic_generator/Memory Traffic Generator1
atg(1).ReadWrite = 'w';% Read/Write direction
% Generated from soc_memory_traffic_generator/Memory Traffic Generator2
atg(2).ReadWrite = 'w';% Read/Write direction
% Generated from soc_memory_traffic_generator/Memory Traffic Generator3
atg(3).ReadWrite = 'w';% Read/Write direction
% Generated from soc_memory_traffic_generator/Memory Traffic Generator4
atg(4).ReadWrite = 'w';% Read/Write direction

% Performance monitor parameters
perfMonMode = 'Profile';% Profile/Trace direction

%% Initialize built-in IP core(s) 

% Create IPCore object for performance monitor core
apmCoreObj=socIPCore(AXIMasterObj, perf_mon,'PerformanceMonitor','Mode',perfMonMode); % Mode is Profile/Trace
% Create IPCore object for traffic generator core(s)
atgCoreObj=socIPCore(AXIMasterObj, atg,'TrafficGenerator');
% Initialize instantiated traffic generator cores
initialize(atgCoreObj);

initialize(apmCoreObj);
%% Run testbench

switch perfMonMode 
    case 'Profile'...

        profilerObj=socMemoryProfiler(hwObj,apmCoreObj);
        % Enable ATG(s)
        start(atgCoreObj);

        % Adjust 'NumRuns' variable to change the number of consecutive runs of the testbench
        NumRuns = 100;
        for n = 1:NumRuns
            collectMemoryStatistics(profilerObj);

        end

        % Plot the Performance
        plotMemoryStatistics(profilerObj);
    case 'Trace'...

        profilerObj=socMemoryProfiler(hwObj,apmCoreObj);
        % Enable ATG(s)
        start(atgCoreObj);

        % Stop trace mode and obtain trace data.
        % Make sure AXI transactions finished before stopping trace mode.
        collectMemoryStatistics(profilerObj);

        % Display diagnostic trace waveforms in Logic Analyzer
        plotMemoryStatistics(profilerObj);
end


%% Release MATLAB as AXI Master object

AXIMasterObj.release;
