// Created by ihdl
module digital_control_top ( 

// CORE
input             porb,
output            en_por,
output            en_vreg,
output            en_vcore,
output            en_refs,

// CLOCK
input             clk_125khz,
output            en_clk_125khz,
input             clk_rdy,
input             tb_fast_clk,
output            fast_clk,
output      [1:0] prog_buck_clk,    
output            en_fmod,          ///enable spead spectrum
output            fmod_ratio,       ///modulation ratio

// TSD
output            en_tsd,
input             tsd,

// INPUT MONITOR
input             vin_det,          ///previous vinvbatt_coarse
input             vin_ok,           ///to comps    vin > Vinlo
input             vin_vsys,          
input             vin_ov,           
input             vin_vsysmin,        ///vinmin_ok      
input             iin_scd,          
output            en_vin_monitor,    
output            en_iin_meter,      

//CELL DETECTION
input             cell_config,     ///vcell
output            en_cell_cfg,     

// SYST MONITOR
input             vsys_ov,          
input             vbuck_ov,         
input             vsys_reconn,      ///drcon
input             vsys_reopen,      ///dopen: attention polarite -> 1= vsys<vdopen
output            en_vsys_monitor,  
output            prog_reconn,      

// BATT MONITOR
input             sysok,          
input             ieoc,             
input             prechg_ok,             
output            en_vbat_monitor,  
output      [2:0] prog_ieoc,        
output            en_eocdet,        
output            en_ichg_meter,    
output            en_dischg_meter,  

// CHARGE CTRL
input             ipeak,
output            en_buck,          
output            en_buck_delayed,
output            eco_mode,         
output            en_rbfet, 
output            en_rbfet_fall_stretch,  
output            en_dynprechg,
output            en_dac_volt,      
output            en_dac_current,

output     [10:0] prog_dvs_vbuck,         
output      [5:0] prog_dvs_ichg,         
output      [5:0] prog_dvs_iin,                  
output      [7:0] prog_vsysmin,           
output      [1:0] vbuck_range,
output            dvs_running,
output            full_charge_state,      

// BATFET
input             batfet_is_on,     
output            batfet_off,       
output            en_prechg,        
output            en_faston,        ///from fsm a wrapper ???

// SMBus
input             sda,
input             scl,
output            sda_out,
input             vddsmbus_det,

//PMO
output            en_pmo_input,
output            en_pmo_battery_chg,
output            en_pmo_battery_dischg,
output            ibat_gain_sel,   
output            pwr_gain_sel,   
output            freeze_voltmeter,       ///pmo_imo_sel, 

//VDROOP
input             vrail_drop,         
input             vrail_low,       
output            en_vrail_monitor,          
output      [1:0] prog_vrail_low,      
output            prochotb,           ///test muxing or control to be added ? 

// FLAG
output            flag_vin_ok,     

// TEST MUX
input             buck_dig_testout,

// TESTMODE
///output            test_mode_ic,    // not used in apollo
output            fast_smbus,

output      [3:0]   testmux_analog_buck,
output      [3:0]   testmux_analog_core,
output      [3:0]   testmux_analog_chgref,
output      [3:0]   testmux_analog_pmo,
output      [3:0]   testmux_analog_top,

output      [20:0]  tb_buck,        
output      [4:0]   tb_osc,        
output      [2:0]   tb_bg,        
output      [2:0]   tb_imeter,        
output      [3:0]   buck_dig_testmux,        


// AUTOCAL
output            en_autocal,
output            clk_cal,
input             cal_out,
input             cal_ok,

// SCAN
input             scan_clk,
input             scan_enable,
input             scan_rstb,
input             scan_data_in,
output            scan_mode_ic,

input             buck_scan_data_out,
output            buck_scan_mode,
output            buck_scan_enable,
input             osc_scan_data_out,
output            osc_scan_mode,
output            osc_scan_enable,

// OTP
input      [95:0] otp,

// OTP #1
input             otp_memvalid_1,
output            otp_sftset_1,
output            otp_sftrst_1,
output            otp_clk_1,
output            otp_reset_1,
output            otp_prog_1,
output            otp_sensez_1,
output            otp_lat_1,
output            otp_cursen1_1,
output            otp_cursen0_1
);

/// ****************************************************************************
/// Declarations
/// ****************************************************************************
wire       [15:0] read_bus;
wire       [15:0] write_bus;
wire        [7:0] addr_bus;
wire        [7:0] smb_addr_bus;
wire        [6:0] IC_address;
wire              WR;
wire              RD;
wire       [31:0] otp_reg;
wire              soft_write;
wire              hard_write;
wire        [1:0] otp_sel;
wire      [255:0] reg_en;

reg               test_mode_reg;
reg               test_mode_tmp;

wire       [1:0]  otp_freq;
wire       [5:0]  otp_ichg;
wire       [2:0]  otp_ipre;
wire       [5:0]  otp_iinlim;
wire      [10:0]  otp_vchg;
wire       [7:0]  otp_vsysmin;
wire              otp_en_eocdet;
wire              otp_i2c_add;
wire              otp_i2c_sel;
wire              otp_vinok_pol;
wire              otp_vinok_sel;
wire              otp_prechg_regu;

wire        [2:0] chrg_state;
wire              precharge_state;
wire              fullcharge_state;
wire              sysrst_state;
wire              config_state;
wire              eoc_state;

wire              int_clk_en;
wire              otp_clk_en;
wire              startup_clk_en;
wire              chrg_clk_en;
wire              detect_clk_en;
wire              vdrp_clk_en;
wire              smbus_clk_en;

wire              chg_fsm_on;
wire              en_pmo_in;
wire              en_pmo_bat;
wire              en_iin_meter_fsm;
wire              en_ichg_meter_fsm;
wire              en_dischg_meter_fsm;
wire              en_vreg_fsm;
wire              en_refs_fsm;
wire              en_vcore_fsm;
 
wire       [2:0]  ipre;
wire       [5:0]  ichg;
wire       [5:0]  iinlim;
wire      [10:0]  vchg;
wire       [5:0]  prog_ichg;
wire       [5:0]  prog_iin;
wire      [10:0]  prog_vchg;
wire       [7:0]  vsysmin;
wire      [1:0]   batdis_tmr_set;
wire              hwrst_tmr_set;
wire      [1:0]   wd_tmr_set;
wire              wd_timeout;
wire              smb_timeout;

wire              bat_dis;
wire              chg_dis;
///wire              wdto_dis;
wire              ldo_mode;
wire              learn;
wire              hw_rst;
wire              eoc;
wire              nb_cell;
wire              cell_fsm_timer_reset;

wire              vin_det_sync;
wire              vin_ok_cell_sync;
wire              tsd_sync;
wire              chg_dis_sync;
wire              bat_dis_sync;
wire              learn_sync;
wire              hw_rst_sync;
wire              ldo_mode_sync;
wire              ieoc_sync;  
wire              eoc_sync;    
wire              vsys_reopen_sync;
wire              vsys_reconn_sync; 
wire              vsys_ov_sync;     
wire              iin_scd_sync;     
wire              vin_ov_sync;
wire              vin_ov_cell_sync;
///wire              batfet_is_on_sync;
wire              ieocdet_en_sync;

wire              vin_ok_dbnc;
wire              vin_range_ok_dbnc;
wire              vin_ok_cell_dbnc;
wire              vin_vsys_dbnc;
wire              vin_vsysmin_100ms_dbnc;
wire              vin_vsysmin_15ms_dbnc;
wire              ieoc_dbnc;
wire              prechg_ok_dbnc;
wire              sysok_dbnc;
wire              vsys_reopen_dbnc;
wire              cell_config_dbnc;

wire              vsys_ov_latch;
wire              iin_scd_latch;
wire              vin_ov_latch;
wire              vsys_reconn_latch;
wire              not_vin_vsys_latch;

