您的位置:首页 > 编程语言 > MATLAB

Matlab与modelsim的联合仿真

2007-05-30 12:35 477 查看
 

[align=center]Matlabmodelsim的联合仿真[/align]
 
signal Generator
Demonstrate Link for ModelSim using MATLAB to implement a signal generator component.
*********************************************************************
----------------------------------------------------------------
-- Module: sig_generator
-- This module is wrapper for the MATLAB signal generator. This
-- level has just ports to connect to in MATLAB. See siggenerator.m for the details.
----------------------------------------------------------------
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
ENTITY sig_generator IS
PORT(   clk             : IN std_logic;
        clk_enable      : IN std_logic;
        reset           : IN std_logic;
        signal_out      : OUT real);
END sig_generator;
ARCHITECTURE matlab OF sig_generator IS
BEGIN
END matlab;
*********************************************************************
----------------------------------------------------------------
-- Module: siggen_top
-- This module is the top level and instantiates the empty
-- wrapper for the MATLAB component that acts as a signal generator.
----------------------------------------------------------------
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
ENTITY siggen_top IS
PORT(   clk             : IN std_logic;
        clk_enable     : IN std_logic;
        reset           : IN std_logic;
        signal_out      : OUT real);
END siggen_top;
ARCHITECTURE rtl OF siggen_top IS
COMPONENT sig_generator
PORT(   clk             : IN std_logic;
        clk_enable      : IN std_logic;
        reset           : IN std_logic;
        signal_out      : OUT real);
END COMPONENT;
FOR ALL : sig_generator
USE ENTITY work.sig_generator(matlab);
BEGIN
u_sig_generator: sig_generator
PORT MAP(   clk             => clk,
            clk_enable      => clk_enable,
            reset           => reset,
            signal_out      => signal_out);
END rtl;
*******************************************************************
function varargout = modsimsiggen(varargin)
% MODSIMSIGGEN - Demonstrate Link for ModelSim using MATLAB to implement a
% signal generator component.
%   MODSIMSIGGEN shows how MATLAB can be used to implement a VHDL
%   component that is used to drive to input of a VHDL project in ModelSim.
%   This demo uses an empty project that displays the output being
%   generated by the MATLAB component. It opens a GUI that allows the user
%   to specify a wave type, amplitude and frequency and creates a VHDL
%   componet that outputs a signal with these characteristics. A project
%   containing this component is then compiled and and loaded in ModelSim.
%   This demo requires a temporary working directory to generate a working
%   ModelSim VHDL project. After creating the VHDL project, this demo
%   starts ModelSim (this requires access to ModelSim from the command
%   line). This demo uses shared memory to complete the link and therefore
%   requires ModelSim to be on the same computer as MATLAB. Once the
%   project is compiled, the simulation can be run from within ModelSim.
%   After running please check the plot in the wave window of ModelSim.
 
