Blame |
Last modification |
View Log
| RSS feed
/*
* Project: REPPOR
*
*
File: $File$
Last update: $Date: 2004/04/08
------------
**/
/*
* Copyright (C) 2004 Pankaj Arora,Gangadharan & Arjumand Ara.
*
* This program is free software; you can redistribute it and/or modify
* it. This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/*--------------------------------------------------------------*/
/* SIMULATION OF REal time Prey PredatOR--REPPOR */
/*--------------------------------------------------------------*/
#include "datastructures.h"
struct Prey chuha
;
/*--------------------------------------------------------------*/
/* Function to draw predator */
/* Parameters : Old x,y position
New x,y position
Old color
New color
Return : none
/*--------------------------------------------------------------*/
void draw_saamp
(int oldx
, int oldy
,int newx
,int newy
,int oldcolor
, int newcolor
)
{
sem_wait
(&grx_mutex
);
grx_disc
(oldx
, oldy
, RAD
, oldcolor
);
grx_disc
(newx
, newy
, RAD
, newcolor
);
sem_post
(&grx_mutex
);
}
/*--------------------------------------------------------------*/
/* Function to draw prey */
/* Parameters : Old x,y position
New x,y position
Return : none
/*--------------------------------------------------------------*/
void draw_prey
(int oldx
,int oldy
,int newx
, int newy
)
{
sem_wait
(&grx_mutex
);
grx_circle
(oldx
,oldy
,PREY_RAD
, GREEN
);
grx_circle
(newx
, newy
, PREY_RAD
, BLACK
);
sem_post
(&grx_mutex
);
}
/*--------------------------------------------------------------*/
/* Function to genearte a random number between a given range*/
/* Parameters : Minimum of range
Maximun of range
Return : Genearted random number
/*--------------------------------------------------------------*/
int generateRandomNumber
(int minR
, int maxR
)
{
int temp
= rand();
temp
= temp
% (maxR
+1);
while(temp
< minR
)
{
temp
= rand();
temp
= temp
% (maxR
+1);
}
return temp
;
}
/*--------------------------------------------------------------*/
/* Function to implement periodic task JET */
/* Description : It fetches the execution time for the tasks in the system.
Then computes the Mean Execution Time of the Predator Tasks and
displays it along with their corresponding periods.
/*----------------------------------------------------------------------------------------*/
TASK jet_task
(void *arg
){
TIME sum
, max
, curr
;
TIME total
[24];
PID id
;
char *buf
;
char tmp
[48];
int i
=0;
int n
;
int JNTASK
=0;
int JXT
= 480;
int JX
= 535;
int JY
= 310;
int JWIDTH
= 50;
int width
;
int widthperiod
= 0;
int maxperiod
= 80000;
float period
=0;
float met
;
memset(buf
, '\0', sizeof(buf
));
grx_text
("MET PERIOD", JXT
+10, JY
-8, LIGHTCYAN
, BLACK
);
grx_rect
(JXT
-4, JY
-12, JXT
+150, JY
+160,WHITE1
);
while (1){
for (id
=2, JNTASK
=0; id
< MAX_PROC
; id
++){
if (jet_getstat
(id
, &sum
, &max
, &n
, &curr
) != -1)
{
met
= (float) (sum
+curr
)/n
;
buf
= proc_table
[id
].
name;
i
=JNTASK
++;
if (strcmp(buf
,SNAKE_R
) == 0) {
period
= sam_red_period
;
grx_text
(buf
, JXT
, JY
+i
*8, MAGENTA
, BLACK
);
widthperiod
= (int) ( (period
/maxperiod
) *JWIDTH
);
width
= (int) ((float) met
/widthperiod
);
grx_box
(JX
, JY
+i
*8, JX
+widthperiod
, JY
+8+i
*8, LIGHTRED
);
grx_box
(JX
, JY
+i
*8, JX
+width
, JY
+8+i
*8, RED
);
}
else if (strcmp(buf
,SNAKE_B
) == 0) {
period
= sam_blue_period
;
grx_text
(buf
, JXT
, JY
+ i
*8, MAGENTA
, BLACK
);
widthperiod
= (int) ( (period
/maxperiod
) *JWIDTH
);
width
= (int) ((float) met
/widthperiod
);
grx_box
(JX
, JY
+i
*8, JX
+widthperiod
, JY
+8+i
*8, LIGHTBLUE
);
grx_box
(JX
, JY
+i
*8, JX
+width
, JY
+8+i
*8, BLUE
);
}
else if (strcmp(buf
,SNAKE_Y
) == 0) {
period
= sam_yellow_period
;
grx_text
(buf
, JXT
, JY
+i
*8, MAGENTA
, BLACK
);
widthperiod
= (int) ( (period
/maxperiod
) *JWIDTH
);
width
= (int) ((float) met
/widthperiod
);
grx_box
(JX
, JY
+i
*8, JX
+widthperiod
, JY
+8+i
*8, YELLOW
);
grx_box
(JX
, JY
+i
*8, JX
+width
, JY
+8+i
*8, GREEN
);
}
}
}
task_endcycle
();
}
}
/*-----------------------------------------------------------------------------*/
/* Function to implement periodic task BarGraph */
/* Description : Calculates number of preys killed by each(Red/Blue/Yellow)
type of predator and then makes a bar graph presenting score
percentages.
/*------------------------------------------------------------------------------*/
TASK make_bargraph
(void *arg
)
{
int i
;
int x1
,y1
,x2
,y2
;
while(1)
{
for(i
=INITIAl_TASKS
;i
<=MAXTASKS
;i
++)
if(saamps
[i
].
number_of_times_killed>0)
{
if(saamps
[i
].
color==LIGHTRED1
)
red_killed
+= saamps
[i
].
number_of_times_killed;
else
if(saamps
[i
].
color==BLUE1
)
blue_killed
+= saamps
[i
].
number_of_times_killed;
else
if(saamps
[i
].
color==YELLOW1
)
yellow_killed
+= saamps
[i
].
number_of_times_killed;
}
int total_killed
= 0 ;
total_killed
= red_killed
+ blue_killed
+ yellow_killed
;
if(total_killed
>0)
{
x1
= XMAX
+BARSIZE
/2;
x2
= x1
+ 10;
y1
= (YMAX
+50)/2;
y2
= (red_killed
*200)/total_killed
;
grx_box
(x1
,YMENU
+45,x2
,y1
,BLACK1
);
grx_box
(x1
,y1
-y2
,x2
,y1
,LIGHTRED1
);
x1
= XMAX
+3*BARSIZE
/2;
x2
= x1
+ 10;
y2
= (blue_killed
*200)/total_killed
;
grx_box
(x1
,YMENU
+45,x2
,y1
,BLACK1
);
grx_box
(x1
,y1
-y2
,x2
,y1
,BLUE1
);
x1
= XMAX
+5*BARSIZE
/2;
x2
= x1
+ 10;
y2
= (yellow_killed
*200)/total_killed
;
grx_box
(x1
,YMENU
+45,x2
,y1
,BLACK1
);
grx_box
(x1
,y1
-y2
,x2
,y1
,YELLOW1
);
}
red_killed
= blue_killed
= yellow_killed
= 0;
task_endcycle
();
}
}
/*----------------------------------------------------------------------------------------*/
/* Function to implement periodic task Prey */
/* Description : Checks for the presence of prey in the system. If
there is no prey it generates prey at random position
else moves the prey randomly till it is caught(killed) by the predator.
It uses function draw_prey to draw prey at new position.
/*----------------------------------------------------------------------------------------*/
TASK prey
(void *arg
)
{
TIME trand
;
trand
= sys_gettime
(NULL
);
srand(trand
);
while (1)
{
if(chuha.
isAlive == 0)
{
int oldx
,oldy
;
oldx
= chuha.
x;
oldy
= chuha.
y;
chuha.
x = generateRandomNumber
(XMIN
,XMAX
);
chuha.
y = generateRandomNumber
(YMIN
,YMAX
);
draw_prey
(oldx
,oldy
,chuha.
x,chuha.
y);
chuha.
isAlive = 1;
}
else
{
int tx
,ty
;
tx
= chuha.
x;
ty
= chuha.
y;
if((chuha.
x-PREY_RAD
*5>XMIN
) && (chuha.
x+PREY_RAD
*5<XMAX
))
chuha.
x = generateRandomNumber
(chuha.
x-PREY_RAD
*5,chuha.
x+PREY_RAD
*5);
else
if(chuha.
x-PREY_RAD
*5>XMIN
)
chuha.
x = generateRandomNumber
(chuha.
x-PREY_RAD
*5,chuha.
x);
else
if(chuha.
x+PREY_RAD
*5<XMAX
)
chuha.
x = generateRandomNumber
(chuha.
x,chuha.
x+PREY_RAD
*5);
if((chuha.
y-PREY_RAD
*5>YMIN
) && (chuha.
y+PREY_RAD
*5<YMAX
))
chuha.
y = generateRandomNumber
(chuha.
y-PREY_RAD
*5,chuha.
y+PREY_RAD
*5);
else
if(chuha.
y-PREY_RAD
*5>YMIN
)
chuha.
y = generateRandomNumber
(chuha.
y-PREY_RAD
*5,chuha.
y);
else
if(chuha.
y+PREY_RAD
*5<YMAX
)
chuha.
y = generateRandomNumber
(chuha.
y,chuha.
y+PREY_RAD
*5);
draw_prey
(tx
,ty
,chuha.
x,chuha.
y);
}
task_endcycle
();
}
}
/*----------------------------------------------------------------------------------------*/
/* Function to implement task Predator */
/* Parameters : Type of predator */
/* Description : Creates new type(passed as argument)predator in the system. Then continuously
move towards prey.
Predator movement is governed by the fact that it has to avoid collision with
other peers in the prowl.
It uses function draw_saamp to draw predator at new position.
/*----------------------------------------------------------------------------------------*/
TASK saamp
(void *arg
)
{
int i
=(int)arg
;
struct Saamp sam
;
int xs
;
int ys
;
int tempxs
,tempys
;
int random
;
TIME trand
;
trand
= sys_gettime
(NULL
);
srand(trand
);
int ox
,oy
;
int flag
,gk
;
ox
= sam.
x = generateRandomNumber
(XMIN
,XMAX
);
oy
= sam.
y = generateRandomNumber
(YMIN
,YMAX
);
sem_wait
(&grid_mutex
);
grid
[(int)(sam.
y/RATIO
)-MAPPING
][(int)(sam.
x/RATIO
)-MAPPING
];
sem_post
(&grid_mutex
);
draw_saamp
(ox
,oy
,sam.
x,sam.
y,GREEN1
,saamps
[i
].
color);
saamps
[i
].
x = sam.
x;
saamps
[i
].
y = sam.
y;
while (1)
{
tempxs
= abs(sam.
x - chuha.
x);
tempys
= abs(sam.
y - chuha.
y);
if(tempxs
<=(PREY_RAD
+RAD
) && tempys
<=(PREY_RAD
+RAD
))
{
chuha.
isAlive = 0;
saamps
[i
].
number_of_times_killed++;
}
if(chuha.
isAlive == 1)
{
if(tempxs
!=0 || tempys
!=0)
{
if(sam.
x!=chuha.
x)
{
if(sam.
x<chuha.
x)
sam.
x++;
else
sam.
x--;
}
if(sam.
y!=chuha.
y)
{
if(sam.
y<chuha.
y)
sam.
y++;
else
sam.
y--;
}
}
}
sem_wait
(&grid_mutex
);
gk
=grid
[(int)(sam.
y/RATIO
)-MAPPING
][(int)(sam.
x/RATIO
)-MAPPING
];
sem_post
(&grid_mutex
);
if(((int)(sam.
y/RATIO
)-MAPPING
!= (int)(oy
/RATIO
)-MAPPING
) || ((int)(sam.
x/RATIO
)-MAPPING
!= (int)(ox
/RATIO
)-MAPPING
))
{
if(gk
==1)
{
int cnt
=1;
while(gk
==1)
{
if((sam.
x-RATIO
*cnt
>XMIN
) && (sam.
x+RATIO
*cnt
<XMAX
))
sam.
x = generateRandomNumber
(sam.
x-RATIO
*cnt
,sam.
x+RATIO
*cnt
);
else
if(sam.
x-RATIO
*cnt
>XMIN
)
sam.
x = generateRandomNumber
(sam.
x-RATIO
*cnt
,sam.
x);
else
if(sam.
x+RATIO
*cnt
<XMAX
)
sam.
x = generateRandomNumber
(sam.
x,sam.
x+RATIO
*cnt
);
if(sam.
y-RATIO
*cnt
>YMIN
&& sam.
y+RATIO
*cnt
<YMAX
)
sam.
y = generateRandomNumber
(sam.
y-RATIO
*cnt
,sam.
y+RATIO
*cnt
);
else
if(sam.
y-RATIO
*cnt
>YMIN
)
sam.
y = generateRandomNumber
(sam.
y-RATIO
*cnt
,sam.
y);
else
if(sam.
y+RATIO
*cnt
<YMAX
)
sam.
y = generateRandomNumber
(sam.
y,sam.
y+RATIO
*cnt
);
cnt
++;
sem_wait
(&grid_mutex
);
gk
=grid
[(int)(sam.
y/RATIO
)-MAPPING
][(int)(sam.
x/RATIO
)-MAPPING
];
if(gk
==0)
{
grid
[(int)(sam.
y/RATIO
)-MAPPING
][(int)(sam.
x/RATIO
)-MAPPING
]=1;
grid
[(int)(oy
/RATIO
)-MAPPING
][(int)(ox
/RATIO
)-MAPPING
]=0;
draw_saamp
(ox
,oy
,sam.
x,sam.
y,GREEN1
,saamps
[i
].
color);
}
sem_post
(&grid_mutex
);
}
}
else
{
sem_wait
(&grid_mutex
);
grid
[(int)(sam.
y/RATIO
)-MAPPING
][(int)(sam.
x/RATIO
)-MAPPING
]=1;
grid
[(int)(oy
/RATIO
)-MAPPING
][(int)(ox
/RATIO
)-MAPPING
]=0;
draw_saamp
(ox
,oy
,sam.
x,sam.
y,GREEN1
,saamps
[i
].
color);
sem_post
(&grid_mutex
);
}
}
else
{
draw_saamp
(ox
,oy
,sam.
x,sam.
y,GREEN1
,saamps
[i
].
color);
}
saamps
[i
].
x = sam.
x;
saamps
[i
].
y = sam.
y;
ox
= sam.
x;
oy
= sam.
y;
task_endcycle
();
}
}
/*----------------------------------------------------------------------------------------*/
/* Function called before system exits. */
/* Description : Closes the graphical interface and exits using sys_end.*/
/*----------------------------------------------------------------------------------------*/
void ciao
(void)
{
grx_close
();
cputs
("End of REPPOR Simulation");
return;
}
/*----------------------------------------------------------------------------------------*/
/* Function called if Alt-x is pressed. */
/* Description : Closes the graphical interface and exits using sys_end.*/
/*----------------------------------------------------------------------------------------*/
void end
(KEY_EVT
*evt
)
{
grx_close
();
cputs
("End of REPPOR Simulation");
sys_end
();
}
/*----------------------------------------------------------------------------------------*/
/* Function to draw initial grid and scorecard */
/*----------------------------------------------------------------------------------------*/
void make_rect
()
{
grx_box
(XMIN
-RAD
-1, YMIN
-RAD
-1, XMAX
+RAD
+1, YMAX
+RAD
+1, GREEN
);
grx_text
("Simulation of REPPOR", XMAX
+5, YMENU
+8, 13, 0);
grx_text
("R/B/Y create predator" , XMAX
+5, YMENU
+23, 12, 0);
grx_text
("Esc exit to DOS" , XMAX
+5, YMENU
+33, 12, 0);
grx_rect
(XMAX
+YMENU
,YMENU
+43,620,(YMAX
+50)/2,WHITE1
);
grx_text
("Scores(% eaten)",XMAX
+25,(YMAX
+50)/2+15,11,BLACK1
);
grx_text
("RED",XMAX
+BARSIZE
/2,(YMAX
+50)/2+5,LIGHTRED1
,0);
grx_text
("BLUE",XMAX
+3*BARSIZE
/2,(YMAX
+50)/2+5,BLUE1
,0);
grx_text
("YELLOW",XMAX
+5*BARSIZE
/2,(YMAX
+50)/2+5,YELLOW1
,0);
}
/*----------------------------------------------------------------------------------------*/
/* Function to draw one kind of predator */
/*----------------------------------------------------------------------------------------*/
void create_snake_red
(HARD_TASK_MODEL m
)
{
saamps
[number_of_tasks
].
color = LIGHTRED1
;
saamps
[number_of_tasks
].
number_of_times_killed = 0;
hard_task_default_model
(m
);
hard_task_def_ctrl_jet
(m
);
hard_task_def_arg
(m
, (void *)number_of_tasks
);
hard_task_def_wcet
(m
, sam_red_wcet
);
hard_task_def_mit
(m
, sam_red_period
);
hard_task_def_group
(m
, SNAKEGROUP
);
hard_task_def_usemath
(m
);
pid
= task_create
(SNAKE_R
, saamp
, &m
, NULL
);
if (pid
== NIL
) {
grx_close
();
perror("Could not create task <saamp>");
sys_abort
(1);
}
task_activate
(pid
);
number_of_tasks
++;
}
/*----------------------------------------------------------------------------------------*/
/* Function to draw one kind of predator */
/*----------------------------------------------------------------------------------------*/
void create_snake_blue
(HARD_TASK_MODEL m
)
{
saamps
[number_of_tasks
].
color = BLUE1
;
saamps
[number_of_tasks
].
number_of_times_killed = 0;
hard_task_default_model
(m
);
hard_task_def_ctrl_jet
(m
);
hard_task_def_arg
(m
, (void *)number_of_tasks
);
hard_task_def_wcet
(m
, sam_blue_wcet
);
hard_task_def_mit
(m
, sam_blue_period
);
hard_task_def_group
(m
, SNAKEGROUP
);
hard_task_def_usemath
(m
);
pid
= task_create
(SNAKE_B
, saamp
, &m
, NULL
);
if (pid
== NIL
) {
grx_close
();
perror("Could not create task <saamp>");
sys_abort
(1);
}
task_activate
(pid
);
number_of_tasks
++;
}
/*----------------------------------------------------------------------------------------*/
/* Function to draw one kind of predator */
/*----------------------------------------------------------------------------------------*/
void create_snake_yellow
(HARD_TASK_MODEL m
){
saamps
[number_of_tasks
].
color = YELLOW1
;
saamps
[number_of_tasks
].
number_of_times_killed = 0;
hard_task_default_model
(m
);
hard_task_def_ctrl_jet
(m
);
hard_task_def_arg
(m
, (void *)number_of_tasks
);
hard_task_def_wcet
(m
, sam_yellow_wcet
);
hard_task_def_mit
(m
, sam_yellow_period
);
hard_task_def_group
(m
, SNAKEGROUP
);
hard_task_def_usemath
(m
);
pid
= task_create
(SNAKE_Y
, saamp
, &m
, NULL
);
if (pid
== NIL
)
{
grx_close
();
perror("Could not create task <saamp>");
sys_abort
(1);
}
task_activate
(pid
);
number_of_tasks
++;
}
/*--------------------------------------------------------------------------------------------------------*/
/* Function main() */
/* Description : Creates the environment for REPPOR simulation.It creates the playfield
and prey task. It also creates different types of predators based on user input.
It uses make_bargraph() and jet_task() functions to dipaly scorecard and system usage
parameters respectively.*/
/*--------------------------------------------------------------------------------------------------------*/
int main
(int argc
, char *argv
[])
{
HARD_TASK_MODEL m
;
HARD_TASK_MODEL m1
;
SOFT_TASK_MODEL bar
;
SOFT_TASK_MODEL jet
;
KEY_EVT key
;
int modenum
;
char c
;
int maxGridX
=(XMAX
-XMIN
)/RATIO
,maxGridY
=(YMAX
-YMIN
)/RATIO
,i
,j
;
sys_atrunlevel
(ciao
, NULL
, RUNLEVEL_BEFORE_EXIT
);
sem_init
(&grx_mutex
,0,1);
sem_init
(&grid_mutex
,0,1);
for(i
=0;i
<maxGridX
;i
++)
for(j
=0;j
<maxGridY
;j
++)
grid
[i
][j
]=0;
/* graphic card Initialization */
if (grx_init
() < 1) {
sys_abort
(1);
}
if (grx_open
(640, 480, 8) < 0) {
cprintf
("GRX Err\n");
sys_abort
(1);
}
key.
ascii='x';
key.
scan=KEY_X
;
key.
flag=ALTL_BIT
;
keyb_hook
(key
, end
);
//Creation of playfield
make_rect
();
hard_task_default_model
(m1
);
hard_task_def_ctrl_jet
(m1
);
hard_task_def_arg
(m1
, (void *)number_of_tasks
);
hard_task_def_wcet
(m1
, prey_wcet
);
hard_task_def_mit
(m1
, prey_period
);
hard_task_def_usemath
(m1
);
pid
= task_create
("Prey", prey
, &m1
, NULL
);
if (pid
== NIL
)
{
grx_close
();
perror("Could not create task <Prey>");
sys_abort
(1);
}
number_of_tasks
++;
task_activate
(pid
);
//Creation of Bar Graph
soft_task_default_model
(bar
);
soft_task_def_level
(bar
,1);
soft_task_def_ctrl_jet
(bar
);
soft_task_def_arg
(bar
, (void *)0);
soft_task_def_met
(bar
, WCET_BARGRAPH
);
soft_task_def_period
(bar
,PERIOD_BARGRAPH
);
soft_task_def_usemath
(bar
);
pid
= task_create
("BarGraph", make_bargraph
, &bar
, NULL
);
if (pid
!= NIL
)
{
task_activate
(pid
);
number_of_tasks
++;
}
//Creation of jet task
soft_task_default_model
(jet
);
soft_task_def_level
(jet
,1);
soft_task_def_arg
(jet
, (void *) 0);
soft_task_def_met
(jet
, WCET_JET
);
soft_task_def_period
(jet
, PERIOD_JET
);
soft_task_def_usemath
(jet
);
pid
= task_create
("JET", jet_task
, &jet
, NULL
);
if (pid
!= NIL
){
task_activate
(pid
);
number_of_tasks
++;
}
c
= keyb_getch
(BLOCK
);
//Creation of predator tasks
do {
if ((number_of_tasks
<= MAXTASKS
))
switch(c
)
{
case 'r':
case 'R':
create_snake_red
(m
);
break;
case 'b':
case 'B':
create_snake_blue
(m
);
break;
case 'y':
case 'Y':
create_snake_yellow
(m
);
break;
default:
c
= keyb_getch
(BLOCK
);
continue;
}
c
= keyb_getch
(BLOCK
);
} while (c
!= ESC
);
grx_close
();
cputs
("End of REPPOR Simulation");
sys_end
();
return 0;
}