/* Original functions from psn00bsdk, commit 5d9aa2d3
 *
 * itob extracted from libpsn00b/include/psxcd.h
 * CdIntToPos (here renamed to drv_ps1_cd_toseekl) extracted from
 * libpsn00b/psxcd/misc.c
 *
 * Original copyright notice:
 *
 * PSn00bSDK CD-ROM library
 * (C) 2020-2023 Lameguy64, spicyjpeg - MPL licensed
 */

/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <drv/ps1/cd/routines.h>
#include <drv/ps1/bios.h>

/**
 * @brief Translates a decimal value to BCD.
 *
 * @details Translates a decimal integer in 0-99 range into a BCD format value.
 */
static unsigned itob(const unsigned i)
{
    return ((i / 10) * 16) | (i % 10);
}

struct CdAsyncSeekL drv_ps1_cd_toseekl(unsigned i)
{
	i += 150;

    return (const struct CdAsyncSeekL)
    {
        .minutes = itob(i / (75 * 60)),
        .seconds = itob((i / 75) % 60),
        .frames = itob(i % 75)
    };
}
