#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA648_A31E_A35E_X31J
extern FTL_LIBRARY* PrimBase;

#ifdef MEDIA645_A31E_A35E
long _stack = 8192;
long _priority = 0;
long _BackGroundIO = 0;
char* _procname = "BJELoad";
#endif


long* F0917_PRIM_15_Memory_Allocate(P3262_l_)
long P3262_l_;
{
#ifdef MEDIA692_X31J
        int L3538_;
#endif
#ifdef MEDIA645_A31E_A35E
        long* L3538_;


        F6011_BeginLibraryFunction();
        if ((L3538_ = AllocMem(P3262_l_ + 4, 0L)) == NULL) {
                F6012_EndLibraryFunction();
                return NULL;
        }
        *L3538_++ = P3262_l_ + 4;
        F6012_EndLibraryFunction();
#endif
#ifdef MEDIA692_X31J
        if ((L3538_ = MALLOC(P3262_l_)) & 0xFF000000) {
                L3538_ = 0;
        }
#endif
        return L3538_;
}

void F0918_PRIM_16_Memory_Free(P3263_pl_)
long* P3263_pl_;
{
#ifdef MEDIA645_A31E_A35E
        long L3539_l_ByteCount;
        long* L3540_pl_Address;


        F6011_BeginLibraryFunction();
        L3540_pl_Address = P3263_pl_;
        if (L3540_pl_Address) {
                L3540_pl_Address--;
                L3539_l_ByteCount = *L3540_pl_Address;
                FreeMem(L3540_pl_Address, L3539_l_ByteCount);
        }
        F6012_EndLibraryFunction();
#endif
#ifdef MEDIA692_X31J
        if (P3263_pl_) {
                MFREE(P3263_pl_);
        }
#endif
}

char F6000_PRIM_17_File_Open(P3264_pc_FileName, P3265_p_Handle)
char* P3264_pc_FileName;
FILE_HANDLE* P3265_p_Handle;
{
        FILE_HANDLE L3541_;


#ifdef MEDIA692_X31J
        if ((short)(L3541_ = OPEN(P3264_pc_FileName, 0L)) < 0) {
#endif
#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        if ((L3541_ = Open(P3264_pc_FileName, MODE_OLDFILE)) == NULL) {
                F6012_EndLibraryFunction();
#endif
                return 1;
        }
        *P3265_p_Handle = L3541_;
#ifdef MEDIA645_A31E_A35E
        F6012_EndLibraryFunction();
#endif
        return 0;
}

void F6001_PRIM_18_File_Close(P3266_Handle)
FILE_HANDLE P3266_Handle;
{
#ifdef MEDIA692_X31J
        if ((short)P3266_Handle >= 0) {
                CLOSE(P3266_Handle);
        }
#endif
#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        if (P3266_Handle) {
                Close(P3266_Handle);
        }
        F6012_EndLibraryFunction();
#endif
}

char F0919_PRIM_19_File_Seek(P3267_Handle, P3268_l_Offset)
FILE_HANDLE P3267_Handle;
long P3268_l_Offset;
{
#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        Seek(P3267_Handle, P3268_l_Offset, OFFSET_BEGINNING);
        F6012_EndLibraryFunction();
#endif
#ifdef MEDIA692_X31J
        if (SEEK(P3267_Handle, P3268_l_Offset, 0) != P3268_l_Offset) {
                return 1;
        }
#endif
        return 0;
}

char F0920_PRIM_20_File_Read(P3269__Handle, P3270_l_Size, P3271_l_Address)
FILE_HANDLE P3269__Handle;
long P3270_l_Size;
long P3271_l_Address;
{
#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        if (Read(P3269__Handle, P3271_l_Address, P3270_l_Size) != P3270_l_Size) {
                F6012_EndLibraryFunction();
#endif
#ifdef MEDIA692_X31J
        if (READ(P3269__Handle, P3271_l_Address, P3270_l_Size) != P3270_l_Size) {
#endif
                return 1;
        }
#ifdef MEDIA645_A31E_A35E
        F6012_EndLibraryFunction();
#endif
        return 0;
}

