kcinj
kcinj

Reputation: 37

testbench writing unexpected output to file

I am writing a testbench for an LFSR and want to write the output's value to a txt file so I can use that for some scripts later. In my waveforms, the hex values of the result are correct, but the output file only consists of 1s, not the actual values. I have been unable to determine why. Here is the testbench:

module lfsr13_tb();

reg clk, rst_n, en;
reg[12:0] seed;
wire[12:0] pseudo_random_val;
integer file, i;

lfsr13 iDUT(.clk(clk), .en(en), .rst_n(rst_n), .seed(seed), .pseudo_random_val(pseudo_random_val));

initial begin
seed = 1;
clk = 0;
rst_n = 0; // assert reset
en = 0;    // disabled to start
file = $fopen("lfsr_output.txt", "w");

@(negedge clk) rst_n = 1;   // deassert reset
@(posedge clk) en = 1;      // assert enable to begin lfsr function
for(i=0; i<=8191; i=i+1) begin
  $fwrite(file, "%h\n", pseudo_random_val);
  end

$fclose(file);
end

always
  #5 clk = ~clk;

endmodule 

I need to have the "pseudo_random_val" written to the output file each period.

Upvotes: 1

Views: 361

Answers (1)

toolic
toolic

Reputation: 62120

Here is what your testbench does: at one point in time (specifically, the 1st posedge of clk), you write the current value of pseudo_random_val 8192 times (which will be the same value) to the file.

This will write the value of pseudo_random_val to the file once per clock period for the duration of the simulation (which may be more or less than 8192 times):

always @(posedge clk) $fwrite(file, "%h\n", pseudo_random_val);

Upvotes: 1

Related Questions