/*
    CSC714 Term Project

    Task Scheduling Simulator

    Group Info:

    sykang      Sangyeol    Kang
    kabhavsa    Kinjal      Bhavsar 
*/

#ifndef _SCHED_H_
#define _SCHED_H_

#define TRUE    (1)
#define FALSE  (0)
#define MAX_LEN_STR (0x4F)

#define MAX_TASK       (32)    // Change the number of tasks if more
#define MAX_PRIORITY   (0)

//defining states of a job
#define READY     (1)
#define RUNNING   (2)
#define BLOCKED   (4)
#define OVER      (8)

#define WINDOW	  (1000)


//sys_state.running
#define BUSY         1
#define IDLE         0

typedef enum
{
  RM,
  DM,
  EDF,
} SCHEDULE_n;

typedef struct qnode_t
{
    unsigned int priority;
    unsigned int taskid;
    unsigned int state;
    unsigned int execution_path;
    unsigned int deadline;
    unsigned int flag;
    unsigned int rec_time;
    unsigned int execution_left;
    struct qnode_t *next;
} *qnode;

typedef struct instance
{
    unsigned int F;
    unsigned int state;
    unsigned int arrival;
    unsigned int time_left;
    unsigned int cDeadline;
    unsigned int missDeadline;
} instance_t;

// Structure containing task information
typedef struct taskset
{
    unsigned int ID;
//    unsigned int flag;     //calculate the instant number of job of a task, used by xiaoqing
//    unsigned int delay;    //delay time of a job, used by xiaoqing
    unsigned int F;        //frequency, record from 0, 0 instand of 3.3GHz used by xiaoqing
    unsigned int phase;
    unsigned int period;
//    unsigned int exec;
    unsigned int deadline;
    unsigned int priority;
    unsigned int num_path;
///    unsigned int exe_cases[16][15];

    unsigned int Fexec[16][15];  //used

    instance_t   cInstance;
    struct qnode_t *c;  /////keeps track of jobs  jobs are queued in the order of their arrival
} tasks_t;

typedef struct tasklist
{
    tasks_t task;
    struct tasklist *next;
} task_list;  //added by xiaoqing

//State structure showing system state
typedef struct sys_state
{
    unsigned int sim_time;
    unsigned int sys_time;
    unsigned int num_tasks; //init in parse
    unsigned int sys_prio;
//    unsigned int exe_task;
    unsigned int running;
//    struct qnode_t *cjob;
//    unsigned int hyperperiod;
//    SCHEDULE_n   scheme;
//    char scheme_name[MAX_LEN_STR];
} state_t;


int main(int argc, char *argv[]);
int parseInput(char *fn);
int parseFreq(char *fn);
int verifyInput(void);
int init_simulator(char *fin);
int init_state(void);
void schedule_taskset(void);
int GCD(int a, int b);
int LCM(int a, int b);
void state_handler(void);
void check_deadline(void);
void scheduler(void);
int calcPriority(SCHEDULE_n type);
int calcMPT(void);
int calcWCRT(int taskID);
int genOutput(void);

/*Basic framework*/
void runSimulator(void);
void runScheduler(void);
qnode creatJobOfTask(int i);
void calculatePriorities(void);
int parseFreq(char *fh);
int select_path(int i);

/*QoS*/
int InitQos(void);
int Calculate_miss(void);
int Calculate_rej(void);
int CalculateQos(void);
void UpdateQos(void);



#endif

