*-----------------------------------------------------------------------------
*  SPG Simulation
*-----------------------------------------------------------------------------
*  Module Description
*
*    The module simulates machine signals. Thats why the use is only
*    permitted together with the JH test device (SPG) or
*    Motor Simulation Unit (MSU) !
*
*-----------------------------------------------------------------------------

#define /s    KL_S_jog_time                            K +12
#define /s    KL_TC_mag_reference_seconds              K +5
#define /s    KL_tool_without_pocket                   K -1
#define /s    KL_emergency_stop_test_168_hours         K +10080

#define /s    ML_cam_clock                             M
#define /s    ML_MBxxx_not_connected                   M
#define /s    ML_MBxxx_without_override                M
#define /s    ML_rapid_traverse_simulation             M
#define /s    ML_magazine_guard_test_ok                M
#define /s    ML_debug_gear_range_2                    M
#define /s    ML_TC_mag_guard_close                    M

*$WS
#define /s    ML_test                                  M
#define /s    O_BCD_display                            M [8]
#define /s    I_BCD_display                            M [8]
#define /s    WL_test                                  W
#define /s    WL_Hex                                   W
#define /s    WL_Dec                                   W

#define /s /c I_inputs_X42                             I42
#define /s /c I_input_X46                              I128

#define /s    BL_cam_position                          B
#define /s    BL_TC_magazine_reference                 B
#define /s    BL_gear_swinging                         B
#define /s    BL_WZ_mag_index                          B

GLOBAL Simulation_begin
GLOBAL Simulation_end

;----------------------------
LBL Simulation_begin
;----------------------------

#if CFG_Simulation = "active"
  L      NP_M4172_1st_PLC_run_after_power_on
  O      NP_M4173_1st_PLC_run_after_compile
  IFT
    L      NP_W496_rapid_traverse_override_NC
    +      NP_W494_factor_feed_override_NC
    +      NP_W492_S_override_NC
    ==     K+0
    S      ML_MBxxx_without_override
  ENDI
#endif

#ifdef $ITNC530PROG$
  L      KL_emergency_stop_test_168_hours
  =      WG_time_next_emergency_stop_test
#endif

  L      MG_TNC_programming_station_with_PLC
  IFT
    S      I_guard_1_closed_locked
    S      I_guard_2_closed_locked

    LN     PN_D1132_control_loop_open
    A      DG_controlled_axes
    =      NP_D1100_axes_release

    LD     I_input_X46
    A      K$01FFFFFF
    ==     K+0
    =      ML_MBxxx_not_connected

    L      MG_iTNC530_PC_programming_station
    O      ML_MBxxx_not_connected
    IFT
      L      MG_marker_one
      R      I_key_NC_start
      S      I_key_NC_stop
      R      I_key_axes_plus
      R      I_key_axes_minus
    ENDI

#if CFG_Simulation = "active"
    LN     MG_iTNC530_PC_programming_station
    S      I_ESA_control_operational
    S      I_ESB_servo_drive_release
#endif

    L      MG_iTNC530_PC_programming_station
    O      ML_MBxxx_without_override
    IFT
      L      K+10000
      =      NP_W496_rapid_traverse_override_NC
      =      NP_W752_rapid_traverse_override_PLC
      =      NP_W494_factor_feed_override_NC
      =      PN_W766_factor_feed_override_PLC
      =      NP_W492_S_override_NC
      =      PN_W764_S_override_PLC
    ENDI

    L      NP_W274_code_pushed_key
    ==     KG_key_Alt_X_TNC_emergency_stop
    R      I_ESA_control_operational
    R      I_ESB_servo_drive_release

    L      MG_marker_one
    S      I_emergency_button_unlocked

    L      NP_W274_code_pushed_key
    ==     KG_key_Ctrl_S_NC_Start
    S      I_key_NC_start

    L      NP_W274_code_pushed_key
    ==     KG_key_Ctrl_X_NC_Stop
    ON     I_ESA_control_operational
    R      I_key_NC_stop

    L      NP_W274_code_pushed_key
    ==     KG_key_ctrl_Y_NC_start_stop
    S      I_key_NC_start
    R      I_key_NC_stop

    L      NP_W274_code_pushed_key
    ==     KG_key_Ctrl_cursor_up_axes_plus
    S      I_key_axes_plus

    L      NP_W274_code_pushed_key
    ==     KG_key_Ctrl_cursor_down_axes_minus
    S      I_key_axes_minus

    LN     MG_spindle_2_active
    IFT
      L      NP_W256_G_code_spindle
      ==     K+0
      =      I_S1_gear_range_1
      =N     I_S1_gear_range_2
    ELSE
      L      NP_W256_G_code_spindle
      ==     K+0
      =      I_S2_gear_range_1
      =N     I_S2_gear_range_2
    ENDI
  ENDI