wire              otp_reg_hrst;
wire              rrst_noff;
wire              rrst_off;
wire              porb_scan;
wire              scl_scan;
wire              WR_scan;
wire              clk_125khz_scan;
wire              chg_fsm_on_scan;
wire              en_vdrp_scan;
wire              en_buck_scan;
wire              smb_clear_scan;
wire              otp_cmd_clr;
wire              clk_1khz;
wire              startup_done;
wire              cell_det_done;
wire              otp_loaded;
wire              clk_8khz;
wire              clk_1hz;
wire              clk_2hz;
wire              clk_4hz;
wire              clk_8hz;
wire              clk_16hz;
wire              clk_32hz;
wire              clk_64hz;
wire              clk_128hz;
wire              clk_256hz;
wire              clk_512hz;
wire              clk_2khz;
wire              clk_4khz;
wire              clk_16khz;
wire              clk_31khz;
wire              clk_62khz;
wire              clk_1hz_en;
wire              clk_2hz_en;
wire              clk_4hz_en;
wire              clk_8hz_en;
wire              clk_16hz_en;
wire              clk_32hz_en;
wire              clk_64hz_en;
wire              clk_128hz_en;
wire              clk_256hz_en;
wire              clk_512hz_en;
wire              clk_1khz_en;
wire              clk_2khz_en;
wire              clk_4khz_en;
wire              clk_8khz_en;
wire              clk_16khz_en;
wire              clk_31khz_en;
wire              clk_62khz_en;
///wire              clk_128hz_en_iv;
///wire              clk_2khz_en_iv;

wire              timeout_4ms;
///wire              fsm_timer_clk_en;
wire              smbus_access;
wire              iv_write_access;
wire              re_latch;
///wire              vinhi_int_on;
wire              prochotb_int;
wire              status_msk;          
wire              fault_msk;           
wire              mask_vbuckov;
wire              wdto_int_out;        
wire              bat_dis_int_out;     
wire              hw_rst_int_out;      
wire              iinshort_int_out;    
wire              buck_ovp_int_out;    
wire              inovp_int_out;       
wire              ipeak_int_out;       
wire              learnb_int_out;      
wire              pre_int_out;         
wire              eoc_int_out;  
wire              sysov_int_out;       

wire              readback;
wire              rst_clk_en;
wire              chg_fsm_exit;
wire              WR_noscan;
wire              WR_fuse_rrst_noff_load;
wire              WR_fuse_rrst_noff_scan;

wire              wrap_batfet_off;
wire              wrap_en_vbat_monitor;
wire              wrap_en_vin_monitor;
wire              wrap_en_vsys_monitor;
wire              wrap_en_iin_meter;
wire              wrap_en_ichg_meter;
wire              wrap_en_dischg_meter;
wire              wrap_en_buck;
wire              wrap_en_prechg;
wire              wrap_clk_125khz_en;
wire              wrap_en_rbfet;
wire              wrap_en_tsd;
wire              wrap_en_dac_volt;
wire              wrap_en_dac_current;
wire              wrap_en_fast_on;
wire              wrap_prechg_ok;
wire              wrap_vin_vsysmin;
wire              wrap_en_cell_config;
wire              wrap_en_pmo_batt_chg;   
wire              wrap_en_pmo_batt_dischg;
wire              wrap_en_faston;
wire              wrap_dvs_running;   
wire        [1:0] wrap_vbuck_range;   

wire              en_input_mgnt;
wire              en_out_dig_mgnt;
wire              f_ichg_500mA;
wire              test_state;
wire        [5:0] dig_mux;
wire              dig_testmux_out;
wire              dig_testmux_out_mux;
wire              otp_emul_zero;
wire        [2:0] tb_loop;          //new decode
reg         [3:0] tb_buck_loop;

wire        [1:0] scan_mode_sel;
wire              ctl_scan_mode;
wire              ctl_scan_enable;
wire              ctl_scan_data_out;

// Test Wrapped Outputs
wire            en_buck_twrap;
wire            en_tsd_twrap;
wire            en_refs_twrap;
wire            en_por_twrap;
wire            clk_125khz_en_twrap;
wire            batfet_off_twrap;
wire            en_vin_monitor_twrap;
wire            en_vbat_monitor_twrap;
wire            en_vsys_monitor_twrap;
wire            en_vcore_twrap;
wire            en_vreg_twrap;
wire            en_rbfet_twrap;
wire            en_prechg_twrap;
wire            en_iin_meter_twrap;
wire            en_ichg_meter_twrap;
wire            en_dischg_meter_twrap;
wire            en_dac_volt_twrap;
wire            en_dac_current_twrap;
wire            en_faston_twrap;
wire            en_cell_cfg_twrap;
wire            dvs_running_twrap;
wire     [1:0]  vbuck_range_twrap;


// SCAN Wrapped Wires - Start
// SCAN Wrapped Inputs
// SCAN Wrapped Outputs
wire            en_vin_monitor_wrap;
wire            en_vbat_monitor_wrap;
wire            en_vsys_monitor_wrap;
wire            en_iin_meter_wrap;
wire            en_dischg_meter_wrap;
wire            en_ichg_meter_wrap;
wire            en_por_wrap;
wire            en_vreg_wrap;
wire            en_vcore_wrap;
wire            en_refs_wrap;
wire            en_buck_wrap;
wire            en_tsd_wrap;
wire            en_rbfet_wrap;
wire            en_rbfet_fall_stretch_wrap;
wire            en_prechg_wrap;
wire            en_dac_volt_wrap;
wire            en_dac_current_wrap;
wire            batfet_off_wrap;
wire            clk_125khz_en_wrap;
wire            test_mode_ic_wrap;
wire            sda_out_wrap;
wire            en_faston_wrap;
wire            en_cell_cfg_wrap;
wire            dvs_running_wrap;
wire     [1:0]  vbuck_range_wrap;


///wire      [1:0] test_register1_wrap;
// SCAN Wrapped Wires - Stop

/// ****************************************************************************
/// Instances
/// ****************************************************************************

// -----------------------------------------------------------------------------
// OTP MAPPING
// -----------------------------------------------------------------------------
dig_map_otp I_dig_map_otp (
   // Inputs
   .otp                      ( otp ),           ///96 bits
   .nb_cell                  ( nb_cell ),
 //  .otp_loaded                  ( otp_loaded ),

   .otp_vchg                  ( otp_vchg ),
   .otp_ichg                  ( otp_ichg ),
   .otp_iinlim                ( otp_iinlim ),
   .otp_vsysmin               ( otp_vsysmin ),
   .otp_ipre                  ( otp_ipre ),     /// a corriger dans la prochaine spec
   .otp_freq                  ( otp_freq ),
   
   .otp_en_eocdet             ( otp_en_eocdet ),
   .otp_i2c_add               ( otp_i2c_add ),                         
   .otp_i2c_sel               ( otp_i2c_sel ),         
   .otp_prechg_regu           ( otp_prechg_regu ),      
   .otp_vinok_pol             ( otp_vinok_pol ) ,  
   .otp_vinok_sel             ( otp_vinok_sel )      
//   .otp_config_fused            ( otp_config_fused )
);

// -----------------------------------------------------------------------------
// SMBus
// -----------------------------------------------------------------------------
dig_smbus I_dig_smbus (
   .reset_b                     ( smb_clear_scan ),     ///porb_scan 
   .porb                        ( porb_scan ),     
   .SDA_in                      ( sda ),
   .scan_mode                   ( ctl_scan_mode ),
   .SCL                         ( scl_scan ),
   .IC_ADR                      ( IC_address ),          
   .read_bus                    ( read_bus ),
   .clk_125khz                  ( clk_125khz_scan ),
   .clk_512hz_en                ( clk_512hz_en ),
   .i2c_like                    ( otp_i2c_sel ),
   
   .SDA_out                     ( sda_out_wrap ),       
   .RD                          ( RD ),
   .WR                          ( WR ),
   .addr_bus                    ( addr_bus ),
   .write_bus                   ( write_bus ),
   .smbus_clk_en                ( smbus_clk_en ),
   .smb_timeout                 ( smb_timeout )   
);

// -----------------------------------------------------------------------------
// I2C wrapper
// -----------------------------------------------------------------------------
dig_i2c_wrapper I_dig_i2c_wrapper (
   .addr_bus                  ( addr_bus ),
   .i2c_sel                   ( otp_i2c_sel ),
   .i2c_address               ( otp_i2c_add ),                  
   
   .IC_address                ( IC_address ),
   .smb_addr_bus              ( smb_addr_bus )
);   

