//kernel.c

#include "kernel.h"


// GLOBAL VARIABLE DEFINITIONS
unsigned int current;		   	 		    // current task id number
unsigned long int system_tick;


// task control block
typedef struct task_block {
	 	unsigned char id; 			  		// ID of task
		enum task_state state;				// State
		unsigned char priority;				// Priority
	 	unsigned char *heap_ptr;			// heap addr while not current task
		unsigned int heap_size;				// heap size
		unsigned char *frame_ptr;			// CCR pointer
		};


// resource control block
typedef struct resource_block {
		unsigned char id;	   				// ID of resource
		enum resource_state state;			// State (busy, free...)
		unsigned char user;					// Current resource user/owner
		signed char queue[4];				// Tasks waiting on resource
		unsigned char queue_pos;			// Next free spot in queue
		};

		
struct task_block task[numtasks];
struct resource_block resource[numresources];



// FUNCTIONS

unsigned char get_task_id() { 
		 return task[current].id; 
}

unsigned char get_task_state(unsigned char id) { 
		 return task[id].state; 
}

unsigned char get_task_priority(unsigned char id) { 
		 return task[id].priority; 
}

void set_task_state(unsigned char id, unsigned char state) {
	 task[id].state=state; 
}

void set_task_priority(unsigned char id, unsigned char priority) {

	 if (priority == 0) priority = 1;
	 task[id].priority=priority;
}