#if CFG_Simulation = "active"

#if CFG_machine_variant  = "MSU_HSCI"
  L      I_key_function_3
  =      I_key_TC_magazine_guard_unlock
#endif

  LN     MG_SK_TC_mag_load_active
  IFT
    L      I_TC_magazine_guard_closed
    AN     ML_TC_mag_guard_close
    R      O_TC_magazine_guard_unlock

    L      I_TC_magazine_guard_closed
    =      ML_TC_mag_guard_close

    L      MG_key_TC_magazine_guard_unlock
    S      O_TC_magazine_guard_unlock
  ENDI

*$WS
;  LN     ML_Magazin_Tuer_Test_ok
;  S      O_WZ_Magazin_Tuer_entriegeln
;
;  L      MG_Steuer_Spannung_verzoegert_ein
;  AN     ML_Magazin_Tuer_Test_ok
;  S      ML_Magazin_Tuer_Test_ok
;  R      O_WZ_Magazin_Tuer_entriegeln

  L      BG_MP4031_number_connected_PL
  >      K+0
  IFT
    L      MG_marker_one
    S      PN_error_machine_simulation_active
  ENDI

;  LD     I_inputs_X42
;  A      K$FFFFFF00
;  >      K+0
;  S      PN_error_machine_simulation_active

  LD     I_inputs_X42
  A      K$FF700000
  >      K+0
  S      PN_error_machine_simulation_active

  L      O_lubrication_on
  =      I_lubricant_pressure_ok

  LN     I_lubricant_level_ok
  S      I_lubricant_level_ok

  L      MG_marker_one
  =      I_PW_210_temperature_ok

  L      MG_marker_one
  =      I_DA300_pressure_ok

  LN     MG_servo_drive_release_axis_groups_active
  IFT
    L      MG_marker_one
    =      I_TC_magazine_guard_closed
    =      I_TC_magazine_guard_locked_closed
  ENDI

  L      NP_M4001_S_nominal_not_in_ramp
  =      I_S1_actual_equal_nominal_rpm
  =      I_S2_actual_equal_nominal_rpm

  L      NP_M4002_S_nominal_0
  =      I_S1_actual_lower_nominal_rpm
  =      I_S2_actual_lower_nominal_rpm

  L      O_S1_tool_unclamping
  =      I_S1_tool_unclamped
  =N     I_S1_tool_clamped

  L      O_S2_tool_unclamping
  =      I_S2_tool_unclamped
  =N     I_S2_tool_clamped

  L      MG_pulse_M_function
  A      MG_M61_alternating_table_1
  S      I_key_enable_table_1

  L      MG_pulse_M_function
  A      MG_M62_alternating_table_2
  S      I_key_enable_table_2

  L      K+0
  =      BL_WZ_mag_index
  REPEAT
    L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
    O      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
    IFT
      INC    STG_TC_mag[BL_WZ_mag_index].WL_TC_mag_simulation_counter

      L      STG_TC_mag[BL_WZ_mag_index].WL_TC_mag_simulation_counter
      MOD    K+25
      <      K+12
      =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter

      L      STG_TC_mag[BL_WZ_mag_index].WL_TC_mag_simulation_counter
      MOD    K+25
      >=     K+6
      A[
        L      STG_TC_mag[BL_WZ_mag_index].WL_TC_mag_simulation_counter
        MOD    K+25
        <=     K+8
      ]
      =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
    ELSE
      L      K+6
      =      STG_TC_mag[BL_WZ_mag_index].WL_TC_mag_simulation_counter
    ENDI

    LN     STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
    AN     STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
    S      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
    S      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position

    L      MG_key_NC_start_p_pulse
    A      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
    IFT
      L      KL_TC_mag_reference_seconds
      =      BL_TC_magazine_reference
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
    O      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
    A      MG_pulse_1_s
    A      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
    IFT
      DEC    BL_TC_magazine_reference
    ENDI

    L      BL_TC_magazine_reference
    <=     K+0
    A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
    A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
    A      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
    =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_reference

    L      BG_step_toolchange