// -----------------------------------------------------------------------------
// CLOCK ENABLE
// -----------------------------------------------------------------------------
///assign clk_125khz_en_twrap = otp_clk_en | startup_clk_en | chrg_clk_en | otg | otg_en | otg_fsm_on | otg_fsm_exit | rst_clk_en | int_clk_en;
assign clk_125khz_en_twrap = otp_clk_en | startup_clk_en | chrg_clk_en | rst_clk_en | int_clk_en | detect_clk_en | vdrp_clk_en | smbus_clk_en | en_autocal;


// -----------------------------------------------------------------------------
// PORB
// -----------------------------------------------------------------------------
///assign en_por_twrap = (vinvbatt_coarse & chg_en) | vddsmbus_det | otg | otg_fsm_on | chg_fsm_on;
assign en_por_twrap = vin_det | vddsmbus_det | chg_fsm_on;         

// -----------------------------------------------------------------------------
// SCAN MUX
// -----------------------------------------------------------------------------
dig_scan_mux I_dig_scan_mux (
   .scan_mode                        ( ctl_scan_mode ),
   .scan_enable                      ( ctl_scan_enable ),
   .scan_rstb                        ( scan_rstb ),
   .scan_clk                         ( scan_clk ),
   .clk_125khz                       ( clk_125khz ),
   .WR                               ( WR ),
   .scl                              ( scl ),
   .porb                             ( porb ),
   .otp_cmd_clr                      ( otp_cmd_clr ),
   .otp_loaded                       ( otp_loaded ),
 
   .chg_fsm_on                       ( chg_fsm_on ),
   .chg_fsm_exit                     ( chg_fsm_exit ),
   .en_vdrp                          ( en_vrail_monitor ),
   .en_buck                          ( en_buck_wrap ),
   .smb_timeout                      ( smb_timeout ),
   
   .clk_125khz_scan                  ( clk_125khz_scan ),
   .WR_scan                          ( WR_scan ),
   .WR_noscan                        ( WR_noscan ),
   .WR_fuse_rrst_noff_scan           ( WR_fuse_rrst_noff_scan ),
   .WR_fuse_rrst_noff_load           ( WR_fuse_rrst_noff_load ),
   .chg_fsm_on_scan                  ( chg_fsm_on_scan ),
   .en_vdrp_scan                     ( en_vdrp_scan ),
   .en_buck_scan                     ( en_buck_scan ),
   .smb_clear_scan                   ( smb_clear_scan ),  
   .scl_scan                         ( scl_scan ),
   .porb_scan                        ( porb_scan ),
   .otp_reg_hrst                     ( otp_reg_hrst ),
   .rrst_noff                        ( rrst_noff ),
   .rrst_off                         ( rrst_off ),
   .rst_clk_en                       ( rst_clk_en )
);

// -----------------------------------------------------------------------------
// SYNC
// -----------------------------------------------------------------------------
dig_sync I_dig_sync (
   .porb                        ( porb_scan ),
   .clk_125khz                  ( clk_125khz_scan ),
   .clk_62khz                   ( clk_62khz ),
   .vin_det                     ( vin_det ), 
   .vin_ok_cell                 ( vin_ok_cell_dbnc ),  
   .vin_ov                      ( vin_ov ),
   .tsd                         ( tsd ),
   .hw_rst                      ( hw_rst ),
   .chg_dis                     ( chg_dis ),    
   .bat_dis                     ( bat_dis ),    
   .learn                       ( learn ),      
   .ldo_mode                    ( ldo_mode ),   
   .ieoc                        ( ieoc ),       
   .eoc                         ( eoc ),        
   .ieocdet_en                  ( en_eocdet ), 
   .vsys_reopen                 ( vsys_reopen ),      
///   .batfet_is_on                ( batfet_is_on ),
   .scan_mode                   ( ctl_scan_mode ),
   
   .vin_det_sync                ( vin_det_sync ),
   .vin_ok_cell_sync            ( vin_ok_cell_sync ),
   .vin_ov_sync                 ( vin_ov_cell_sync ),    ///to be reviewed
   .tsd_sync                    ( tsd_sync ),
   .hw_rst_sync                 ( hw_rst_sync ),
   .chg_dis_sync                ( chg_dis_sync ), 
   .bat_dis_sync                ( bat_dis_sync ), 
   .learn_sync                  ( learn_sync ),  
   .ldo_mode_sync               ( ldo_mode_sync ),
   .ieoc_sync                   ( ieoc_sync ),  
   .eoc_sync                    ( eoc_sync ),       
   .ieocdet_en_sync             ( ieocdet_en_sync ),
   .vsys_reopen_sync            ( vsys_reopen_sync )
///   .batfet_is_on_sync           ( batfet_is_on_sync )         
   );


// -----------------------------------------------------------------------------
// DEBOUNCE
// -----------------------------------------------------------------------------
dig_comps I_dig_comps (
   .clk_1khz                    ( clk_1khz ),        
   .clk_8khz                    ( clk_8khz ),
   .clk_125khz                  ( clk_125khz_scan ),
   .clk_128hz                   ( clk_128hz ),
   .clk_8hz                     ( clk_8hz ),
   .chg_fsm_on                  ( chg_fsm_on_scan ),
   .scan_mode                   ( ctl_scan_mode ),
   
   .vin_ok                      ( vin_ok ),         
///   .vin_ok_sync                 ( vin_ok_sync ),        
   .vin_vsys                    ( vin_vsys ),       
   .vin_vsysmin                 ( vin_vsysmin ),   
   .iin_short                   ( iin_scd & vin_monitor_blk ),             
   .vin_ov                      ( vin_ov  & vin_monitor_blk ),              
   .vsys_ov                     ( vsys_ov & vsys_monitor_blk),       
   .vsys_reopen                 ( vsys_reopen_sync ),    
   .vsys_reconn                 ( vsys_reconn ),         
   .sysok                       ( sysok ),            
   .ieoc                        ( ieoc_sync ),      
   .vpre                        ( prechg_ok ),             
   .vcell                       ( cell_config ),         
      
//cells
   .vin_ok_cell_dbnc            ( vin_ok_cell_dbnc ), 
   .vcell_dbnc                  ( cell_config_dbnc ),
//charger
   .vin_ok_dbnc                 ( vin_ok_dbnc ),        
   .vin_range_ok_dbnc           ( vin_range_ok_dbnc ),        
   .vin_vsys_dbnc               ( vin_vsys_dbnc ),     
   .vin_vsysmin_15ms_dbnc         ( vin_vsysmin_15ms_dbnc ),
   .vin_vsysmin_100ms_dbnc        ( vin_vsysmin_100ms_dbnc ),
        
   .vsys_reopen_dbnc            ( vsys_reopen_dbnc ),   
   .sysok_dbnc                  ( sysok_dbnc ),        
   .ieoc_dbnc                   ( ieoc_dbnc ),
   .vpre_dbnc                   ( prechg_ok_dbnc ),
         
   .vsys_reconn_latch           ( vsys_reconn_latch ),
   .vsys_reconn_sync            ( vsys_reconn_sync ),
   .vsys_ov_latch               ( vsys_ov_latch ),      
   .vsys_ov_sync                ( vsys_ov_sync ),
   .iin_short_latch             ( iin_scd_latch ),      
   .iin_short_sync              ( iin_scd_sync ),
   .vin_ov_latch                ( vin_ov_latch ),       
   .vin_ov_sync                 ( vin_ov_sync ),
   .not_vin_vsys_latch          ( not_vin_vsys_latch )
);

// -----------------------------------------------------------------------------
// STARTUP
// -----------------------------------------------------------------------------
dig_startup_fsm I_dig_startup_fsm (
   .porb                        ( porb_scan ),
   .clk_8khz                    ( clk_8khz ),
   .clk_rdy                     ( clk_rdy ),
   .vin_det                     ( vin_det ),
   .chg_fsm_on                  ( chg_fsm_on ),
   .otp_loaded                  ( otp_loaded ),
   .clk_125khz_en               ( clk_125khz_en_wrap ),
   .scan_mode                   ( ctl_scan_mode ),
   
   .en_refs                     ( en_refs_fsm ),    ///en_refs_twrap
   .en_vcore                    ( en_vcore_fsm ),   ///en_vcore_twrap
   .en_vreg                     ( en_vreg_fsm ),    ///en_vreg_twrap
   .startup_done                ( startup_done ),
   .startup_clk_en              ( startup_clk_en )
);

