/*-----------------------------------------------------------------------------+
|                                                                              |
|       Copyright (c), Raisonance S.A.S., 1987-2006                            |
|                                                                              |
+------------------------------------------------------------------------------+
|                                                                              |
|       File: MAIN_ST7.H                                                       |
|                                                                              |
+--------------+-------+----------------------------------------------+--------+
|     DATE     |VERSION| HISTORY/CHANGES                              |   BY   |
|  (DD.MM.YY)  | VX.Y  |                                              |        |
+--------------+-------+----------------------------------------------+--------+
|   05.09.05   |  1.0  | Start of implementation                      |   SC   |
|   30.03.06   |  1.2  | Redefinition of alloc_size_t                 |   SC   |
+--------------+-------+----------------------------------------------+--------+
|Purpose    :                                                                  |
|   Special definitions for ST7                                                |
+-----------------------------------------------------------------------------*/

#ifndef LOCAL_GENERIC
   #define LOCAL_GENERIC
#endif

#ifndef _ROM_MODELS           // Used for checking __ROM_MODEL__
   #define _ROM_MODELS
   #define __SMALL         0
   #define __LARGE         1
#endif

/*** FOR ALLOC.H ***/
#ifdef _ALLOC_H_
   #ifndef allocmem
      #define allocmem data
      #undef alloc_size_t
      #define alloc_size_t int
   #endif
#endif

#if (defined(_STDIO_H_) || defined(_STDDEF_H_) || defined(_ALLOC_H_) || defined(_STRING_H_) || defined(_SETJMP_H_))
   #pragma noauto
#endif

/*** FOR ALLOC.H, STRING.H, STDIO.H and STDDEF.H ***/
#if (defined(_STDIO_H_) || defined(_STDDEF_H_) || defined(_ALLOC_H_) || defined(_STRING_H_))
   #ifndef _SIZE_T
      #define _SIZE_T
      typedef unsigned short size_t;
   #endif
#endif

#ifdef _STRING_H_
   #define string_tvar char
   
   #if __STM8__ == 1
      #define FAR_GENERIC far
      extern void FAR_GENERIC* fmemcpy     (void FAR_GENERIC* dest, const void FAR_GENERIC *src, unsigned n) reentrant;
      extern unsigned          lstrlen     (const char FAR_GENERIC* s) reentrant;
   #endif
   extern void  LOCAL_GENERIC* lmemcpy     (void LOCAL_GENERIC* dest, const void LOCAL_GENERIC *src, unsigned n) reentrant;
   extern void  LOCAL_GENERIC* lmemset     (const void  LOCAL_GENERIC* s,  string_tvar c, size_t n) reentrant;

#endif

/*** FOR STDARG.H ***/
#ifdef _STDARG_H_
   #ifndef __Stack_Space__
      #define __Stack_Space__ data

		extern unsigned int  _getSP_(void) reentrant;   /*  Intrinsic RCST7 function  */

      /*----------------------------------------------------------------------------*/
      /* The following macros allow to locate the argument into the stack.          */
      /* The symbol __STARTPOS__ is internal for the compiler.                      */
      /*----------------------------------------------------------------------------*/
      #define va_start(ap,v) ap = (va_list)( _getSP_() + __STARTPOS__)
      #define va_arg(ap,t) (*(t __Stack_Space__ *)  ((ap+=sizeof(t)) - sizeof(t)) )
   #endif
#endif

/*** FOR STDARG.H ***/
#ifdef _STDLIB_H_
   #ifndef RAND_MAX
      #define RAND_MAX 32767
   #endif

   #ifndef _DIV_LDIV_
      #define _DIV_LDIV_
   typedef struct { int quot, rem; } div_t;
   div_t div(int numer, int denom) reentrant;

   typedef struct { long quot, rem; } ldiv_t;
   ldiv_t ldiv(long numer, long denom) reentrant;
   #endif

#endif

/*** FOR CTYPE.H ***/
#ifdef _CTYPE_H_
   #ifndef iscntrl
      extern int isalnum(int c) reentrant;
      extern int isalpha(int c) reentrant;
      extern int iscntrl(int c) reentrant;
      extern int isdigit(int c) reentrant;
      extern int isgraph(int c) reentrant;
      extern int islower(int c) reentrant;
      extern int isprint(int c) reentrant;
      extern int ispunct(int c) reentrant;
      extern int isspace(int c) reentrant;
      extern int isupper(int c) reentrant;
      extern int isxdigit(int c) reentrant;
      extern int tolower(int c) reentrant;
      extern int toupper(int c) reentrant;

      #define isalnum(c) (isalpha(c)||isdigit(c)) 
      #define isalpha(c) (isupper(c)||islower(c)) 
      #define iscntrl(c) (((c)>=0)&&(((c)<=0x1f)||((c)==0x7f))) 
      #define isdigit(c) (((c)>='0')&&((c)<='9')) 
      #define isgraph(c) (((c)!=' ')&&isprint(c)) 
      #define islower(c) (((c)>='a')&&((c)<='z')) 
      #define isprint(c) (((c)>=' ')&&((c)<='~')) 
      #define ispunct(c) ((((c)>' ')&&((c)<='~'))&&!isalnum(c)) 
      #define isspace(c) ((((c)>=0x09)&&((c)<=0x0D))||((c)==0x20))
      #define isupper(c) (((c)>='A') &&((c)<='Z'))
      #define isxdigit(c) (isdigit(c)||((c)>='a'&&(c)<='f')||((c)>='A'&&(c)<='F'))
      #define tolower(c) (isupper(c)?((c)-'A'+'a'):(c)) 
      #define toupper(c) (islower(c)?((c)-'a'+'A'):(c)) 
   #endif
#endif

/*** FOR ERRNO.H ***/
#ifdef _ERRNO_H
extern int errno;
#endif

/*** FOR SETJMP.H ***/
#ifdef _SETJMP_H_
   #define LOCAL_FAR data
   #if (__STM8__ == 1)
      #if (__ROM_MODEL__ == __LARGE)
         #define _JBLEN 5
      #else
         #define _JBLEN 4
      #endif
   #else
      #define _JBLEN 3
   #endif
#endif

/*** FOR MATH.H ***/
#ifdef _MATH_H_
   // HUGE_VAL is the infinity as returned by RCST7.
   // It will be encoded as 0x7F800000 in IEEE-754 big endian format.
   #ifdef HUGE_VAL
      #undef HUGE_VAL
   #endif
   #define HUGE_VAL (1e999)
#endif
