Subversion Repositories shark

Rev

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