/*
 * 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.h>
#include <drv/ps1/types.h>
#include <drv/ps1/cd.h>
#include <drv/ps1/mc.h>
#include <drv/ps1/pad.h>
#include <drv/ps1/sio.h>
#include <stdlib.h>

struct drv_port *drv_ps1_init(const struct drv_event *const ev)
{
    struct drv_port *ret = NULL;
    struct drv_ps1_mc *mc = NULL;
    struct drv_ps1_cd *cd = NULL;
    struct drv_ps1_pad *pad = NULL;
    struct drv_ps1_sio *sio = NULL;

    if (!(mc = drv_ps1_mc_init(ev))
        || !(cd = drv_ps1_cd_init(ev))
        || !(pad = drv_ps1_pad_init(ev))
        || !(sio = drv_ps1_sio_init(ev))
        || !(ret = malloc(sizeof *ret)))
        goto failure;

    *ret = (const struct drv_port)
    {
        .mc = mc,
        .cd = cd,
        .pad = pad,
        .sio = sio
    };

    return ret;

failure:
    drv_ps1_mc_free(mc);
    drv_ps1_cd_free(cd);
    drv_ps1_pad_free(pad);
    drv_ps1_sio_free(sio);
    free(mc);
    return NULL;
}
