/*
 *	occam I/O workspace setup
 *	Copyright (C) 1996-1999 Jim Moores
 *	Modifications (C) 1999-2003 Fred Barnes  <frmb2@ukc.ac.uk>
 *	Based on the KRoC/sparc kernel Copyright (C) 1994-2000 D.C. Wood and P.H. Welch
 *
 *	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.
 */

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

#if defined(OOS_BUILD)

void init_occam_io (void)
{
	return;
}

#else /* !OOS_BUILD */

#include <sys/types.h>

#include <sched_consts.h>
#include <arch/sched_types.h>
#include <arch/sched_asm_inserts.h>
#include <arch/sched.h>

#include <dmem_if.h>


word _kbd_workspace_bottom[13];
word _scr_workspace_bottom[128];
word _err_workspace_bottom[12];


/* pointers to the right offsets into each workspace for convenience */
/* this is slightly different to an assembler label. */
word *kbd_workspace;
word *scr_workspace;
word *err_workspace;


/* the actual locations holding the channel words.  Obviously if the */
/* channel address is needed then the address of (&) operator must be */
/* used */
word *kbd_chan[12];
word *scr_chan[12];
word *err_chan[12];

#ifndef OOS_BUILD
#ifdef BLOCKING_SYSCALLS
word *kbd_termchan;
#endif
#endif	/* !OOS_BUILD */

extern void kroc_screen_jumpcode (void);
extern void kroc_error_jumpcode (void);

#ifndef OOS_BUILD
#ifdef BLOCKING_SYSCALLS
extern void kroc_keyboard_jumpcode (void);
#endif
#endif	/* !OOS_BUILD */


/*
 *	void init_occam_io (int tlpiface)
 *	initialises the occam IO workspaces
 */
