Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion 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! To participate you need to register. Registration is free. Click here to register now.

Writing data from memory into a text file

Status
Not open for further replies.

Gayathrirani

Newbie level 4
Newbie level 4
Joined
Jul 11, 2014
Messages
7
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Visit site
Activity points
73
I've read data from txt file(containing pixels of image),Binarize it.Now i want the binarized output to be written into another txt file.Below is my coding.When i run it, i get the following error.

"# ** Error: (vsim-8323) D:/G/Reference materials/Sample codes/Iris Localization/imgbinary.v(25): $fwrite : Argument number 3 is an unpacked type, and may only be printed with the '%p' format.
# Error loading design" .What's wrong in this code??

Code:
module file_readmemh_1; 
  reg [7:0] data_1 [0:1023];
  reg [7:0] data_th [0:1023];
  parameter th=8'b1000110;
  initial $readmemh("lenaimghex_1.txt", data_1);
  integer i,j,bin;
  initial begin
        $display("rdata:");
               for (i=0; i < 1024; i=i+1) 
        $display("%d:%h",i,data_1[i]);
      end
      initial begin
         for (j=0; j < 1024; j=j+1)
      if (data_1[j]>=th)
        begin
      data_th[j]<=8'b1;
    end
  else
    begin
      data_th[j]<=8'b0;
       end
  end
initial begin
bin=$fopen("lena_binthresh.txt","w");
$fwrite(bin,"%h\n",data_th);
$fclose(bin);
 end
 endmodule
 
Last edited:

I've read data from txt file(containing pixels of image),Binarize it.Now i want the binarized output to be written into another txt file.Below is my coding.When i run it, i get the following error.

"# ** Error: (vsim-8323) D:/G/Reference materials/Sample codes/Iris Localization/imgbinary.v(25): $fwrite : Argument number 3 is an unpacked type, and may only be printed with the '%p' format.
# Error loading design" .What's wrong in this code??

Code:
  reg [7:0] data_th [0:1023];
initial begin
bin=$fopen("lena_binthresh.txt","w");
$fwrite(bin,"%h\n",[COLOR="#FF0000"][B]data_th[/B][/COLOR]);
$fclose(bin);
 end
 endmodule
It tells you exactly what is wrong: Argument number 3 is an unpacked type
You should put the $fwrite in a for loop and use the index to access each element of the arrray: i.e.: data_th

- - - Updated - - -

I'm not sure your code will do what you want as the three initial blocks are all run in parallel and depending on how the scheduling of them occurs you may end up with no data_th values (other than X's) that can be written to the lena_binthresh.txt file, as there is no guarantee that the 2nd initial block that initializes the data_tb array will always be scheduled first.

You should combine all three of these initial blocks into one as the inputs are dependent on one initial block finishing before the next one starts. (My guess is you write a lot of software)
 

    V

    Points: 2
    Helpful Answer Positive Rating
Thank you for your suggestion.I changed the code accordingly.Now the error is "(18): $fwrite : Argument 1 is an unknown file descriptor"
Code:
module file_readmemh_1; 
  reg [7:0] data_1 [0:1023];
  reg [7:0] data_th [0:1023];
  integer j,bin;
  parameter th=8'b1000110;
  initial  begin
  $readmemh("lenaimghex_1.txt", data_1);
  for (j=0; j < 1024; j=j+1)
  begin
      if (data_1[j]>=th)
      data_th[j]<=8'b1;
  else
    begin
      data_th[j]<=8'b0;
      end
 bin=$fopen("lena_binthresh.txt","w");
 $fwrite(bin,"%h\n",data_th[j]);
 $fclose(bin);
 end
 end
 endmodule
I made 2 other tries.But didn't get the result.
1.I removed the begin/end statements of 'for' loop.The design compiles and runs fine.But i get xx in .txt file.
2.In addition to first try,I used separate 'for ' loop before $fwrite.I got 1024 xx values in .txt file.
 

You didn't check that "bin" variable to see if it contains an actual file desciptor did you? For all you know that $fopen() could be failing horribly at the attempt of opening a file for writing.

Better read the docs of $fopen() return values and adjust accordingly.
 

Why don't you try with '$fstrobe' command instead of $fwrite. I normally use that command and it works. I tried with $fwrite command once and it didn't work correctly, and didn't show any error too.
 

Not that using $fstrobe() helps in case of an illegal file handle, but what the hell, you might get lucky without the best practice of checking returned file handles for another day.
 

I suggested $fstrobe because I was also getting 'x' values in the text file when I tried $fwrite. I only have a limited knowledge about file handles. I was just sharing my experience. Sorry if it was a foolish suggestion.
 

I made 2 other tries.But didn't get the result.
1.I removed the begin/end statements of 'for' loop.The design compiles and runs fine.But i get xx in .txt file.
2.In addition to first try,I used separate 'for ' loop before $fwrite.I got 1024 xx values in .txt file.
The problem is with fallowing non blocking statements;
Code:
for (j=0; j < 1024; j=j+1)
  begin
      if (data_1[j]>=th)
      data_th[j]<=8'b1;
  else
    begin
      data_th[j]<=8'b0;
      end
$fwrite(bin,"%h\n",data_th[j]);
In above code value data_th[j] is not available immediately to write into file,so it is writing ''xx'' value.Change above code to blocking statements and also change the position of fclose,fopen as shown in below code.
Code:
module file_readmemh_1; 
  reg [7:0] data_1 [0:1023];
  reg [7:0] data_th [0:1023];
  integer j,bin;
  parameter th=8'b1000110;
  initial  begin
  $readmemh("lenaimghex_1.txt", data_1);
  bin=$fopen("lena_binthresh.txt","w");
  for (j=0; j < 1024; j=j+1)
  begin
      if (data_1[j]>=th)
      data_th[j]=8'b1;
  else
    begin
      data_th[j]=8'b0;
      end
 
 $fwrite(bin,"%h\n",data_th[j]);
 
 end
 $fclose(bin);
 end
 endmodule
 

    V

    Points: 2
    Helpful Answer Positive Rating
I suggested $fstrobe because I was also getting 'x' values in the text file when I tried $fwrite. I only have a limited knowledge about file handles. I was just sharing my experience. Sorry if it was a foolish suggestion.
Your suggestion might even work, since re-reading the post I see that the displayed code probably is not representative for ALL his attempts. So maybe in some permutation that we don't get to see your $fstrobe suggestion actually will help. Because if he does sometimes gets a file (as per his list item 1.) , he sometimes gets a legitimate file handle.

Anyways, the advice to properly check the returned file handles still stands. Doing so prevents lots of stupid stuff.

Not that I really expect people to actually do so. In HDL country it seems popular to just assume file operations always succeed. :p
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top