#define ANOMALY_IO
#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
#ifdef MEDIA699_X31J_AU2E_AU3E
int16_t G3888_ = 1;
#endif
#ifdef MEDIA670_F31E_F31J
unsigned char G0042_auc_Graphic562_Bitmap_ArrowPointer[M075_BITMAP_BYTE_COUNT(18, 18)] = {
        0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0x0F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0x0F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0xFF, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0xFF, 0x0F, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0xFF, 0xFF, 0x10, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0xFF, 0xFF, 0x0F, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0xFF, 0xFF, 0x00, 0x00, 0x10, 0x11, 0x11, 0x11,
        0xF0, 0x0F, 0xFF, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11,
        0xF0, 0x10, 0xF0, 0x0F, 0x11, 0x11, 0x11, 0x11, 0x11,
        0x00, 0x11, 0xF0, 0x0F, 0x11, 0x11, 0x11, 0x11, 0x11,
        0x10, 0x11, 0x01, 0xFF, 0x10, 0x11, 0x11, 0x11, 0x11,
        0x11, 0x11, 0x01, 0xFF, 0x10, 0x11, 0x11, 0x11, 0x11,
        0x11, 0x11, 0x11, 0x00, 0x10, 0x11, 0x11, 0x11, 0x11,
        0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
char G4079_MWORK[MosWorkSize];
int16_t G4071_i_MSE_STATE = 1;
unsigned char G4075_aauc_MSE_CURSOR[4][512];
long G4073_ai_HOT_X[4];
long G4074_ai_HOT_Y[4];
int16_t G8092_MousePointerType;
int G4155_i_CurrentMouseButtonsStatus;
int G4153_i_CurrentMouseX;
int G4154_i_CurrentMouseY;
int G4152_i_CurrentMouseButtonsStatus;
int G4156_i_CurrentMouseButtonsStatus = -1;
#endif
#ifdef MEDIA667_P31J
/* similar to seg029.C (segment #31) in CSB PC-98 */

unsigned char G0042_auc_Graphic562_Bitmap_ArrowPointer[M075_BITMAP_BYTE_COUNT(18, 18)] = {
        0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0xCC, 0xCC, 0xCC,
        0x0F, 0xF0, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0x0C, 0x0F, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x00, 0xCC, 0x0F, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0C, 0xCC, 0xC0, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xC0, 0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0x00, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
unsigned char G0043_auc_Graphic562_Bitmap_HandPointer[M075_BITMAP_BYTE_COUNT(18, 18)] = {
        0xFF, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0x0F, 0xF0, 0xF0, 0xF0, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0xC0, 0xFF, 0x0F, 0x0F, 0x0F, 0x0C, 0xCC, 0xCC, 0xCC,
        0xCC, 0x0F, 0xF0, 0xF0, 0xF0, 0xF0, 0xCC, 0xCC, 0xCC,
        0xCC, 0xC0, 0xFF, 0x0F, 0x0F, 0x0F, 0x0C, 0xCC, 0xCC,
        0xCC, 0xC0, 0xFF, 0xF0, 0xF0, 0xF0, 0xF0, 0xCC, 0xCC,
        0x00, 0xCC, 0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0xCC, 0xCC,
        0xFF, 0x0C, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC,
        0x0F, 0xF0, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC,
        0x0F, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC,
        0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC,
        0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0xCC,
        0xCC, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0xCC,
        0xCC, 0xC0, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xCC,
        0xCC, 0xCC, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xCC,
        0xCC, 0xCC, 0xCC, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0xCC,
        0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
        0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
BOOLEAN G7089_ = C0_FALSE;
#endif


#ifdef MEDIA692_X31J
int16_t F2071_(int16_t L6061_) {
        return (G3888_ <= 0) ? 0 : -1;
}
#endif

#ifdef MEDIA699_X31J_AU2E_AU3E
void F0077_MOUSE_EnableScreenUpdate_CPSE(
)
{
}

void F0078_MOUSE_DisableScreenUpdate(
)
{
}

void F2072_HidePointer(
)
{
        if (G3888_++ == 0) {
                USIO_06_HidePointer();
        }
}

void F2060_ShowPointer(
)
{
        if (G3888_-- == 1) {
                USIO_07_ShowPointer();
        }
}
#endif

#ifdef MEDIA667_P31J
void F7325_VerticalBlankRoutine(
)
{
        int16_t L4118_i_Unreferenced = 0;


        asm     push    ds
        asm     mov     ax, cs:V0713000_DSRegisterBackup
        asm     mov     ds, ax
        G7110_RandomSeed += 3;
        if (G7108_BlinkingCursorTimer) {
                G7108_BlinkingCursorTimer -= 3;
        }
        if (G7108_BlinkingCursorTimer < 0) {
                G7108_BlinkingCursorTimer = 0;
        }
        G7089_ = C1_TRUE;
        asm     pop     ds
}

int16_t F7326_MouseClickHandler(int16_t P3570_i_X, int16_t P3571_i_Y, int16_t P3572_i_Buttons)
{
        int16_t L4119_i_Unreferenced = 0;


        asm     push    ds
        asm     mov     ax, cs:V0713000_DSRegisterBackup
        asm     mov     ds, ax
        if ((P3572_i_Buttons < C32_MOUSE_EVENT_CHANGE_SCREEN_REGION) && (P3572_i_Buttons & 3)) {
                F0359_COMMAND_ProcessClick_CPSC(P3570_i_X, P3571_i_Y, P3572_i_Buttons);
        }
        asm     pop     ds
}

void F0066_MOUSE_Initialize(
)
{
        (*(G2161_IODriver->IODRV_09_RegisterMousePointerBitmap))(C0_POINTER_ARROW, G0042_auc_Graphic562_Bitmap_ArrowPointer, 0, 0);
        (*(G2161_IODriver->IODRV_09_RegisterMousePointerBitmap))(C1_POINTER_HAND, G0043_auc_Graphic562_Bitmap_HandPointer, 0, 0);
        (*(G2161_IODriver->IODRV_10_SetMousePointer))(C0_POINTER_ARROW);
        (*(G2161_IODriver->IODRV_02_SetMouseHandler))(F7326_MouseClickHandler);
        (*(G2161_IODriver->IODRV_14_SetCustomVerticalBlankRoutine))(F7325_VerticalBlankRoutine);
}

void F0783_LockMouse(
)
{
        (*(G2161_IODriver->IODRV_05_LockMouse))();
}

void F0784_UnlockMouse(
)
{
        (*(G2161_IODriver->IODRV_06_UnlockMouse))();
}

void F0078_MOUSE_DisableScreenUpdate(
)
{
        (*(G2161_IODriver->IODRV_07_ShowMousePointer))();
}

void F0077_MOUSE_EnableScreenUpdate_CPSE(
)
{
        (*(G2161_IODriver->IODRV_08_HideMousePointer))();
}

void F0704_(
)
{
        (*(G2161_IODriver->IODRV_03_))();
}

void F0705_(
)
{
        (*(G2161_IODriver->IODRV_04_))();
}

void F0785_SetMousePointerCoordinates(int16_t P3597_i_A, int16_t P3598_i_B)
{
        (*(G2161_IODriver->IODRV_12_SetMousePointerCoordinates))(P3597_i_A, P3598_i_B);
}

void F0706_GetMouseState(int16_t* P3599_pi_A, int16_t* P3600_pi_B, int16_t* P3601_pi_C)
{
        (*(G2161_IODriver->IODRV_13_GetMouseState))(P3599_pi_A, P3600_pi_B, P3601_pi_C);
}

int F7327_GetCharacterFromKeyboard(
)
{
        (*(G2162_IODriver->IODRV_25_))();
}

BOOLEAN F0539_INPUT_Cconis(
)
{
        return (*(G2162_IODriver->IODRV_01_IsKeyboardInputPresent))();
}

BOOLEAN F0712_AnyKeyboardOrMouseInput(
)
{
        int16_t L4120_i_A;
        int16_t L4121_i_B;
        int16_t L4122_i_C;


        F0706_GetMouseState(&L4120_i_A, &L4121_i_B, &L4122_i_C);
        return (L4122_i_C != 0) || (F0539_INPUT_Cconis());
}

void F0713_InitIOInterrupt(
)
{
        G2162_IODriver = G2161_IODriver = (IO_DRIVER*)getvect(C254_DM_IO_INTERRUPT);
        asm     mov     ax, ds
        asm     mov     cs:V0713000_DSRegisterBackup, ax
        return;
        V0713000_DSRegisterBackup:
        asm     V0713000_DSRegisterBackup      dw      0
}

int16_t F0714_GetReadFirstSectorResult(int16_t P3602_i_)
{
        return (*(G2162_IODriver->IODRV_18))(P3602_i_);
}

int16_t F0715_GetConvertedDeviceType(int16_t P2399_i_)
{
        return (*(G2162_IODriver->IODRV_20_))(P2399_i_);
}

int16_t F0716_CallRazAXInIBMIO(int16_t P3603_i_)
{
        (*(G2162_IODriver->IODRV_17_))(P3603_i_);
}

void F0717_GetVolumeName(int16_t P3604_i_, char* P3605_pc_)
{
        (*(G2161_IODriver->IODRV_19_GetVolumeName))(P3604_i_, P3605_pc_);
}

int16_t F0718_GetTime(
)
{
        return (*(G2162_IODriver->IODRV_21_))();
}
#endif
#ifdef MEDIA670_F31E_F31J
void F0078_MOUSE_DisableScreenUpdate(
void
)
{
        CLI;
        if (G4071_i_MSE_STATE-- == 1) {
                MOS_disp(1);
        }
        STI;
}

void F0077_MOUSE_EnableScreenUpdate_CPSE(
void
)
{
        CLI;
        if (G4071_i_MSE_STATE++ == 0) {
                MOS_disp(0);
        }
        STI;
}

_Far void F2239_TownsMouseHandler(
void
)
{
        MOS_rdpos(&G4155_i_CurrentMouseButtonsStatus, &G4153_i_CurrentMouseX, &G4154_i_CurrentMouseY);
        G4152_i_CurrentMouseButtonsStatus = ((G4155_i_CurrentMouseButtonsStatus >> 1) & 1) | ((G4155_i_CurrentMouseButtonsStatus * 2) & 2);
        if (G4156_i_CurrentMouseButtonsStatus != G4152_i_CurrentMouseButtonsStatus) {
                F0359_COMMAND_ProcessClick_CPSC((int16_t)G4153_i_CurrentMouseX, (int16_t)G4154_i_CurrentMouseY, (int16_t)G4152_i_CurrentMouseButtonsStatus);
                G4156_i_CurrentMouseButtonsStatus = G4152_i_CurrentMouseButtonsStatus;
        }
}

void F0706_GetMouseState(
int16_t* P2388_pi_ SEPARATOR
int16_t* P2389_pi_ SEPARATOR
int16_t* P2390_pi_ FINAL_SEPARATOR
{
        CLI;
        *P2388_pi_ = G4153_i_CurrentMouseX;
        *P2389_pi_ = G4154_i_CurrentMouseY;
        *P2390_pi_ = G4152_i_CurrentMouseButtonsStatus;
        STI;
}

void F0785_SetMousePointerCoordinates(
int16_t P2386_i_ SEPARATOR
int16_t P2387_i_ FINAL_SEPARATOR
{
}

void F2236_IODRV_9_SetMousePointerBitmap(
int16_t        P6096_i_PointerIndex    SEPARATOR
unsigned char* P6097_puc_PointerBitmap SEPARATOR
int16_t        P6098_i_PointerWidth    SEPARATOR
int16_t        P6099_i_HotX            SEPARATOR
int16_t        P6100_i_HotY            FINAL_SEPARATOR
{
        int16_t L6480_;
        int16_t L6479_;
        unsigned char L6478_;
        unsigned char* L6481_puc_PointerPattern;
        unsigned char* L6482_puc_PointerMask;
        int16_t L6483_;
        unsigned char L6484_;


        G4073_ai_HOT_X[P6096_i_PointerIndex] = P6099_i_HotX;
        G4074_ai_HOT_Y[P6096_i_PointerIndex] = P6100_i_HotY;
        G4075_aauc_MSE_CURSOR[P6096_i_PointerIndex][0] = 3; /* Width 24 pixels */
        G4075_aauc_MSE_CURSOR[P6096_i_PointerIndex][1] = 18; /* Height 18 pixels  */
        L6481_puc_PointerPattern = &G4075_aauc_MSE_CURSOR[P6096_i_PointerIndex][2];
        L6482_puc_PointerMask = &G4075_aauc_MSE_CURSOR[P6096_i_PointerIndex][218];
        for (L6483_ = 0; L6483_ < 18; L6483_++) {
                L6478_ = 0;
                L6479_ = 0;
                for (L6480_ = 0; L6480_ < 9; L6480_++) {
                        L6484_ = *P6097_puc_PointerBitmap++;
                        L6478_ <<= 2;
                        if ((L6484_ & 0x0F) == 1) {
                                L6478_ |= 2;
                        }
                        if ((L6484_ >> 4) == 1) {
                                L6478_ |= 1;
                        }
                        if (++L6479_ == 4) {
                                *L6482_puc_PointerMask++ = L6478_;
                                L6478_ = 0;
                                L6479_ = 0;
                        }
                        if (!(L6484_ & 0x0F)) {
                                L6484_ = (L6484_ & 0xF0) | 0x01;
                        }
                        if (!(L6484_ >> 4)) {
                                L6484_ = (L6484_ & 0x0F) | 0x10;
                        }
                        *L6481_puc_PointerPattern++ = L6484_;
                }
                P6097_puc_PointerBitmap += (P6098_i_PointerWidth - 18) >> 1;
                L6478_ = (L6478_ << 6) | 0x3F;
                *L6482_puc_PointerMask++ = L6478_;
                L6481_puc_PointerPattern += 3;
        }
}

void F2237_IODRV_10_SetMousePointer(
int16_t P6101_i_ FINAL_SEPARATOR
{
        CLI;
        F0077_MOUSE_EnableScreenUpdate_CPSE();
        MOS_type(2, G4073_ai_HOT_X[P6101_i_], G4074_ai_HOT_Y[P6101_i_], G4075_aauc_MSE_CURSOR[P6101_i_]); /* 2: Color */
        G8092_MousePointerType = P6101_i_;
        F0078_MOUSE_DisableScreenUpdate();
        STI;
}

void F0066_MOUSE_Initialize(
void
)
{
        F2236_IODRV_9_SetMousePointerBitmap(C0_POINTER_ARROW, G0042_auc_Graphic562_Bitmap_ArrowPointer, 18, 0, 0);
        F2237_IODRV_10_SetMousePointer(C0_POINTER_ARROW);
}

void F2240_INIT_TOWNS_MOUSE(
void
)
{
        MOSP_start(G4079_MWORK, sizeof(G4079_MWORK));
        MOS_resolution(0, 3);
        MOS_resolution(1, 3);
        MOS_horizon(0, 319);
        MOS_vertical(0, 199);
        MOS_setpos(251, 51);
        MOS_pulse(16, 16);
        MOS_entsub(31, (void(*)())F2239_TownsMouseHandler);
}

void F2241_RESTORE_TOWNS_MOUSE(
void
)
{
        MOS_entsub(0, (void(*)())F2239_TownsMouseHandler);
        MOS_end();
}
#endif
#endif
