`define otp_div2_dly    otp[43:42]
integer j;
real     div2_time1;
real     div2_time2;  

initial begin
   #100_000;
    div2_time1 = 0;
    div2_time2 = 0;
       
   // Slew
   // ----------------------------------------------------------------
   vcore_vin_uvlo.VIN_SLEW  = 100;
   vddi2c_detect = 1'b1;             
   sel_in_reg =1'b1;
   #100_000;
   
   $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");
   @(posedge cp_div2);   
   $display("\n enter div2");
   i2c_read_fields(fnGetAddr("i2c_div2_dly_fld"),8'hff);

   #200_000;
// ----------------------------------------------------------------
// DIV2
// ----------------------------------------------------------------   
   $display("\n Check I2C prog_div2_dly settings ");
   $display("---------------------------------------------------\n");
   for (j=0;j<=7;j=j+1) begin
    i2c_div2_dly_fld = j;
    i2c_write_fields(fnGetAddr("i2c_div2_dly_fld"));      
    i2c_read_fields(fnGetAddr("i2c_div2_dly_fld"),8'hff);
    #50_000;       
    vcore_vin_uvlo.vin_lvl(9000);    
    @(negedge mode_out); 
    div2_time1 = $realtime;
    @(posedge cp_pass_thru);
    div2_time2 = $realtime;
    if (j<7) begin
      check_time(div2_time1,div2_time2,70_000*(1<<j), 80_000*(1<<j));
    end  
    else begin
      check_time(div2_time1,div2_time2,0, 1000);  
    end
    #10;
    check_digital_net_id("cp_div2",cp_div2,0);
    #50_000;       
    vcore_vin_uvlo.vin_lvl(10000);    
    @(posedge cp_div2);
    $display("\n re-enter div2");
    #10;
    check_digital_net_id("cp_pass_thru",cp_pass_thru,0);
    #50_000;                           
   end

   i2c_div2_dly_fld = 0;
   i2c_write_fields(fnGetAddr("i2c_div2_dly_fld"));      
   
   $display("\n UVLO");
   vcore_vin_uvlo.VIN_SLEW  = 10;
   vcore_vin_uvlo.vin_lvl(0);    
   #500_000;
   
   $display("\n Check OTP prog_div2_dly default settings ");
   $display("---------------------------------------------------\n");
   for (j=0;j<=3;j=j+1) begin
    force `otp_div2_dly = j;
    
    $display("\n VIN plug @ 11V");
    vcore_vin_uvlo.VIN_SLEW  = 100;
    vcore_vin_uvlo.vin_lvl(11000);
    
    @(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");
    @(posedge cp_div2);
    $display("\n enter div2");
    i2c_read_fields(fnGetAddr("i2c_div2_dly_fld"),8'hff);

    $display("\n UVLO");
    vcore_vin_uvlo.VIN_SLEW  = 10;
    vcore_vin_uvlo.vin_lvl(0);
    #500_000;
   end
      
   repeat (1) #1_000_000;
   // End
   // ----------------------------------------------------------------
   END_SIM;
   
end

always @(*) begin
   case ( `otp_div2_dly )
   2'b00: i2c_div2_dly_fld = 3'b001;
   2'b01: i2c_div2_dly_fld = 3'b000;
   2'b10: i2c_div2_dly_fld = 3'b011;
   2'b11 :i2c_div2_dly_fld = 3'b111;
   endcase
end     



