integer i;

initial begin
   #100_000;

   // Slew
   // ----------------------------------------------------------------
   vcore_vin_uvlo.VIN_SLEW  = 100;   
   vddi2c_detect = 1'b1;
   sel_in_reg = 1'b1;       
   i = 0;    
   #100_000;
   
   $display("\n VIN plug @ 7V");
   vcore_vin_uvlo.vin_lvl(7000);   
 
   @(posedge vin_uvlo);
   $display("\n enter Latch_otp");
   
   @(posedge en_clo_det);
   $display("\n CLO detection on going");
      
   @(posedge en_cp);
   $display("\n enter precharge");
    
// ----------------------------------------------------------------
// PASS TROUGH
// ----------------------------------------------------------------  
   $display("\n enter test_mode");
   i2c_master.i2cWr(8'd126,8'h17);
   i2c_master.i2cWr(8'd126,8'h51);

   $display("\n verify test_mode");
   i2c_test_mode_sns_fld = 1;
   i2c_read_fields(`TEST_REGISTER0,8'hf8);
  
       
    for (i=0;i<4;i=i+1) begin
    $display("\n run conversion for channel %d",i);
     i2c_tb_adc_channel_fld = i;
     i2c_write_fields(fnGetAddr("i2c_tb_adc_channel_fld"));   
     
     i2c_adc_manual_fld = 1'b1;
     i2c_adc_request_fld =1'b1;    
     i2c_write_fields(fnGetAddr("i2c_adc_request_fld"));   
     #1_000; //resynchro
 
     data1 = $random;
     data2 = $random;
     data3 = $random;
     data4 = $random;
     check_digital_net_id("en_adc",en_adc,i2c_adc_request_fld);
     check_digital_net_id("en_iin_meter",en_iin_meter,i2c_adc_request_fld);
     check_digital_net_id("en_vin_meter",en_vin_meter,i2c_adc_request_fld);
     check_digital_net_id("adc_signed_conv",adc_signed_conv,i2c_tb_adc_signed_fld);
     check_digital_net_id("adc_az_dis",adc_az_dis,1'b0);
     check_digital_net_id("adc_ext_vref",adc_ext_vref,1'b0);
             
     @(posedge adc_enconv);
     check_digital_net_id("adc_channel_mux_ctrl",adc_channel_mux_ctrl,i);

     @(posedge adc_eoc);
     i2c_adc_data_msb_fld = data[9:3];
     i2c_adc_data_lsb_fld = data[2:0];
     i2c_adc_data_sign_fld = data[10];
             
     i2c_adc_request_fld = 1'b0;    
     i2c_read_fields(fnGetAddr("i2c_adc_request_fld"),8'hff);        

     check_digital_net_id("en_adc",en_adc,i2c_adc_request_fld);
     check_digital_net_id("en_iin_meter",en_iin_meter,i2c_adc_request_fld);
     check_digital_net_id("en_vin_meter",en_vin_meter,i2c_adc_request_fld);
     check_digital_net_id("adc_signed_conv",adc_signed_conv,i2c_tb_adc_signed_fld);
     check_digital_net_id("adc_az_dis",adc_az_dis,1'b0);
     check_digital_net_id("adc_ext_vref",adc_ext_vref,1'b0);

    //read results
     i2c_read_fields(fnGetAddr("i2c_adc_data_lsb_fld"),8'hff);   
     i2c_read_fields(fnGetAddr("i2c_adc_data_msb_fld"),8'hff);   
     #1_000_000;
   end

   
   repeat (1) #1_000_000;
// ----------------------------------------------------------------

   // End
   // ----------------------------------------------------------------
   END_SIM;
   
end

// -------------------------------------------------------------------------------------
// monitoring
// -------------------------------------------------------------------------------------
// check converted data

always @ (posedge adc_enconv)
   begin 
      data_expected <= data; 
     // data_sign_expected <= data_sign;                          
      $write("\n start conversion \n");
end

   
always @ (posedge adc_eoc)
   begin
     serial_data_expected <= data_expected;
   $write("\n end conversion");
     // if (mode==0)
      check_digital_net_id("adc_data_out",adc_data_out,data_expected);
   $write("\n---------------------------------------------------\n");
end

