/* File "osc_sched.c" copyright (c) 2001 by Collin Lally of Polytech Industries. This software is covered by the GNU General Public License. It is free to the public, and may be modified and/or redistributed at will, as long as all end-products remain under the GPL and are free to the public. */
#include "io.h"
#include "kbd_driver1.h"
#include "sched_osc.h"
#include "video.h"

int *p_stack_ptr[MAX_P_NUM];

int set_stack_ptr(pid_num)
int pid_num;
{
	int *retval;

	*stack_ptr = &process_stack[(int)pid_num][0];
	return retval;
};

int set_p_stack_ptr(pid_num, priority_num)
int *pid_num;
int *priority_num;
{
	int *retval;

	*p_stack_ptr[(int)pid_num] = &process_stack[(int)pid_num][0];
	return retval;
	
};

void set_pid(int *pid, p_type_t *p_type) /* Administrator only */
{
	int *c_pid;

	if(p_type == "kernel"){
		pid = &pid_struct[(int)pid]->pid;
	};
	if(p_type == "k_app"){
		pid = &pid_struct[(int)pid]->pid;
	};
	if(p_type == "usr_apps"){
		pid = &pid_struct[(int)pid]->pid;
	};
	if(p_type == "daemon_app"){
		pid = &pid_struct[(int)pid]->pid;
	};
};

void get_pid(process)
char *process;
{
	int i;
	int *tmp_pid;
	
	while(pid_struct[i]->p_name != (char*)process && i < 50){
		i++;
	};
	if(pid_struct[i]->p_name == (char*)process){
		*tmp_pid = &pid_struct[i]->pid;
		printlong(tmp_pid);
	};
};

inline void long_jmp(int address, int *pid_num, int *priority_num)
{
	set_jmp();
	asm_jmp(address);
	set_stack_ptr(pid_num);
};

inline void set_jmp(pid, stackptr) /* Set an address for return to */
int *pid;
int *stackptr;
{
	asm("push %ax");
	asm("push %dx");
	asm("push %ss");
	asm("push %ds");
	stackptr = jmp_buf->jmp_stack_buf;
	pid = jmp_buf->jmp_pid_buf;
};

void sys_call(call_num, pid)
int *call_num;
int *pid;
{
	int i;
	char c[256];
	
	if(call_num >= 11 || call_num < 0){
		kprint("error: no such system call");
	};
	if(call_num == 01){
		i = 0;
		while((wait_for_keyboard_done1() == 1) && i < 255){
			c[i] = getch();
			i++;
		};
	};
};

void create_process(address_t *ad_space, p_type_t *p_type, stack_ptr_t *stackp_ptr, int pid, int *priority, int *exec_ptr)
{
	int *pre_pid;
	int *pre_stack_ptr;

	ad_space = add_space_s->address_space[(int)pid][0];
	p_type = pid_struct[(int)pid]->p_name;
	stackp_ptr = set_p_stack_ptr(pid, priority);
	set_pid(pid, p_type);
	pre_pid = pid_struct[(int)--pid]->p_id_num;
	pre_stack_ptr = set_p_stack_ptr(pre_pid, priority);
	set_jmp(pre_pid, pre_stack_ptr);
	long_jmp_p(pid, pid, priority, exec_ptr);
};