/*
 *	KRoC C scheduler definitions and function prototypes
 *	Copyright (C) 1995, 1996, 1997 D.J. Beckett
 *	Modifications for oos (C) 2002 Brian Vinter
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * $Source: /proj/ofa/rts/RCS/rts.h,v $
 *
 * $Id: rts.h,v 1.22 1997/08/12 14:39:16 djb1 Exp $
 *
 * (C) Copyright 1995,1996,1997 D.J. Beckett <D.J.Beckett@ukc.ac.uk>
 * University of Kent at Canterbury
 */

#ifndef RTS_H
#define RTS_H

#ifdef HAVE_CONFIG_H
	#include <config.h>
#endif

#if !defined(OOS_BUILD)
	#include <stdio.h>
	#if defined(HAVE_STRING_H)
		#include <string.h>
	#elif defined(HAVE_STRINGS_H)
		#include <strings.h>
	#endif
	#if defined(HAVE_UNISTD_H)
		#include <unistd.h>
	#endif
	#if defined(HAVE_STDLIB_H)
		#include <stdlib.h>
	#endif

	#include <sys/types.h>

	#if defined(HAVE_SYS_WAIT_H)
		#include <sys/wait.h>
	#endif
	#if defined(TIME_WITH_SYS_TIME)
		#include <sys/time.h>
		#include <time.h>
	#elif defined(HAVE_SYS_TIME_H)
		#include <sys/time.h>
	#else
		#include <time.h>
	#endif

	#include <sys/resource.h>
	#include <sys/termios.h>

	#if defined(HAVE_SYS_TIMERS_H)
		#include <sys/timers.h>
	#endif

	#include <signal.h>

	#if defined(HAVE_ERRNO_H)
		#include <errno.h>
	#endif
#endif


#include <ukcthreads_types.h>

/* Unix system call error codes */

#define ERR             -1

/* Blocking sys-call signal */
/* I don't want to use LinuxThreads particularly, clumsy */
#if defined(BLOCKING_SYSCALLS)
	#define SIGBLOCKING	SIGUSR1	/* frmb2 */
#endif	/* BLOCKING_SYSCALLS */

/* keyboard signals */

#define SIGKBD          SIGUSR2/* phw & dcw */
/* changed so that it can work with LinuxThreads which uses SIGUSR1 */
#if 0
#define SIGPANIC        SIGUSR2 /* phw & dcw */
#endif
/* Signal magic */
typedef void (*SigParam)(int);

/* tty chars */

#define NULL_CHAR       (char)0
#define END_CHAN        (char)255

/* trap codes */

#define _SETERR         16      /* unconditional (SETERR) */
#define _OFLOW          17      /* arithmetic overflow */
#define _RANGE          18      /* range check (CSUB0, CCNT1, CSNGL, CWORD) */
#define _FLOAT          19      /* floating-point (FPCHKERR, FPSETERR) */
#define _UNREC          20      /* unrecognized instruction */
#define _UNIMP          21      /* unimplemented instruction */


/* Globals, defined in rtsmain.c */
#ifndef RTS_MAIN

#ifdef DEBUG_RTS
extern bool trace;
#endif

#endif

/* Globals defined in sched.s */
/*extern int kbd_chan;  moved to 386/occam_io.h */

extern char kbd_sync;           /* packed in one word (dcw) */
extern char tim_sync;
#if 0
extern char panic_sync;
#endif
extern int combined_sync;

/* in the entry code */
extern int do_print_memstats;

/* KROC C scheduler function prototypes (was proto.h) */

/* cdump.c */
void dump (int n, int iptr, int wptr, int a, int b, int c);
void fdump (int code, int *fa_p, int fb_h, int fb_l, int fc_h, int fc_l);

/* kbdproc.c */
#ifndef BLOCKING_SYSCALLS
	void keyboard_process (int user_pid, int to_user_fd, int from_user_fd, bool is_a_tty);
#endif

/* userproc.c */
void occam_exit(int exit_status, int occam_iptr);
void user_bad_exit (void);
void user_good_exit (void);
void user_tim_handler (void);
#if 0
	void user_panic_handler (void);
#endif
void user_kbd_handler (void);
void user_overflow (void);
#if defined(SIGNAL_TYPE_SYSV)
	void user_trap_handler (int sig);
	void user_fp_handler (int sig);
#else
	void user_trap_handler (int sig, int code, struct sigcontext *scp, char *addr);
	void user_fp_handler (int sig, int code, struct sigcontext *scp, char *addr);
#endif
char read_the_kbd (void);
void acknowledge (void);
void user_to_kbd_sync (void);
void impossible (int n, int occam_iptr);
void status (int erfl);
void dead (int erfl);
void dead_quiet (int erfl);
unsigned int utime (void);
#if defined(TARGET_CPU_386)
	unsigned int rtime (void);
#else
	unsigned int rtime (void);
#endif
#ifndef HAVE_UALARM
	int ualarm(int delay, int interval);
#endif
int safe_pause (void);
#ifdef ENABLE_CPU_TIMERS
int safe_pause_timeout (int usecs);
#endif
void user_process (int kbd_pid, int to_kbd_fd, int from_kbd_fd, bool is_a_tty);

/* rtsmain.c */
extern char *short_cmdline;
extern char *long_cmdline;

extern int kroc_argc;
extern char **kroc_argv;

/* support.c */
int main (int argc, char **argv);

/* sched.s - occam program entry */
int _enter (void);

#ifdef BLOCKING_SYSCALLS
	void init_kbdio (int is_a_tty);
	int kill_kbdio (void);
	void _read_keyboard (int *);
#endif

/* tty.c */
void save_tty_state (void);
void restore_tty_state (void);

#endif /* rts.h */
