1

I'm trying to write a testbench that will output all the values read and evaluated from a file into a text file. But I can only get a 1 line in the output file instead of the 32 lines. Can someone shed some light?

`timescale 100ns/1ps
module multtest;

reg clk,reset;
reg signed [7:0] a, b;
reg signed [15:0] result,res;
integer fread, fw;
reg [7:0] in_a, in_b;
reg [47:0] in_r;
wire signed[15:0] result1;

mult mult_0 (.clk(clk) , .reset(reset), .A(a), .B(b), .result(result1));

initial 
begin 
 fread = $fopen ("goldenresult","r");
fw = $fopen ("goldresult.txt","w");
clk = 1'b0;
reset = 1'b1;
#200; 
reset = 1'b0;
#200;
reset = 1'b1;
end
always 
#2.5 clk = ~clk; 

//conditon for reset
always @ (reset == 1'b1)
begin
a <= 0;
b <= 0;
result <= 0;
end


always @(posedge clk)

begin

//Verifying the result when testmode = 0  and reset = 0

        if (reset == 1'b0) 
            begin

                while ($fscanf(fread, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, result) != 6) begin end

                    $display ("a = %b, b = %b, result = %b", a, b, result);

                    end
            end

/*always @(posedge clk)
@(negedge reset)

if (reset == 1'b0) 
            begin

                while ($fscanf(fread, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, result) != 6) begin end

 $fwrite(fw, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, res);

end*/
endmodule

I've commented out the portion of the code that was supposed to write. Am i doing anything wrong. The output is in this format:

a = 10111010, b = 00111011, result = 0000000000000000

Its supposed to write all 32 lines but instead writes only 1. Although it correctly displays the output in modelsim.

JUBER
  • 45
  • 1
  • 7

2 Answers2

1

This is line fw = $fopen ("goldresult.txt","w"); create new file for writing or truncate it to zero length. So in your case when you call the $fwrite(fw, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, res); function you re-write each time values.

To add new values you need to use another mode fa = $fopen ("goldresult.txt","a+"); a+ (uses for append; open or create for update at end-of-file)

In code it looks like:

fa = $fopen ("goldresult.txt","a+");
$fwrite(fa, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, res);

and don't forget to close the file $fclose(fa);

Roman
  • 375
  • 2
  • 7
  • The fa lien is not working and also the fclose doesn't seem to work. I can get the output written onto a .txt file only if i close modelsim. Also the output is not displayed line by line but on the same line. How do i make it appear on different lines? – JUBER Apr 18 '17 at 18:12
  • Please add full code of all modules, that I can simulate it – Roman Apr 19 '17 at 00:14
0

The problem is in the way that you are doing the while loop. At the end of the while loop line you have begin end. That means that the only thing that happens in the while loop is just the condition that is being tested ($fscanf). You also want the $fwrite to be in the while loop, so you need to put the end after the the write.

The other problem is that you are testing for != 6 when instead you should continue the while loop when == 6.

You've also said that you'd like a carriage return after each output, so you should use $fdisplay instead of $fwrite. The only difference between them is the inclusion of a carriage return.

while ($fscanf(fread, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, result) == 6) begin
  $fdisplay(fw, "%s = %b, %s = %b, %s = %b", in_a, a, in_b, b, in_r, res);
end
Brad Budlong
  • 1,775
  • 11
  • 10
  • your method doesn't work. I can get the output written onto a .txt file only if i close modelsim. Also the output is not displayed line by line but on the same line. How do i make it appear on different lines? – JUBER Apr 18 '17 at 18:11
  • I've made the changes to continue the loop while valid data is present "==6" and to insert carriage returns. – Brad Budlong Apr 19 '17 at 06:02