I have three variables, x,y,z which acts as an input to my SV model. Each of the input are of 16bit length binary data and I have 500 values of them. I need to supply them as input to my module at every posedge of the clock and each after #20ns delay. This is via the TB and also I need to write the output values into a text file. Can someone help me with code. Here is what I have written.

logic [15:0] X,Y,Z;
logic clk, vectornum;

logic [15:0] testvectors [499:0];

or or1(.*);

initial begin
   clk =0;
   forever #5ns clk #~clk;

initial $readmemb("xyz.txt",testvectors);

always@(posedge clk)
   #20 {X,Y,Z};

1 Answer 1


Surely you need some thing more along the lines of :

#20 {X,Y,Z} = testvectors[vectornum];

However $readmemb is meant for initialising memories. Reading the file line by line using scanf would seem more appropriate here. This would also be similar syntax used to write the output to a file.

integer data_in_file; //file handler
integer i;
integer r_in; //file handler

//input data
logic signed [31:0] dat1_in;
logic signed [31:0] dat2_in;
logic signed [31:0] dat3_in;

initial begin
data_in_file  = $fopen("data.in", "r");
for(i=0; (i<500) && (!$feof(data_in_file)); i=i+1) begin
   //Expecting 3 decimal numbers
   r_in  = $fscanf(data_in_file, "%d %d %d\n", dat1_in, dat2_in, dat3_in);

To write a file:

integer data_file_out;
logic signed [31:0] a; //Data driven some where else

initial begin
  data_file_out= $fopen("test", "w");
  //place write inside loop
  $fwrite(file, "%b", a); //Wrte a as Binary number 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.