% Copyright 2005 The MathWorks, Inc.
 
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @modsimsiggen_OpeningFcn, ...
                   'gui_OutputFcn', @modsimsiggen_OutputFcn, ...
                   'gui_LayoutFcn', [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
 
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
 
 
% --- Executes just before modsimsiggen is made visible.
function modsimsiggen_OpeningFcn(hObject, eventdata, handles, varargin)
 
global waveform
global amp
global freq
global frequnits
global clockperiod
 
% Initialize global variables
waveform = 'sine';
amp = 1;
freq = 1e6;
frequnits = 'Hertz';
clockperiod = 80e-9;
 
% Set build directory to be default temporary directory
if ispc
    projdir = tempdir;
else
    projdir = tempname;
end
set(handles.projdiredit,'String',projdir);
 
% Choose default command line output for modsimsiggen
handles.output = hObject;
 
% Update handles structure
guidata(hObject, handles);
 
disp('The modsimsiggen demo shows one way to do component modeling in');
disp('MATLAB. In this demo a signal generated is modeled in MATLAB and');
disp('implemented as a VHDL component in a ModelSim simulation.');
disp(' ');
disp('This demo will open up a MATLAB GUI in which the parameters can be');
disp('set. Once they are set, ''Start simulation'' can be clicked to open');
disp('ModelSim and build the project. At this point, the simulation can be');
disp('run from within ModelSim and the GUI will be closed.');
disp(' ');
disp('This demo requires a temporary directory to generate a VHDL working');
disp('directory in ModelSim. After creating the VHDL work directory,');
disp('ModelSim is started (this requires access to ModelSim from the');
disp('command line). This demo uses shared memory to complete the link and');
disp('therefore requires ModelSim to be on the same computer as MATLAB.');
disp(' ');
disp('See these files for details:');
disp(['    ', ...
    fullfile(matlabroot,'toolbox','modelsim','modelsimdemos','vhdl','siggen','sig_generator.vhd')]);
disp(['    ',...
    fullfile(matlabroot,'toolbox','modelsim','modelsimdemos','vhdl','siggen','siggen_top.vhd')]);
disp(['    ',...
    fullfile(matlabroot,'toolbox','modelsim','modelsimdemos','siggenerator.m')]);
disp('The file sig_generator.vhd contains the MATLAB component that we are');
disp('modeling. It contains an entity with an empty architecture.');
disp('The matlabcp command drives the VHDL outputs of the instance created');
disp('from this entity');
disp('The file siggen_top.vhd contains an instance of the MATLAB component.');
disp('The file siggenerator.m contains the actual component behaviorial');
disp('model.');
 
 
% --- Outputs from this function are returned to the command line.
function varargout = modsimsiggen_OutputFcn(hObject, eventdata, handles)
 
% Get default command line output from handles structure
varargout{1} = handles.output;
 
 
% Set background color of uicontrols to be white on Windows.
function setbackgroundwhite(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
 
 
% Callback for "Browse"
function projdirbrowse_Callback(hObject, eventdata, handles)
 
projdir = uigetdir(get(handles.projdiredit,'String'),'Select VHDL Project Directory');
if ~isnumeric(projdir)
    set(handles.projdiredit,'String',projdir);
end
 
 
% Callback for "Waveform"
function waveformpopup_Callback(hObject, eventdata, handles)
 
global waveform
 
switch get(hObject,'Value')
    case 1
        waveform = 'sine';
    case 2
        waveform = 'square';
    case 3
        waveform = 'sawtooth';
    case 4
        waveform = 'random';
end
 
 
% Callback for "Amplitude"
function ampedit_Callback(hObject, eventdata, handles)
 
global amp
 
a = str2num(get(hObject,'String'));
 
if a <=0
    errordlg('Amplitude must be greater than zero.','Invalid setting');
    set(hObject,'String',num2str(amp));
else
    amp = a;
end
 
 
% Callback for "Frequency"
function freqedit_Callback(hObject, eventdata, handles)
 
global freq
 
f = str2num(get(hObject,'String'));
 
if f <=0
    errordlg('Frequency must be greater than zero.','Invalid setting');
    set(hObject,'String',num2str(freq));
else
    freq = f;
end
 
 
% Callback for "Units"
function unitspopup_Callback(hObject, eventdata, handles)
 
global frequnits
 
switch get(hObject,'Value')
    case 1
        frequnits = 'Hertz';
    case 2
        frequnits = 'rand/sec';
end
 
 
% Callback for "Sample Time"
function samptimeedit_Callback(hObject, eventdata, handles)
 
global clockperiod
 
t = str2num(get(hObject,'String'));
 
if t <=0
    errordlg('Sample time must be greater than zero.','Invalid setting');
    set(hObject,'String',num2str(clockperiod));
else
    clockperiod = t;
end
 
 
% Callback for "View callback M-file"
function mfilebutton_Callback(hObject, eventdata, handles)
 
edit(mfilename);
 
 
% Callback for "Start simulation"
function startbutton_Callback(hObject, eventdata, handles)
 
global clockperiod
global amp              % Used here to determine scaling of display
 
% Deactivate GUI
set(handles.projdiredit,'Enable','off');
set(handles.projdirbrowse,'Enable','off');
set(handles.waveformpopup,'Enable','off');
set(handles.ampedit,'Enable','off');
set(handles.freqedit,'Enable','off');
set(handles.unitspopup,'Enable','off');
set(handles.samptimeedit,'Enable','off');
set(handles.startbutton,'Enable','off');
 
% Specify source files
srcfile1 = fullfile(matlabroot,'toolbox','modelsim','modelsimdemos','vhdl','siggen','sig_generator.vhd');
srcfile2 = fullfile(matlabroot,'toolbox','modelsim','modelsimdemos','vhdl','siggen','siggen_top.vhd');
unixsrcfile1 = ['"' strrep(srcfile1,'/','/') '"'];
unixsrcfile2 = ['"' strrep(srcfile2,'/','/') '"'];
 
% Set up project directory
projdir = fullfile(get(handles.projdiredit,'String'),mfilename);
warnstatus = warning('off','MATLAB:MKDIR:DirectoryExists');
mkdir(projdir);
warning(warnstatus);
unixprojdir = strrep(projdir,'/','/');
 
% Specify Tcl commands
tclcmd = { ['cd ',unixprojdir],... % change to temporary working directory
            'vlib work',... % create design library
           ['vcom -performdefaultbinding ' unixsrcfile1],... % compile VHDL files
           ['vcom -performdefaultbinding ' unixsrcfile2],...
            'vsimmatlab work.siggen_top ',... % load instance of VHDL entity for cosimulation with MATLAB
            'matlabcp u_sig_generator -mfunc siggenerator',... % initiates MATLAB component for entity
            'add wave sim:/siggen_top/clk',... % add signals to display
            'add wave sim:/siggen_top/clk_enable',...
            'add wave sim:/siggen_top/reset',...
           ['add wave -height 100 -radix decimal -format analog-step ',...
            '-scale ' num2str(40/amp) ' -offset 1 ',...
            'sim:/siggen_top/signal_out'],...
            'force sim:/siggen_top/clk_enable 1 0',... % apply stimuli to signals
            'force sim:/siggen_top/reset 1 0, 0 120 ns',...
           ['force sim:/siggen_top/clk 1 0 ns, 0 ' num2str(1e9*clockperiod/2) ' ns -r ' num2str(1e9*clockperiod) ' ns'],...
         };
 
% Start MATLAB server component using shared memory
disp(' ');
disp('To enable access from ModelSim, HDLDAEMON is used with appropriate');
disp('link settings. The following messages are produced by HDLDAEMON to');
disp('indicate link status...');
dstatus = hdldaemon('status');
if isempty(dstatus)
    % not running - start it
    dstatus = hdldaemon;    % tell user what's happening
elseif strcmp(dstatus.comm,'shared memory')
    % already running
    % user knows what's happening from previous hdldaemon('status')
elseif strcmp(dstatus.comm,'sockets')
    % running with different comm - stop and restart it
    disp('Shutting down HDLDaemon to restart it with shared memory');
    hdldaemon('kill');
    dstatus = hdldaemon;
else
    error('unexpected return value from hdldaemon(''status'')');
end
 
close;
h = msgbox(['ModelSim is being started and the project is being compiled.',...
            'To run the simulation, use the run command in ModelSim, ',...
            'specifying the appropriate simulation time. ',...
            'For example type "run 10000". Examine the ModelSim''s wave window.' ],...
            'Starting ModelSim');
pause(1.0);
 
% Start ModelSim
disp(' ');
disp('Starting simulation in ModelSim...');
vsim('tclstart',tclcmd);
 
disp(' ');
disp('After running the demo, examine the output in the Wave window of ModelSim.');
disp('Be sure to quit ModelSim and shut down the MATLAB server by using');
disp('HDLDAEMON KILL once you are done with this demo as each time the demo');
disp('is run, a new ModelSim is started.');
disp(' ');
disp('This concludes the demo.');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息