/***************************************************************************
 * Copyright (C) 2026 Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * AI Disclosure: This file was largely AI-generated by Copilot (Sonnet 4.6).
 * The AI-generated portions may be considered public domain (CC0-1.0)
 * and not subject to the project's licence. The human contributor has
 * reviewed and verified that the code is correct.
 *
 * SPDX-License-Identifier: MIT and CC0-1.0
 **************************************************************************/

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "mmio_mock.h"

static uint32_t mock_mstatus = 0U;
#define csr_read(csr)             (mock_mstatus)
#define csr_write(csr, val)       do { mock_mstatus = (uint32_t)(val); } while (0)
#define csr_read_clear(csr, val)  ({ uint32_t _v = mock_mstatus; mock_mstatus &= ~(uint32_t)(val); _v; })
#define csr_read_set(csr, val)    ({ uint32_t _v = mock_mstatus; mock_mstatus |= (uint32_t)(val); _v; })

#include "bits.h"
#define PULP_SOC_PERIPHERALS_ADDR 0U
#define PULP_FC_IRQ_OFFSET        0U
#include "irq.h"
#include "../../bsp/irq.c"

static void test_irq_enable(void)
{
    memset(mock_mmio, 0, sizeof(mock_mmio));
    irq_enable(BIT(10));
    assert(mock_mmio[IRQ_REG_MASK_SET_OFFSET / 4U] == BIT(10));
    printf("test_irq_enable: PASS\n");
}

static void test_irq_disable(void)
{
    memset(mock_mmio, 0, sizeof(mock_mmio));
    irq_disable(BIT(10));
    assert(mock_mmio[IRQ_REG_MASK_CLEAR_OFFSET / 4U] == BIT(10));
    printf("test_irq_disable: PASS\n");
}

static void test_irq_clint_enable(void)
{
    mock_mstatus = 0U;
    (void)irq_clint_enable();
    assert((mock_mstatus & 0x8U) != 0U);
    printf("test_irq_clint_enable: PASS\n");
}

int main(void)
{
    test_irq_enable();
    test_irq_disable();
    test_irq_clint_enable();
    printf("All IRQ tests passed.\n");
    return 0;
}
