%% FPGA-in-the-Loop Simulation Using MATLAB System Object
%
% This example use a MATLAB System object and a FPGA to verify a register
% transfer level (RTL) design of a Fast Fourier Transform (FFT) of size 8
% written in Verilog. The FFT is commonly used in digital signal processing
% to produce frequency distribution of a signal.
%
% To verify the correctness of this FFT, a MATLAB System object testbench
% is provided. This testbench generates a periodic sinusoidal input to the
% HDL design under test (DUT) and plots the Fourier Coefficients in the
% Complex Plane.

%   Copyright 2012 The MathWorks, Inc.

%% Set FPGA Design Software Environment
%
% Before using FPGA-in-the-Loop, make sure your system environment is set
% up properly for accessing FPGA design software. You can use the function
% *hdlsetuptoolpath* to add FPGA design software to the system path for the
% current MATLAB session.
%

%% Launch FilWizard
%
% Launch the FIL Wizard prepopulated with the FFT example information.
% Enter your FPGA board information in the first step, follow every step of
% the Wizard and generate the FPGA programming file and FIL System object.

%%
%   filWizard('fft_hdlsrc/fft8_sysobj_fil.mat');

%% Program FPGA
% 
% Program the FPGA with the generated programming file. Before continuing, make
% sure the FIL Wizard has finished the FPGA programming file generation.
% Also make sure your FPGA board is turned on and connected properly.

%%
%   run('fft8_fil/fft8_programFPGA');

%% Instantiate SineWave System Objects
%
% The following code instantiates the system objects that represent the
% sine wave generator (F=100Hz, Sampling=1000Hz, complex fix point output). 

%%
%   SinGenerator = dsp.SineWave('Frequency ', 100, ...
%                             'Amplitude', 1, ...
%                             'Method', 'Table lookup', ...
%                             'SampleRate', 1000, ...
%                             'OutputDataType', 'Custom', ...
%                             'CustomOutputDataType', numerictype([], 10, 9), ...
%                             'ComplexOutput',true);

%% Instantiate the FPGA-in-the-Loop System Object
%
% fft8_fil is a customized FILSimulation System object, which represents
% the HDL implementation of the FFT running on the FPGA in this simulation
% system.

%%
%   Fft = fft8_fil;
              
%% Run the Simulation
%
% This example simulates the sine wave generator and the FFT HDL implementation
% via the FPGA-in-the-Loop System object. This section of the code calls
% the processing loop to process the data sample-by-sample.

%%
%   for ii=1:1000
%       % Read 1 sample from the sine wave generator
%       ComplexSinus = step(SinGenerator);
%       % Send/receive 1 sample to/from the HDL FFT on the FPGA
%       [RealFft, ImagFft] = step(Fft,real(ComplexSinus),imag(ComplexSinus));
%       % Store the FFT sample in a vector
%       ComplexFft(ii) = RealFft + ImagFft*1i; 
%   end

%% Display the Fourier Coefficients
%
% Plot the Fourier Coefficients in the Complex Plane.

%%
%   % Discard the first 12 samples (initialization of the HDL FFT)
%   ComplexFft(1:12)=[];
%
%   % Display the FFT
%   plot(ComplexFft,'ro');
%   title('Fourier Coefficients in the Complex Plane');
%   xlabel('Real Axis');
%   ylabel('Imaginary Axis');

%%
% <<../plotResult.jpg>>

%%
% This concludes the "FPGA-in-the-Loop simulation using MATLAB System
% Object" example.