#if Tool_changing_arm = &Single_arm
    <      K+22
#else
    <      K+25
#endif
    IFT
      L      NP_M4521_TOOL_CALL_T_0_programmed
      =N     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty
#if Tool_changing_arm > &Single_arm
      L      WG_tool_in_spindle
      >      K+0
#endif
      =      I_S1_tool_located_in_spindle
      =      I_S2_tool_located_in_spindle
    ELSE
      L      BG_step_toolchange
#if Tool_changing_arm = &Single_arm
      <      K+30
#else
      <      K+32
#endif
      IFT
        L      MG_marker_one
        =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty
      ELSE

#if CFG_Tool_changing_system ! "inactive"
#if Middle_position_exchange = &active
        LN     NP_M4521_TOOL_CALL_T_0_programmed
        =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty
#else
        L      NP_M4521_TOOL_CALL_T_0_programmed
        =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty
#endif
#endif

#if Tool_changing_arm > &Single_arm
        L      WG_TC_auto_new_tool_number
        >      K+0
        =      I_S1_tool_located_in_spindle
        =      I_S2_tool_located_in_spindle
#else
        L      WG_TC_auto_tool_number
        >      K+0
        =      I_S1_tool_located_in_spindle
        =      I_S2_tool_located_in_spindle
#endif
      ENDI
    ENDI

#if Tool_changing_arm = &Pickup
    LN     NP_M4521_TOOL_CALL_T_0_programmed
    =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty

    L      BG_step_toolchange
    <      K+21
    IFT
      L      NP_M4521_TOOL_CALL_T_0_programmed
      =      I_S1_tool_located_in_spindle
      =      I_S2_tool_located_in_spindle
    ENDI

    L      BG_step_toolchange
    >      K+38
    IFT
      L      NP_W262_tool_pocket_number
      ==     KL_tool_without_pocket
      O      NP_M4521_TOOL_CALL_T_0_programmed
      =N     I_S1_tool_located_in_spindle
      =N     I_S2_tool_located_in_spindle
    ENDI

#endif

#if CFG_TC_transport_carriage = "active"
    L      BG_step_toolchange
    <      K+15
    IFT
      LN     NP_M4521_TOOL_CALL_T_0_programmed
      =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty
    ELSE
      L      MG_marker_zero
      =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_not_empty
    ENDI
#endif

    L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_pocket_put_in
    A      MG_pulse_1_s
    R      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_out
    S      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_in

    L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_pocket_put_out
    A      MG_pulse_1_s
    S      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_out
    R      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_in

    L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
    =N     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
    =      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out

    INC    BL_WZ_mag_index

    L      BL_WZ_mag_index
    >=     WG_TC_magazine_number
  UNTILT


  L      NP_M4172_1st_PLC_run_after_power_on
  O      NP_M4173_1st_PLC_run_after_compile
  S      I_TC_arm_cw
  R      I_TC_arm_ccw

  L      O_TC_arm_turn_cw
  AN     I_TC_arm_ccw
  A      MG_pulse_1_s
  S      I_TC_arm_cw

  L      O_TC_arm_turn_cw
  A      MG_pulse_1_s
  R      I_TC_arm_ccw

  L      O_TC_arm_turn_ccw
  AN     I_TC_arm_cw
  A      MG_pulse_1_s
  S      I_TC_arm_ccw

  L      O_TC_arm_turn_ccw
  A      MG_pulse_1_s
  R      I_TC_arm_cw

  L      O_TC_arm_to_middle_position
  A      MG_pulse_1_s
  S      I_TC_arm_middle_pos

  LN     O_TC_arm_put_out
  A      MG_pulse_1_s
  R      I_TC_arm_put_out
  S      I_TC_arm_put_in

  L      O_TC_arm_put_out
  A      MG_pulse_1_s
  S      I_TC_arm_put_out
  R      I_TC_arm_put_in

  L      O_TC_magazine_to_spindle_position
  A      MG_pulse_1_s
  S      I_TC_magazine_in_spindle_position
  R      I_TC_magazine_in_basic_position

  LN     O_TC_magazine_to_spindle_position
  A      MG_pulse_1_s
  R      I_TC_magazine_in_spindle_position
  S      I_TC_magazine_in_basic_position