void F6002_PRIM_22_Console_PrintMessage(P3272_pc_)
char* P3272_pc_;
{
#ifdef MEDIA692_X31J
        char L3542_ac_[120];
        register char* L3543_pc_;
        register char L3544_c_;


        L3543_pc_ = L3542_ac_;
        while (L3544_c_ = *P3272_pc_++) {
                *L3543_pc_++ = L3544_c_;
                if (L3544_c_ == '\n') {
                        *L3543_pc_++ = '\r';
                }
        }
        *L3543_pc_ = '\0';
        PRINT(L3542_ac_);
#endif
#ifdef MEDIA645_A31E_A35E
        FILE_HANDLE L3545_ps_FileHandle;


        F6011_BeginLibraryFunction();
        if (L3545_ps_FileHandle = Output()) {
                Write(L3545_ps_FileHandle, P3272_pc_, (long)strlen(P3272_pc_));
        }
        F6012_EndLibraryFunction();
#endif
}

unsigned char F6003_PRIM_23_Input_WaitForChar(
)
{
        FILE_HANDLE L3546_;
        short L3547_;


#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        if (L3546_ = Input()) {
                L3547_ = WaitForChar(L3546_, 1L);
                F6012_EndLibraryFunction();
                return L3547_;
        }
        F6012_EndLibraryFunction();
#endif
#ifdef MEDIA692_X31J
        if (KEYSNS()) {
                return 1;
        }
#endif
        return 0;
}

#ifdef MEDIA692_X31J
short
#endif
int F6004_PRIM_24_Input_Read(
)
{
#ifdef MEDIA692_X31J
        register short
#endif
        FILE_HANDLE L3548_;
#ifdef MEDIA645_A31E_A35E
        char L3549_c_;


        F6011_BeginLibraryFunction();
        if (L3548_ = Input()) {
                Read(L3548_, &L3549_c_, (long)sizeof(L3549_c_));
                F6012_EndLibraryFunction();
                return L3549_c_;
        }
        F6012_EndLibraryFunction();
        return 0;
#endif
#ifdef MEDIA692_X31J
        L3548_ = GETC();
        if ((unsigned char)L3548_ != 0) {
                L3548_ &= 0xFF;
        }
        return L3548_;
#endif
}

void F6005_PRIM_21_FTL_FatalLoaderError(P3273_l_)
long P3273_l_;
{
#ifdef MEDIA692_X31J
        static char L3550_ac_FatalError[] = "PrimError 0x0000***";


        F0933_GetHexStringFromValue(&L3550_ac_FatalError[12], P3273_l_);
        F6019_F6002_PRIM_22_Console_PrintMessage(L3550_ac_FatalError);
        F6019_F6002_PRIM_22_Console_PrintMessage("\n");
#endif
#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        F6019_F6002_PRIM_22_Console_PrintMessage("FTL Fatal Loader Error\n");
#endif
        for (;;);
#ifdef MEDIA645_A31E_A35E
        F6012_EndLibraryFunction();
#endif
}

long F6006_PRIM_07_FTL_Execute(P3274_ps_, P3275_l_)
FTL_EXECUTABLE* P3274_ps_;
long P3275_l_;
{
        long L3551_l_;


#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
#endif
        L3551_l_ = F6007_CallFTLProgram(P3274_ps_, P3275_l_, PrimBase, P3274_ps_->JumpTableAddress, P3274_ps_->A5World);
#ifdef MEDIA645_A31E_A35E
        F6012_EndLibraryFunction();
#endif
        return L3551_l_;
}

#ifdef MEDIA645_A31E_A35E
/* long F6007_CallFTLProgram(FTL_EXECUTABLE*, long Parameter, FTL_LIBRARY* FTLLibraryBase, long JumpTableAddress, char* A5World) */
#asm
        _F6007_CallFTLProgram:
                link    a6,#0
                movem.l d3-d7/a2-a6,-(sp)
                move.l  $C(a6),-(sp)    ; long Parameter
                move.l  8(a6),-(sp)     ; FTL_EXECUTABLE*
                move.l  $10(a6),-(sp)   ; FTL_LIBRARY* FTLLibraryBase
                movea.l $14(a6),a0      ; long JumpTableAddress
                movea.l $18(a6),a5      ; char* A5World
                jsr     (a0)
                lea     $C(sp),sp
                movem.l (sp)+,d3-d7/a2-a6
                unlk    a6
                rts
