Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

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