#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA000_S10EA
/* Custom INIT.C based on original supplied with Megamax C and modified by FTL.
Two variants are used on the game disk: one for START.PRG and one for the game itself.
Other programs use the original INIT.C file supplied with Megamax C.

Changes from original INIT.C:
- Removed _argc, _argv and call to _initargcv()
- Removed call to exit()

Additional change for START.PRG:
- Removed call to GEMDOS Mshrink() to return unused memory to the system */

/*
        This is the very first stuff that executes when a program is run.
        It sets up the global variables, strings, stack etc.
*/

#if EXETYPE == C03_GAME
#define STACKSIZE 2000
#endif
#if EXETYPE == C02_LAUNCHER
#define STACKSIZE 8192
#endif

/* Base page definitions */
#define ltpa 0          /* Low TPA address */
#define htpa 4          /* High TPA address */
#define lcode 8         /* Code segment start */
#define codelen 12      /* Code segment length */
#define ldata 16        /* Data segment start */
#define datalen 20      /* Data segment length */
#define lbss 24         /* Bss  segment start */
#define bsslen 28       /* Bss  segment length */
#define freelen 32      /* free segment length */
#define resvd 36        /* Reserved */
#define fcb2 56         /* 2nd parsed fcb */
#define fcb1 92         /* 1st parsed fcb */
#define command 128     /* Command tail */

extern _init(), _main(), main();
char *_base;    /* points to base page of program */

#ifndef LINT
overlay "init!"
asm {
        _init:  /* initialization entry point; RTS plugged in by linker */
}
#endif

#ifndef LINT
overlay "main"
asm {
        _main:
#if EXETYPE == C03_GAME
                move.l  4(A7),A5                ; A5=basepage address
                move.l  bsslen(A5),D6
#endif
#if EXETYPE == C02_LAUNCHER
                move.l  A7,A5                   ; save A7 so we can get the base page address
                move.l  4(A5),A5                ; A5=basepage address
#endif
                move.l  codelen(A5),D0
                add.l   datalen(A5),D0
#if EXETYPE == C03_GAME
                add.l   D6,D0
#endif
#if EXETYPE == C02_LAUNCHER
                add.l   bsslen(A5),D0
#endif
                add.l   #256+STACKSIZE,D0       ;D0=basepage+textlen+datalen+bsslen+stack
                move.l  D0,D1
#if EXETYPE == C03_GAME
                add.l   A5,D0                   ; compute stack top
                andi.w  #-2,D0                  ; ensure even byte boundary
                move.l  D0,A7                   ; setup user stack

                move.l  D1,-(A7)                ; return storage above stack to system
                move.l  A5,-(A7)
                clr.w   -(A7)                   ; junk word
                move    #0x4a,-(A7)             ; return excess storage
                trap    #1
                adda.w  #12,A7
#endif
#if EXETYPE == C02_LAUNCHER
                add.l   A5,D1                   ; compute stack top
                and.l   #-2,D1                  ; ensure even byte boundary
                move.l  D1,A7                   ; setup user stack
#endif

                move.l  lbss(A5), A0            ;swap data and bss segments
#if EXETYPE == C03_GAME
                move.l  A0, A1
                adda.l  D6, A1
#endif
#if EXETYPE == C02_LAUNCHER
                move.l  lbss(A5), A1
                adda.l  bsslen(A5), A1
#endif
                move.l  datalen(A5), D0
                subq.l  #1, D0                  ; dbf loop is stupid
#if EXETYPE == C03_GAME
                ble.s   swapcont                ;might not have any data
#endif
#if EXETYPE == C02_LAUNCHER
                ble     swapcont                ;might not have any data
#endif
        swap:
                move.b  -(A0), -(A1)
                dbf     D0,swap
        swapcont:

                move.l  ldata(A5), A0           ;clear bss segment
#if EXETYPE == C03_GAME
                move.l  D6, D0
                move.l  A0, A4
                adda.l  D6, A4
#endif
#if EXETYPE == C02_LAUNCHER
                move.l  bsslen(A5), D0
#endif
                subq.l  #1, D0
        clear:
                clr.b   (A0)+
                dbf     D0, clear

#if EXETYPE == C02_LAUNCHER
                move.l  ldata(A5), A4           ;A4 points between bss and data
                adda.l  bsslen(A5), A4

#endif
                move.l  A5, _base(A4);

                move.l  lcode(A5), A5           ;A5 points to jump table

                jsr     _init                   ;initialize globals and statics now
                ;call application's entry point
#if EXETYPE == C03_GAME
                jmp     main
#endif
#if EXETYPE == C02_LAUNCHER
                jsr     main
#endif
}
#endif
#endif