void init_occam_io (int tlpiface)
{
	int i;
	int shared_adjustment = sizeof (word);

	#if defined(DM_DEBUG) && (DM_DEBUG == 1)
		extadd_ord_mem (_kbd_workspace_bottom, 13 * sizeof(word), MODE_READ | MODE_WRITE);
		extadd_ord_mem (_scr_workspace_bottom, 128 * sizeof(word), MODE_READ | MODE_WRITE);
		extadd_ord_mem (_err_workspace_bottom, 12 * sizeof(word), MODE_READ | MODE_WRITE);
		extadd_ord_mem (kbd_chan, 8 * sizeof(word), MODE_READ | MODE_WRITE);
		extadd_ord_mem (scr_chan, 8 * sizeof(word), MODE_READ | MODE_WRITE);
		extadd_ord_mem (err_chan, 8 * sizeof(word), MODE_READ | MODE_WRITE);
	#endif

	if (tlpiface & TLP_CTTD) {
		shared_adjustment += (2 * sizeof (word));		/* for the descriptor and ops-hook fields */
	}
	kbd_workspace = &(_kbd_workspace_bottom[8]);
	for (i=0; i<13; i++) {
		_kbd_workspace_bottom[i] = NotProcess_p;
	}
	#if defined(BLOCKING_SYSCALLS) && !defined(OOS_BUILD)
		/* keyboard is handled using blocking system calls */
		_kbd_workspace_bottom[7] = (word)kroc_keyboard_jumpcode;
		/* kbd_workspace points here */
		_kbd_workspace_bottom[8] = 0;
		_kbd_workspace_bottom[9] = (tlpiface & TLP_SHAREDKYB) ? ((word)&kbd_chan) + shared_adjustment : (word)&kbd_chan;
		_kbd_workspace_bottom[10] = (word)&kbd_termchan;
		kbd_termchan = NotProcess_p;
	#endif	/* BLOCKING_SYSCALLS && !OOS_BUILD */

	scr_workspace = &(_scr_workspace_bottom[108]);
	_scr_workspace_bottom[105] = 0;
	_scr_workspace_bottom[106] = (word)NotProcess_p;
	_scr_workspace_bottom[107] = (word)kroc_screen_jumpcode;
	/* this is where scr_workspace points to */
	_scr_workspace_bottom[108] = 0;
	_scr_workspace_bottom[109] = (tlpiface & TLP_SHAREDSCR) ? ((word)&scr_chan) + shared_adjustment : (word)&scr_chan;
	_scr_workspace_bottom[110] = 0;

	err_workspace = &(_err_workspace_bottom[3]);
	_err_workspace_bottom[0] = 0;
	_err_workspace_bottom[1] = (word)NotProcess_p;
	_err_workspace_bottom[2] = (word)kroc_error_jumpcode;
	/* this is where err_workspace points to */
	_err_workspace_bottom[3] = 0;
	_err_workspace_bottom[4] = (tlpiface & TLP_SHAREDERR) ? ((word)&err_chan) + shared_adjustment : (word)&err_chan;
	_err_workspace_bottom[5] = 0;

	kbd_chan[0] = NotProcess_p;
	scr_chan[0] = NotProcess_p;
	err_chan[0] = NotProcess_p;

	/* need to do these carefully -- channel now at offset 1, refcount at offset 0 */

	if (tlpiface & TLP_SHAREDKYB) {
		int v = 0;

		/* input (server) side of this is shared */
		kbd_chan[v++] = (word *)2;				/* ref-count */
		if (tlpiface & TLP_CTTD) {
			kbd_chan[v++] = (word *)0;			/* type-description pointer */
			kbd_chan[v++] = (word *)0;			/* user-operations pointer */
		}
		kbd_chan[v++] = NotProcess_p;				/* chan-word */
		kbd_chan[v++] = (word *)((word)NotProcess_p | 1);	/* client, mixed value/Fptr */
		kbd_chan[v++] = NotProcess_p;				/* client, Bptr */
		kbd_chan[v++] = (word *)((word)NotProcess_p | 1);	/* server, mixed value/Fptr */
		kbd_chan[v++] = NotProcess_p;				/* server, Bptr */
		kbd_chan[v++] = (word *)(0x00020001);			/* KRoC.net special state (server shared not claimed, client unshared) */
		kbd_chan[v++] = (word *)((word)NotProcess_p | 1);	/* state, mixed value/Fptr */
		kbd_chan[v++] = NotProcess_p;				/* state, Bptr */
	}
	if (tlpiface & TLP_SHAREDSCR) {
		int v = 0;

		/* output (client) side of this is shared */
		scr_chan[v++] = (word *)2;				/* ref-count */
		if (tlpiface & TLP_CTTD) {
			scr_chan[v++] = (word *)0;			/* type-description pointer */
			scr_chan[v++] = (word *)0;			/* user-operations pointer */
		}
		scr_chan[v++] = NotProcess_p;				/* chan-word */
		scr_chan[v++] = (word *)((word)NotProcess_p | 1);	/* client, mixed value/Fptr */
		scr_chan[v++] = NotProcess_p;				/* client, Bptr */
		scr_chan[v++] = (word *)((word)NotProcess_p | 1);	/* server, mixed value/Fptr */
		scr_chan[v++] = NotProcess_p;				/* server, Bptr */
		scr_chan[v++] = (word *)(0x00010002);			/* KRoC.net special state (client shared not claimed, server unshared) */
		scr_chan[v++] = (word *)((word)NotProcess_p | 1);	/* state, mixed value/Fptr */
		scr_chan[v++] = NotProcess_p;				/* state, Bptr */
	}
	if (tlpiface & TLP_SHAREDERR) {
		int v = 0;

		/* output (client) side of this is shared */
		err_chan[v++] = (word *)2;				/* ref-count */
		if (tlpiface & TLP_CTTD) {
			err_chan[v++] = (word *)0;			/* type-description pointer */
			err_chan[v++] = (word *)0;			/* user-operations pointer */
		}
		err_chan[v++] = NotProcess_p;				/* chan-word */
		err_chan[v++] = (word *)((word)NotProcess_p | 1);	/* client, mixed value/Fptr */
		err_chan[v++] = NotProcess_p;				/* client, Bptr */
		err_chan[v++] = (word *)((word)NotProcess_p | 1);	/* server, mixed value/Fptr */
		err_chan[v++] = NotProcess_p;				/* server, Bptr */
		err_chan[v++] = (word *)(0x00010002);			/* KRoC.net special state (client shared not claimed, server unshared) */
		err_chan[v++] = (word *)((word)NotProcess_p | 1);	/* state, mixed value/Fptr */
		err_chan[v++] = NotProcess_p;				/* state, Bptr */
	}

	return;
}


/*
 *	int addr_in_kbdproc (word addr)
 *	returns true if `addr' is part of the keyboard process's workspace
 */
int addr_in_kbdproc (word addr)
{
	if ((addr >= (word)_kbd_workspace_bottom) && (addr <= (word)&(_kbd_workspace_bottom[12]))) {
		return 1;
	}
	return 0;
}

#endif	/* !OOS_BUILD */