#if CFG_TC_transport_carriage = "active"
  L      O_TC_arm_gripper_1_open
  A      MG_pulse_1_s
  S      I_TC_arm_gripper_1_opened

  LN     O_TC_arm_gripper_1_open
  A      MG_pulse_1_s
  R      I_TC_arm_gripper_1_opened

  L      O_TC_arm_gripper_2_open
  A      MG_pulse_1_s
  S      I_TC_arm_gripper_2_opened

  LN     O_TC_arm_gripper_2_open
  A      MG_pulse_1_s
  R      I_TC_arm_gripper_2_opened

  L      O_TC_arm_to_spindle_position
  =      I_TC_arm_spindle_pos
  R      I_TC_arm_basic_pos

  L      O_TC_arm_to_magazine_position
  =      I_TC_arm_magazine_pos
  R      I_TC_arm_basic_pos

  L      O_TC_arm_to_spindle_position
  O      O_TC_arm_to_magazine_position
  A      O_TC_arm_to_basic_position
  O      NP_M4172_1st_PLC_run_after_power_on
  O      NP_M4173_1st_PLC_run_after_compile
  S      I_TC_arm_basic_pos
#else
  LN     O_TC_arm_grippers_close
  A      MG_pulse_1_s
  S      I_TC_arm_grippers_opened

  L      O_TC_arm_grippers_close
  A      MG_pulse_1_s
  R      I_TC_arm_grippers_opened

  L      O_TC_arm_to_spindle_position
  =      I_TC_arm_spindle_pos

  L      O_TC_arm_to_basic_position
  =      I_TC_arm_basic_pos
#endif

  L      O_tool_lock_open
  =      I_tool_lock_open
  =N     I_tool_lock_close

  L      O_TC_pickup_put_out
  =      I_TC_pickup_put_out
  =N     I_TC_pickup_put_in


#if Tool_changing_arm = &Cam_driven_arm

*  L   I_TC_arm_braking
*  O   I_TC_arm_middle_pos
*  =   MG_TC_braking

  L      NP_M4172_1st_PLC_run_after_power_on
  O      NP_M4173_1st_PLC_run_after_compile
  S      I_TC_arm_brake_pos
  IFT
    L      K+0
    =      BL_cam_position
  ENDI

  L      MG_pulse_1_s
  XO     ML_cam_clock
  =      ML_cam_clock

  L      MG_pulse_1_s
  A      ML_cam_clock
  A      O_TC_arm_turn_cw
  IFT
    INC    BL_cam_position
  ENDI

  L      MG_pulse_1_s
  A      ML_cam_clock
  A      O_TC_arm_turn_ccw
  IFT
    DEC    BL_cam_position
  ENDI

  L      BL_cam_position
  >      K+3
  IFT
    L      K+0
    =      BL_cam_position
  ENDI

  L      BL_cam_position
  <      K+0
  IFT
    L      K+3
    =      BL_cam_position
  ENDI

  L      O_TC_arm_turn_cw
  O      O_TC_arm_turn_ccw
  A      MG_pulse_1_s
  AN     ML_cam_clock
  S      I_TC_arm_brake_pos

  L      O_TC_arm_turn_cw
  O      O_TC_arm_turn_ccw
  A      MG_pulse_1_s
  A      ML_cam_clock
  R      I_TC_arm_brake_pos

  L      BL_cam_position
  ==     K+0
  =      I_TC_arm_basic_pos

  L      BL_cam_position
  ==     K+1
  O[
    L      BL_cam_position
    ==     K+3
  ]
  =      I_TC_arm_spindle_pos

  L      BL_cam_position
  ==     K+2
  =      I_TC_arm_middle_pos
  R      I_TC_arm_brake_pos

  L      O_TC_arm_turn_cw
  O      O_TC_arm_turn_ccw
#if TC_gripper = &active
  AN     ML_cam_clock
#else
  A      ML_cam_clock
#endif
  R      I_TC_arm_basic_pos
  R      I_TC_arm_spindle_pos
  R      I_TC_arm_middle_pos