// -----------------------------------------------------------------------------
// CLOCK
// -----------------------------------------------------------------------------
dig_clk I_dig_clk (
   .clk_125khz                  ( clk_125khz_scan ),
   .vcore_rdy                   ( porb_scan ),
   .scan_en                     ( ctl_scan_enable ),
   .tb_fast_clk                 ( tb_fast_clk ),
   .clk_62khz_en                ( clk_62khz_en ),
   .clk_31khz_en                ( clk_31khz_en ),
   .clk_16khz_en                ( clk_16khz_en ),
   .clk_8khz_en                 ( clk_8khz_en ),
   .clk_4khz_en                 ( clk_4khz_en ),
   .clk_2khz_en                 ( clk_2khz_en ),
   .clk_1khz_en                 ( clk_1khz_en ),
   .clk_512hz_en                ( clk_512hz_en ),
   .clk_256hz_en                ( clk_256hz_en ),
   .clk_128hz_en                ( clk_128hz_en ),
   .clk_64hz_en                 ( clk_64hz_en ),
   .clk_32hz_en                 ( clk_32hz_en ),
   .clk_16hz_en                 ( clk_16hz_en ),
   .clk_8hz_en                  ( clk_8hz_en ),
   .clk_4hz_en                  ( clk_4hz_en ),
   .clk_2hz_en                  ( clk_2hz_en ),
   .clk_1hz_en                  ( clk_1hz_en ),
   .clk_512mhz_en               ( clk_512mhz_en ), ///new
   .clk_256mhz_en               ( clk_256mhz_en),  ///new
   .clk_62khz                   ( clk_62khz ),
   .clk_31khz                   ( clk_31khz ),
   .clk_16khz                   ( clk_16khz ),
   .clk_8khz                    ( clk_8khz ),
   .clk_4khz                    ( clk_4khz ),
   .clk_2khz                    ( clk_2khz ),
   .clk_1khz                    ( clk_1khz ),
   .clk_512hz                   ( clk_512hz ),
   .clk_256hz                   ( clk_256hz ),
   .clk_128hz                   ( clk_128hz ),
   .clk_64hz                    ( clk_64hz ),
   .clk_32hz                    ( clk_32hz ),
   .clk_16hz                    ( clk_16hz ),
   .clk_8hz                     ( clk_8hz ),
   .clk_4hz                     ( clk_4hz ),
   .clk_2hz                     ( clk_2hz ),
   .clk_1hz                     ( clk_1hz ),
   .clk_512mhz                  ( clk_512mhz ), ///new
   .clk_256mhz                  ( clk_256mhz )  ///new
);


// -----------------------------------------------------------------------------
// IINLIM
// -----------------------------------------------------------------------------
///assign clk_128hz_en_iv = (tb_fast_clk) ? 1'b1 : clk_128hz_en;
///assign clk_2khz_en_iv  = (tb_fast_clk) ? 1'b1 : clk_2khz_en;

dig_iv_prog I_dig_iv_prog (
///   .porb                        ( porb_scan ),
///   .clk_125khz                  ( clk_125khz_scan ),
   
   .ipre                        ( ipre ),                
   .iinlim                      ( iinlim ),
   .ichg                        ( ichg ),                
   .vchg                        ( vchg ),
   .vsysmin                     ( vsysmin ),             
   .full_charge                 ( fullcharge_state ),
   .pre_charge                  ( precharge_state ),
///   .en_prechg                   ( en_prechg ), ///???
   .tb_ichg_500ma               ( f_ichg_500mA ),     

   .vset_ichg                   ( prog_ichg ),           
   .vset_vchg                   ( prog_vchg ),
   .vset_iin                    ( prog_iin ),
   .vset_vsysmin                ( prog_vsysmin ),
   .vbuck_range                 ( vbuck_range_twrap )              

);

// -----------------------------------------------------------------------------
// FSM 4ms timer
// -----------------------------------------------------------------------------
dig_fsm_timer I_dig_fsm_timer (
   .porb                        ( porb_scan ),
   .clk_125khz                  ( clk_125khz_scan ),
   .fsm_timer_clk_en            ( clk_4khz_en ),
   .cell_fsm_on                 ( config_state ),  ///cell_fsm_on
   .cell_state_change           ( cell_fsm_timer_reset ),
   .timeout_4ms                 ( timeout_4ms )
);

// -----------------------------------------------------------------------------
// CHARGE FSM
// -----------------------------------------------------------------------------
dig_chrg_fsm I_dig_chrg_fsm (
   .porb                        ( porb_scan ),
   .clk_125khz                  ( clk_125khz_scan ),
   .clk_1khz_en                 ( clk_1khz_en ),
   .clk_32hz_en                 ( clk_32hz_en ),
   .clk_16hz_en                 ( clk_16hz_en ),
   .clk_8hz_en                  ( clk_8hz_en ),
   .clk_1hz_en                  ( clk_1hz_en ),
   .clk_512mhz_en               ( clk_512mhz_en ),
   .clk_256mhz_en               ( clk_256mhz_en ),
  
   .startup_done                ( startup_done ),
   .cell_det_done               ( cell_det_done ),
   .vin_det                     ( vin_det_sync ),
   .vin_ok                      ( vin_ok_dbnc),
   .sysok                       ( sysok_dbnc ),
   .vin_vsys_dbnc               ( vin_vsys_dbnc ),
   .not_vin_vsys_latch          ( not_vin_vsys_latch ),
   .vin_vsysmin                 ( vin_vsysmin_15ms_dbnc ),
   .ieoc                        ( ieoc_dbnc ),
   .vsys_reopen                 ( vsys_reopen_dbnc ),
   .vsys_reconn_latch           ( vsys_reconn_latch ),
   .vsys_reconn_sync            ( vsys_reconn_sync ),
   .vsys_ov_latch               ( vsys_ov_latch ),
   .vsys_ov_sync                ( vsys_ov_sync ),
   .iin_scd_latch               ( iin_scd_latch ),
   .iin_scd_sync                ( iin_scd_sync ),
   .vin_ov_latch                ( vin_ov_latch ),
   .vin_ov_sync                 ( vin_ov_sync ),
///   .batfet_is_on                ( batfet_is_on_sync), 
   .prechg_ok                   ( prechg_ok_dbnc ),
   
   .scan_mode                   ( ctl_scan_mode ),
   .iv_write_access             ( iv_write_access ),
   .tsd                         ( tsd_sync ),
   .sysov_int_out               ( sysov_int_out ),    ///sysov_int_out a resynchroniser
   .iinshort_int_out            ( iinshort_int_out ), ///iinshort_int_out a resynchroniser
   .chg_dis                     ( chg_dis_sync ),               
   .bat_dis                     ( bat_dis_sync ),               
   .learn                       ( learn_sync ),       
   .ldo_mode                    ( ldo_mode_sync ),    
   .hw_rst                      ( hw_rst_sync ),         
   .wd_tmr_set                  ( wd_tmr_set ),       
   .batdis_tmr_set              ( batdis_tmr_set ),   
   .hwrst_tmr_set               ( hwrst_tmr_set ),    
   .eoc                         ( eoc_sync ),         
   .en_ieocdet                  ( ieocdet_en_sync ),  
   .en_rbfet_wrap               ( en_rbfet_wrap ),
   .tb_test_state               ( test_state ),
   .test_mode_ic_wrap           ( test_mode_ic_wrap ),
  
   .chrg_state                  ( chrg_state ),             
   .clk_125khz_en               ( chrg_clk_en ),
   .en_prechg                   ( en_prechg_twrap ),
   .en_tsd                      ( en_tsd_twrap ),     /// since there is no interrupt, no actions on TSD in PMO or VDRP, TSD is not enabled
   .en_rbfet                    ( en_rbfet_twrap ),
   .en_rbfet_fall_stretch       ( en_rbfet_fall_stretch_wrap ),   
   .en_buck                     ( en_buck_twrap ),
   .en_iin_meter                ( en_iin_meter_fsm ),          
   .en_ichg_meter               ( en_ichg_meter_fsm ),          
   .en_dischg_meter             ( en_dischg_meter_fsm ),       
   .en_dac_volt                 ( en_dac_volt_twrap ),
   .en_dac_current              ( en_dac_current_twrap ),
   
   .precharge_state             ( precharge_state ),     
   .fullcharge_state            ( full_charge_state ),
   .sysrst_state                ( sysrst_state ),
   .config_state                ( config_state ),
   .eoc_state                   ( eoc_state ),
   .wd_timeout                  ( wd_timeout ),
   .batfet_off                  ( batfet_off_twrap ),
   .en_faston                   ( en_faston_twrap ),             
   .en_vin_monitor              ( en_vin_monitor_twrap ),
   .en_vsys_monitor             ( en_vsys_monitor_twrap ),
   .en_vbat_monitor             ( en_vbat_monitor_twrap ),
   .chg_fsm_on                  ( chg_fsm_on ),
   .chg_fsm_exit                ( chg_fsm_exit ),
   .vin_monitor_blk             ( vin_monitor_blk ),
   .vsys_monitor_blk            ( vsys_monitor_blk )
);

