ModelSimSimulink Co-Simulation 2022
ModelSimSimulink Co-Simulation 2022
Spring in 2022
2
A. Introduction to Case Study
of ModelSim Simulation
For ModelSim 5.7d
Refer from:
•TS. Nguyen Khanh Quang, Teaching Material, 2022.
•Volnei A. Pedroni, Circuit Design and Simulation with VHDL,
Second Edition, The MIT Press, 2010.
113/03/14 3
1. Create a new project
113/03/14 4
1. Create a new project
Example
113/03/14 5
2. Open a project
(project file)
testbench file
113/03/14 7
Compile
113/03/14 8
library ieee; -- Terminal count decoder
Counter.vhd use ieee.std_logic_1164.all; IF (cnt = 0 and direction = '0') THEN
use Ieee.std_logic_arith.all; max_min <= '1';
use ieee.std_logic_unsigned.all; ELSIF (cnt = 255 and direction = '1') THEN
max_min <= '1';
------------------------------------------
ELSE
ENTITY counter IS max_min <= '0';
PORT( END IF;
clk, count_ena : IN BIT; END PROCESS;
clear, load, direction : IN BIT; END a;
p : IN INTEGER RANGE 0 TO 255;
max_min : OUT BIT;
qd : OUT INTEGER RANGE 0 TO 255);
END counter;
------------------------------------------
ARCHITECTURE a OF counter IS
BEGIN
PROCESS (clk, clear, load)
VARIABLE cnt : INTEGER RANGE 0 TO 255;
BEGIN
IF (clear = '0') THEN -- Asynchrnous clear
cnt := 0;
ELSIF (load = '1' and clear = '1') THEN -- Asynchronous load
cnt := p;
ELSE
IF (clk'EVENT AND clk = '1') THEN
IF (count_ena = '1' and direction = '0') THEN
cnt := cnt - 1;
ELSIF (count_ena = '1' and direction = '1') THEN
cnt := cnt + 1;
END IF;
END IF;
END IF;
qd <= cnt;
113/03/14 9
Counter_tb.vhd
LIBRARY ieee; BEGIN
USE ieee.std_logic_1164.all; ----- counter_1 instantiation ---------------
----------------------------------- Counter_1: counter PORT MAP (clk, counter_ena,
ENTITY counter_tb IS clear, load, direction, P, max_min, qd);
END counter_tb; ----- Stimuli generation --------------
----------------------------------------- clk <= NOT clk AFTER 40ns;
ARCHITECTURE testbench of counter_tb IS clear <= '1' AFTER 0ns;
----- counter declaration --------------- counter_ena <= '1' AFTER 0ns;
COMPONENT counter load <= '0' AFTER 0ns;
PORT ( clk, count_ena : IN BIT; direction <= '1' AFTER 0ns;
clear, load, direction : IN BIT; P <= 20 AFTER 0ns;
p : IN INTEGER RANGE 0 TO 255; END testbench;
max_min : OUT BIT;
qd : OUT INTEGER RANGE 0 TO 255);
END COMPONENT;
113/03/14 10
3. Simulation
Compilation result
113/03/14 11
Add waveform
Choose Sim tab
Right click “counter_tb => Add => Add to Wave
113/03/14 12
Setting the simulation time
113/03/14 13
Running
Simulation results
113/03/14 14
End Simulation
113/03/14 15
B. Introduction to
ModelSim/Simulink Co-simulation
For ModelSim 5.7d
Matlab 2008a
113/03/14 16
Introduction to ModelSim/Simulink Co-simulation
Co-simulate ModelSim/Simulink
y=f(x)
113/03/14 17
MATLAB AND MODELSIM LINKING
(For the link to work, Modelsim has to be invoked from the command prompt of
MATLAB.
For this purpose, MATLAB needs to know the location of MODELSIM )
113/03/14 18
MODELSIM/MATLAB SIMULINK CO-SIMULATION
1. Open ModelSim in Matlab main window to make the communication
>> configuremodelsim
113/03/14 20
2.2. Add vhd file
(Step 1)
113/03/14 22
2.4. Setting file properties
- Choose all files
- Right click and choose Properties
- Click tab VHDL, choose “Use 1993 Language Syntax”
113/03/14 23
2.4. Setting file properties (cont.)
- Choose 2 files “220models” and “220pack”
- Right click and choose Properties
- At tab VHDL, choose “lpm” in ‘Compile to library’ index
113/03/14 24
2.5. Compile program
- Choose all files
- Right click and choose Compile => Compile All
113/03/14 25
2.6. Simulation
- At main window, type “vsimu work.counter”
(Command’s structure: vsimu work.name_of_entity1 work. name_of_entity2)
# Reading C:/Modeltech_5.7d/tcl/vsim/pref.tcl
# do {D:/Softwares/MATLAB/R2008a/bin/tp2eb1637e_fa87_4651_9eb1_209b9189e1d8}
# Loading project counter
# Compile of 220model.vhd was successful.
# Compile of 220pack.vhd was successful.
# Compile of counter.vhd was successful.
# 3 compiles, 0 failed with no errors.
ModelSim> vsimu work.counter
# vsim -foreign {simlinkserver
{D:/Softwares/MATLAB/R2008a/toolbox/modelsim/windows32/liblfmhdls_vs05.dll} ; -socket
4449 } work.counter
# Loading C:/Modeltech_5.7d/win32/../std.standard
# Loading C:/Modeltech_5.7d/win32/../ieee.std_logic_1164(body)
# Loading C:/Modeltech_5.7d/win32/../ieee.std_logic_arith(body)
# Loading C:/Modeltech_5.7d/win32/../ieee.std_logic_unsigned(body)
# Loading work.counter(a)
# Loading D:/Softwares/MATLAB/R2008a/toolbox/modelsim/windows32/liblfmhdls_vs05.dll
113/03/14 26
3. Matlab
3.1. Create new model
- At main window, type “Simulink”
- At Simulink Library Browser, choose EDA Simulator Link MQ
113/03/14 27
3.2. Setting HDL block parameters
- At tab Ports, add inputs, outputs and their parameters
113/03/14 28
3.2. Setting HDL block parameters (cont.)
- At tab ‘Clock’, add clock and timing period
113/03/14 29
3.2. Setting HDL block parameters (cont.)
113/03/14 30
3.2. Setting HDL block parameters (cont.)
113/03/14 31
3.3. Create Model and start Co-simulation
bin 1
Display 1
clear
1 Convert clear
Constant5
qd
load (SI) bin 0001 0100
qd
0 Convert load
Display 3
Constant1
direction
1 Convert direction
Constant2
count_ ena
1 Convert count_ ena
max_ min bin 0
max_ min
Constant3
Display6
P 20 int8 P
Constant4
HDL Cosimulation HDL Cosimulation
Display2
113/03/14 32
3.3. Create Model and start Co-simulation (cont.)
113/03/14 33
3.3. Create Model and start Co-simulation (cont.)
113/03/14 34
3.3. Create Model and start Co-simulation (cont.)
113/03/14 35
3.3. Create Model and start Co-simulation (cont.)
113/03/14 36
3.3. Create Model and start Co-simulation (cont.)
113/03/14 37
ModelSim/Simulink Co-simulation
Case study
113/03/14 38
113/03/14 39
113/03/14 40
Example-1 Sum of Product
113/03/14 41
Example-1 Sum of Product
113/03/14 42
Example-1 Sum of Product
0.2 Convert x1
bin 0000 1100 0010 0111
0.3 Convert x2
0.094940185546875
-0.1 Convert x3
fixdt(1,16,0) y -K-
0: unsigned
1: signed Q format (Q0) 0.15 Convert a2
0.5 Convert a3
5.9814453125015 e-005
x1
Subtract error
x2
x3
0.095
fcn y
a1
Display 1
a2
function y = fcn(x1,x2,x3,a1,a2,a3)
a3
113/03/14 Embedded
y=a1*x1+a2*x2+a3*x3 43
MATLAB Function
Example-1 Sum of Product
0.2 Convert x1
bin 0000 1100 0010 0111
0.3 Convert x2
0.094940185546875
-0.1 Convert x3
y -K-
0.15 Convert a2
0.5 Convert a3
Sum of Product
5.9814453125015 e-005
x1
Subtract error
x2
x3
0.095
fcn y
a1
Display 1
a2
a3
Embedded
113/03/14 MATLAB Function 44
Example-2 Filter Design
A second-order low-pass filter is designed with
bandwidth 5Hz and damping ratio 1.0
• The continuous transfer function of the second order low
pass filter is:
113/03/14 45
Example-2 Filter Design
After the bilinear transform with sampling time 0.01,
its discrete transfer function is
113/03/14 46
Example-2 Filter Design
I. Technique description
113/03/14 47
Example-2 Filter Design
II. VHDL Code
113/03/14 48
Example-2 Filter Design
113/03/14 49
Example-2 Filter Design
113/03/14 50
Example-2 Filter Design
113/03/14 51
Example-2 Filter Design
III. Simulation Result (ModelSim/Simulink Co-simulation)
113/03/14 52
Example-2 Filter Design
113/03/14 53
Example-2 Filter Design
113/03/14 54
Example-2 Filter Design
113/03/14 55
Thank you for your attention
113/03/14 56