/*
 * Copyright (c) 2026, Chloe M.
 * Provided under the BSD-3 clause.
 *
 * Description:  i8254 timer driver
 * Author:       Chloe M.
 */

#include <machine/i8254.h>
#include <machine/pio.h>

USHORT
MdPitGetCount(VOID)
{
    UCHAR Low, High;

    MdOutb(I8254_COMMAND, 0x00);
    Low = MdInb(0x40);
    High = MdInb(0x40);
    return (High << 8) | Low;
}

VOID
MdPitSetCount(USHORT Value)
{
    MdOutb(I8254_COMMAND, 0x34);
    MdOutb(0x40, Value & 0xFF);
    MdOutb(0x40, (Value >> 8) & 0xFF);
}

VOID
MdPitSetFrequency(UQUAD Frequency)
{
    UQUAD Divisor;

    Divisor = I8254_DIVIDEND / Frequency;
    if ((I8254_DIVIDEND % Frequency) > (Frequency / 2)) {
        ++Divisor;
    }

    MdPitSetCount(Divisor);
}
