#ifndef _PYROS_TASKS
#define _PYROS_TASKS

#include "types.h"
#include "mmanager.h"
#include "timer.h"

#define MAX_TASKS 500
#define TASK_ACTIVE 1
#define TASK_SUSPENDED 2

#define ROOT 1
#define USER 0
struct PROCESS tasks[MAX_TASKS];
unsigned short current_task=0;
unsigned char tasking=0;

void enable_tasking(void)
{
	tasking=1;
}

void disable_tasking(void)
{
	tasking=0;
}

void create_task(void (*task)(),char *name,unsigned char access)
{
	unsigned int temp;
	unsigned short c=0;

	asm("cli");
	while(tasks[c].status!=0)
	{
		c++;
		if(c==MAX_TASKS)
		{
			asm("mov $7, %eax");
			asm("int $0x80");
			return;
		}
	}

	tasks[c].r=kmalloc(0)+0x1000;
	tasks[c].stack=(unsigned int)tasks[c].r;
	tasks[c].r->eax=0;
	tasks[c].r->ebx=0;
	tasks[c].r->ecx=0;
	tasks[c].r->edx=0;
	tasks[c].r->edi=0;
	tasks[c].r->esi=0;
	tasks[c].r->eip=(unsigned int)task;
	tasks[c].r->esp=kmalloc(0)+1000;
	tasks[c].r->ebp=tasks[c].r->esp;
	tasks[c].r->cs=0x08;
	tasks[c].r->fs=0x10;
	tasks[c].r->ds=0x10;
	tasks[c].r->es=0x10;
	tasks[c].r->gs=0x10;
	tasks[c].r->eflags=0x202;
	tasks[c].r->int_no=0;
	tasks[c].r->err_code=0;
	tasks[c].status=TASK_ACTIVE;
	tasks[c].access=access;
	tasks[c].pid=c;
	strcpy(tasks[c].name,name);
	asm("sti");
}

void switch_tasks(struct REGS *r)
{
	if(current_task==MAX_TASKS) current_task=-1;

	if(tasks[current_task++].status!=0)
	{
		tasks[current_task].r->eax=r->eax;
		tasks[current_task].r->ebx=r->ebx;
		tasks[current_task].r->ecx=r->ecx;
		tasks[current_task].r->edx=r->edx;
		tasks[current_task].r->esi=r->esi;
		tasks[current_task].r->edi=r->edi;
		tasks[current_task].r->esp=r->esp;
		tasks[current_task].r->ebp=r->ebp;
		tasks[current_task].r->ss=r->ss;
		tasks[current_task].r->cs=r->cs;
		tasks[current_task].r->es=r->es;
		tasks[current_task].r->ds=r->ds;
		tasks[current_task].r->fs=r->fs;
		tasks[current_task].r->gs=r->gs;
		tasks[current_task].r->eip=r->eip;
		tasks[current_task].r->eflags=r->eflags;
		tasks[current_task].r->int_no=r->int_no;
		tasks[current_task].r->err_code=r->err_code;

		current_task++;
	}
	else
	{
		tasks[current_task].r->eax=r->eax;
		tasks[current_task].r->ebx=r->ebx;
		tasks[current_task].r->ecx=r->ecx;
		tasks[current_task].r->edx=r->edx;
		tasks[current_task].r->esi=r->esi;
		tasks[current_task].r->edi=r->edi;
		tasks[current_task].r->esp=r->esp;
		tasks[current_task].r->ebp=r->ebp;
		tasks[current_task].r->ss=r->ss;
		tasks[current_task].r->cs=r->cs;
		tasks[current_task].r->es=r->es;
		tasks[current_task].r->ds=r->ds;
		tasks[current_task].r->fs=r->fs;
		tasks[current_task].r->gs=r->gs;
		tasks[current_task].r->eip=r->eip;
		tasks[current_task].r->eflags=r->eflags;
		tasks[current_task].r->int_no=r->int_no;
		tasks[current_task].r->err_code=r->err_code;

		current_task++;
		while(tasks[current_task].status==0)
		{
			if(current_task==MAX_TASKS) current_task=0;
			else current_task++;
		}

		current_task++;
	}
	asm("mov %0, %%esp"::"g"(tasks[current_task].stack));
}

short return_pid(void)
{
	return current_task;
}

unsigned char return_access(unsigned short pid)
{
	return tasks[pid].access;
}

char *return_name(unsigned short pid)
{
	return tasks[pid].name;
}
#else
#endif
