0% found this document useful (0 votes)
19 views55 pages

ModelSimSimulink Co-Simulation 2022

The document outlines a course on Digital System Design, focusing on ModelSim simulation and VHDL programming. It includes an introduction to FPGA, co-simulation with Simulink, and practical examples such as counter design and filter implementation. The document also provides detailed steps for project creation, simulation, and examples of digital system applications.

Uploaded by

leductai020902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views55 pages

ModelSimSimulink Co-Simulation 2022

The document outlines a course on Digital System Design, focusing on ModelSim simulation and VHDL programming. It includes an introduction to FPGA, co-simulation with Simulink, and practical examples such as counter design and filter implementation. The document also provides detailed steps for project creation, simulation, and examples of digital system applications.

Uploaded by

leductai020902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Digital System Design

Spring in 2022

TS. Nguyen Khanh Quang


A. Introduction to Case Study
of ModelSim Simulation
For ModelSim 5.7d

Spring 2022 in DUT

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

1. Introduction to digital system design (Aug. 28) (by Ying-Shieh Kung)


● Introduction to FPGA
● Introduction to VHDL
● Case study in controller and filter design using VHDL
2. ModelSim/Simulink co-simulation (Aug. 28) (by Quang )
● Introduction to ModelSim Simulation
● Introduction to ModelSim/Simulink co-simulation
● Case study
3. FPGA implementation (…) (…)
● Introduction to Altera DE2-115 Development and Education Board
● Altera DE2-115 Board Programming
● FPGA Project Experiments

3
1. Create a new project

03/27/2025 4
1. Create a new project

Type VHDL code here

Example

03/27/2025 5
2. Open a project

(project file)

testbench file

original VHDL code


03/27/2025 6
Add vhd file and 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

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;

----- signal declaration -----------


SIGNAL clk: BIT := '0';
SIGNAL counter_ena: BIT := '0';
SIGNAL clear: BIT := '0';
SIGNAL load: BIT := '0';
SIGNAL direction: BIT := '0';
SIGNAL P : INTEGER :=0;
SIGNAL max_min : BIT;
SIGNAL qd: INTEGER;

03/27/2025 10
3. Simulation

Choose Library tab


Right click “counter_tb => Simulate

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

Change the running 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

Fall 2016 in STUST, Taiwan

03/27/2025 16
Introduction to ModelSim/Simulink Co-simulation

Matlab Simulink ModelSim

• To verify the functionality


of the HDL correctly match the • Chip design using HDL
system specifications (Hardware Description Languages)
• Environment for multidomain • High-level tool for designing
simulation and Model-Based high-performance DSP systems
Design for dynamic and using FPGAs
embedded systems

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

Identify the ModelSim installation to be configured for MATLAB and Simulink

Do you want configuremodelsim to locate installed ModelSim executables [ y ] / n ? y

Select a ModelSim installation to be configured:

[1] C:\Modeltech_5.7d\win32 ModelSim SE 5.7d


[0] None

Select ModelSim installation to be configured: 1

Previous MATLAB startup file found in this installation of ModelSim:


C:\Modeltech_5.7d\win32\..\tcl\ModelSimTclFunctionsForMATLAB.tcl
Do you want to replace this file [y]/n? y
ModelSim successfully configured to be used with MATLAB and Simulink

>> vsim('socketsimulink',4449) (Open ModelSim)


03/27/2025 18
2. ModelSim
2.1. Create new project
- File => New => Project
- Type “counter” (any project name)

03/27/2025 19
2.2. Add vhd file

(Step 1)

(Step 2: Choose all files)

Co-simulation & Library files

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

bin 0001 0100

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

- Sum of Product (SoP)


- Filter Design

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.5 Convert a1 Gain 1 Output

0: unsigned
Q format (Q0)
0.15 Convert a2
1: signed
0.5 Convert a3

data length (16bits)


Sum of Product

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.5 Convert a1 Gain 1 Output

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:

• Its fourth order low pass filter:

03/27/2025 44
Example-2 Filter Design
After the bilinear transform with sampling time 0.01,
its discrete transfer function is

Difference equation after Q15 format: (y(n)*32768, or x(n)*32768)


Y(n)= 95486*Y(n-1) – 104366*Y(n-2) + 50692*Y(n-3) - 9234*Y(n-4) +
11*X(n) + 44*X(n-1) + 67*X(n-2) + 44*X(n-3) + 11*X(n-4)
Where x(n) are inputs and y(n) are outputs

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

You might also like