/*
# Copyright (C) 2011-2012 audin.
# 2012/10 Added function _getpid() and _exit().
# 2011/06 Modified for the heap problem in _sbrk_r().
# 2011/04 Modified for etc.
# http://avr.paslog.jp/article/2027785.html
# This file is licensed under the below License.
*/

#define __SYSCALLS__
#define _vStackTop _estack


#if defined( __SYSCALLS__ )
/****************************************************************************
*  Copyright (c) 2009 by Michael Fischer. All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*  2. Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*  3. Neither the name of the author nor the names of its contributors may
*     be used to endorse or promote products derived from this software
*     without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
*  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
*  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
*  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*
****************************************************************************
*  History:
*
*  2012/10  audin    Added function _getpid() and _exit().
*  2011/03  audin    Modified.
*  28.03.09  mifi   First Version, based on the original syscall.c from
*                   newlib version 1.17.0
****************************************************************************/

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifdef __cplusplus
	extern "C" {
#endif /* __cplusplus */

#define WEAK __attribute__ ((weak))

#define _KILL   0
#define _GETPID 0
#define _READ_R 0
#define _LSEEK_R 0
#define _WRITE_R 1
#define _CLOSE_R 0
#define _SBRK_R 1
#define _FSTAT_R 0
#define _ISATTY_R 0
/*** added 2012/12 *********************************************************/
#if _KILL
int WEAK _kill(int pid, int sig){ return 0; }
#endif

/*** added 2012/10 *********************************************************/
#if _GETPID
int _getpid()     { return 1;    }
//int WEAK _exit(int val) { return (-1); }
#endif

/***************************************************************************/
#if _READ_R
int _read_r (struct _reent *r, int file, char * ptr, int len)
{
	errno = EINVAL;
	return -1;
}
#endif

/***************************************************************************/
#if _LSEEK_R
int _lseek_r (struct _reent *r, int file, int ptr, int dir)
{
	return 0;
}
#endif

/***************************************************************************/
#if _WRITE_R
void WEAK UART_putc( unsigned char ch ){/*dummy*/}
extern void UART_putc( unsigned char ch );
int _write_r (struct _reent *r, int file, char * ptr, int len)
{

#if 1
	int index;

	/* For example, output string by UART */
	for(index=0; index<len; index++)
	{
		if (ptr[index] == '\n')
		{
			UART_putc('\r');
		}

		UART_putc(ptr[index]);
	}
#endif

	return len;
}
#endif

/***************************************************************************/
#if _CLOSE_R
int _close_r (struct _reent *r, int file)
{
	return 0;
}
#endif

/***************************************************************************/
#if _SBRK_R
/* Register name faking - works in collusion with the linker.  */
register char * stack_ptr __asm ("sp");
caddr_t _sbrk_r (struct _reent *r, int incr)
{
	extern char   end __asm ("end");	/* Defined by the linker.  */
	static char * heap_end;
	char        * prev_heap_end;
	extern char   _vStackTop;			/* Defined by the linker.  */
	char        * pStack;

	if (heap_end == NULL){
		heap_end = & end;
	}
	prev_heap_end = heap_end;

	pStack = ( stack_ptr > heap_end ) ? stack_ptr : &_vStackTop;

	if (heap_end + incr > pStack)
	{
		/* Some of the libstdc++-v3 tests rely upon detecting
        out of memory errors, so do not abort here.  */
#if 0
		extern void abort (void);

		_write_r( r, 1, "_sbrk: Heap and stack collision\n", 32);
		while(1); /* Terminate here */
		/* abort (); */
#else
		errno = ENOMEM;
		return (caddr_t) -1;
#endif
	}

	heap_end += incr;

	return (caddr_t) prev_heap_end;
}
#endif
/***************************************************************************/
#if _FSTAT_R
int _fstat_r (struct _reent *r, int file, struct stat * st)
{
	memset (st, 0, sizeof (* st));
	st->st_mode = S_IFCHR;
	return 0;
}
#endif
/***************************************************************************/
#if _ISATTY_R
int _isatty_r(struct _reent *r, int fd)
{

	return 1;
}
#endif

/*** EOF ***/


#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __SYSCALLS__ */
