#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA517_F20E_F20J_X30J_P20JA_P20JB_F31E_F31J_X31J_P31J
#ifdef MEDIA551_F20E_F20J_F31E_F31J
int16_t G4118_;
#endif
#ifdef MEDIA472_P20JB_P31J
#if EXETYPE == C03_GAME
char G2216_ac_[32] = { 0x40, 0x80, 0x21, 0xFC, 0x0E, 0x88, 0x02, 0x50, 0x41, 0x60, 0x2F, 0x80, 0x00, 0xFC, 0x01, 0x40, 0xE0, 0x40, 0x2F, 0xFE, 0x20, 0x40, 0x24, 0x44, 0x24, 0x44, 0x57, 0xFC, 0x88, 0x00, 0x07, 0xFE };
#endif
#endif


#ifdef MEDIA459_P20JA_P20JB_P31J
int16_t F0949_JAPANESE_(
int16_t P2417_i_ FINAL_SEPARATOR
{
        asm     mov     ax, P2417_i_
        if (_AH >= 0xE0) {
                asm     sub     ah, 40h
        }
        asm     sub     ah, 80h
        asm     add     ah, ah
        if (_AL > 0x9E) {
                asm     sub     al, 7Eh
        } else {
                asm     dec     ah
                asm     cmp     al, 7Fh
                asm     cmc
                asm     sbb     al, 1Fh
        }
        asm     add     ah, 20h
}

void F0950_JAPANESE_(
int16_t P2418_i_  SEPARATOR
char*   P2419_pc_ FINAL_SEPARATOR
{
        asm     push    di
        asm     cld
        asm     mov     al, 0Bh
        asm     out     68h, al
        asm     mov     ax, P2418_i_
        asm     xchg    ah, al
        asm     sub     al, 20h
        asm     out     0A3h, al
        asm     mov     al, ah
        asm     out     0A1h, al
        asm     les     di, P2419_pc_
        asm     mov     ah, 0
        asm     mov     cx, 10h
        loc_3555F:
        asm     mov     al, ah
        asm     or      al, 20h
        asm     pushf
        asm     cli
        asm     out     0A5h, al
        asm     in      al, 0A9h
        asm     popf
        asm     stosb
        asm     mov     al, ah
        asm     pushf
        asm     cli
        asm     out     0A5h, al
        asm     in      al, 0A9h
        asm     popf
        asm     stosb
        asm     inc     ah
        asm     loop    loc_3555F
        asm     mov     al, 0Ah
        asm     out     68h, al
        asm     pop     di
}

void F0951_JAPANESE_GetCharacterPattern(
int16_t P2420_i_CharacterIndex    SEPARATOR
char*   P2421_pc_CharacterPattern FINAL_SEPARATOR /* 16 bytes buffer */
{
        asm     push    ds
        asm     cld
        asm     mov     cx, 10h         /* Each pattern contains 16 lines of 8 bits */
        asm     les     di, P2421_pc_CharacterPattern
        asm     mov     si, P2420_i_CharacterIndex
        asm     shl     si, 1
        asm     shl     si, 1
        asm     shl     si, 1
        asm     shl     si, 1           /* Each character pattern is 16 bytes large */
        asm     mov     ax, 0A100h      /* Address of ANK character patterns */
        asm     mov     ds, ax
        asm     rep movsb               /* Copy pattern from DS:SI to ES:DI */
        asm     pop     ds
}
#endif

#ifdef MEDIA458_P20JA_P20JB
void F1001_JAPANESE_LoadANKCharacterPatterns(
void
)
{
#ifdef MEDIA457_P20JA
        unsigned int16_t L2951_ui_;


        for (L2951_ui_ = 0; L2951_ui_ < 256; L2951_ui_++) {
                F0693_WaitVerticalBlank();
                asm     pushf
                asm     cli
                asm     cld
                asm     mov     ax, L2951_ui_
                asm     out     0A3h, al
                asm     mov     al, 0
                asm     out     0A1h, al
                asm     mov     ax, 0A100h
                asm     mov     es, ax
                asm     mov     di, L2951_ui_
                asm     shl     di, 1
                asm     shl     di, 1
                asm     shl     di, 1
                asm     shl     di, 1
                asm     mov     ah, 0
                asm     mov     cx, 10h
                loc_34156:
                asm     mov     al, ah
                asm     out     0A5h, al
                asm     in      al, 0A9h
                asm     stosb
                asm     inc     ah
                asm     loop    loc_34156
                asm     mov     al, 0Ah
                asm     popf
        }
#endif
#ifdef MEDIA469_P20JB /* Similar to F8422_LoadANKCharacterPatterns in NEC816 */
                asm     jmp     loc_34585
                asm     word_3457F      dw 0
                asm     dword_34581     dd 0
                loc_34585:
                asm     mov     ax, 350Ah
                asm     int     21h             /* DOS - 2+ - GET INTERRUPT VECTOR */
                                                /* AL = interrupt number */
                                                /* Return: ES:BX = value of interrupt vector */
                asm     mov     word ptr cs:dword_34581, bx
                asm     mov     word ptr cs:dword_34581+2, es
                asm     push    ds
                asm     mov     dx, seg InterruptVector
                asm     mov     ds, dx
                asm     mov     dx, offset InterruptVector
                asm     mov     ax, 250Ah
                asm     int     21h             /* DOS - SET INTERRUPT VECTOR */
                                                /* AL = interrupt number */
                                                /* DS:DX = new vector to be used for specified interrupt */
                asm     pop     ds
                asm     jmp     loc_345EB
                asm     InterruptVector:
                asm     cmp     cs:word_3457F, 100h
                asm     jz      loc_345E6
                asm     push    ax
                asm     push    es
                asm     push    di
                asm     push    cx
                asm     cld
                asm     mov     ax, cs:word_3457F
                asm     mov     di, ax
                asm     out     0A3h, al
                asm     mov     al, 0
                asm     out     0A1h, al
                asm     mov     ax, 0A100h
                asm     mov     es, ax
                asm     shl     di, 1
                asm     shl     di, 1
                asm     shl     di, 1
                asm     shl     di, 1
                asm     mov     ah, 0
                asm     mov     cx, 10h
                loc_345D2:
                asm     mov     al, ah
                asm     out     0A5h, al
                asm     in      al, 0A9h
                asm     stosb
                asm     inc     ah
                asm     loop    loc_345D2
                asm     pop     cx
                asm     pop     di
                asm     pop     es
                asm     pop     ax
                asm     inc     cs:word_3457F
                loc_345E6:
                asm     jmp     cs:dword_34581
                loc_345EB:
                asm     cmp     cs:word_3457F, 100h
                asm     jnz     loc_345EB
                asm     push    ds
                asm     mov     dx, word ptr cs:dword_34581+2
                asm     mov     ds, dx
                asm     mov     dx, word ptr cs:dword_34581
                asm     mov     ax, 250Ah
                asm     int     21h             /* DOS - SET INTERRUPT VECTOR */
                                                /* AL = interrupt number */
                                                /* DS:DX = new vector to be used for specified interrupt */
                asm     pop     ds
#endif
}
#endif

int16_t F0952_JAPANESE_Print(
unsigned char* P2422_puc_String        SEPARATOR
int16_t        P2423_i_ByteIndex       SEPARATOR
#ifdef MEDIA493_F20E_F20J_P20JA_P20JB_F31E_F31J_P31J
char*          P2424_pc_Bitmap         SEPARATOR
#endif
#ifdef MEDIA607_X30J_X31J
register char* P2424_pc_Bitmap         SEPARATOR
#endif
int16_t*       P2425_pi_Width          SEPARATOR
int16_t*       P2426_pi_Height         SEPARATOR
int16_t        P2427_i_Textcolor       SEPARATOR
int16_t        P2428_i_BackgroundColor FINAL_SEPARATOR
{
#ifdef MEDIA551_F20E_F20J_F31E_F31J
        int16_t L2962_i_BytesPerCharacter;
        unsigned char L2961_uc_;


        L2961_uc_ = P2422_puc_String[P2423_i_ByteIndex];
#ifdef MEDIA488_F20E_F20J
        if ((L2961_uc_ == 27)) { /* 0x1B */
#endif
#ifdef MEDIA670_F31E_F31J
        if ((L2961_uc_ == 27) || (L2961_uc_ == 124)) { /* 0x1B  0x7C */
#endif
                *P2425_pi_Width = *P2426_pi_Height = 0;
                return P2423_i_ByteIndex + 1;
        }
        if (((L2961_uc_ >= 129) && (L2961_uc_ <= 159)) || ((L2961_uc_ >= 224) && (L2961_uc_ <= 252))) {
                L2962_i_BytesPerCharacter = 2;
                *P2425_pi_Width = 16;
        } else {
                L2962_i_BytesPerCharacter = 1;
                *P2425_pi_Width = 8;
        }
        *P2426_pi_Height = 16;
        if (!G4118_) {
                EGB_textZoom(G4105_WORK, 1, 16, 16);
                EGB_textZoom(G4105_WORK, 0, 8, 16);
                G4118_ = C1_TRUE;
        }
        if (P2424_pc_Bitmap == NULL) {
                return P2423_i_ByteIndex + L2962_i_BytesPerCharacter;
        }
        EGB_resolutionRam(G4105_WORK, 128, 4, ((int)((M100_PIXEL_WIDTH(P2424_pc_Bitmap) + 31) & 0xFFFFFFE0) >> 1) * 2, M101_PIXEL_HEIGHT(P2424_pc_Bitmap), P2424_pc_Bitmap);
        EGB_writePage(G4105_WORK, 128);
        EGB_writeMode(G4105_WORK, 9);
        EGB_color(G4105_WORK, 0, P2427_i_Textcolor);
        EGB_color(G4105_WORK, 1, P2428_i_BackgroundColor);
        EGB_paintMode(G4105_WORK, 0x222);
        WORD(G4106_EGBPARA + 0) = 0;
        WORD(G4106_EGBPARA + 2) = 0;
        WORD(G4106_EGBPARA + 4) = 15;
        WORD(G4106_EGBPARA + 6) = 15;
        EGB_viewport(G4105_WORK, G4106_EGBPARA);
        WORD(G4106_EGBPARA + 0) = 0;
        WORD(G4106_EGBPARA + 2) = 15;
        WORD(G4106_EGBPARA + 4) = L2962_i_BytesPerCharacter;
        BYTE(G4106_EGBPARA + 6) = P2422_puc_String[P2423_i_ByteIndex];
        BYTE(G4106_EGBPARA + 7) = P2422_puc_String[P2423_i_ByteIndex + 1];
        EGB_sjisString(G4105_WORK, G4106_EGBPARA);
        return P2423_i_ByteIndex + L2962_i_BytesPerCharacter;
#endif
#ifdef MEDIA459_P20JA_P20JB_P31J
        int16_t L2952_i_BytesPerCharacter;
        int16_t L2953_i_;
        int16_t L2954_i_;
        int16_t L2955_i_;
        int16_t L2956_i_;
        unsigned char L2957_auc_[36];
        char* L2958_pc_;
        register int16_t L2959_i_;
        register int16_t L2960_i_;


        L2958_pc_ = &L2957_auc_[2];
        L2957_auc_[1] = P2422_puc_String[P2423_i_ByteIndex];
        if ((L2957_auc_[1] == 0x1B) || (L2957_auc_[1] == 0x7C)) {
                *P2425_pi_Width = *P2426_pi_Height = 0;
                return P2423_i_ByteIndex + 1;
        }
        if (((L2957_auc_[1] >= 129) && (L2957_auc_[1] <= 159)) || ((L2957_auc_[1] >= 224) && (L2957_auc_[1] <= 252))) {
                L2952_i_BytesPerCharacter = 2;
                *P2425_pi_Width = 16;
        } else {
                L2952_i_BytesPerCharacter = 1;
                *P2425_pi_Width = 8;
        }
        *P2426_pi_Height = 16;
        if (P2424_pc_Bitmap) {
                if (L2952_i_BytesPerCharacter == 2) {
                        L2960_i_ = F0949_JAPANESE_((P2422_puc_String[P2423_i_ByteIndex] << 8) | (P2422_puc_String[P2423_i_ByteIndex + 1]));
#ifdef MEDIA469_P20JB
                        if (L2960_i_ == 0x7423) {
                                for (L2953_i_ = 0; L2953_i_ < 32; L2953_i_++) {
                                        L2957_auc_[L2953_i_ + 2] = G2216_ac_[L2953_i_];
                                }
                        } else {
#endif
#ifdef MEDIA667_P31J
#if EXETYPE == C03_GAME
                        if (L2960_i_ == 0x7423) {
                                for (L2953_i_ = 0; L2953_i_ < 32; L2953_i_++) {
                                        L2957_auc_[L2953_i_ + 2] = G2216_ac_[L2953_i_];
                                }
                        } else {
#endif
#endif
                                F0950_JAPANESE_(L2960_i_, &L2957_auc_[2]);
#ifdef MEDIA469_P20JB
                        }
#endif
#ifdef MEDIA667_P31J
#if EXETYPE == C03_GAME
                        }
#endif
#endif
                } else {
                        L2960_i_ = P2422_puc_String[P2423_i_ByteIndex];
                        F0951_JAPANESE_GetCharacterPattern(L2960_i_, &L2957_auc_[2]);
                }
#ifdef MEDIA458_P20JA_P20JB
                L2956_i_ = M100_PIXEL_WIDTH(P2424_pc_Bitmap) >> 1;
#endif
#ifdef MEDIA667_P31J
#if EXETYPE == C03_GAME
                L2956_i_ = M100_PIXEL_WIDTH(P2424_pc_Bitmap) >> 1;
#endif
#if EXETYPE == C06_CEDT
                L2956_i_ = 32;
#endif
#endif
                if (L2952_i_BytesPerCharacter == 2) {
                        L2955_i_ = 0;
                        for (L2953_i_ = 0; L2953_i_ < 32; L2953_i_ += 2) {
                                L2959_i_ = L2955_i_;
                                L2955_i_ += L2956_i_;
                                L2960_i_ = (L2957_auc_[L2953_i_ + 2] << 8) | L2957_auc_[L2953_i_ + 3];
                                for (L2954_i_ = 0; L2954_i_ < 8; L2954_i_++) {
                                        if (L2960_i_ & 0x8000) {
                                                P2424_pc_Bitmap[L2959_i_] = P2427_i_Textcolor << 4;
                                        } else {
                                                P2424_pc_Bitmap[L2959_i_] = P2428_i_BackgroundColor << 4;
                                        }
                                        if (L2960_i_ & 0x4000) {
                                                P2424_pc_Bitmap[L2959_i_++] |= P2427_i_Textcolor;
                                        } else {
                                                P2424_pc_Bitmap[L2959_i_++] |= P2428_i_BackgroundColor;
                                        }
                                        L2960_i_ = L2960_i_ << 2;
                                }
                        }
                } else {
                        L2955_i_ = 0;
                        for (L2953_i_ = 0; L2953_i_ < 16; L2953_i_++) {
                                L2959_i_ = L2955_i_;
                                L2955_i_ += L2956_i_;
                                L2957_auc_[1] = L2957_auc_[2 + L2953_i_];
                                for (L2954_i_ = 0; L2954_i_ < 4; L2954_i_++) {
                                        if (L2957_auc_[1] & 0x80) {
                                                P2424_pc_Bitmap[L2959_i_] = P2427_i_Textcolor << 4;
                                        } else {
                                                P2424_pc_Bitmap[L2959_i_] = P2428_i_BackgroundColor << 4;
                                        }
                                        if (L2957_auc_[1] & 0x40) {
                                                P2424_pc_Bitmap[L2959_i_++] |= P2427_i_Textcolor;
                                        } else {
                                                P2424_pc_Bitmap[L2959_i_++] |= P2428_i_BackgroundColor;
                                        }
                                        L2957_auc_[1] = L2957_auc_[1] << 2;
                                }
                        }
                }
        }
        return P2423_i_ByteIndex + L2952_i_BytesPerCharacter;
#endif
#ifdef MEDIA607_X30J_X31J
        register unsigned char L2961_uc_;
        register int16_t L2962_i_BytesPerCharacter;
        register unsigned char L2963_uc_;
        register unsigned char* L2964_puc_;
        int16_t L2965_i_;
        long L2966_l_SupervisorStack;


        L2961_uc_ = P2422_puc_String[P2423_i_ByteIndex];
        if ((L2961_uc_ == 27) || (L2961_uc_ == 124)) { /* 0x1B  0x7C */
                *P2425_pi_Width = *P2426_pi_Height = 0;
                return P2423_i_ByteIndex + 1;
        }
        if (((L2961_uc_ >= 129) && (L2961_uc_ <= 159)) || ((L2961_uc_ >= 224) && (L2961_uc_ <= 252))) {
                L2962_i_BytesPerCharacter = 2;
                *P2425_pi_Width = 16;
        } else {
                L2962_i_BytesPerCharacter = 1;
                *P2425_pi_Width = 8;
        }
        *P2426_pi_Height = 16;
        if (P2424_pc_Bitmap == NULL) {
                return P2423_i_ByteIndex + L2962_i_BytesPerCharacter;
        }
        if (L2962_i_BytesPerCharacter == 1) {
                L2964_puc_ = (unsigned char*)0xF3A800 + ((long)L2961_uc_ << 4);
        } else {
                L2963_uc_ = P2422_puc_String[P2423_i_ByteIndex + 1];
                asm {
                        moveq   #0x16,d0        /* FNTADR */
                        move.b  d7,d1
                        lsl.w   #8,d1
                        move.b  d5,d1           /* Kanji code (Shift-JIS / JIS) */
                        moveq   #8,d2           /* Size (6: 6x12,12x12, 8: 8X16,16x16, 12: 12X24,24x24) */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        movea.l d0,a3
                }
        }
        L2965_i_ = M724_BITMAP_BYTE_WIDTH(M100_PIXEL_WIDTH(P2424_pc_Bitmap)) - M724_BITMAP_BYTE_WIDTH(*P2425_pi_Width);
#ifdef MEDIA577_X30J
        L2966_l_SupervisorStack = F1027_Supervisor(0L);
#endif
#ifdef MEDIA692_X31J
        asm {
                clr.l   -(sp)
                dc.w    0xFF20  /* DOS CALL SUPER */
                addq.l  #4,sp
                move.l  d0,L2966_l_SupervisorStack(a6)
        }
#endif
        asm {
                        movem.l d3-d5,-(sp)
                        move.w  P2428_i_BackgroundColor(a6),d0
                        move.w  P2427_i_Textcolor(a6),d1
                        moveq   #0x10,d2
                @Tloc_1EC34:
                        move.w  d6,d5
                @Tloc_1EC36:
                        move.b  (a3)+,d3
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC40
                        move.w  d1,d4
                        bra.s   @Tloc_1EC42
                @Tloc_1EC40:
                        move.w  d0,d4
                @Tloc_1EC42:
                        lsl.w   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC4C
                        or.w    d1,d4
                        bra.s   @Tloc_1EC4E
                @Tloc_1EC4C:
                        or.w    d0,d4
                @Tloc_1EC4E:
                        lsl.w   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC58
                        or.w    d1,d4
                        bra.s   @Tloc_1EC5A
                @Tloc_1EC58:
                        or.w    d0,d4
                @Tloc_1EC5A:
                        lsl.w   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC64
                        or.w    d1,d4
                        bra.s   @Tloc_1EC66
                @Tloc_1EC64:
                        or.w    d0,d4
                @Tloc_1EC66:
                        lsl.l   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC70
                        or.w    d1,d4
                        bra.s   @Tloc_1EC72
                @Tloc_1EC70:
                        or.w    d0,d4
                @Tloc_1EC72:
                        lsl.l   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC7C
                        or.w    d1,d4
                        bra.s   @Tloc_1EC7E
                @Tloc_1EC7C:
                        or.w    d0,d4
                @Tloc_1EC7E:
                        lsl.l   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC88
                        or.w    d1,d4
                        bra.s   @Tloc_1EC8A
                @Tloc_1EC88:
                        or.w    d0,d4
                @Tloc_1EC8A:
                        lsl.l   #4,d4
                        lsl.b   #1,d3
                        bcc.s   @Tloc_1EC94
                        or.w    d1,d4
                        bra.s   @Tloc_1EC96
                @Tloc_1EC94:
                        or.w    d0,d4
                @Tloc_1EC96:
                        move.l  d4,(a4)+
                        subq.w  #1,d5
                        bne.s   @Tloc_1EC36
                        adda.w  L2965_i_(a6),a4
                        subq.w  #1,d2
                        bne.s   @Tloc_1EC34
                        movem.l (sp)+,d3-d5
        }
#ifdef MEDIA577_X30J
        F1027_Supervisor(L2966_l_SupervisorStack);
#endif
#ifdef MEDIA692_X31J
        asm {
                move.l  L2966_l_SupervisorStack(a6),-(sp)
                dc.w    0xFF20  /* DOS CALL SUPER */
                addq.l  #4,sp
        }
#endif
        return P2423_i_ByteIndex + L2962_i_BytesPerCharacter;
#endif
}
#endif
