user3056350
user3056350

Reputation: 67

Trying to write to a text file from Verilog but the output file is empty

I need to write 2 variables from Verilog modules one by one in a file. The variables are updated on rising edge of a one cycle signal freq_rdy. I am using following code.

integer write_file1;
integer freq_rdy_1,freq_rdy_2;

initial begin

write_file1 = $fopen("frequencies.txt","w");        
freq_rdy_1 = testbench.UUT.read_controller.freq_rdy;
freq_rdy_2 = testbench.UUT.read_controller_2.freq_rdy;
        
@(posedge freq_rdy_1)
    $fwrite(write_file1,"%d \n",testbench.UUT.read_controller.frequency_i); 

@(posedge freq_rdy_2)  
    $fwrite(write_file1,"%d \n",testbench.UUT.read_controller_2.frequency_i); 

 
#1000000
$fclose(write_file1);    
end

The output text file is empty. What am I doing wrong in this code?

Upvotes: 0

Views: 1957

Answers (2)

user3056350
user3056350

Reputation: 67

I can see edges in waveform.

The problem was solved by replacing w with a, i.e.

Instead of

$fopen("frequencies.txt","w");

I wrote

$fopen("frequencies.txt","a");

Though I have to delete file every time I run simulation.

Rest of logic is implemented using always blocks.

initial begin 
write_file1 = $fopen("frequencies.txt","a");
end 
always @(posedge. testbench.UUT.read_controller.freq_rdy)         begin 
$fwrite(write_file1,"%d \n",testbench.UUT.read_controller.frequency_i);
end 
always @(posedge testbench.UUT.read_controller_2.freq_rdy)       begin 
$fwrite(write_file1,"%d \n",testbench.UUT.read_controller_2.frequency_i); 
end

Upvotes: 0

toolic
toolic

Reputation: 62120

The problem is that you only write to the file when you see a posedge of either freq_rdy_1 or freq_rdy_2. But, you never get a posedge of those signals because you only set them once (before the @posedge statements). Therefore, you never write anything to the file.

Perhaps this is what you are looking for:

integer write_file1;
initial begin
    write_file1 = $fopen("frequencies.txt","w");
    forever begin
        fork
            begin
                @(posedge testbench.UUT.read_controller.freq_rdy);
                $fwrite(write_file1,"%d \n",testbench.UUT.read_controller.frequency_i);
            end
            begin
                @(posedge testbench.UUT.read_controller_2.freq_rdy);
                $fwrite(write_file1,"%d \n",testbench.UUT.read_controller_2.frequency_i);
            end
            begin
                #1_000_000;
                $fclose(write_file1);
            end
        join
    end
end

Upvotes: 2

Related Questions