integer j;
integer adc_per_div2;
reg ext_adc;  
reg adc_ext_clk;
reg check_serializer;
reg check_serializer_done;
//reg     [10:0] data_expected;
//reg     [10:0] serial_data_expected;
//reg     [10:0] serializer_data_out;   

initial begin
   ext_adc =1'b0;
   adc_per_div2 = 665;
   adc_ext_clk =1'b0;
   check_serializer =1'b0;
   check_serializer_done =1'b0;
   data_expected        = 11'b0000000000;
   serial_data_expected = 11'b0000000000;
   #100_000;

   // Slew
   // ----------------------------------------------------------------
   vcore_vin_uvlo.VIN_SLEW  = 100;   
   vddi2c_detect = 1'b1;
   sel_in_reg = 1'b1;       
   #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);
         
   $display("\n enter ADC DFT config");
   i2c_tb_adc_dft_config_fld = 1'b1;
   i2c_tb_adc_ext_vref_fld = 1'b1;
   i2c_tb_adc_channel_fld = 3;
   i2c_write_fields(fnGetAddr("i2c_tb_adc_dft_config_fld"));   
   #100;
   check_digital_net_id("cli_in_en",cli_in_en,1'b1);
   check_digital_net_id("mode_in_en",mode_in_en,1'b1); 
    
   $display("\n run conversion for channel 4");     
   i2c_adc_manual_fld = 1'b1;
   i2c_adc_request_fld =1'b1;    
   i2c_write_fields(fnGetAddr("i2c_adc_request_fld"));   
   #1_000; //resynchro
 
   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'b1);
             
   $display("\n start external clock");     
   ext_adc = 1'b1;  

   #35_000; //resynchro
   $display("\n start conv");     
   mode_in_reg = 1'b1;
   #5_000;
   mode_in_reg = 1'b0;
    
   @(posedge adc_eoc);
   $display("\n  conversion done");
   check_digital_net_id("adc_channel_mux_ctrl",adc_channel_mux_ctrl,3);
   
   @(posedge adc_clk);
   #1;
   @(posedge adc_clk);
   check_serializer =1'b1;
  //  #10_000;
   @(posedge check_serializer_done);
   check_serializer =1'b0;
   #5_000;   
     
   i2c_adc_data_msb_fld = data[9:3];
   i2c_adc_data_lsb_fld = data[2:0];
   i2c_adc_data_sign_fld = data[10];
           
   $display("\n  check request resetted but biasing maintained");
   i2c_adc_request_fld = 1'b0;
   i2c_read_fields(fnGetAddr("i2c_adc_request_fld"),8'hff);        
   check_digital_net_id("en_adc",en_adc,1'b1);
   check_digital_net_id("en_iin_meter",en_iin_meter,1'b1);
   check_digital_net_id("en_vin_meter",en_vin_meter,1'b1);
   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'b1);

   $display("\n  read results");
   i2c_read_fields(fnGetAddr("i2c_adc_data_lsb_fld"),8'hff);   
   i2c_read_fields(fnGetAddr("i2c_adc_data_msb_fld"),8'hff);   
   
   #5_000;   
   
   for (j=3;j>=0;j=j-1) begin 
   data1 = $random;
   data2 = $random;
   data3 = $random;
   data4 = $random;
   
   if (j!=3 ) begin
   i2c_tb_adc_channel_fld = j;
   i2c_write_fields(fnGetAddr("i2c_tb_adc_channel_fld")); 
   i2c_adc_request_fld =1'b1;    
   i2c_write_fields(fnGetAddr("i2c_adc_request_fld"));         
   #30_000;
   end   
   #5_000;
  
   $display("\n start conv");     
   mode_in_reg = 1'b1;
   #5_000;
   mode_in_reg = 1'b0;   
   
   @(posedge adc_eoc);
   $display("\n  conversion done");
   check_digital_net_id("adc_channel_mux_ctrl",adc_channel_mux_ctrl,j);
   
   @(posedge adc_clk);
   #1;
   @(posedge adc_clk);
   check_serializer =1'b1;
  //  #10_000;
   @(posedge check_serializer_done);
   check_serializer =1'b0;
   #5_000;   
     
   i2c_adc_data_msb_fld = data[9:3];
   i2c_adc_data_lsb_fld = data[2:0];
   i2c_adc_data_sign_fld = data[10];
           
   $display("\n  check request resetted but biasing maintained");
   i2c_adc_request_fld = 1'b0;
   i2c_read_fields(fnGetAddr("i2c_adc_request_fld"),8'hff);        
   check_digital_net_id("en_adc",en_adc,1'b1);
   check_digital_net_id("en_iin_meter",en_iin_meter,1'b1);
   check_digital_net_id("en_vin_meter",en_vin_meter,1'b1);
   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'b1);

   $display("\n  read results");
   i2c_read_fields(fnGetAddr("i2c_adc_data_lsb_fld"),8'hff);   
   i2c_read_fields(fnGetAddr("i2c_adc_data_msb_fld"),8'hff);   
   
   #5_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

//---------------------------------------------------
// External clock
//---------------------------------------------------
always @ (posedge ext_adc) 
 begin
   #10_000;
   while ( ext_adc ) begin
      #(adc_per_div2);
      adc_ext_clk = ~adc_ext_clk;
      cli_in_reg  = adc_ext_clk;

   end
end

//---------------------------------------------------
// serializer
//---------------------------------------------------
always @ (posedge check_serializer)  //serial_frame not accessible
   begin
     check_serializer_done =1'b0;
     serializer_data_out = 11'b0000000000;       
//     while (check_serializer) begin
      for (count=0; count <= 10; count=count+1) begin
      @(posedge adc_clk);        //#665; adc_per
         if (count==11) begin
            serializer_data_out[10 - count] <= clo_out;   //serial_data
            @(posedge adc_clk);     //#665; 
         end   
         else 
         serializer_data_out[10 - count] <= clo_out ;  //serial_data
      end
     count=0;
     check_serializer_done =1'b1;
//     end 
   end   

always @ (posedge check_serializer_done) 
   begin
      #1;
      check_digital_net_id("serializer_data_out",serializer_data_out,serial_data_expected);
   end
