Subversion Repositories shark

Rev

Rev 1123 | Rev 1382 | Go to most recent revision | 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
 *
1377 giacomo 37
 * CVS :        $Id: aster.c,v 1.3 2004-04-17 11:36:12 giacomo 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>
80
#include <modules/sem.h>
81
#include <modules/hartport.h>
82
#include <modules/cabs.h>
83
#include <string.h>
84
 
85
#define __VPAGING__
86
 
1377 giacomo 87
#include <drivers/shark_linuxc26.h>
88
#include <drivers/shark_input26.h>
89
#include <drivers/shark_keyb26.h>
90
 
1120 pj 91
#include <drivers/crtwin.h>
92
 
93
int num_aster = 0;
94
#define ASTER_LIM       67
95
 
96
CAB cc;
97
BYTE esc = FALSE;
1377 giacomo 98
PID shutdown_task_PID;
1120 pj 99
 
1377 giacomo 100
int device_drivers_init() {
101
 
102
        KEYB_PARMS kparms = BASE_KEYB;
103
 
104
        LINUXC26_register_module();
105
 
106
        keyb_def_ctrlC(kparms, NULL);
107
 
108
        INPUT26_init();
109
 
110
        KEYB26_init(&kparms);
111
 
112
        return 0;
113
 
114
}
115
 
116
int device_drivers_close() {
117
 
118
        KEYB26_close();
119
 
120
        INPUT26_close();
121
 
122
        return 0;
123
 
124
}
125
 
126
TASK shutdown_task_body(void *arg) {
127
 
128
        device_drivers_close();
129
 
130
        sys_shutdown_message("-- S.Ha.R.K. Closed --\n");
131
 
132
        sys_end();
133
 
134
        return NULL;
135
 
136
}
137
 
138
void set_shutdown_task() {
139
 
140
        NRT_TASK_MODEL nrt;
141
 
142
        nrt_task_default_model(nrt);
143
 
144
        shutdown_task_PID = task_create("Shutdown Task",shutdown_task_body,&nrt,NULL);
145
        if (shutdown_task_PID == NIL) {
146
                sys_shutdown_message("Error: Cannot create shutdown task\n");
147
                sys_end();
148
        }
149
 
150
}
151
 
152
 
1120 pj 153
TASK asteroide(void)
154
{
155
  int i = 1;
156
  int y = rand() % 7 + 1;
157
  while (i < ASTER_LIM) {
158
    puts_xy(i,y,WHITE,"*");
159
    task_endcycle();
160
 
161
    puts_xy(i,y,WHITE," ");
162
    i++;
163
  }
164
  num_aster--;
165
  return 0;
166
}
167
 
168
DWORD taskCreated = 0;
169
 
170
TASK aster(void)
171
{
172
  PID p;
173
  SOFT_TASK_MODEL m_soft;
174
  int r;
175
  WIN w;
176
 
177
  win_init(&w,0,0,ASTER_LIM,8);
178
  win_frame(&w,BLACK,WHITE,"Asteroids",2);
179
 
180
  soft_task_default_model(m_soft);
181
  soft_task_def_met(m_soft,2000);
182
  soft_task_def_ctrl_jet(m_soft);
183
 
184
  srand(7);
185
  while (1) {
186
    if (num_aster < 5) {
187
      r = (rand() % 50) - 25;
188
      soft_task_def_arg(m_soft,(void *)((rand() % 7)+1));
189
      soft_task_def_period(m_soft,(50 + r)*1000);
190
      p = task_create("aaa",asteroide,(TASK_MODEL *)&m_soft,NULL);
191
      taskCreated++;
192
      task_activate(p);
193
      num_aster++;
194
    }
195
 
196
    task_endcycle();
197
  }
198
}
199
 
200
TASK clock()
201
{
202
  WIN w;
203
  int s = 0, m = 0;
204
 
205
  win_init(&w,68,0,11,2);
206
  win_frame(&w,BLACK,WHITE,"Clk",1);
207
 
208
  while(1) {
209
    printf_xy(70,1,WHITE,"%2d : %2d",m,s);
210
    task_endcycle();
211
 
212
    if (++s > 59) {
213
      s = 0;
214
      m++;
215
    }
216
    printf_xy(70,1,WHITE,"%2d : %2d",m,s);
217
    task_endcycle();
218
  }
219
}
220
 
221
TASK title()
222
{
223
  PORT t;
224
  WIN w;
225
  int i,pos = 77;
226
  char msg[85],tmp[85],ss[2];
227
  BYTE c;
228
 
229
  win_init(&w,0,9,79,2);
230
  win_frame(&w,BLACK,WHITE,"Title",2);
231
 
232
  for (i=0; i < 77; i++) msg[i] = ' ';
233
  msg[77] = 0;
234
 
235
  t = port_connect("title",1,STREAM,READ);
236
 
237
  while (1) {
238
    port_receive(t,&c,BLOCK);
239
    ss[0] = c;
240
    ss[1] = 0;
241
    strcat(msg,ss);
242
    puts_xy(1,10,WHITE,msg);
243
    pos++;
244
    if (pos > 77) {
245
      strcpy(tmp,&(msg[1]));
246
      tmp[pos-1] = 0;
247
      pos -= 1;
248
      strcpy(msg,tmp);
249
    }
250
    task_endcycle();
251
  }
252
}
253
 
1377 giacomo 254
#define STR "..................... S.Ha.R.K. ....................."\
1120 pj 255
            "              Guarantees hard tasks                "\
256
            "          Includes soft periodic tasks             "\
257
            "TB server for decrementing the aperiodic response time       "\
258
            "SRP for both hard & soft aperiodic tasks        "\
259
            "Portability toward other compilers/system       "\
260
            "                                                             "\
261
            "Programmers :    Gerardo Lamastra (lamastra@sssup2.sssup.it)    "\
262
            "    Giuseppe Lipari (lipari@sssup2.sssup.it)        "\
263
            "Research coordinator: Giorgio Buttazzo (giorgio@sssup1.sssup.it)"\
264
            "                                                   "\
265
            "                                                   "\
266
            "                                                   "
267
 
268
static char GreetMsg[1600];
269
 
270
TASK put(void)
271
{
272
  PORT p;
273
 
274
  strcpy(GreetMsg,STR);
275
 
276
  p = port_create("title",strlen(GreetMsg),1,STREAM,WRITE);
277
  while(1) {
278
    port_send(p,GreetMsg,BLOCK);
279
    task_endcycle();
280
  }
281
}
282
 
283
TASK ccc(void)
284
{
285
  WIN w;
286
  char *m;
287
 
288
  win_init(&w,68,3,10,3);
289
  win_frame(&w,BLACK,WHITE,"CCC",2);
290
  puts_xy(70,4,WHITE,"Cab");
291
 
292
  while(1) {
293
    m = cab_getmes(cc);
294
    puts_xy(72,5,WHITE,m);
295
    cab_unget(cc,m);
296
    task_endcycle();
297
  }
298
}
299
 
300
 
301
TASK write_keyb()
302
{
303
  BYTE c;
304
  char *msg;
305
 
306
  while (1) {
307
    c = keyb_getchar();
308
    if (c == ESC) {
309
      esc = TRUE;
1123 pj 310
      task_endcycle();
1120 pj 311
    }
312
    else {
313
#ifdef __VPAGING__
314
      if (c == 's') {
315
        if (get_visual_page() == 0) set_visual_page(1);
316
        else if (get_visual_page() == 1) set_visual_page(0);
317
      }
318
#endif
319
      msg = cab_reserve(cc);
320
      msg[0] = c;
321
      msg[1] = 0;
322
      cab_putmes(cc,msg);
323
    }
324
  }
325
}
326
 
327
#define DELTA 200000.0
328
double carico(double rif,BYTE init)
329
{
330
  double i;
331
  DWORD t1 = 0,t2 = 1000;
332
  double u;
333
 
334
  i = 0.0;
335
  do {
336
    i += 1;
337
  } while (i <= DELTA);
338
 
339
  u = i / ((double) (t2 - t1));
340
 
341
  if (init) return u;
342
  else return (1.0 - u/rif);
343
}
344
 
345
int main(int argc, char **argv)
346
{
347
  PID p1,p2,p3,p4,p5,p6;
348
 
349
  HARD_TASK_MODEL m_per;
350
  SOFT_TASK_MODEL m_soft;
351
  NRT_TASK_MODEL m_nrt;
352
 
353
  struct timespec t;
354
 
1377 giacomo 355
  set_shutdown_task();
1120 pj 356
 
1377 giacomo 357
  device_drivers_init();
358
 
1120 pj 359
#ifdef __VPAGING__
360
  set_active_page(1);
361
  set_visual_page(1);
362
#endif
363
 
364
  CRSR_OFF();
365
  clear();
366
  puts_xy(0,20,WHITE,"Press ESC to exit demo.");
367
  cc = cab_create("Cab",2,2);
368
 
369
  soft_task_default_model(m_soft);
370
  soft_task_def_period(m_soft,500000);
371
  soft_task_def_met(m_soft,1000);
372
  soft_task_def_group(m_soft, 1);
373
  p1 = task_create("Aster",aster,&m_soft,NULL);
374
  if (p1 == -1) {
375
    perror("Aster.C(main): Could not create task <aster>");
376
    sys_abort(-1);
377
  }
378
 
379
  hard_task_default_model(m_per);
380
  hard_task_def_mit(m_per,500000);
381
  hard_task_def_wcet(m_per,1000);
382
  hard_task_def_group(m_per, 1);
383
  p2 = task_create("Clock",clock,&m_per,NULL);
384
  if (p2 == -1) {
1377 giacomo 385
    sys_shutdown_message("Aster.C(main): Could not create task <Clock>");
386
    task_activate(shutdown_task_PID);
387
    return 0;
1120 pj 388
  }
389
 
390
  soft_task_def_period(m_soft, 50000);
391
  p3 = task_create("Title",title,&m_soft, NULL);
392
  if (p3 == -1) {
1377 giacomo 393
    sys_shutdown_message("Aster.C(main): Could not create task <Title>");
394
    task_activate(shutdown_task_PID);
395
    return 0;
1120 pj 396
  }
397
 
398
  soft_task_def_period(m_soft, 1000000);
399
  p4 = task_create("Put",put,&m_soft, NULL);
400
  if (p4 == -1) {
1377 giacomo 401
    sys_shutdown_message("Aster.C(main): Could not create task <Put>");
402
    task_activate(shutdown_task_PID);
403
    return 0;
1120 pj 404
  }
405
 
406
  nrt_task_default_model(m_nrt);
407
  nrt_task_def_group(m_nrt, 1);
408
  p5 = task_create("Write",write_keyb,&m_nrt,NULL);
409
  if (p5 == -1) {
1377 giacomo 410
    sys_shutdown_message("Aster.C(main): Could not create task <Write>");
411
    task_activate(shutdown_task_PID);
412
    return 0;
1120 pj 413
  }
414
 
415
  hard_task_def_mit(m_per, 50000);
416
  p6 = task_create("CabTask",ccc,&m_per,NULL);
417
  if (p6 == -1) {
1377 giacomo 418
    sys_shutdown_message("Aster.C(main): Could not create task <CabTask>\n");
419
    task_activate(shutdown_task_PID);
420
    return 0;
1120 pj 421
  }
422
 
423
  group_activate(1);
424
 
425
  while (!esc) {
1123 pj 426
    sys_gettime(&t);
1120 pj 427
    printf_xy(0,21,WHITE,"Clock : %-9ds %-9dns",(int)t.tv_sec, (int)t.tv_nsec);
428
  }
429
 
430
  group_kill(1);
431
  clear();
432
  CRSR_STD();
433
#ifdef __VPAGING__
434
  set_active_page(0);
435
  set_visual_page(0);
436
#endif
1377 giacomo 437
 
438
  task_activate(shutdown_task_PID);
1120 pj 439
  return 0;
1377 giacomo 440
 
1120 pj 441
}
442