real     bias_time1;
real     bias_time2;  

initial begin
   #100_000;
   bias_time1 = 0;
   bias_time2 = 0; 

   // Slew
   // ----------------------------------------------------------------
   vcore_vin_uvlo.VIN_SLEW  = 100;   
   vddi2c_detect = 1'b1;
   sel_in_reg = 1'b1;           

   #100_000;
   data1 = $random;
   data2 = $random;
   data3 = $random;
   data4 = $random;
   
   $display("\n VIN plug @ 10V");
   vcore_vin_uvlo.vin_lvl(10000);   
 
   @(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");
    
// ----------------------------------------------------------------
// DIV2
// ----------------------------------------------------------------  
   @(posedge cp_div2);
   $display("\n enter div2");
   $display("\n// clear interrupts");
   i2c_adc_eoc_int_fld = 1'b1;
   i2c_read_fields(fnGetAddr("i2c_adc_eoc_int_fld"),8'h00);
    
   $display("\n// Manual conv request by I2C");
   i2c_adc_manual_fld = 1'b1;
   i2c_adc_request_fld =1'b1;
   
   i2c_vin_fld   = data1[9:2];    
   i2c_iin_fld   = data2[9:2];    
   i2c_temp_fld  = data3[9:2];    
          
   i2c_write_fields(fnGetAddr("i2c_adc_request_fld"));   
   #1_000; //resynchro
   bias_time1 = $realtime;
   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,1'b1);
   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_clk);
   bias_time2 = $realtime;
   check_time(bias_time1,bias_time2,30_000*0.94, 30_000*1.06);  
   
   @(posedge adc_enconv);  
   check_digital_net_id("adc_channel_mux_ctrl",adc_channel_mux_ctrl,0);    
   @(posedge adc_eoc);
   @(posedge adc_enconv);   
   check_digital_net_id("adc_channel_mux_ctrl",adc_channel_mux_ctrl,1);    
   @(posedge adc_eoc);
   @(posedge adc_enconv);   
   check_digital_net_id("adc_channel_mux_ctrl",adc_channel_mux_ctrl,2);    
   @(posedge adc_eoc);   
   i2c_adc_request_fld =1'b0;
   #10_000;

   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,1'b1);
   check_digital_net_id("adc_az_dis",adc_az_dis,1'b0);
   check_digital_net_id("adc_ext_vref",adc_ext_vref,1'b0);

   $display("\n// check status");
   i2c_adc_eoc_stat_fld = 1'b1;
   i2c_read_fields(fnGetAddr("i2c_adc_eoc_stat_fld"),8'hff);
 
   $display("\n// clear interrupt");
   i2c_adc_eoc_int_fld = 1'b1;
   i2c_read_fields(fnGetAddr("i2c_adc_eoc_int_fld"),8'hff);
   
   $display("\n// read back status");
   i2c_adc_eoc_stat_fld = 1'b0;
   i2c_read_fields(fnGetAddr("i2c_adc_eoc_stat_fld"),8'hff);
   
   $display("\n// read back interrupt");
   i2c_adc_eoc_int_fld = 1'b0;
   i2c_read_fields(fnGetAddr("i2c_adc_eoc_int_fld"),8'hff);
   
   $display("\n// read results");
   i2c_read_fields(fnGetAddr("i2c_vin_fld"),8'hff);
   i2c_read_fields(fnGetAddr("i2c_iin_fld"),8'hff);
   i2c_read_fields(fnGetAddr("i2c_temp_fld"),8'hff);
   
   #500_000;
   
   $display("\n// Return to periodic conv");
   i2c_read_fields(fnGetAddr("i2c_adc_request_fld"),8'hff);

   i2c_adc_manual_fld = 1'b0;
   i2c_write_fields(fnGetAddr("i2c_adc_request_fld"));   
   #1_000;
   i2c_read_fields(fnGetAddr("i2c_adc_request_fld"),8'hff);
   
   data1 = $random;
   data2 = $random;
   data3 = $random;
   data4 = $random;
   
   repeat (3) begin  
   @(posedge adc_eoc);   
   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


