# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# core/engines: the SID emulation engines (DESIGN §2.4). EngineSettings, the factory that
# turns them into a libsidplayfp builder and SidConfig, and our builder for classic reSID
# 1.0, which libsidplayfp 3.x no longer has, and for Lankila's original resid-fp
# (libsidplayfp 0.1.1).

# The reSID builder is a libsidplayfp sidbuilder, so it needs libsidplayfp's private
# headers (sidemu.h, Event.h), and with them config.h's PACKAGE/VERSION macros, plus reSID's
# own headers and their likely()/unlikely() macros. Its own object library keeps all of
# that out of the rest of the module.
add_library(flipsid_engines_resid OBJECT
    resid_builder.cpp
    resid_emu.cpp
)
target_include_directories(flipsid_engines_resid PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(flipsid_engines_resid
    PUBLIC ext::sidplayfp
    PRIVATE ext::sidplayfp_internal ext::resid
)
flipsid_configure_target(flipsid_engines_resid)

# The same for the Lankila builder, with resid-fp's headers (and its RESID_INLINE, M_PI...
# macros) in place of reSID's.
add_library(flipsid_engines_lankila OBJECT
    lankila_builder.cpp
    lankila_emu.cpp
)
target_include_directories(flipsid_engines_lankila PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(flipsid_engines_lankila
    PUBLIC ext::sidplayfp
    PRIVATE ext::sidplayfp_internal ext::residfp2011
)
flipsid_configure_target(flipsid_engines_lankila)

# The write tee (DESIGN §7.3) is a sidbuilder too, over any of the others, so it needs the
# same private headers, and nothing of the engines'.
add_library(flipsid_engines_tee OBJECT
    write_tee.cpp
)
target_include_directories(flipsid_engines_tee PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(flipsid_engines_tee
    PUBLIC ext::sidplayfp
    PRIVATE ext::sidplayfp_internal
)
flipsid_configure_target(flipsid_engines_tee)

add_library(flipsid_engines STATIC
    chip_presets.cpp
    engine_factory.cpp
    filter_curves.cpp
    lankila_presets.cpp
    sid_sound.cpp
)
add_library(flipsid::engines ALIAS flipsid_engines)

target_include_directories(flipsid_engines PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
# ext::resid's and ext::residfp2011's archives have to reach the final link along with the
# object libraries' code.
target_link_libraries(flipsid_engines
    PUBLIC ext::sidplayfp
    PRIVATE flipsid_engines_resid ext::resid flipsid_engines_lankila ext::residfp2011
            flipsid_engines_tee
)

# The measured filter curves for the graphs (assets/fc-curves/README.md): about 60 KB of
# text in the binary.
include(flipsid_embed)
set(curveDir "${PROJECT_SOURCE_DIR}/assets/fc-curves")
flipsid_embed_files(flipsid_engines
    NAMESPACE flipsid::embedded
    HEADER flipsid/engines/embedded_fc_curves.h
    FILES
        alankila6581R4AR3789=${curveDir}/alankila/6581R4AR_3789.txt
        alankila6581R239841=${curveDir}/alankila/6581R2_3984_1.txt
        alankila6581R239842=${curveDir}/alankila/6581R2_3984_2.txt
        grue6581R4AR=${curveDir}/grue/6581r4ar.txt
        krullo6581=${curveDir}/KruLLo/krullo.txt
        lordNightmare6581R34285=${curveDir}/lord_nightmare/r3-4285.txt
        lordNightmare6581R34485=${curveDir}/lord_nightmare/r3-6581r3-4485-redone.txt
        lordNightmare6581R41986S=${curveDir}/lord_nightmare/r4-1986s.txt
        nata6581R2=${curveDir}/nata/r2.txt
        resid6581=${curveDir}/resid/r2.txt
        resid8580=${curveDir}/resid/r5.txt
        trurl65810784=${curveDir}/Trurl_Ext/6581_0784.txt
        trurl65813384=${curveDir}/Trurl_Ext/6581_3384.txt
        trurl6581R30486S=${curveDir}/Trurl_Ext/6581R3_0486S.txt
        trurl6581R34885=${curveDir}/Trurl_Ext/6581R3_4885.txt
        trurl6581R4AR3789=${curveDir}/Trurl_Ext/6581R4AR_3789.txt
        trurl6581R4AR4486=${curveDir}/Trurl_Ext/6581R4AR_4486_unreliable.txt
        trurl8580R51489=${curveDir}/Trurl_Ext/8580R5_1489.txt
        trurl8580R53691=${curveDir}/Trurl_Ext/8580R5_3691.txt
        zrx6581R20384=${curveDir}/ZrX-oMs/6581R2_0384.txt
        zrx6581R21984=${curveDir}/ZrX-oMs/6581R2_1984.txt
        zrx6581R23684=${curveDir}/ZrX-oMs/6581R2_3684.txt
        zrx6581R33985=${curveDir}/ZrX-oMs/6581R3_3985.txt
        zrx6581R4AR2286=${curveDir}/ZrX-oMs/6581R4AR_2286.txt
)

flipsid_configure_target(flipsid_engines)
