/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include "coop.h"

/****************************************************************************/
/*					Library-wise static data								*/
/****************************************************************************/

BZ_STAT	_bz_stat;

/****************************************************************************/
/*					API functions											*/
/****************************************************************************/

/** @addtogroup coop_init */
/** @{ */

/**
*	Initialises the kernel.
*
*	This function should be called from an interrupt context. It never
*	returns to the caller, rather, it starts the first user process in
*	the system. The kernel configuration is passed to this function by
*	a structure, which can be located in a read-only location.
*
*	@param init	Pointer to a BZ_INIT structure that contains the kernel
*				configuration.
*
*	@note
*		The function does not check the validity of the parameters. If
*		you specify, for example, a PID for the first process that is
*		out of the range of the process table, the system will crash.
*		The kernel does some sanity checks when it's running, but it
*		assumes that you do initialise it properly.
*
*	@attention
*		This function must be called from an interrupt context, with
*		interrupts disabled. It never returns but starts the first
*		user process.
*/

void	BZ_Init( const BZ_INIT *init )
{
BZ_STAT		*base;
int			i;

	base = &_bz_stat;

	// IRQ handler and idle function

	base->idle = init->idle;
	base->irqs = init->irqs;

	// Message ports

	base->pnum = init->port_num;
	base->port = init->port_tab;

	for ( i = 0 ; i < init->port_num ; i++ ) {

		base->port[ i ].pid  = 255;
		base->port[ i ].head = 255;
		base->port[ i ].tail = 255;
	}

	// Message pool

	if ( init->mesg_num ) {

		base->pool = init->mesg_tab;
		base->free = 0;

		for ( i = 0 ; i < init->mesg_num - 1 ; i++ )

			base->pool[ i ].link = i + 1;

		base->pool[ i ].link = 255;
	}
	else {

		base->free = 255;
	}

	// Process related

	base->proc = init->proc_tab;
	base->maxp = init->proc_num;
	base->mode = init->schedule;

	for ( i = 0 ; i < init->proc_num ; i++ ) {

		base->proc[ i ].sigs = 0;
		base->proc[ i ].mask = 0;
		base->proc[ i ].cntx = NULL;
	}

	base->self = init->init_pid;
	base->proc[ base->self ].cntx = init->init_stk;

	// Timer related

	base->uptu = 0;
	base->upts = 0;
	base->tptr = NULL;

	// Start the kernel

	_bz_init( init->init, init->init_stk );
}

/** @} */

