set(IB_US_TIMER_CAPTURE "" CACHE PATH "Flycast capture after timer setup")
set(IB_US_FIRST_FRAME_REFERENCE "" CACHE PATH "Captura do Flycast com a entrada, o log de eventos e os quadros de referência da IB05")
set(IB_US_ROUTE_REFERENCE "" CACHE PATH "Captura do Flycast com a entrada e os quadros do roteiro da IB06")
set(IB_US_GDI "" CACHE FILEPATH "GDI local da edição USA")
set(IB_US_FLASH "" CACHE FILEPATH "Cópia local de 128 KiB da flash usada na captura de referência")
set(IB_US_BOOT_CAPTURE "" CACHE PATH "Captura local pareada do Flycast para validar a RAM")
set(IB_US_ROUTINE_CAPTURE "" CACHE PATH "Captura da rotina USA e de seu retorno")
set(IB_US_CHAIN_CAPTURE "" CACHE PATH "Captura contínua da entrada USA até o retorno da rotina")
set(IB_US_STARTUP_CAPTURE "" CACHE PATH "Captura contínua da entrada até a primeira chamada em 8C02E394")
set(IB_US_STARTUP_RETURN_CAPTURE "" CACHE PATH "Captura contínua até o retorno em 8C02E398")
if(IB_US_BOOT_IMAGE)
    include(boot_blocks.cmake)
endif()
if(TARGET ib_game_image)
    add_executable(ib_us_full_replay us_cached_boot.cpp)
    target_compile_definitions(ib_us_full_replay PRIVATE IB_FULL_IMAGE IB_HAS_STARTUP IB_HAS_STARTUP_CALL)
    target_link_libraries(ib_us_full_replay PRIVATE ib_game_image)
    function(ib_full_checkpoint label capture)
        if(${capture})
            add_test(NAME us_full_${label} COMMAND ib_us_full_replay ${ARGN})
            set_tests_properties(us_full_${label} PROPERTIES WORKING_DIRECTORY "${${capture}}" TIMEOUT 30)
        endif()
    endfunction()
    ib_full_checkpoint(boot IB_US_BOOT_CAPTURE)
    ib_full_checkpoint(routine IB_US_ROUTINE_CAPTURE routine)
    ib_full_checkpoint(chain IB_US_CHAIN_CAPTURE chained)
    ib_full_checkpoint(startup IB_US_STARTUP_CAPTURE startup)
    ib_full_checkpoint(startup_return IB_US_STARTUP_RETURN_CAPTURE startup-return)
    ib_full_checkpoint(timer_setup IB_US_TIMER_CAPTURE timer-setup)