#endif

  L      NP_W256_G_code_spindle
  >      K+1
  A      PN_M4134_strobe_G_range_S_speed
  IFT
    L      K+1
    =      NP_W256_G_code_spindle
  ENDI

  LN     MG_spindle_wye_delta_switchover
  IFT
    LN     NP_M4070_strobe_G_code
    IFT
      L      KL_S_jog_time
      =      BL_gear_swinging

      LN     MG_spindle_2_active
      IFT
        L      NP_W256_G_code_spindle
        ==     K+0
        =      I_S1_gear_range_1
        =N     I_S1_gear_range_2
      ELSE
        L      NP_D364_S_nominal_rpm
        =      NP_D368_S_actual_rpm

        L      NP_W256_G_code_spindle
        ==     K+0
        =      I_S2_gear_range_1
        =N     I_S2_gear_range_2
      ENDI
    ELSE
      L      MG_marker_one
      R      I_S1_gear_range_1
      R      I_S1_gear_range_2
      R      I_S2_gear_range_1
      R      I_S2_gear_range_2

      L      BL_gear_swinging
      <      K+0
      IFT
        LN     MG_spindle_2_active
        IFT
          L      NP_W256_G_code_spindle
          ==     K+0
          S      I_S1_gear_range_1
          SN     I_S1_gear_range_2
        ELSE
          L      NP_W256_G_code_spindle
          ==     K+0
          S      I_S2_gear_range_1
          SN     I_S2_gear_range_2
        ENDI
      ENDI

      L      MG_pulse_250_ms
      IFT
        DEC    BL_gear_swinging
      ENDI
    ENDI
  ENDI

  L      ML_debug_gear_range_2
  R      I_S1_gear_range_2

  L      MG_spindle_2_active
  =      I_S2_spindle_active

  LN     MG_3D_head_setup_ok
  S      MG_3D_head_setup_ok
#endif

EM

;----------------------------
LBL Simulation_end
;----------------------------

#if CFG_Simulation = "active"

  L      ML_rapid_traverse_simulation
  O      MG_SK_rapid_traverse
  S      PN_M4561_rapid_traverse

  L      MG_marker_one
  R      O_S1_gear_range_1
  R      O_S1_gear_range_2
  R      O_S2_gear_range_1
  R      O_S2_gear_range_2

  LN     MG_power_delayed_on
  R      PN_error_tool_not_clamped

  L      MG_key_NC_start
  A      MG_key_NC_stop
  S      MG_error_inhibit

  L      MG_error_inhibit
  R      PN_error_S_actual_equal_nominal_rpm

;  L      NP_M4070_Strobe_G_Code
;  =      O_LED_G_S_Strobe
;  IFT
;    PS     NP_W256_G_Code_Spindel
;    CM     Dec_Hex_Wandlung
;    PLW
;    B=     O_BCD_Anzeige
;  ENDI
;
;  L      NP_M4071_Strobe_S_Code
;  =      O_LED_G_S_Strobe
;  IFT
;    PS     NP_W258_S_Code
;    CM     Dec_Hex_Wandlung
;    PLW
;    B=     O_BCD_Anzeige
;  ENDI
;
;  L      NP_M4072_Strobe_M_Funktion
;  =      O_LED_M_Strobe
;  IFT
;    PS     NP_W260_M_Code
;    CM     Dec_Hex_Wandlung
;    PLW
;    B=     O_BCD_Anzeige
;  ENDI
;
;  L      NP_M4073_Strobe_TOOL_CALL
;  =      O_LED_TOOL_CALL_Strobe
;  IFT
;    PS     NP_W264_Werkzeug_Nummer
;    CM     Dec_Hex_Wandlung
;    PLW
;    B=     O_BCD_Anzeige
;  ENDI
;
;  L      MG_WZW_Magazin_Start
;  =      O_LED_TOOL_DEF_Strobe
;  IFT
;*    PS     NP_W262_Werkzeug_Platz_Nummer
;    PS     WG_WZ_Magazin_Ist_Platz
;    CM     Dec_Hex_Wandlung
;    PLW
;    B=     O_BCD_Anzeige
;  ENDI

  L      ML_test
  IFT
    R      ML_test

    INC    WL_test

    PS     WL_test
    CM     Dec_Hex_conversion
    PLW
    =      WL_Hex
    B=     O_BCD_display

    LB     O_BCD_display
    PSW
    CM     Hex_Dec_transformation
    PLW
    =      WL_Dec
  ENDI

EM

LBL Hex_Dec_transformation
  PLW
  PSW
  /      K+16
  X      K+10
  +[
    PLW
    MOD    K+16
  ]
  PSW

EM

LBL Dec_Hex_conversion
  PLW
  PSW
  /      K+10
  X      K+16
  +[
    PLW
    MOD    K+10
  ]
  PSW

#endif

EM
