Read Array in Text File Into Memory Verilog Readmem

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Word Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more than! To participate you need to register. Registration is free. Click here to annals now.

  • Forums
  • Digital Design and Embedded Programming
  • PLD, SPLD, GAL, CPLD, FPGA Design
Yous are using an out of engagement browser. Information technology may not display this or other websites correctly.
You should upgrade or utilize an alternative browser.

How to Read a file into verilog test bench and pass it to the verilog lawmaking

  • Thread starter gadagkar.rohit
  • Starting time date
Status
Not open up for further replies.
  • #one
Joined
Apr 7, 2010
Messages
23
Helped
1
Reputation
ii
Reaction score
1
Trophy points
i,283
Location
San Jose
Action points
1,439
Hi,

I need to generate a betoken from MATLAB and store the data into a file and then utilize that file as an input to verilog code.

i but need to know how to read a file in verilog. and in what format should the file be to be able to be read in verilog.

can someone please help me with that.

  • #two
Joined
November 4, 2010
Messages
42
Helped
6
Reputation
12
Reaction score
vi
Trophy points
1,288
Location
Israel
Activity points
one,561
$readmemh() is usful for such tasks.
It reads text file that contains hex values into array.
google readmemh to find some examples.
  • #3
  • #iv
Joined
Nov iv, 2010
Messages
42
Helped
half dozen
Reputation
12
Reaction score
6
Trophy points
1,288
Location
Israel
Activity points
1,561
If you could attach the Verlog code and the text file with values, I can attempt and get over it.
  • #v
Joined
April 7, 2010
Messages
23
Helped
i
Reputation
ii
Reaction score
1
Trophy points
i,283
Location
San Jose
Activity points
1,439
i am running the verilog code on quartus 2. and so i may need to attach the complete project.
should i do that?
  • #six
Joined
Nov 4, 2010
Messages
42
Helped
half-dozen
Reputation
12
Reaction score
6
Trophy points
1,288
Location
State of israel
Action points
i,561
Pitiful, I don't take quartus... (I mainly work with Xilinx).
Maybe 2 files are enough, I will endeavour to go over them:
1. The test-bench Verilog file, that contains the $readmemh() role call, the array that the values are read into, and the loop that goes over the values in this array.
2. The text file with the hex values.
  • #seven
Joined
Apr 7, 2010
Messages
23
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
San Jose
Activity points
1,439
its non allowing me to attach the .dat file. the dat file is basically a matlab generated file. i ve generated a random signal and saved the values by converting the indicate values to binary.
i take attached the testbench though. i guess in that location is a problem with the test bench code.
thanks.

Attachments

  • #8
Joined
Nov iv, 2010
Messages
42
Helped
6
Reputation
12
Reaction score
half-dozen
Bays points
1,288
Location
Israel
Action points
ane,561
one. I would recommend moving the ii $readmemh() lines into the final initial cake, but before the for() loop.

2. The data files should non be binary files, but text files with hex values readable with a simple text editor (like Notepad).
See example here: https://www.rose-hulman.edu/~doering/PLD_Oasis/files/verilog/system_tasks/information.txt

in your case (16 bit values), the file tin await like this:
1f
1234
3d4
55a3
0
9be3

I'm not familiar with MATLAB .dat file. If they don't look like this, y'all may need some script or Excel part to convert MATLAB output to the in a higher place text format

three. To debug the file reading affair, I would propose adding a $display() line inside the for() loop, just after X39 = mem_X; to print the D39 and X39 values.

4. D39 and X39 are 16 $.25 vectors. You lot can't alphabetize them with i that counts till 2999.

v. I don't fully understand the Adaptive_LMS_Filter instantiation line. The for() loop starts and ends at time 0, and all the 3000 values go through D39 and X39 during time 0, then after reset is released and clocking starts, the filter volition just see a single (constant) value in D39 and X39 - the last value from the file.

6. If you meant to nowadays the list of values to the filter, a new value in each clock, I judge you should do something like the attached.

Attachments

  • #9
Joined
Apr 7, 2010
Messages
23
Helped
i
Reputation
2
Reaction score
1
Trophy points
1,283
Location
San Jose
Activity points
1,439
Thank You so much for your help
  • #ten
Joined
Apr 7, 2010
Messages
23
Helped
1
Reputation
2
Reaction score
1
Bays points
1,283
Location
San Jose
Activity points
1,439
hi yoramgr,

this time i take to read a two-D array...

really I am processing an image this fourth dimension. the image is of 288 x 384 10 3 assortment.

i have taken the pixel values into a txt file in hex format from MATLAB. and every bit you told me last time, the values in file are hex values and the file itself is not in hex format.

now i accept to input those values to verilog and process them.

any suggestions ?

  • #11
I don't use Verilog for testbenches, but in VHDL testbenches , information technology's piece of cake to read Matlab generated files with general file I/O commands. I guess, information technology should piece of work with Verilog $fread too. It'southward specially useful, when you lot want to supply a waveform to a betoken processing algorithm, i sample per clock bike. But you should exist as well able to write the data to a memory object.
  • #12
Joined
Apr seven, 2010
Letters
23
Helped
one
Reputation
2
Reaction score
1
Trophy points
1,283
Location
San Jose
Activity points
i,439
Hello FvM,

cheers for you reply.
I accept not all the same used any ane of them...simply using which of these, readmemh or fread will i be able to read the complete 2D array and shop information technology as a 2D array.
then that i tin can process the data by indexing the row and column.

  • #13