endif()
if(TARGET ib_game_image)
    add_executable(ib_us_first_frame us_first_frame.cpp)
    target_link_libraries(ib_us_first_frame PRIVATE ib_game_image ib_legacy_boot ib_legacy_sh4_interpreter)
    if(IB_US_FIRST_FRAME_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
        add_test(NAME us_first_frame
                 COMMAND ib_us_first_frame "${IB_US_FIRST_FRAME_REFERENCE}" "${IB_US_GDI}" "${IB_US_FLASH}"
                         "${CMAKE_CURRENT_BINARY_DIR}/us_first_frame")
        set_tests_properties(us_first_frame PROPERTIES TIMEOUT 120)
    endif()
endif()
# IB07: a second, independent extraction of the disc compared sector by sector.
set(IB_US_SHADOW_GDI "" CACHE FILEPATH "Segunda extração GDI do disco USA para o comparador em sombra")
set(IB_US_SHADOW_FILES "" CACHE PATH "Pasta com files/ e disc.index do provedor nativo (IB08) para o comparador em sombra")
set(_ib_shadow)
if(IB_US_SHADOW_FILES)
    set(_ib_shadow --shadow-files "${IB_US_SHADOW_FILES}")
elseif(IB_US_SHADOW_GDI)
    set(_ib_shadow --shadow-gdi "${IB_US_SHADOW_GDI}")
endif()
if(TARGET ib_game_image)
    add_executable(ib_us_input_route us_input_route.cpp)
    target_link_libraries(ib_us_input_route PRIVATE ib_game_image ib_legacy_hybrid)
    if(TARGET ib_native_vulkan)
        target_link_libraries(ib_us_input_route PRIVATE ib_native_vulkan)
        target_compile_definitions(ib_us_input_route PRIVATE IB_HAS_VULKAN)
    endif()
    if(IB_US_ROUTE_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
        add_test(NAME us_input_route
                 COMMAND ib_us_input_route "${IB_US_ROUTE_REFERENCE}" "${PROJECT_SOURCE_DIR}/config/ib06_route_us.txt"
                         "${IB_US_GDI}" "${IB_US_FLASH}" "${CMAKE_CURRENT_BINARY_DIR}/us_input_route" ${_ib_shadow})
        set_tests_properties(us_input_route PROPERTIES TIMEOUT 600)
    endif()
endif()
if(TARGET ib_game_image)
    add_executable(ib_us_disc_entry us_disc_entry.cpp)
    target_link_libraries(ib_us_disc_entry PRIVATE ib_game_image ib_legacy_hybrid)
    if(IB_US_FIRST_FRAME_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
        add_test(NAME us_disc_entry
                 COMMAND ib_us_disc_entry "${IB_US_FIRST_FRAME_REFERENCE}" "${IB_US_GDI}" "${IB_US_FLASH}")
        set_tests_properties(us_disc_entry PROPERTIES TIMEOUT 120)
    endif()
endif()
# IB06: the Builder installs from the user's disc image (long: recompiles and compiles the game).
set(IB_US_DISC_IMAGE "" CACHE FILEPATH "Imagem local do disco USA (GDI ou CHD) para o teste do Builder")
set(IB_CHDMAN "" CACHE FILEPATH "chdman.exe para imagens CHD")
if(WIN32 AND IB_US_DISC_IMAGE AND TARGET ib-builder)
    set(_builder_args --disc "${IB_US_DISC_IMAGE}" --out "${CMAKE_CURRENT_BINARY_DIR}/builder-install")
    if(IB_CHDMAN)
        list(APPEND _builder_args --chdman "${IB_CHDMAN}")
    endif()
    add_test(NAME builder_install COMMAND ib-builder ${_builder_args})
    set_tests_properties(builder_install PROPERTIES TIMEOUT 7200 PASS_REGULAR_EXPRESSION "verify ok")
endif()
# IB08: native file provider against the raw tracks, every sector of every data track.
if(IB_US_GDI)
    add_executable(ib_us_file_disc us_file_disc.cpp)
    target_link_libraries(ib_us_file_disc PRIVATE ib_legacy_boot ib_port)
    add_test(NAME us_file_disc COMMAND ib_us_file_disc "${IB_US_GDI}" "${CMAKE_CURRENT_BINARY_DIR}/us_file_disc")
    set_tests_properties(us_file_disc PROPERTIES TIMEOUT 1800)
endif()
# IB09: repeated runs keep the device events in the same order and time.
if(TARGET ib_game_image)
    add_executable(ib_us_event_order us_event_order.cpp)
    target_link_libraries(ib_us_event_order PRIVATE ib_game_image ib_legacy_hybrid)
    if(IB_US_FIRST_FRAME_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
        add_test(NAME us_event_order COMMAND ib_us_event_order "${IB_US_FIRST_FRAME_REFERENCE}" "${IB_US_GDI}" "${IB_US_FLASH}" 1600)
        set_tests_properties(us_event_order PROPERTIES TIMEOUT 600)
    endif()
endif()
# IB11: route with a memory card from the save store.
if(TARGET ib_game_image)
    add_executable(ib_us_save_route us_save_route.cpp)
    target_link_libraries(ib_us_save_route PRIVATE ib_game_image ib_legacy_hybrid ib_native_files)
endif()
if(TARGET ib_us_save_route AND IB_US_FIRST_FRAME_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
    add_test(NAME us_card_boot COMMAND ib_us_save_route "${IB_US_FIRST_FRAME_REFERENCE}" "${PROJECT_SOURCE_DIR}/config/ib11_card_boot.txt"
             "${IB_US_GDI}" "${IB_US_FLASH}" "${CMAKE_CURRENT_BINARY_DIR}/card-save" "${CMAKE_CURRENT_BINARY_DIR}/card-boot")
    set_tests_properties(us_card_boot PROPERTIES TIMEOUT 300 PASS_REGULAR_EXPRESSION "block_reads=[1-9]")
endif()
# IB12: the Sofdec movie of the disc against an independent decoder.
set(IB_US_MOVIE_DIR "" CACHE PATH "Pasta com SFD000.SFD e as referências ref.rgba e ref.pcm (ffmpeg)")
add_executable(ib_us_movie us_movie.cpp)
target_link_libraries(ib_us_movie PRIVATE ib_native_movie)
if(IB_US_MOVIE_DIR)
    add_test(NAME us_movie COMMAND ib_us_movie "${IB_US_MOVIE_DIR}/SFD000.SFD" "${IB_US_MOVIE_DIR}/ref.rgba" "${IB_US_MOVIE_DIR}/ref.pcm")
endif()
# IB13: software and Vulkan execute the same command lists along the IB06 route.
if(TARGET ib_game_image AND TARGET ib_native_vulkan)
    add_executable(ib_us_render_backends us_render_backends.cpp)
    target_link_libraries(ib_us_render_backends PRIVATE ib_game_image ib_legacy_hybrid ib_native_vulkan)
    if(IB_US_FIRST_FRAME_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
        add_test(NAME us_render_backends COMMAND ib_us_render_backends "${IB_US_FIRST_FRAME_REFERENCE}"
                 "${PROJECT_SOURCE_DIR}/config/ib06_route_us.txt" "${IB_US_GDI}" "${IB_US_FLASH}" 10
                 "${CMAKE_CURRENT_BINARY_DIR}/render-backends")
        set_tests_properties(us_render_backends PROPERTIES TIMEOUT 900)
    endif()
endif()
# IB13: the IB06 route rendered by Vulkan against the Flycast captures.
if(TARGET ib_us_input_route AND TARGET ib_native_vulkan AND IB_US_ROUTE_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
    add_test(NAME us_input_route_vulkan
             COMMAND ib_us_input_route "${IB_US_ROUTE_REFERENCE}" "${PROJECT_SOURCE_DIR}/config/ib06_route_us.txt"
                     "${IB_US_GDI}" "${IB_US_FLASH}" "${CMAKE_CURRENT_BINARY_DIR}/us_input_route_vulkan")
    set_tests_properties(us_input_route_vulkan PROPERTIES TIMEOUT 900 ENVIRONMENT "IB_RENDER_BACKEND=vulkan"
                         PASS_REGULAR_EXPRESSION "render_backend=vulkan fallbacks=0")
endif()
# IB14: AICA output along the IB06 route: voice events, levels and the sample clock.
if(TARGET ib_game_image)
    add_executable(ib_us_audio_route us_audio_route.cpp)
    target_link_libraries(ib_us_audio_route PRIVATE ib_game_image ib_legacy_hybrid)
    if(TARGET ib_native_vulkan)
        target_link_libraries(ib_us_audio_route PRIVATE ib_native_vulkan)
        target_compile_definitions(ib_us_audio_route PRIVATE IB_HAS_VULKAN)
    endif()
    if(IB_US_FIRST_FRAME_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
        add_test(NAME us_audio_route COMMAND ib_us_audio_route "${IB_US_FIRST_FRAME_REFERENCE}"
                 "${PROJECT_SOURCE_DIR}/config/ib06_route_us.txt" "${IB_US_GDI}" "${IB_US_FLASH}"
                 "${CMAKE_CURRENT_BINARY_DIR}/audio-route")
        set_tests_properties(us_audio_route PROPERTIES TIMEOUT 900 PASS_REGULAR_EXPRESSION "audio route ok")
    endif()
endif()
# IB14 performance: the IB06 route as Illbleed.exe runs it (Vulkan, render beside the CPU,
# display reported when the render completes) against the Flycast captures.
if(TARGET ib_us_input_route AND TARGET ib_native_vulkan AND IB_US_ROUTE_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
    add_test(NAME us_input_route_game_config
             COMMAND ib_us_input_route "${IB_US_ROUTE_REFERENCE}" "${PROJECT_SOURCE_DIR}/config/ib06_route_us.txt"
                     "${IB_US_GDI}" "${IB_US_FLASH}" "${CMAKE_CURRENT_BINARY_DIR}/us_input_route_game_config")
    set_tests_properties(us_input_route_game_config PROPERTIES TIMEOUT 900
                         ENVIRONMENT "IB_RENDER_BACKEND=vulkan;IB_ASYNC_RENDER=1"
                         PASS_REGULAR_EXPRESSION "render_backend=vulkan fallbacks=0")
endif()
# IB14: internal resolution 2x (Vulkan): the reduced frame in VRAM still matches the Flycast captures.
if(TARGET ib_us_input_route AND TARGET ib_native_vulkan AND IB_US_ROUTE_REFERENCE AND IB_US_GDI AND IB_US_FLASH)
    add_test(NAME us_input_route_scale2
             COMMAND ib_us_input_route "${IB_US_ROUTE_REFERENCE}" "${PROJECT_SOURCE_DIR}/config/ib06_route_us.txt"
                     "${IB_US_GDI}" "${IB_US_FLASH}" "${CMAKE_CURRENT_BINARY_DIR}/us_input_route_scale2")
    set_tests_properties(us_input_route_scale2 PROPERTIES TIMEOUT 900
                         ENVIRONMENT "IB_RENDER_BACKEND=vulkan;IB_ASYNC_RENDER=1;IB_RENDER_SCALE=2"
                         PASS_REGULAR_EXPRESSION "render_backend=vulkan fallbacks=0")
endif()
