Subversion Repositories shark

Rev

Rev 1547 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1120 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2000 Paolo Gai, Gerardo Lamastra and Giuseppe Lipari
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 *
1552 pj 37
 * CVS :        $Id: aster.c,v 1.6 2005-02-25 11:10:46 pj Exp $
1120 pj 38
 
39
 Author:      Gerardo Lamastra
40
 Giuseppe Lipari
41
 Date:        1/10/96
42
 
43
 File:        Aster.C
44
 Revision:    1.6
45
 
46
*/
47
 
48
/*
49
   Well, this is only a stupid demo which intend to show many
50
   HARTIK+ capabilities; the application is structured in the followig
51
   way: there is an ASTER task wich randomly creates some ASTEROID tasks
52
   which are displayed into the first window; each task is HARD/PERIODIC
53
   and auto-kills itself when it reaches the window end!
54
   An other couple of tasks, TITLE & PUT give an example of port
55
   communication facility; the server task creates the port, the client
56
   task connect to it and uses the server to accomplish some stuff.
57
   Port can be declared READ/WRITE and can model ONE-TO-ONE communication
58
   or MANY-TO-ONE communication.
59
   Finally a second couple of tasks realizes a communiation through CABs;
60
   each time a key is pressed, the ascii code is posted into the CAB by the
61
   CCC task while the second task, WRITE, displays it on the screen and
62
   perform other silly actions.
63
   Finally a CLOCK task is implemented to test system clock.
64
   Please note that usually the HARTIK+ application is made up of a task
65
   group which interacts among them, while the main() function, which
66
   became a task itself when the kernel is activated, is suspended until
67
   the system is ready to terminate; the MAIN task can also be used to make
68
   other background activities, but it should not be killed; when the
69
   application terminates, the control is passed to MAIN which kills
70
   everybody, shut down the system and can handle other operations using
71
   the services available with the previou operating system (I.E. the DOS).
72
   If you need to manage sudden abort/exception you should install your own
73
   exception handler and raise it through the exc_raise() primitive to
74
   make the system abort safely!
75
   Remember that the exit functions posted through sys_atexit() will be
76
   executed in both cases, to allow clean system shutdown.
77
*/
78
 
79
#include <kernel/kern.h>
1552 pj 80
#include <sem/sem/sem.h>
81
#include <hartport/hartport/hartport.h>
82
#include <cabs/cabs/cabs.h>
1120 pj 83
#include <string.h>
84
 
1377 giacomo 85
#include <drivers/shark_keyb26.h>
86
 
1382 giacomo 87
//#define __VPAGING__
88
 
1120 pj 89
#include <drivers/crtwin.h>
90
 
91
int num_aster = 0;
92
#define ASTER_LIM       67
93
 
94
CAB cc;
95
BYTE esc = FALSE;
96
 
97
TASK asteroide(void)
98
{
99
  int i = 1;
100
  int y = rand() % 7 + 1;
101
  while (i < ASTER_LIM) {
102
    puts_xy(i,y,WHITE,"*");
103
    task_endcycle();
104
 
105
    puts_xy(i,y,WHITE," ");
106
    i++;
107
  }
108
  num_aster--;
109
  return 0;
110
}
111
 
112
DWORD taskCreated = 0;
113
 
114
TASK aster(void)
115
{
116
  PID p;
117
  SOFT_TASK_MODEL m_soft;
118
  int r;
119
  WIN w;
120
 
121
  win_init(&w,0,0,ASTER_LIM,8);
122
  win_frame(&w,BLACK,WHITE,"Asteroids",2);
123
 
124
  soft_task_default_model(m_soft);
125
  soft_task_def_met(m_soft,2000);
126
  soft_task_def_ctrl_jet(m_soft);
127
 
128
  srand(7);
129
  while (1) {
130
    if (num_aster < 5) {
131
      r = (rand() % 50) - 25;
132
      soft_task_def_arg(m_soft,(void *)((rand() % 7)+1));
133
      soft_task_def_period(m_soft,(50 + r)*1000);
134
      p = task_create("aaa",asteroide,(TASK_MODEL *)&m_soft,NULL);
135
      taskCreated++;
136
      task_activate(p);
137
      num_aster++;
138
    }
139
 
140
    task_endcycle();
141
  }
142
}
143
 
144
TASK clock()
145
{
146
  WIN w;
147
  int s = 0, m = 0;
148
 
149
  win_init(&w,68,0,11,2);
150
  win_frame(&w,BLACK,WHITE,"Clk",1);
151
 
152
  while(1) {
153
    printf_xy(70,1,WHITE,"%2d : %2d",m,s);
154
    task_endcycle();
155
 
156
    if (++s > 59) {
157
      s = 0;
158
      m++;
159
    }
160
    printf_xy(70,1,WHITE,"%2d : %2d",m,s);
161
    task_endcycle();
162
  }
163
}
164
 
165
TASK title()
166
{
167
  PORT t;
168
  WIN w;
169
  int i,pos = 77;
170
  char msg[85],tmp[85],ss[2];
171
  BYTE c;
172
 
173
  win_init(&w,0,9,79,2);
174
  win_frame(&w,BLACK,WHITE,"Title",2);
175
 
176
  for (i=0; i < 77; i++) msg[i] = ' ';
177
  msg[77] = 0;
178
 
179
  t = port_connect("title",1,STREAM,READ);
180
 
181
  while (1) {
182
    port_receive(t,&c,BLOCK);
183
    ss[0] = c;
184
    ss[1] = 0;
185
    strcat(msg,ss);
186
    puts_xy(1,10,WHITE,msg);
187
    pos++;
188
    if (pos > 77) {
189
      strcpy(tmp,&(msg[1]));
190
      tmp[pos-1] = 0;
191
      pos -= 1;
192
      strcpy(msg,tmp);
193
    }
194
    task_endcycle();
195
  }
196
}
197
 
1377 giacomo 198
#define STR "..................... S.Ha.R.K. ....................."\
1120 pj 199
            "              Guarantees hard tasks                "\
200
            "          Includes soft periodic tasks             "\
201
            "TB server for decrementing the aperiodic response time       "\
202
            "SRP for both hard & soft aperiodic tasks        "\
203
            "Portability toward other compilers/system       "\
204
            "                                                             "\
205
            "Programmers :    Gerardo Lamastra (lamastra@sssup2.sssup.it)    "\
206
            "    Giuseppe Lipari (lipari@sssup2.sssup.it)        "\
207
            "Research coordinator: Giorgio Buttazzo (giorgio@sssup1.sssup.it)"\
208
            "                                                   "\
209
            "                                                   "\
210
            "                                                   "
211
 
212
static char GreetMsg[1600];
213
 
214
TASK put(void)
215
{
216
  PORT p;
217
 
218
  strcpy(GreetMsg,STR);
219
 
220
  p = port_create("title",strlen(GreetMsg),1,STREAM,WRITE);
221
  while(1) {
222
    port_send(p,GreetMsg,BLOCK);
223
    task_endcycle();
224
  }
225
}
226
 
227
TASK ccc(void)
228
{
229
  WIN w;
230
  char *m;
231
 
232
  win_init(&w,68,3,10,3);
233
  win_frame(&w,BLACK,WHITE,"CCC",2);
234
  puts_xy(70,4,WHITE,"Cab");
235
 
236
  while(1) {
237
    m = cab_getmes(cc);
238
    puts_xy(72,5,WHITE,m);
239
    cab_unget(cc,m);
240
    task_endcycle();
241
  }
242
}
243
 
244
 
245
TASK write_keyb()
246
{
247
  BYTE c;
248
  char *msg;
249
 
250
  while (1) {
251
    c = keyb_getchar();
252
    if (c == ESC) {
253
      esc = TRUE;
1123 pj 254
      task_endcycle();
1120 pj 255
    }
256
    else {
257
#ifdef __VPAGING__
258
      if (c == 's') {
259
        if (get_visual_page() == 0) set_visual_page(1);
260
        else if (get_visual_page() == 1) set_visual_page(0);
261
      }
262
#endif
263
      msg = cab_reserve(cc);
264
      msg[0] = c;
265
      msg[1] = 0;
266
      cab_putmes(cc,msg);
267
    }
268
  }
269
}
270
 
271
#define DELTA 200000.0
272
double carico(double rif,BYTE init)
273
{
274
  double i;
275
  DWORD t1 = 0,t2 = 1000;
276
  double u;
277
 
278
  i = 0.0;
279
  do {
280
    i += 1;
281
  } while (i <= DELTA);
282
 
283
  u = i / ((double) (t2 - t1));
284
 
285
  if (init) return u;
286
  else return (1.0 - u/rif);
287
}
288
 
289
int main(int argc, char **argv)
290
{
291
  PID p1,p2,p3,p4,p5,p6;
292
 
293
  HARD_TASK_MODEL m_per;
294
  SOFT_TASK_MODEL m_soft;
295
  NRT_TASK_MODEL m_nrt;
296
 
297
  struct timespec t;
298
 
299
#ifdef __VPAGING__
300
  set_active_page(1);
301
  set_visual_page(1);
302
#endif
303
 
304
  CRSR_OFF();
305
  clear();
306
  puts_xy(0,20,WHITE,"Press ESC to exit demo.");
307
  cc = cab_create("Cab",2,2);
308
 
309
  soft_task_default_model(m_soft);
310
  soft_task_def_period(m_soft,500000);
311
  soft_task_def_met(m_soft,1000);
312
  soft_task_def_group(m_soft, 1);
313
  p1 = task_create("Aster",aster,&m_soft,NULL);
314
  if (p1 == -1) {
315
    perror("Aster.C(main): Could not create task <aster>");
1547 pj 316
    exit(-1);
1120 pj 317
  }
318
 
319
  hard_task_default_model(m_per);
320
  hard_task_def_mit(m_per,500000);
321
  hard_task_def_wcet(m_per,1000);
322
  hard_task_def_group(m_per, 1);
323
  p2 = task_create("Clock",clock,&m_per,NULL);
324
  if (p2 == -1) {
1377 giacomo 325
    sys_shutdown_message("Aster.C(main): Could not create task <Clock>");
1547 pj 326
    exit(1);
1120 pj 327
  }
328
 
329
  soft_task_def_period(m_soft, 50000);
330
  p3 = task_create("Title",title,&m_soft, NULL);
331
  if (p3 == -1) {
1377 giacomo 332
    sys_shutdown_message("Aster.C(main): Could not create task <Title>");
1547 pj 333
    exit(1);
1120 pj 334
  }
335
 
336
  soft_task_def_period(m_soft, 1000000);
337
  p4 = task_create("Put",put,&m_soft, NULL);
338
  if (p4 == -1) {
1377 giacomo 339
    sys_shutdown_message("Aster.C(main): Could not create task <Put>");
1547 pj 340
    exit(1);
1120 pj 341
  }
342
 
343
  nrt_task_default_model(m_nrt);
344
  nrt_task_def_group(m_nrt, 1);
345
  p5 = task_create("Write",write_keyb,&m_nrt,NULL);
346
  if (p5 == -1) {
1377 giacomo 347
    sys_shutdown_message("Aster.C(main): Could not create task <Write>");
1547 pj 348
    exit(1);
1120 pj 349
  }
350
 
351
  hard_task_def_mit(m_per, 50000);
352
  p6 = task_create("CabTask",ccc,&m_per,NULL);
353
  if (p6 == -1) {
1377 giacomo 354
    sys_shutdown_message("Aster.C(main): Could not create task <CabTask>\n");
1547 pj 355
    exit(1);
1120 pj 356
  }
357
 
358
  group_activate(1);
359
 
360
  while (!esc) {
1123 pj 361
    sys_gettime(&t);
1120 pj 362
    printf_xy(0,21,WHITE,"Clock : %-9ds %-9dns",(int)t.tv_sec, (int)t.tv_nsec);
363
  }
364
 
365
  group_kill(1);
366
  clear();
367
  CRSR_STD();
368
#ifdef __VPAGING__
369
  set_active_page(0);
370
  set_visual_page(0);
371
#endif
1377 giacomo 372
 
1547 pj 373
  exit(0);
1382 giacomo 374
 
1120 pj 375
  return 0;
1377 giacomo 376
 
1120 pj 377
}
378