Joined
November iv, 2010
Messages
42
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,288
Location
State of israel
Activity points
ane,561
Yous can define 3 arrays, each 2D (assuming viii bit for each element):

reg [7:0] mem_R[0:287][0:383];
reg [7:0] mem_G[0:287][0:383];
reg [vii:0] mem_B[0:287][0:383];

prepare 3 files and effort to readmemh 3 times. I'm not certain about the file information order (row by row or column by column), but you can try to play effectually with information technology, with pocket-sized array outset.

  • #14
Joined
April 7, 2010
Messages
23
Helped
1
Reputation
2
Reaction score
one
Trophy points
1,283
Location
San Jose
Activity points
1,439
man this is getting frustrating...as u had suggested i tried generating three different files for R, G and B.

when i open the text file to check the values the file for K is getting generated properly but for R and B the txt files are showing garbage values.

so i am going to try with merely one file. i deceit waste product more fourth dimension in generating the 3 files. exercise u take any proffer for that.

  • #fifteen
Joined
Nov 4, 2010
Messages
42
Helped
6
Reputation
12
Reaction score
six
Trophy points
1,288
Location
Israel
Activeness points
ane,561
Well, you tin can always gather all the information in one file, get-go third of the file volition hold R values, 2d tertiary will hold G values, and last third will concur B values.
Total: file volition hold 331776 values (288 x 384 x 3).
In Verilog, define 3 arrays of 2D, and 1 array 1D:

reg [seven:0] mem_R[0:287][0:383];
reg [7:0] mem_G[0:287][0:383];
reg [seven:0] mem_B[0:287][0:383];
reg [seven:0] mem_File[0:331775];

then $readmemh() the file into mem_File assortment, and add a few loops to copy from mem_File into mem_R, mem_G and mem_B.

  • #16
Joined
April 7, 2010
Letters
23
Helped
1
Reputation
ii
Reaction score
1
Trophy points
1,283
Location
San Jose
Activity points
one,439
i have made 3 different files for R, M and B files. now i am merely trying to read R the file.

i hve attached the text file. please check it.

what might be the fault in this code
module Edge_Detection();

reg clk;

integer fd;
integer i;
integer r;
wire [7:0] im_r[110591:0];

initial
begin
fd = $fopen("C:\Users\Rohit\Documents\MATLAB\image_r.txt","r");
end

initial
begin
clk = 1;
for (i = 0; i <= 110592; i = i+1)
begin
r = $fscanf(fd,"%b",im_r);
$display("%b",im_r);
terminate
end

always
brainstorm
clk = ~clk;
end

initial
begin
$fclose(fd);
$finish;
end

//Edge_Detection ED (clk);

endmodule

Attachments

  • #17
Joined
Nov 4, 2010
Messages
42
Helped
half-dozen
Reputation
12
Reaction score
half dozen
Trophy points
1,288
Location
State of israel
Activeness points
1,561
ane. To actually store the data in the array, I think you lot better define im_r every bit reg, and not every bit wire.

2. In the fopen line, there is a space in the filename (between "image_r.tx" and the "t").

3. There is a common confusion with "binary" contents of a file. When you open up image_r.txt file with a text editor, and can see "00000001" etc, then that'due south a text file, and when reading it in pure binary reading function (like $fread), information technology will give you lot the ascii codes of "0" (0x30) and "1" (0x31), and a Line-Feed lawmaking (0x10) after each eight characters.
For your file format, you better use:
$readmemb("C:\Users\Rohit\Documents\MATLAB\image_r.txt", im_r);
instead of $fopen, $fread and $fclose.

4. To be able to read your debug data in $brandish, you lot better change the "%b" to "%h" or "%0h" (that's a 'cipher' before the 'h') .

---------- Post added at 15:xix ---------- Previous mail service was at 15:18 ----------

another minor issue:
for (i = 0; i <= 110592; i = i+one)
should be:
for (i = 0; i < 110592; i = i+ane)

  • #xviii
Joined
Apr 7, 2010
Messages
23
Helped
1
Reputation
2
Reaction score
1
Trophy points
one,283
Location
San Jose
Activity points
1,439
Thank you a ton yoramgr!!! :razz:
I have used readmemb. I am able to read from a file and write to a file in verilog properly. I have some issues to solve in MATLAB now.
thanks a lot.

I didnt have to employ for loop for readmemb. it reads the whole file at a time
for $brandish i needed to employ the loop to prove one data at a time.
same for $fdisplay to write in a file, used a loop.

  • #nineteen
Joined
April 7, 2010
Messages
23
Helped
1
Reputation
2
Reaction score
1
Trophy points
i,283
Location
San Jose
Activity points
i,439
one more question yoramgr

can i use display in a test bench?

can i pass a two - D assortment from a test demote to verilog code.

i am trying to ship an array of data eight bit wide and with depth 110592

its like [7:0] im_b_x[110591:0]

  • #20
Joined
Sep 13, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Bays points
ane,281
Activity points
1,290
I am also stuck at the same place
Status
Not open for further replies.

Similar threads

  • Text file reading in verilog exam demote
  • Forums
  • Digital Pattern and Embedded Programming
  • PLD, SPLD, GAL, CPLD, FPGA Design

gatewoodillaitty.blogspot.com

Source: https://www.edaboard.com/threads/how-to-read-a-file-into-verilog-test-bench-and-pass-it-to-the-verilog-code.192683/

0 Response to "Read Array in Text File Into Memory Verilog Readmem"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel