 /*
  * UAE - The Un*x Amiga Emulator
  *
  * cpp magic
  *
  * Copyright 1995, 1996 Bernd Schmidt, Ed Hanway
  */

#define UAEMAJOR 0
#define UAEMINOR 6
#define UAEURSAMINOR 4

typedef enum { KBD_LANG_US, KBD_LANG_DE, KBD_LANG_SE, KBD_LANG_FR, KBD_LANG_IT } KbdLang;

extern int version;
extern int framerate;
extern int produce_sound;
extern int correct_aspect;
extern int use_fast_draw;
extern int use_debugger;
extern int use_serial;
extern int illegal_mem;
extern int use_fast_mem;
extern int use_gfxlib;
extern int no_xhair;
extern int use_lores;
extern int automount_uaedev;
extern int fake_joystick;
extern KbdLang keyboard_lang;
extern int color_mode;
extern int screen_res;
extern int sound_desired_bits;
extern int sound_desired_freq;
extern int sound_desired_bsiz;
extern int allow_save;
extern int no_gui;
extern long hardfile_size;

/* Contains the filename of .uaerc */
extern char optionsfile[];

/* AIX doesn't think it is Unix. Neither do I. */
#if defined(_ALL_SOURCE) || defined(_AIX)
#undef __unix
#define __unix
#endif

#define MAX_COLOR_MODES 5
#define MAX_SCREEN_MODES 4

extern void target_specific_usage(void);

extern char df0[], df1[], df2[], df3[], romfile[], prtname[], sername[];

#ifndef __unix
extern void parse_cmdline(int argc, char **argv);
#endif

#ifdef __mac__
/* Apparently, no memcpy :-/ */
static __inline__ void *memcpy(void *to, void *from, int size)
{
    BlockMove(from, to, size);
}
#endif

/* strdup() may be non-portable if you have a weird system */
static char *my_strdup(const char*s)
{
    /* The casts to char * are there to shut up the compiler on HPUX */
    char *x = (char*)malloc(strlen((char *)s) + 1);
    strcpy(x, (char *)s);
    return x;
}

#undef REGPARAM
#define fast_memcmp memcmp
#define memcmpy generic_memcmpy

static __inline__ int generic_memcmpy(void *foo, const void *bar, int len)
{
    int res = memcmp(foo, bar, len);
    if (res)
	memcpy(foo, bar, len);
    return res;
}

#if defined(__GNUC_MINOR__)

#ifdef __i386__

#define INTEL_FLAG_OPT

#undef fast_memcmp
#undef memcmpy

/*
 * Careful: These inline functions may be used only _once_. We'd want to 
 * generate unique labels for each inline function, but unfortunately not each
 * installation of GCC comes with GAS. Bletch.
 */

static __inline__ int fast_memcmp(const void *foo, const void *bar, int len)
{
    int differs, baz;
    __asm__ __volatile__ ("subl $4, %2\n"
			  "jc  LLA2\n"
			  "LLA1:\n"
			  "movl (%0),%%ebx\n"
			  "cmpl (%1),%%ebx\n"
			  "jne LLA5\n"
			  "addl $4, %0\n"
			  "addl $4, %1\n"
			  "subl $4, %2\n"
			  "jnc  LLA1\n"
			  "LLA2:\n"
			  "addl $4, %2\n"
			  "jz LLA4\n"
			  "LLA3:\n"
			  "movb (%0),%%bl\n"
			  "cmpb (%1),%%bl\n"
			  "jne LLA5\n"
			  "incl %0\n"
			  "incl %1\n"
			  "decl %2\n"
			  "jnz LLA3\n"
			  "LLA4:\n"
			  "movl $0, %3\n"
			  "jmp LLA6\n"
			  "LLA5:\n"
			  "movl $1, %3\n"
			  "LLA6:\n"
			  : "=&r" (foo), "=&r" (bar), "=&rm" (len), "=rm" (differs),
			    "=&b" (baz)
			  : "0" (foo), "1" (bar), "2" (len), "3" (baz) : "cc");
    return differs;
}

static __inline__ int memcmpy(void *foo, const void *bar, int len)
{
    int differs, baz = 0, uupzuq = 0;

    __asm__ __volatile__ ("subl %1, %2\n"
			  "movl $0, %0\n"
			  "subl $16, %3\n"
			  "jc LLB7\n"
			  
			  "LLB8:\n"
			  "movl (%1),%%ecx\n"
			  "movl (%2,%1),%%ebx\n"
			  "xorl %%ebx, %%ecx\n"
			  "movl %%ebx, (%1)\n"
			  "orl %%ecx, %0\n"
			  
			  "movl 4(%2,%1),%%ebx\n"
			  "movl 4(%1),%%ecx\n"
			  "xorl %%ebx, %%ecx\n"
			  "movl %%ebx, 4(%1)\n"
			  "orl %%ecx, %0\n"
			  
			  "movl 8(%2,%1),%%ebx\n"
			  "movl 8(%1),%%ecx\n"
			  "xorl %%ebx, %%ecx\n"
			  "movl %%ebx, 8(%1)\n"
			  "orl %%ecx, %0\n"
			  
			  "movl 12(%2,%1),%%ebx\n"
			  "movl 12(%1),%%ecx\n"
			  "xorl %%ebx, %%ecx\n"
			  "movl %%ebx, 12(%1)\n"
			  "orl %%ecx, %0\n"
			  
			  "addl $16, %1\n"
			  "subl $16, %3\n"
			  "jnc  LLB8\n"
			  
			  "LLB7:\n"
			  "addl $16, %3\n"
			  "subl $4, %3\n"
			  "jc  LLB2\n"
			  
			  "LLB1:\n"
			  "movl (%2,%1),%%ebx\n"
			  "movl (%1),%%ecx\n"
			  "xorl %%ebx, %%ecx\n"
			  "movl %%ebx, (%1)\n"
			  "orl %%ecx, %0\n"
			  "addl $4, %1\n"
			  "subl $4, %3\n"
			  "jnc  LLB1\n"
			  
			  "LLB2:\n"
			  "addl $4, %3\n"
			  "jz LLB6\n"
			  "xorl %%ecx, %%ecx\n"
			  
			  "LLB3:\n"
			  "movb (%2,%1),%%bl\n"
			  "movb (%1),%%cl\n"
			  "xorb %%bl,%%cl\n"
			  "movb %%bl,(%1)\n"
			  "orl %%ecx, %0\n"
			  "incl %1\n"
			  "decl %3\n"
			  "jnz LLB3\n"
			  
			  "LLB6:\n"
			  : "=m" (differs)
			  : "r" (foo), "r" (bar), "r" (len), "b" (baz), "c" (uupzuq) : "cc", "memory");
    /* Now tell the compiler that foo, bar and len have been modified 
     * If someone finds a way to express all this cleaner in constraints that
     * GCC 2.7.2 understands, please FIXME */
    __asm__ __volatile__ ("" : "=rm" (foo), "=rm" (bar), "=rm" (len) : :  "ebx", "ecx", "edx", "eax", "esi", "memory");

    return differs;
}
#endif /* __i386__ */

#if __GNUC__ > 2 || __GNUC_MINOR__ > 6

#if defined(__i386__) && !defined(USE_PROFILING)
#define REGPARAM __attribute__((regparm(3)))
#endif /* __i386__ */

#endif /* GCC version 2.7 or higher */
#endif /* GCC 2 */

#ifndef REGPARAM
#define REGPARAM
#endif

/*
 * You can specify numbers from 0 to 5 here. It is possible that higher
 * numbers will make the CPU emulation slightly faster, but if the setting
 * is too high, you will run out of memory while compiling.
 * Best to leave this as it is.
 */
#define CPU_EMU_SIZE 0

#ifdef DONT_WANT_SOUND
#undef LINUX_SOUND
#undef AF_SOUND
#undef SOLARIS_SOUND
#undef SGI_SOUND
#endif

/* #define NEED_TO_DEBUG_BADLY */

