Joint simulation of MATLAB and Modelsim through file read/write

Source: Internet
Author: User
Tags one more line

Although Modelsim is very powerful, the simulation waveform can be displayed in multiple forms, but when it involves the simulation verification of digital signal processing algorithms, it seems a little inadequate. As the strength of Matlab, digital signal processing not only has a large number of functions related to digital signal processing, but also has powerful graphic display functions, therefore, when performing FPGA verification of digital signal processing algorithms, Matlab greatly accelerates algorithm verification.

 

For the joint simulation of MATLAB and Modelsim, I can see two methods on the Internet. One is to establish the joint simulation interfaces of MATLAB and Modelsim through link for Modelsim; another method is to implement joint simulation of MATLAB and Modelsim through file read/write. I have not carefully studied the first method. I have probably looked at it. It seems that the process is complicated, but it must be very powerful, there is an article about link for Modelsim http://space.ednchina.com/Upload/2009/11/16/9e8d0364-20ed-4583-a85e-4d1fc50783a7.rar "target = _ blank>, interested friends can go to have a look. The second method can only be implemented through several file read/write functions, and can basically meet the requirements of the current simulation. So I will mainly discuss this method here, I hope I can give some advice, because I can only be a beginner.

 

1. MATLAB generates data for Modelsim simulation

 

When FPGA performs algorithm verification, it is often necessary to input simulation data, which can be generated using FPGA. However, if the data generation process is complex, it will take a lot of effort, the accuracy of the generated data cannot be guaranteed. For example, if we want to verify the algorithm of a Communication Receiver, We need to generate the sending data first, that is, we need to first create a transmitter. If this process is also implemented by FPGA, it is also a complicated process. At this time, we can use MATLAB to use various internal functions of MATLAB to generate the desired signal, and then use it as the input signal of the FPGA receiving module after being fixed. This will undoubtedly save a lot of time and effort.

 

The following is a simple example to illustrate how to use the data generated by Matlab as Modelsim simulation.

 

First, we should use the main string data of-8 bits in a cycle, and then write it into the sin.txt file in the form of 16-in-progress.

 

N = 256;

 

N = 1:256;

 

X = fix (128 + (2 ^ 7-1) * sin (2 * pI * n/n ));

 

FID = fopen('sin.txt ', 'wt ');

 

Fprintf (FID, '% x \ n', X );

 

Fclose (FID );

 

Is the content of the captured data file

 

 

Then copy the corresponding sin.txt file to the Modelsim project, first define an 8bit x 256 array in the Tilde file, and then use the $ readmemh command to read the data in the file. The related Tilde code is as follows:

 

Reg [] data_mem []; // defines an 8-bit x 256 Array

 

Initial

 

Begin

 

$ Readmemh ("sin.txt", data_mem); // read data from sin.txt into the storage data_mem

 

End

 

For more information about $ readmemh usage, see the reference book of OpenGL.

 

Data_mem can be used as your test data. For example, you can use the following code to send data from data_mem to data_out:

 

Always @ (posedge CLK)

 

Begin

 

If (RST)

 

Begin

 

Data_out <= 8'd0;

 

I <= 8'd0;

 

End

 

Else

 

Begin

 

Data_out <= data_mem [I]; // outputs data in the memory

 

I <= I + 8'd1;

 

End

 

End

 

With data_out, a sine wave is generated by molesim simulation:

 

2. MATLAB analyzes the data generated by Modelsim simulation

 

MATLAB processes the data generated by Modelsim simulation through file read/write. That is to say, a certain signal in the simulation process is written into a file by using the OpenGL statement, and then the data in this file is read in the MATLAB, and the analysis can be carried out in the Matlab.

 

A simple example is provided to illustrate the entire process.

 

Write the data_outdata of the signal to the data_out.txt file in the following example logsyntax.

 

Integer w_file;

 

Initial w_file = $ fopen ("data_out.txt ");

 

Always @ (I)

 

Begin

 

$ Fdisplay (w_file, "% H", data_out );

 

If (I = 8 'd255) // 256 data records are written in total.

 

$ Stop;

 

End

 

Is part of the screenshot data_out.txt:

 

 

 

Then, you can compile the program for compiling the field named "Matlab" to read and analyze the data in data_out.txt. The following MATLAB program reads data and displays the data waveform through graphs.

 

FID = fopen('data_out.txt ', 'R ');

 

For I = 256;

 

Num (I) = fscanf (FID, '% x', 1); % refers to reading a data from the file indicated by FID in hexadecimal format.

 

End

 

Fclose (FID );

 

Plot (Num );

 

When using the fscanf function, pay attention to the following two points,

 

First, ensure that the format of the read data is the same as the format of the data stored in the file. For example, the file is saved in hexadecimal format, therefore, it should also be read in hexadecimal format during reading.

 

Second, ensure that the number of data in the file is consistent with the set number of reads (256 here. For example, remove the redundant line breaks in the generated file data_out.txt (usually one more line at the end). Otherwise, Matlab treats the empty line as a data, resulting in inconsistent numbers of the two, resulting in an error in MATLAB.

 

The following figure shows the data in data_out.txt and the waveform displayed:

 

 

 

Of course, with MATLAB, a powerful tool, you can easily view the signal spectrum and other information.

 

In addition, there are multiple methods for writing data to files through the kernel. the system function $ fdisplay is used, and of course there are several commands such as $ fmonitor and $ fwrite, the following describes the differences between these commands.




  • $ Fdisplay

    This command requires a trigger condition to write data to a file. For example, the trigger condition of the above example is always (I), which is written only when I changes. A line break is automatically added each time data is written.


  • $ Fmonitor

    This command does not need to trigger conditions. data can be written to a file as long as there are changes. For example, you can use the following statement:


    Initial $ fmonitor (w_file, "% H", data_out );


    In this way, the data_out data generated during the entire simulation process can be written into the file.


  • $ Fwrite

    This command is basically the same as $ fdisplay. It also requires a trigger condition to write data. The difference is that line breaks are not automatically added for every write of data. For example, you can use the following statement:


    Always @ (posedge CLK)


    Begin


    $ Fwrite (w_file, "% H \ n", data_out );


    End

 

For a detailed introduction to these commands, you can refer to the relevant data of the Tilde.

 

A brief summary of the functions used above:



  • MATLAB functions include fopen, fscanf, and fclose.
  • ModelSim functions include: $ fopen, $ fclose, $ readmemh, $ readmemb, $ fmonitor, $ fdisplay, $ fwrite.

 

The above are some of my experiences on the joint simulation of MATLAB and modesim. If you have other better methods, I hope you can give me some advice!

 

The following is the relevant code, including the Matlab code and the OpenGL code.

 Transferred from:Http://bbs.ednchina.com/BLOG_ARTICLE_285466.HTM

Joint simulation of MATLAB and Modelsim through file read/write

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.