reg [7:0] data1_old;
reg [7:0] data2_old;
reg [7:0] data3_old;
reg [7:0] data1_new;
reg [7:0] data2_new;
reg [7:0] data3_new;

initial begin
   #100_000;

   // Slew
   // ----------------------------------------------------------------
   vddi2c_detect = 1'b1;
   sel_in_reg = 1'b1;
   
     
   repeat (1) #1_000_000;

   $display("\n VIN plug @ 7V");
   $display("---------------------------------------------------\n");
   vcore_vin_uvlo.vin_lvl(7000);   
   

   $display("\n PASS_THRU");
   $display("---------------------------------------------------\n");
    @(posedge cp_pass_thru);
   #1000;
   
   $display("\n Read I2C: check VI consistency #1");
   $display("---------------------------------------------------\n");
   @ (posedge en_adc); // First conv skipped
   @ (posedge en_adc);
   #170_000;
   i2c_master.i2cRd2(8'd1);
   if (i2c_master.i2c_rdback_val !== data1_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val,data1_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: V read OK: %h",data1_old);
   end
   if (i2c_master.i2c_rdback_val2 !== data2_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val2,data2_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: I read OK: %h",data2_old);
   end
 
   $display("\n Read I2C: check VI consistency #2");
   $display("---------------------------------------------------\n");
   @ (posedge en_adc);
   @ (posedge en_adc);
   #216_000;
   i2c_master.i2cRd2(8'd1);
   if (i2c_master.i2c_rdback_val !== data1_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val,data1_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: V read OK: %h",data1_old);
   end
   if (i2c_master.i2c_rdback_val2 !== data2_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val2,data2_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: I read OK: %h",data2_old);
   end
 
   $display("\n Read I2C: check VI consistency #1 (HS)");
   $display("---------------------------------------------------\n");
   @ (posedge en_adc);
   @ (posedge en_adc);
   #170_000;
   i2c_master.i2cRdHS2(8'd1);
   if (i2c_master.i2c_rdback_val !== data1_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val,data1_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: V read OK: %h",data1_old);
   end
   if (i2c_master.i2c_rdback_val2 !== data2_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val2,data2_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: I read OK: %h",data2_old);
   end
 
   $display("\n Read I2C: check VI consistency #2 (HS)");
   $display("---------------------------------------------------\n");
   @ (posedge en_adc);
   @ (posedge en_adc);
   #266_000;
   i2c_master.i2cRdHS2(8'd1);
   if (i2c_master.i2c_rdback_val !== data1_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val,data1_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: V read OK: %h",data1_old);
   end
   if (i2c_master.i2c_rdback_val2 !== data2_old) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val2,data2_old);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: I read OK: %h",data2_old);
   end
 
    $display("\n Read I2C (HS)");
   $display("---------------------------------------------------\n");
   @ (posedge en_adc);
   @ (posedge en_adc);
   #1_000_000;
   i2c_master.i2cRdHS2(8'd1);
   if (i2c_master.i2c_rdback_val !== data1_new) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val,data1_new);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: V read OK: %h",data1_new);
   end
   if (i2c_master.i2c_rdback_val2 !== data2_new) begin
      PRINT_ERROR("");
      $display("FAIL: V read should be old one: %h io %h",i2c_master.i2c_rdback_val2,data2_new);
   end
   else begin
      PRINT_PASS("");
      $display("PASS: I read OK: %h",data2_new);
   end
 
  // End
   // ----------------------------------------------------------------
   repeat (1) #1_000_000;
   END_SIM;
   
end

always @ (posedge en_adc) begin
   data1_old = data1[9:2];
   data2_old = data2[9:2];
   data3_old = data3[9:2];
   data1 = $random;
   data2 = $random;
   data3 = $random;
   data1_new = data1[9:2];
   data2_new = data2[9:2];
   data3_new = data3[9:2];
end