#endasm
#endif

void F6008_PRIM_29_Empty(
)
{
#ifdef MEDIA645_A31E_A35E
        F6011_BeginLibraryFunction();
        F6012_EndLibraryFunction();
#endif
}

char* F6009_GetInitialA5World(
)
{
#ifdef MEDIA645_A31E_A35E
        #asm
                move.l  a4,d0
        #endasm
#endif
#ifdef MEDIA692_X31J
        return 0;
#endif
}

void F6010_ExecuteLibraryFunction(P3276_ps_FTLLibrary, P3277_RequestedFunctionOrdinal)
FTL_LIBRARY* P3276_ps_FTLLibrary;
int16_t P3277_RequestedFunctionOrdinal;
{
#ifdef MEDIA645_A31E_A35E
        #asm
                movem.l d3-d7/a2-a6,-(sp)
                movea.l 8(a5),a0        ; P3276_ps_FTLLibrary
                move.w  12(a5),d0       ; P3277_RequestedFunctionOrdinal
                moveq   #$FFFFFFFC,d1   ; -4
                muls.w  d0,d1           ; Negative offset to requested function
                movea.l (a0,d1.l),a1    ; A1 = Address of requested function
                movea.l $C(a0),a5       ; A5 = P3276_ps_FTLLibrary->FTLExecutable
                jsr     (a1)
                movem.l (sp)+,d3-d7/a2-a6
        #endasm
#endif
#ifdef MEDIA692_X31J
        X68_CallDrvrVector(P3276_ps_FTLLibrary, (long)P3277_RequestedFunctionOrdinal, P3276_ps_FTLLibrary->FTLExecutable.A5World);
#endif

}

#ifdef MEDIA645_A31E_A35E
#asm
        _F6011_BeginLibraryFunction:
                        movea.l (sp)+,a1
#endasm
#ifdef MEDIA618_A31E
#asm
                        movem.l d3/a4/a6,-(sp)
#endasm
#endif
#ifdef MEDIA739_A35E
#asm
                        move.l  a4,-(sp)
                        move.l  a6,-(sp)
#endasm
#endif
#asm
                        movea.l 12(a0),a4       ; Restore base.FTLLibrary.FTLExecutable.A5World to access
                                                ; the data segment of the FTL executable
                        jmp     (a1)

        _F6012_EndLibraryFunction:
                        movea.l (sp)+,a1
#endasm
#ifdef MEDIA618_A31E
#asm
                        movem.l (sp)+,d3/a4/a6
#endasm
#endif
#ifdef MEDIA739_A35E
#asm
                        movea.l (sp)+,a6
                        movea.l (sp)+,a4
#endasm
#endif
#asm
                        jmp     (a1)
#endasm
#endif
#ifdef MEDIA692_X31J
#asm
        .XDEF        _X68_execute            ; F6007_CallFTLProgram
        .XDEF        _X68_CallDrvrVector

_X68_execute:
        link    a6,#0
        movem.l d2-d7/a2-a5,-(sp)
        move.l  12(a6),-(sp)   ; Parameter
        move.l  8(a6),-(sp)    ; FTL_EXECUTABLE*
        move.l  16(a6),-(sp)   ; FTLLibraryBase
        movea.l 20(a6),a0      ; JumpTableAddress
        movea.l 24(a6),a5      ; A5World
        jsr     (a0)
        lea     $C(sp),sp
        movem.l (sp)+,d2-d7/a2-a5
        unlk    a6
        rts

_X68_CallDrvrVector:
        link    a6,#0
        movem.l d2-d7/a2-a5,-(sp)
        move.l  12(a6),d0       ; RequestedFunctionOrdinal
        movea.l 8(a6),a0        ; FTLLibrary
        moveq   #$FFFFFFFC,d1   ; -4
        muls.w  d0,d1           ; Negative offset to requested function
        movea.l a0,a1
        adda.l  d1,a1
        movea.l (a1),a1         ; A1 = Address of requested function
        movea.l $10(a6),a5      ; A5 = FTLLibrary->FTLExecutable->A5World
        jsr     (a1)
        movem.l (sp)+,d2-d7/a2-a5
        unlk    a6
        rts
#endasm
#endif
#endif
