[verilog] write the answer in .txt using $fwrite

  • Thread starter hoheiho
  • Start date
In summary, there appears to be an issue with printing binary numbers from the MEM array to a .txt file using the $fwrite function. To solve this, a for loop can be used to iterate through each element in the array and print out the binary number associated with it to the .txt file.
  • #1
hoheiho
47
0

Homework Statement


Hi, I am now currently working with a CPU. Now I want to write the RAM's MEM into a .txt file.

Code:
reg [31:0] 	mem [ 0 : (32'h0000_5000 / 4)-1 ] 

  // To .txt
   integer  	test_file;
    initial 
    begin
      test_file = $fopen("result_file.txt","w"); 
    end

//the MEM part
   always @ (posedge wb_clk_i)
     begin
	if (ram_we)
	  begin
	  mem[adr] <= wr_data;
	  $fwrite(test_file,"%b\n",mem);    // I have only added this part
	  end  
     end

Then I got an error message : $fwrite : Argument number 3 is an unpacked type, and may only be printed with the '%p' format.

I have tried to change it to %p. But it print out somethings like dec number. I prefer the test_file.txt will got all the binary number from MEM.

Could anyone seen the problem?

Thanks for the help
Ivan
 
Physics news on Phys.org
  • #2
Homework EquationsN/AThe Attempt at a SolutionIt appears that you are trying to print the binary numbers from the MEM array to a .txt file using the $fwrite function. The third argument of the $fwrite function must be specified with the '%p' format, as the error message suggests. However, the '%p' format prints out decimal numbers, not binary numbers. To solve this issue, you can use a for loop to iterate through each element in the MEM array and use the $fwrite function to print out the binary number associated with that element to the .txt file. For example, the following code should do the trick:initial begin test_file = $fopen("result_file.txt","w"); endalways @ (posedge wb_clk_i)begin if (ram_we) begin mem[adr] <= wr_data; for (int i=0; i<(32'h0000_5000/4); i++) begin $fwrite(test_file,"%b\n",mem); end end end
 

1. What is the purpose of using $fwrite in Verilog?

The $fwrite command in Verilog is used to write formatted data into a specified file. It is commonly used in simulation and testbench environments to generate output files for analysis and debugging purposes.

2. How is $fwrite different from $display in Verilog?

While both $fwrite and $display are used to print data, $fwrite allows for more control over the formatting of the data and the ability to write to a specific file. $display only prints to the console and does not allow for formatting options.

3. Can $fwrite be used to write data into multiple files simultaneously?

Yes, $fwrite can be used to write data into multiple files simultaneously by specifying different file names in the command. This is particularly useful for debugging multiple signals or modules in a single simulation run.

4. What is the syntax for using $fwrite in Verilog?

The basic syntax for using $fwrite is: $fwrite(file_handle, format, data); where file_handle is the variable representing the file to be written to, format is the desired format for the data, and data is the data to be written to the file. It is important to note that the file_handle must be opened using the $fopen command before using $fwrite.

5. Can $fwrite be used to write non-ASCII characters into a file?

Yes, $fwrite can be used to write non-ASCII characters into a file by using the appropriate format specifier. For example, the %c format specifier can be used to write a single character into the file, including non-ASCII characters. However, it is important to ensure that the file is opened with the appropriate file encoding to support non-ASCII characters.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
20K
  • Electrical Engineering
Replies
2
Views
2K
  • General Engineering
Replies
1
Views
2K
  • General Engineering
Replies
1
Views
12K
Back
Top