Joint simulation of MATLAB and Modelsim through file read/write

Source: Internet
Author: User
Tags one more line
(Conversion) implement joint simulation of MATLAB and Modelsim through file read/write Source of citation uses file read/write mode to achieve joint simulation of MATLAB and Modelsim-xuanyuan's blog although Modelsim is very powerful, the simulation waveform can be displayed in multiple forms, however, when it comes to digital signal processing Algorithm It seems a little inadequate during simulation verification. 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 on the Internet about link for ModelSim.ArticleHttp://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.

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.

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

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

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

Copy the corresponding sin.txt file to the Modelsim project, first define an 8bit x 256 array in the OpenGL file, and then read the data in the file through the $ readmemh command.CodeAs 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:

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.

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

Then we can compile a short section of MATLABProgramRead 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 ');

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

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.

 

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:

Functions related to MATLAB include fopen and Modelsim: $ fopen, $ fclose, $ readmemh, $ readmemb, $ fmonitor, $ fdisplay, and $ 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!

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.