ModelSimSimulink Co-Simulation 2022
ModelSimSimulink Co-Simulation 2022
Spring in 2022
Refer from:
• TS. Nguyen Khanh Quang, Teaching Material, 2022.
• Volnei A. Pedroni, Circuit Design and Simulation with VHDL,
Second Edition, The MIT Press, 2010.
03/27/2025 2
Course Description
3
1. Create a new project
03/27/2025 4
1. Create a new project
Example
03/27/2025 5
2. Open a project
(project file)
testbench file
03/27/2025 7
Compile
03/27/2025 8
-- Terminal count decoder
IF (cnt = 0 and direction = '0') THEN
Counter.vhd max_min <= '1';
library ieee;
ELSIF (cnt = 255 and direction = '1') THEN
use ieee.std_logic_1164.all;
max_min <= '1';
use Ieee.std_logic_arith.all; ELSE
use ieee.std_logic_unsigned.all; max_min <= '0';
------------------------------------------ END IF;
ENTITY counter IS END PROCESS;
PORT( END a;
clk, count_ena : IN BIT;
clear, load, direction : IN BIT;
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;
03/27/2025 qd <= cnt; 9
Counter_tb.vhd
03/27/2025 10
3. Simulation
Compilation result
03/27/2025 11
Add waveform
Choose Sim tab
Right click “counter_tb => Add => Add to Wave
03/27/2025 12
Setting the simulation time
03/27/2025 13
Running
Simulation results
03/27/2025 14
End Simulation
03/27/2025 15
B. Introduction to
ModelSim/Simulink Co-simulation
For ModelSim 5.7d
Matlab 2008a
03/27/2025 16
Introduction to ModelSim/Simulink Co-simulation
Co-simulate ModelSim/Simulink
y=f(x)
03/27/2025 17
MODELSIM/MATLAB SIMULINK CO-SIMULATION
1. Open ModelSim in Matlab main window to make the communication
>> configuremodelsim
03/27/2025 19
2.2. Add vhd file
(Step 1)
03/27/2025
Original VHDL code 20
2.3. Add new library
- File => New => Library
- Choose “a new library and a logical mapping to it
- At the library name, type “lpm”
03/27/2025 21
2.4. Setting file properties
- Choose all files
- Right click and choose Properties
- Click tab VHDL, choose “Use 1993 Language Syntax”
03/27/2025 22
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
03/27/2025 23
2.5. Compile program
- Choose all files
- Right click and choose Compile => Compile All
03/27/2025 24
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
03/27/2025 25
3. Matlab
3.1. Create new model
- At main window, type “Simulink”
- At Simulink Library Browser, choose EDA Simulator Link MQ
03/27/2025 26
3.2. Setting HDL block parameters
- At tab Ports, add inputs, outputs and their parameters
03/27/2025 27
3.2. Setting HDL block parameters (cont.)
- At tab ‘Clock’, add clock and timing period
03/27/2025 28
3.2. Setting HDL block parameters (cont.)
03/27/2025 29
3.3. Create Model and start Co-simulation
bin 1
Display 1
clear
1 Convert clear
Constant 5
qd
load (SI) bin 0001 0100
qd
0 Convert load
Display 3
Constant 1
direction
1 Convert direction
Constant 2
count _ena
1 Convert count _ena
max _min bin 0
max _min
Constant 3
Display 6
P 20 int 8 P
Constant 4
HDL Cosimulation HDL Cosimulation
Display 2
03/27/2025 30
3.3. Create Model and start Co-simulation (cont.)
03/27/2025 31
3.2. Setting HDL block parameters (cont.)
03/27/2025 32
3.3. Create Model and start Co-simulation (cont.)
03/27/2025 33
3.3. Create Model and start Co-simulation (cont.)
03/27/2025 34
3.3. Create Model and start Co-simulation (cont.)
03/27/2025 35
3.3. Create Model and start Co-simulation (cont.)
03/27/2025 36
ModelSim/Simulink Co-simulation
Case study
03/27/2025 37
03/27/2025 38
03/27/2025 39
Example-1 Sum of Product
03/27/2025 40
Example-1 Sum of Product
03/27/2025 41
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
Q format (Q0)
0.15 Convert a2
1: signed
0.5 Convert a3
5.9814453125015 e-005
x1
Subtract error
x2
x3
0.095
fcn y
a1
Display 1
a2
a3
function y = fcn(x1,x2,x3,a1,a2,a3)
y=a1*x1+a2*x2+a3*x3
03/27/2025 Embedded
MATLAB Function
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
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
03/27/2025 MATLAB Function 43
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:
03/27/2025 44
Example-2 Filter Design
After the bilinear transform with sampling time 0.01,
its discrete transfer function is
03/27/2025 45
Example-2 Filter Design
I. Technique description
03/27/2025 46
Example-2 Filter Design
II. VHDL Code
03/27/2025 47
Example-2 Filter Design
03/27/2025 48
Example-2 Filter Design
03/27/2025 49
Example-2 Filter Design
03/27/2025 50
Example-2 Filter Design
III. Simulation Result (ModelSim/Simulink Co-simulation)
03/27/2025 51
Example-2 Filter Design
03/27/2025 52
Example-2 Filter Design
03/27/2025 53
Example-2 Filter Design
03/27/2025 54
Thank you for your attention
03/27/2025 55