// -----------------------------------------------------------------------------
// BUCK DVS 
// -----------------------------------------------------------------------------
dig_buck_dvs I_dig_buck_dvs (
   .clk_125khz                  ( clk_125khz_scan ),              
   .dvs_clk                     ( clk_62khz ),              ///dvs step= 16us
   .en_buck                     ( en_buck_scan ),
   .pre_charge                  ( precharge_state ),
   .prechg_regu                 ( otp_prechg_regu ),
   .prog_vchg                   ( prog_vchg ),
   .prog_vsysmin                ( prog_vsysmin ),   
   .prog_iin                    ( prog_iin ),
   .prog_ichg                   ( prog_ichg ),

   .en_buck_dly                 ( en_buck_delayed_wrap ),   
   .vchrg_dvs                   ( prog_dvs_vbuck ),
   .iin_dvs                     ( prog_dvs_iin ),
   .ichrg_dvs                   ( prog_dvs_ichg ),
   .vbuck_dvs_running           ( dvs_running_twrap )
);

// -----------------------------------------------------------------------------
// CELLS DETECTION 
// -----------------------------------------------------------------------------
dig_cell_detect I_dig_cell_detect (
   .porb                        ( porb_scan ),
   .clk_125khz                  ( clk_125khz_scan ),
   .vin_ok                      ( vin_ok_cell_sync ),
   .vin_ok_fsm                  ( vin_ok_dbnc ),
   .vin_ov                      ( vin_ov_cell_sync ),
   .vcell                       ( cell_config_dbnc ),             
   .timeout_4ms                 ( timeout_4ms ),
   .cell_detect_dis             ( 1'b0 ),

   .clk_125khz_en               ( detect_clk_en ),
   .nb_cell                     ( nb_cell ),
   .en_cell_cfg                 ( en_cell_cfg_twrap ),                
   .cell_fsm_timer_reset        ( cell_fsm_timer_reset ),
   .cell_det_done               ( cell_det_done )
);   

// -----------------------------------------------------------------------------
// PMO function 
// -----------------------------------------------------------------------------
assign   en_pmo_input_wrap          =  en_pmo_in ;                
assign   en_pmo_batt_chg_twrap      =  en_pmo_bat ;               
assign   en_pmo_batt_dischg_twrap   =  en_pmo_bat;

// -----------------------------------------------------------------------------
// CORE control 
// -----------------------------------------------------------------------------
assign   en_iin_meter_twrap      = en_iin_meter_fsm    | en_pmo_in; 
assign   en_ichg_meter_twrap     = en_ichg_meter_fsm   | en_pmo_bat;
assign   en_dischg_meter_twrap   = en_dischg_meter_fsm | en_pmo_bat;


assign   en_refs_twrap           = en_refs_fsm  | en_pmo_input | en_pmo_bat | en_vrail_monitor; 
assign   en_vreg_twrap           = en_vreg_fsm  | en_pmo_input | en_pmo_bat | en_vrail_monitor; 
assign   en_vcore_twrap          = en_vcore_fsm | en_pmo_input | en_pmo_bat; 

// -----------------------------------------------------------------------------
// VDROOP function 
// -----------------------------------------------------------------------------
dig_vdroop I_dig_vdroop (
   .clk_128hz                   ( clk_128hz ),        
   .clk_1khz                    ( clk_1khz ),        
   .clk_125khz                  ( clk_125khz_scan ),
   .en_vdrp                     ( en_vdrp_scan ),  
   .vrail_drop                  ( vrail_drop ), 
   .vrail_low                   ( vrail_low ),            
   .scan_mode                   ( ctl_scan_mode ),
   
   .vdrp_clk_en                 ( vdrp_clk_en ),
   .prochotb                     ( prochotb_int )        
);
   
// -----------------------------------------------------------------------------
// OTP
// -----------------------------------------------------------------------------
dig_otp I_dig_otp (
   .clk_125khz                  ( clk_125khz_scan ),
   .vcore_rdy                   ( porb_scan ),
   
   .hard_write                  ( hard_write ),
   .soft_write                  ( soft_write ),
   .otp_sel                     ( otp_sel ),
   .re_latch                    ( re_latch ),
//   .cursen                      ( cursen ),
   
   .data_write                  ( otp_reg ),
   .otp_memvalid                ( otp_memvalid_1 ),
   
   .tb_sftrst_en                ( otp_emul_zero ),
   .tb_otp_cnt                  ( 1'b0 ),
   
   .scan_mode                   ( ctl_scan_mode ),
   
//   .otp_cursen_1                ( {otp_cursen1_1,otp_cursen0_1} ),
   .otp_lat                     ( otp_lat_1 ),
   .otp_sensez                  ( otp_sensez_1 ),
   .otp_sftrst                  ( otp_sftrst_1 ),
   .otp_sftset                  ( otp_sftset_1 ),
   .otp_reset                   ( otp_reset_1 ),
   .otp_prog                   ( otp_prog_1 ),
   .otp_clk                    ( otp_clk_1 ),
   
   .otp_loaded                  ( otp_loaded ),
   .otp_cmd_clr                 ( otp_cmd_clr ),
   .otp_clk_en                  ( otp_clk_en )
);

// -----------------------------------------------------------------------------
// INTERRUPT - FLAG
// -----------------------------------------------------------------------------
dig_int I_dig_int (
   .clk_8khz                    ( clk_8khz ),
   .porb                        ( porb_scan ),
   .scan_mode                   ( ctl_scan_mode ),
   .otp_vinok_pol               ( otp_vinok_pol ), 
   .vinok_sel                   ( vinok_sel ), 
   .vin_range_ok                ( vin_range_ok_dbnc ),
   .vin_vsysmin                 ( vin_vsysmin_100ms_dbnc ),
   .status_msk                  ( status_msk ),
   .fault_msk                   ( fault_msk ),  
   
   .iinshort_int_out            ( iinshort_int_out ),
   .buck_ovp_int_out            ( buck_ovp_int_out ),
   .inovp_int_out               ( inovp_int_out ),
   .ipeak_int_out               ( ipeak_int_out ),
   .wdto_int_out                ( wdto_int_out ),
   .learnb_int_out              ( learnb_int_out ),
   .pre_int_out                 ( pre_int_out ),
   .eoc_int_out                 ( eoc_int_out ),
   .sysov_int_out               ( sysov_int_out ),
           
   .clk_125khz_en               ( int_clk_en ),
   .flag                        ( flag_vin_ok )
);


// -----------------------------------------------------------------------------
// REGS
// -----------------------------------------------------------------------------
 dig_regs_smbus I_dig_regs_smbus (
    .addr_bus                    ( smb_addr_bus ),
    .write_bus                   ( write_bus ),
    .RD                          ( RD ),
    .WR                          ( WR_scan ),
    .WR_noscan                   ( WR_noscan ),
    .WR_fuse_rrst_noff           ( WR_fuse_rrst_noff_scan ),    
    .WR_fuse_rrst_noff_load      ( WR_fuse_rrst_noff_load),

    .test_mode                   ( test_mode_ic_wrap ),
    .test_mode_sns               ( test_mode_ic_wrap ),
    
    .otp_reg_hrst                ( otp_reg_hrst),
    .porb_scan                   ( porb_scan ),
    .porb_noscan                 ( porb ),
    .rrst_off                    ( rrst_off ),
    .rrst_noff                   ( rrst_noff ),       
    .rrst_noff_fuse              ( porb_scan ),  

    .read_en                     ( readback ),
    .otp                         ( otp ),             ///size?

    .scan_mode                   ( ctl_scan_mode ),
    .scan_clk                    ( scan_clk ),

    .state                       ( chrg_state ),      

    .wdto_int                    ( wd_timeout ),
    .bat_dis_int                 ( bat_dis_sync & sysrst_state ), 
    .hw_rst_int                  ( hw_rst_sync & sysrst_state ), 
    .iinshort_int                ( iin_scd_latch ), 
    .buck_ovp_int                ( vbuck_ov & ~mask_vbuckov), 
    .inovp_int                   ( vin_ov_latch ),          /// a masquer au demarrage ?? ( vin_ovlo_itrp & vinhi_int_on),
    .sysov_int                   ( vsys_ov_latch),          
    .ipeak_int                   ( ipeak ),                  
    .learnb_int                  ( ~learn ),                
    .pre_int                     ( precharge_state ), 
    .eoc_int                     ( eoc_state ), 

    .status_msk                  ( status_msk ),
    .fault_msk                   ( fault_msk ),

    .man_id                      ( 16'd0 ),
    .dev_id                      ( 7'b0001001 ),          /// idem smb_address ??
    
    .reg_en                      ( reg_en ),
    .smbus_access                ( smbus_access ),

    .wdto_int_out                ( wdto_int_out ),
    .bat_dis_int_out             ( bat_dis_int_out ), 
    .hw_rst_int_out              ( hw_rst_int_out ),
    .iinshort_int_out            ( iinshort_int_out ), 
    .buck_ovp_int_out            ( buck_ovp_int_out ),
    .inovp_int_out               ( inovp_int_out ),      
    .ipeak_int_out               ( ipeak_int_out ),
    .learnb_int_out              ( learnb_int_out ), 
    .pre_int_out                 ( pre_int_out ),
    .eoc_int_out                 ( eoc_int_out ),
    .sysov_int_out               ( sysov_int_out ), /// ???
    
    .bat_dis                     ( bat_dis ),      
    .chg_dis                     ( chg_dis ),      
    .eoc                         ( eoc ),      
    .en_eocdet                   ( en_eocdet ),    
    .otp_en_eocdet               ( otp_en_eocdet ),
    .ieoc                        ( prog_ieoc ),
    .hw_rst                      ( hw_rst ),         
    .ldo_mode                    ( ldo_mode ),     
    .learn                       ( learn ),  
    .hwrst_tmr_set               ( hwrst_tmr_set ), 
    .batdis_tmr_set              ( batdis_tmr_set ),
    .wd_tmr_set                  ( wd_tmr_set ),    
    .eco_mode                    ( eco_mode ),
    .drcon                       ( prog_reconn ),
    .vinok_sel                   ( vinok_sel ),
    .otp_vinok_sel               ( otp_vinok_sel ),    
    .en_dynprechg                ( en_dynprechg ),
    .en_fmod                     ( en_fmod_wrap ),
       
    .ichg                        ( ichg ),
    .otp_ichg                    ( otp_ichg ),
    .ipre                        ( ipre ), 
    .otp_ipre                    ( otp_ipre ), 
    .iinlim                      ( iinlim ),
    .otp_iinlim                  ( otp_iinlim ),
    .vchg                        ( vchg ),
    .otp_vchg                    ( otp_vchg ),
    .vsysmin                     ( vsysmin ),    ///to iv_prog  
    .otp_vsysmin                 ( otp_vsysmin ),
   
    .vlobat_reg                  ( prog_vrail_low ),              
    .en_vdrp                     ( en_vrail_monitor ),
    .en_pmo_in                   ( en_pmo_in ), 
    .en_pmo_bat                  ( en_pmo_bat ),      
    .pmo_volt_gain               ( pwr_gain_sel ),    
    .pmo_ibat_gain               ( ibat_gain_sel ),      
    .pmo_imo_sel                 ( freeze_voltmeter ),      
    
    .freq_sel                    ( prog_buck_clk ),   
    .otp_freq                    ( otp_freq ),
    
    .scan_mode_sel               ( scan_mode_sel ), 
    .test_state                  ( test_state ),
    .fast_clk                    ( fast_clk ),
    .tb_en_autocal               ( en_autocal_wrap ),
    .blk_vbuck_ov                (  ),    ///blk_vbuck_ov
    .blk_iin_scd                 (  ),
    .blk_vin_ov                  (  ),
    .blk_vsys_ov                 (  ),
    .blk_vsys_reconn             (  ),
    .fast_smbus                  ( fast_smbus ),
    .f_clk_ready                 ( ),
    .tb_force_clk_en             ( ),
    .tb_fmod_ratio               ( fmod_ratio_wrap ),     ///pas de wrapper  
    .tb_rn_fmod                  ( ),
    .tb_fmod_test                ( ),
    
    .dig_mux                     ( dig_mux ),               /// to be updated ?
 ///   .test_register1              ( test_register1_wrap ),
    .testmux_analog_buck         ( testmux_analog_buck ),
    .testmux_analog_chgref       ( testmux_analog_chgref ),
    .testmux_analog_core         ( testmux_analog_core ), 
    .testmux_analog_pmo          ( testmux_analog_pmo ),
    .testmux_analog_top          ( testmux_analog_top ),
    .tb_osc                      ( tb_osc ),
    .tb_imeter                   ( tb_imeter ),
    .tb_bg                       ( tb_bg ),
    .tb_buck                     ( tb_buck[14:0] ),
    .tb_force_switch             ( tb_buck[16:15]),       ///a wrapper tb_force_switch_wrap
    .tb_loop                     ( tb_loop ),  
        
    .en_output_wrapper           ( en_out_dig_mgnt ),    
    .wrap_clk_125khz_en          ( wrap_clk_125khz_en ),
    .wrap_en_buck                ( wrap_en_buck ),
    .wrap_batfet_off             ( wrap_batfet_off ),
    .wrap_en_tsd                 ( wrap_en_tsd ),
    .wrap_en_prechg              ( wrap_en_prechg ),
    .wrap_en_rbfet               ( wrap_en_rbfet ),
    .wrap_en_vin_monitor         ( wrap_en_vin_monitor ),
    .wrap_en_vbat_monitor        ( wrap_en_vbat_monitor ),
    .wrap_en_vsys_monitor        ( wrap_en_vsys_monitor ),
    .wrap_en_cell_cfg            ( wrap_en_cell_cfg ),
    .wrap_en_faston              ( wrap_en_faston ),
    .wrap_en_pmo_batt_chg        ( wrap_en_pmo_batt_chg ),
    .wrap_en_pmo_batt_dischg     ( wrap_en_pmo_batt_dischg ),    
    .wrap_en_iin_meter           ( wrap_en_iin_meter ),
    .wrap_en_ichg_meter          ( wrap_en_ichg_meter ),
    .wrap_en_dischg_meter        ( wrap_en_dischg_meter ),
    .wrap_dvs_running            ( wrap_dvs_running ),
    .wrap_en_dac_current         ( wrap_en_dac_current ),
    .wrap_en_dac_volt            ( wrap_en_dac_volt ),
    .wrap_vbuck_range            ( wrap_vbuck_range ),
        
    .cursen                      ( {otp_cursen1_1,otp_cursen0_1} ),
    .readback                    ( readback ),
    .re_latch                    ( re_latch ),
    .sw                          ( soft_write ),
    .hw                          ( hard_write ),    
    .otp_sel                     ( otp_sel ),
    .read_bus                    ( read_bus ),
    .otp_reg                     ( otp_reg ),
    .otp_emul_zero               ( otp_emul_zero )
    
 );
 
assign iv_write_access = WR & ( reg_en[20] | reg_en[21] | reg_en[62] | reg_en[63] );
    
// -----------------------------------------------------------------------------
// REGS
// -----------------------------------------------------------------------------
// dig_regs I_dig_regs (
//    .WR_fuse_rrst_noff_hrst           ( WR_fuse_rrst_noff_hrst_scan ),    //WR_fuse_scan
//    .WR_fuse_rrst_noff_hrst_load      ( WR_fuse_rrst_noff_hrst_load ),    //WR_fuse_load
//    .fuse_rrst_noff_hrst         ( porb_scan ),
//    
//    .vbusok                      ( ~vbusuv_dbnc & ~vbusov_dbnc ),              
//    .vinlo                       ( vinlo_latch | ~vinvbatt_acc_twrap ),
// );

// -----------------------------------------------------------------------------
// TEST MODE
// -----------------------------------------------------------------------------
/// write '18','71'in register 99 to enter testmode
/// write '18','71'in register 98 to exit testmode
/// to be validated when test mapping available

always @ (posedge WR_scan or negedge porb_scan ) begin 
    if (!porb_scan ) begin
       test_mode_tmp <= 1'b0;
       test_mode_reg <= 1'b0;
    end
    // Test mode (tmp) for entering
   else if (reg_en[99] && (write_bus == 16'h0018) && !test_mode_reg) begin 
       test_mode_tmp <= 1'b1;
    end
    // Test mode (tmp) for exiting
    else if (reg_en[98]  && (write_bus == 16'h0018) &&  test_mode_reg) begin 
       test_mode_tmp <= 1'b1;
    end
    // Test mode enter
    else if (reg_en[99] && (write_bus == 16'h0071) && test_mode_tmp && !test_mode_reg) begin 
       test_mode_reg <= 1'b1;
       test_mode_tmp <= 1'b0;
    end
  // Test mode exit
    else if (reg_en[98]  && (write_bus == 16'h0071) && test_mode_tmp && test_mode_reg) begin 
       test_mode_reg <= 1'b0;
       test_mode_tmp <= 1'b0;
    end
end

assign test_mode_ic_wrap = test_mode_reg;

// -----------------------------------------------------------------------------
// SCAN
// -----------------------------------------------------------------------------
dig_scan_ctl I_dig_scan_ctl (
   .scan_mode_sel               ( scan_mode_sel ),
   .scan_enable                 ( scan_enable ),
   .ctl_scan_data_out           ( ctl_scan_data_out ),
   .buck_scan_data_out          ( buck_scan_data_out ),
   .osc_scan_data_out           ( osc_scan_data_out ),
   .dig_testmux_out_mux         ( dig_testmux_out_mux ),
   
   .ctl_scan_mode               ( ctl_scan_mode ),
   .buck_scan_mode              ( buck_scan_mode ),
   .osc_scan_mode               ( osc_scan_mode ),
   .scan_mode_ic                ( scan_mode_ic ),
   .ctl_scan_enable             ( ctl_scan_enable ),
   .buck_scan_enable            ( buck_scan_enable ),
   .osc_scan_enable             ( osc_scan_enable ),
   .dig_testmux_out             ( dig_testmux_out )
);

// -----------------------------------------------------------------------------
// TEST_MUX
// -----------------------------------------------------------------------------
dig_test_mux I_dig_test_mux (
   .dig_mux                     ( dig_mux ),
   .flag_polarity               ( 1'b0 ),          ///no more necessary thanks to push pull
   .flag_int                    ( prochotb_int ),
   .in1_test                    ( clk_125khz ),
   .in2_test                    ( vin_det ),     
   .in3_test                    ( vin_ok ),      
   .in4_test                    ( vin_vsys ),     
   .in5_test                    ( vin_ov ),      
   .in6_test                    ( vin_vsysmin ),   
   .in7_test                    ( iin_scd ),
   .in8_test                    ( cell_config ),     
   .in9_test                    ( ieoc ),      
   .in10_test                   ( sysok ),   
   .in11_test                   ( prechg_ok ),      
   .in12_test                   ( vsys_ov ),      
   .in13_test                   ( vbuck_ov  ),
   .in14_test                   ( vsys_reconn ), 
   .in15_test                   ( vsys_reopen ), 
   .in16_test                   ( 1'b0 ),        
   .in17_test                   ( tsd ),        
   .in18_test                   ( otp_prog_1 ),
   .in19_test                   ( batfet_is_on ),  
   .in20_test                   ( vrail_drop ),      
   .in21_test                   ( vrail_low ),    
   .in22_test                   ( cal_out ),       
   .in23_test                   ( cal_ok ),        
   .in24_test                   ( 1'b0 ),        ///ilim2
   .in25_test                   ( 1'b0 ),       ///otg
   .in26_test                   ( 1'b0 ),        ///syspower  
   .in27_test                   ( 1'b0 ),        ///vsensp_ov  
   .in28_test                   ( 1'b0 ),        ///factory_pin
   .in29_test                   ( 1'b0 ),
   .in30_test                   ( 1'b0 ),
   .in31_test                   ( 1'b0 ),///a remonter peut etre en top cell pour le switcher
   .in32_test                   ( buck_dig_testout ),
   .flag                        ( dig_testmux_out_mux )  
);

//dig_testmux_out= muxing of scan data and dig test mux
assign prochotb = ( test_mode_ic_wrap ) ? dig_testmux_out : prochotb_int;

// -----------------------------------------------------------------------------
// TEST WRAPPER
// -----------------------------------------------------------------------------
dig_test_wrapper I_dig_test_wrapper (
   // Inputs
   .en_out_dig_mgnt             ( en_out_dig_mgnt ), 
   .wrap_batfet_off             ( wrap_batfet_off ),
   .wrap_en_buck                ( wrap_en_buck ),
   .wrap_en_rbfet               ( wrap_en_rbfet ),
   .wrap_en_tsd                 ( wrap_en_tsd ), 
   .wrap_en_dac_current         ( wrap_en_dac_current ),
   .wrap_en_dac_volt            ( wrap_en_dac_volt ),      
   .wrap_en_prechg              ( wrap_en_prechg ),
   .wrap_clk_125khz_en          ( wrap_clk_125khz_en ),
   .wrap_en_vin_monitor         ( wrap_en_vin_monitor ),
   .wrap_en_vbat_monitor        ( wrap_en_vbat_monitor ),
   .wrap_en_vsys_monitor        ( wrap_en_vsys_monitor ),   
   .wrap_en_iin_meter           ( wrap_en_iin_meter ),
   .wrap_en_ichg_meter          ( wrap_en_ichg_meter ),
   .wrap_en_dischg_meter        ( wrap_en_dischg_meter ),
   .wrap_en_pmo_batt_chg        ( wrap_en_pmo_batt_chg ),
   .wrap_en_pmo_batt_dischg     ( wrap_en_pmo_batt_dischg ),
   .wrap_en_faston              ( wrap_en_faston ),
   .wrap_en_cell_cfg            ( wrap_en_cell_cfg ),
   .wrap_vbuck_range            ( wrap_vbuck_range ),
   .wrap_dvs_running            ( wrap_dvs_running ),
      
   .batfet_off_twrap            ( batfet_off_twrap ),
   .en_buck_twrap               ( en_buck_twrap ),
   .en_rbfet_twrap              ( en_rbfet_twrap ),
   .en_vcore_twrap              ( en_vcore_twrap ),
   .en_vreg_twrap               ( en_vreg_twrap ),
   .en_refs_twrap               ( en_refs_twrap ),
   .en_por_twrap                ( en_por_twrap ),
   .en_tsd_twrap                ( en_tsd_twrap ),
   .en_dac_current_twrap        ( en_dac_current_twrap ),
   .en_dac_volt_twrap           ( en_dac_volt_twrap ),      
   .en_prechg_twrap             ( en_prechg_twrap ),
   .clk_125khz_en_twrap         ( clk_125khz_en_twrap ),
   .en_vbat_monitor_twrap       ( en_vbat_monitor_twrap ),
   .en_vsys_monitor_twrap       ( en_vsys_monitor_twrap ),
   .en_vin_monitor_twrap        ( en_vin_monitor_twrap ),
   .en_iin_meter_twrap          ( en_iin_meter_twrap ),
   .en_ichg_meter_twrap         ( en_ichg_meter_twrap ),
   .en_dischg_meter_twrap       ( en_dischg_meter_twrap ),   
   .en_pmo_batt_chg_twrap       ( en_pmo_batt_chg_twrap ),   
   .en_pmo_batt_dischg_twrap    ( en_pmo_batt_dischg_twrap ),
   .en_faston_twrap             ( en_faston_twrap ),
   .en_cell_cfg_twrap           ( en_cell_cfg_twrap ),
   .vbuck_range_twrap           ( vbuck_range_twrap ),
   .dvs_running_twrap           ( dvs_running_twrap ),
   // Outputs
   .batfet_off                  ( batfet_off_wrap ),
   .en_buck                     ( en_buck_wrap ),
   .en_rbfet                    ( en_rbfet_wrap ),
   .en_vcore                    ( en_vcore_wrap ),
   .en_vreg                     ( en_vreg_wrap ),
   .en_refs                     ( en_refs_wrap ),
   .en_por                      ( en_por_wrap ), 
   .en_tsd                      ( en_tsd_wrap ),
   .en_dac_current              ( en_dac_current_wrap ),
   .en_dac_volt                 ( en_dac_volt_wrap ),
   .en_prechg                   ( en_prechg_wrap ),           
   .clk_125khz_en               ( clk_125khz_en_wrap ),
   .en_vbat_monitor             ( en_vbat_monitor_wrap ),
   .en_vin_monitor              ( en_vin_monitor_wrap ),
   .en_vsys_monitor             ( en_vsys_monitor_wrap ),
   .en_iin_meter                ( en_iin_meter_wrap ),
   .en_ichg_meter               ( en_ichg_meter_wrap ),
   .en_dischg_meter             ( en_dischg_meter_wrap ),   
   .en_pmo_batt_chg             ( en_pmo_batt_chg_wrap ),
   .en_pmo_batt_dischg          ( en_pmo_batt_dischg_wrap ),
   .en_faston                   ( en_faston_wrap ),
   .en_cell_cfg                 ( en_cell_cfg_wrap ),
   .vbuck_range                 ( vbuck_range_wrap ),
   .dvs_running                 ( dvs_running_wrap )
   );

// -----------------------------------------------------------------------------
// SCAN WRAPPER
// -----------------------------------------------------------------------------
dig_scan_wrapper I_dig_scan_wrapper (
// Inputs
   .scan_mode                     ( ctl_scan_mode ),

// Outputs
   .en_vin_monitor                ( en_vin_monitor ),
   .en_vin_monitor_wrap           ( en_vin_monitor_wrap ),
   .en_vbat_monitor               ( en_vbat_monitor ),
   .en_vbat_monitor_wrap          ( en_vbat_monitor_wrap ),
   .en_vsys_monitor               ( en_vsys_monitor ),
   .en_vsys_monitor_wrap          ( en_vsys_monitor_wrap ),  
   .en_iin_meter                  ( en_iin_meter ),              
   .en_iin_meter_wrap             ( en_iin_meter_wrap ),              
   .en_ichg_meter                 ( en_ichg_meter ),
   .en_ichg_meter_wrap            ( en_ichg_meter_wrap ),
   .en_dischg_meter               ( en_dischg_meter ),    
   .en_dischg_meter_wrap          ( en_dischg_meter_wrap ),    
   .en_por                        ( en_por ),
   .en_por_wrap                   ( en_por_wrap ),
   .en_vreg                       ( en_vreg ),
   .en_vreg_wrap                  ( en_vreg_wrap ),
   .en_vcore                      ( en_vcore ),
   .en_vcore_wrap                 ( en_vcore_wrap ),
   .en_refs                       ( en_refs ),
   .en_refs_wrap                  ( en_refs_wrap ),
   .en_buck                       ( en_buck ),
   .en_buck_wrap                  ( en_buck_wrap ),
   .en_buck_delayed               ( en_buck_delayed ),
   .en_buck_delayed_wrap          ( en_buck_delayed_wrap ),   
   .en_tsd                        ( en_tsd ),
   .en_tsd_wrap                   ( en_tsd_wrap ),
   .en_rbfet                      ( en_rbfet ),
   .en_rbfet_wrap                 ( en_rbfet_wrap ),
   .en_rbfet_fall_stretch         ( en_rbfet_fall_stretch ),  
   .en_rbfet_fall_stretch_wrap    ( en_rbfet_fall_stretch_wrap ), 
   .en_prechg                     ( en_prechg ),
   .en_prechg_wrap                ( en_prechg_wrap ),   
   .en_dac_volt                   ( en_dac_volt ),
   .en_dac_volt_wrap              ( en_dac_volt_wrap ),
   .en_dac_current                ( en_dac_current ),
   .en_dac_current_wrap           ( en_dac_current_wrap ),
   .en_pmo_input                  ( en_pmo_input ),
   .en_pmo_input_wrap             ( en_pmo_input_wrap ),
   .en_pmo_batt_chg               ( en_pmo_battery_chg ),
   .en_pmo_batt_chg_wrap          ( en_pmo_batt_chg_wrap ),
   .en_pmo_batt_dischg            ( en_pmo_battery_dischg ),
   .en_pmo_batt_dischg_wrap       ( en_pmo_batt_dischg_wrap ),
   .batfet_off                    ( batfet_off ),
   .batfet_off_wrap               ( batfet_off_wrap ),
   .clk_125khz_en                 ( en_clk_125khz ),
   .clk_125khz_en_wrap            ( clk_125khz_en_wrap ),
   .en_fmod                       ( en_fmod ),
   .en_fmod_wrap                  ( en_fmod_wrap ),
   .fmod_ratio                    ( fmod_ratio ),
   .fmod_ratio_wrap               ( fmod_ratio_wrap ),
   .en_autocal                    ( en_autocal ),
   .en_autocal_wrap               ( en_autocal_wrap ),
   .en_faston                     ( en_faston ),
   .en_faston_wrap                ( en_faston_wrap ),
   .en_cell_cfg                   ( en_cell_cfg ),
   .en_cell_cfg_wrap              ( en_cell_cfg_wrap ),
   .vbuck_range                   ( vbuck_range ),
   .vbuck_range_wrap              ( vbuck_range_wrap ),
   .dvs_running                   ( dvs_running ),
   .dvs_running_wrap              ( dvs_running_wrap ),

   .test_mode_ic                  ( test_mode_ic ),
   .test_mode_ic_wrap             ( test_mode_ic_wrap ),
   .sda_out_wrap                  ( sda_out_wrap ),         
   .sda_out                       ( sda_out )   
 ///   .test_register1                ( test_register1[1:0] ),         ///hsson ... 
 ///   .test_register1_wrap           ( test_register1_wrap )
);

// -----------------------------------------------------------------------------
// TEST DECODE
// -----------------------------------------------------------------------------
dig_cgc cgc_clk_cal  ( .clkout( clk_cal  ), .clkin( clk_62khz ), .en( en_autocal ), .se( 1'b0 ) );


always @ (*) begin         ///a revoir
   case (tb_loop)
      3'b000  : tb_buck_loop = 4'b0000;
      3'b001  : tb_buck_loop = 4'b0001;      //   500mv onto vc_buck
      3'b010  : tb_buck_loop = 4'b0010;      //   vset_iin onto vc_buck
      3'b011  : tb_buck_loop = 4'b0100;      //   vset_ichg onto vc_buck
      3'b100  : tb_buck_loop = 4'b1000;      //   vset_vchg onto vc_buck
      default : tb_buck_loop = 4'b0000;
   endcase
end

assign tb_buck[20:17] = tb_buck_loop;

assign buck_dig_testmux  = ( dig_mux[5:4] == 2'b10 ) ? dig_mux[3:0] : 4'b0000;


endmodule
