source: soft/giet_vm/xml/xml_parser.c @ 217

Last change on this file since 217 was 217, checked in by alain, 12 years ago

Introducing comments in xml_parser.c

File size: 65.0 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////////
2// File     : xml_parser.c
3// Date     : 14/04/2012
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////////
7// This program translate a "map.xml" source file to a binary file "map.bin" that
8// can be directly loaded in the boot ROM and used by the GIET-VM operating system.
9//
10// This map.xml file contains :
11// 1) the multi-cluster/multi-processors hardware architecture description
12// 2) the various multi-threaded software applications
13// 3) the mapping directives bor both the tasks and the virtual segments.
14// The corresponding C structures are defined in the "mapping_info.h" file.
15//
16// This program also generates the "hard_config.h" and the "giet_vsegs.ld" files,
17// required  to compile the GIET-VM code.
18///////////////////////////////////////////////////////////////////////////////////////
19
20#include  <stdlib.h>
21#include  <fcntl.h>
22#include  <sys/types.h>
23#include  <sys/stat.h>
24#include  <unistd.h>
25#include  <stdio.h>
26#include  <string.h>
27#include  <assert.h>
28#include  <libxml/xmlreader.h>
29#include  <mapping_info.h>
30#include  <irq_handler.h>
31
32#define MAX_CLUSTERS    1024
33#define MAX_PSEGS               4096
34#define MAX_VSPACES             1024
35#define MAX_TASKS               4096
36#define MAX_MWMRS               4096
37#define MAX_VSEGS               4096
38#define MAX_VOBJS               8192
39#define MAX_PROCS               1024
40#define MAX_IRQS                8192
41#define MAX_COPROCS             4096
42#define MAX_CP_PORTS    8192
43#define MAX_PERIPHS             8192
44
45#define XML_PARSER_DEBUG   0
46
47///////////////////////////////////////////////////////////////////////////////////
48//      global variables used to store and index the data structures
49///////////////////////////////////////////////////////////////////////////////////
50
51mapping_header_t*       header;
52mapping_cluster_t*      cluster[MAX_CLUSTERS];  // cluster array
53mapping_pseg_t*         pseg[MAX_PSEGS];                // pseg array
54mapping_vspace_t*       vspace[MAX_VSPACES];    // vspace array
55mapping_vseg_t*         vseg[MAX_VSEGS];                // vseg array
56mapping_vobj_t*         vobj[MAX_VOBJS];                // vobj array
57mapping_task_t*         task[MAX_TASKS];                // task array
58mapping_proc_t*         proc[MAX_PROCS];                // proc array
59mapping_irq_t*          irq[MAX_IRQS];                  // irq array
60mapping_coproc_t*       coproc[MAX_COPROCS];    // coproc array
61mapping_cp_port_t*          cp_port[MAX_CP_PORTS];      // coproc port array
62mapping_periph_t*       periph[MAX_PERIPHS];    // peripheral array
63
64// Index for the various arrays
65
66unsigned int            cluster_index  = 0;
67unsigned int            vspace_index   = 0;
68unsigned int            global_index   = 0;
69unsigned int            pseg_index     = 0;             
70
71unsigned int            proc_index     = 0;
72unsigned int            proc_loc_index = 0;
73
74unsigned int            irq_index      = 0;
75unsigned int            irq_loc_index  = 0;
76
77unsigned int            coproc_index     = 0;
78unsigned int            coproc_loc_index = 0;
79
80unsigned int            cp_port_index     = 0;
81unsigned int            cp_port_loc_index = 0;
82
83unsigned int            periph_index     = 0;
84unsigned int            periph_loc_index = 0;
85
86unsigned int            vseg_index     = 0;
87unsigned int            vseg_loc_index = 0;
88
89unsigned int            task_index     = 0;
90unsigned int            task_loc_index = 0;
91
92unsigned int            vobj_index     = 0;
93unsigned int            vobj_loc_index = 0;
94unsigned int            vobj_count     = 0;
95
96
97/////////////////////////
98// found peripheral
99/////////////////////////
100char found_timer = 0;
101char found_icu = 0;
102char found_xicu = 0;
103char found_dma = 0;
104
105
106//////////////////////////////////
107//needed to generate map_config.ld
108//////////////////////////////////
109unsigned int cluster_y              = 0;
110unsigned int cluster_x              = 0; 
111unsigned int nb_proc_max            = 0; // max number of processors per cluster
112unsigned int nb_timer_channel_max   = 0; // max number of user timer
113unsigned int nb_dma_channel_max     = 0;
114unsigned int nb_tty_channel         = 0;
115unsigned int nb_ioc_channel         = 0;
116unsigned int nb_nic_channel         = 0;
117unsigned int io_mmu_active          = 0;
118unsigned int use_xicu               = 0xFFFFFFFF;
119
120
121//////////////////////////////////
122//needed to generate giet_vseg.ld
123//////////////////////////////////
124
125//kernel and boot code
126unsigned int kernel_code_base    = 0x80000000;  /* kernel code */
127unsigned int kernel_data_base    = 0x80010000;  /* system cacheable data */
128unsigned int kernel_uncdata_base = 0x80080000;  /* system uncacheable data */
129unsigned int kernel_init_base    = 0x80090000;  /* system init entry */ 
130
131unsigned int boot_code_base      = 0xBFC00000;   /* boot code */
132unsigned int boot_stack_base     = 0xBFC08000;  /* boot temporary stack */ 
133unsigned int boot_mapping_base   = 0xBFC0C000;  /* mapping_info blob */
134
135//periphs
136unsigned int tim_base_offset = 0xFFFFFFFF;
137unsigned int tty_base_offset = 0xFFFFFFFF;
138unsigned int dma_base_offset = 0xFFFFFFFF;
139unsigned int ioc_base_offset = 0xFFFFFFFF;
140unsigned int fbf_base_offset = 0xFFFFFFFF;
141unsigned int icu_base_offset = 0xFFFFFFFF;
142unsigned int gcd_base_offset = 0xFFFFFFFF;
143unsigned int iob_base_offset = 0xFFFFFFFF;
144
145
146//////////////////////////////////////////////////////////////////////
147// This arrray is useful to build a temporary list of vobj references.
148// The struct vobj_ref_s is formed by a vspace_name and a vobj_name.
149// This array is used to set the attribute vobj_id of a cp_port
150// once all the vspace have been parsed.
151/////////////////////////////////////////////////////////////////////
152typedef struct vobj_ref_s
153{
154    char vspace_name[32];
155    char vobj_name[32];
156}vobj_ref_t;
157
158vobj_ref_t*             cp_port_vobj_ref[MAX_CP_PORTS]; 
159
160
161//////////////////////////////////////////////////
162unsigned int getIntValue( xmlTextReaderPtr reader, 
163                          const char*      attributeName, 
164                          unsigned int*    ok )
165{
166    unsigned int        value = 0;
167    unsigned int        i;
168    char        c;
169
170    char* string = (char*)xmlTextReaderGetAttribute(reader, (const xmlChar*)attributeName);
171
172    if ( string == NULL )  // missing argument
173    {
174        *ok = 0;
175        return 0;
176    }
177    else
178    {
179        if ( (string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X')) )  // Hexa
180        {
181            for ( i = 2 ; (string[i] != 0) && (i < 10) ; i++ )
182            {
183                c = string[i];
184                if      ((c >= '0') && (c <= '9')) value = (value<<4) + string[i] - 48;
185                else if ((c >= 'a') && (c <= 'f')) value = (value<<4) + string[i] - 87;
186                else if ((c >= 'A') && (c <= 'F')) value = (value<<4) + string[i] - 55;
187                else   
188                {
189                    *ok = 0;
190                    return 0;
191                }
192            }
193        }
194        else                                                            // Decimal
195        {
196            for ( i = 0 ; (string[i] != 0) && (i < 9) ; i++ )
197            {
198                c = string[i];
199                if ((c >= '0') && (c <= '9')) value = (value*10) + string[i] - 48;
200                else 
201                {
202                    *ok = 0;
203                    return 0;
204                }
205            }
206        }
207        *ok = 1;
208        return value; 
209    }
210} // end getIntValue()
211
212///////////////////////////////////////////////
213char* getStringValue ( xmlTextReaderPtr reader, 
214                       const char*      attributeName, 
215                       unsigned int*    ok )
216{
217    char* string = (char*)xmlTextReaderGetAttribute(reader, (const xmlChar*)attributeName);
218
219   
220    if ( string == NULL )  // missing argument
221    {
222        *ok = 0;
223        return NULL;
224    }
225    else
226    {
227        //we read only string smaller than 32 byte
228        if(strlen(string) > 32)
229        {
230            printf("[XML ERROR] all strings must be less than 32 bytes\n");
231            exit(1);
232        }
233
234        *ok = 1;
235        return string;
236    }
237} // end getStringValue()
238
239///////////////////////////////////////
240int getPsegId( unsigned int     cluster_id,
241               char*        pseg_name )
242{
243    unsigned int pseg_id;
244    unsigned int pseg_min = cluster[cluster_id]->pseg_offset;
245    unsigned int pseg_max = pseg_min + cluster[cluster_id]->psegs;
246
247    for ( pseg_id = pseg_min ; pseg_id < pseg_max ; pseg_id++ )
248    {
249        if ( strcmp(pseg[pseg_id]->name, pseg_name) == 0 ) 
250        {
251            return pseg_id;
252        }
253    }
254    return -1;
255}
256
257////////////////////////////////////////////
258int getVspaceId( char*         vspace_name)
259{
260    unsigned int vspace_id;
261
262    for( vspace_id = 0; vspace_id < vspace_index ; vspace_id++)
263    {
264        if( !strcmp(vspace[vspace_id]->name, vspace_name))
265        {
266            return vspace_id;
267        }
268    }
269    return -1;
270}
271
272////////////////////////////////////////////
273int getVobjLocId( unsigned int  vspace_id,
274                  char*             vobj_name,
275                  unsigned int  vspace_max)
276{
277    unsigned int vobj_id;
278    unsigned int vobj_min = vspace[vspace_id]->vobj_offset;
279    unsigned int vobj_max = vobj_min + vspace_max;
280
281    for ( vobj_id = vobj_min ; vobj_id < vobj_max ; vobj_id++ )
282    {
283        if ( strcmp(vobj[vobj_id]->name, vobj_name) == 0 )
284        {
285             return (vobj_id - vobj_min);
286        }
287    }
288    return -1;
289}
290
291/////////////////////////////////////////
292void taskNode ( xmlTextReaderPtr reader )
293/////////////////////////////////////////
294{
295    unsigned int        ok;
296    unsigned int        value;
297    char*               str;
298
299    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
300
301    if ( task_index >= MAX_TASKS )
302    {
303        printf("[XML ERROR] The number of tasks is larger than %d\n", MAX_TASKS);
304    }
305
306#if XML_PARSER_DEBUG
307printf("   task %d\n", task_loc_index);
308#endif
309
310    task[task_index] = (mapping_task_t*)malloc(sizeof(mapping_task_t));
311
312    ////////// get name attribute
313    str = getStringValue(reader, "name", &ok);
314    if ( ok )
315    {
316#if XML_PARSER_DEBUG
317printf("      name = %s\n", str);
318#endif
319        strncpy( task[task_index]->name, str, 31 );
320    }
321    else
322    {
323        printf("[XML ERROR] illegal or missing <name> attribute for task (%d,%d)\n", 
324               vspace_index, task_loc_index);
325        exit(1);
326    }
327
328    ///////// get clusterid attribute
329    value = getIntValue(reader,"clusterid", &ok);
330    if ( ok )
331    {
332#if XML_PARSER_DEBUG
333printf("      clusterid = %x\n", value);
334#endif
335        if ( value >= header->clusters )
336        {
337            printf("[XML ERROR] <clusterid> too large for task (%d,%d)\n",
338                   vspace_index, task_loc_index);
339            exit(1);
340        }
341        task[task_index]->clusterid = value;
342    } 
343    else
344    {
345        printf("[XML ERROR] illegal or missing <clusterid> attribute for task (%d,%d)\n",
346               vspace_index, task_loc_index);
347        exit(1);
348    }
349
350    ////////// get proclocid attribute
351    value = getIntValue(reader,"proclocid", &ok);
352    if ( ok )
353    {
354#if XML_PARSER_DEBUG
355printf("      proclocid = %x\n", value);
356#endif
357        if ( value >= cluster[task[task_index]->clusterid]->procs )
358        {
359            printf("[XML ERROR] <proclocid> too large for task (%d,%d)\n",
360                   vspace_index, task_loc_index);
361            exit(1);
362        }
363        task[task_index]->proclocid = value;
364    } 
365    else
366    {
367        printf("[XML ERROR] illegal or missing <locprocid> attribute for task (%d,%d)\n", 
368                vspace_index, task_loc_index);
369        exit(1);
370    }
371
372    ////////// get stackname attribute
373    str = getStringValue(reader, "stackname" , &ok);
374    if ( ok )
375    {
376        int index = getVobjLocId( vspace_index, str , vobj_loc_index);
377        if ( index >= 0 ) 
378        {
379#if XML_PARSER_DEBUG
380printf("      stackname = %s\n", str);
381printf("      stackid   = %d\n", index);
382#endif
383            task[task_index]->vobjlocid = index;
384        }
385        else             
386        {
387            printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
388                    vspace_index, task_loc_index);
389            exit(1);
390        }
391    } 
392    else
393    {
394        printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
395                vspace_index, task_loc_index);
396        exit(1);
397    }
398
399    ////////// get startid  attribute
400    value = getIntValue(reader,"startid", &ok);
401    if ( ok )
402    {
403#if XML_PARSER_DEBUG
404printf("      startid = %x\n", value);
405#endif
406        task[task_index]->startid = value;
407    } 
408    else
409    {
410        printf("[XML ERROR] illegal or missing <startid> attribute for task (%d,%d)\n", 
411                vspace_index, task_loc_index);
412        exit(1);
413    }
414
415    /////////// get usetty  attribute (optionnal : 0 if missing)
416    value = getIntValue(reader,"usetty", &ok);
417    if ( ok )
418    {
419#if XML_PARSER_DEBUG
420printf("      usetty = %x\n", value);
421#endif
422        task[task_index]->use_tty = value;
423    } 
424    else
425    {
426        task[task_index]->use_tty = 0;
427    }
428
429    /////////// get usenic  attribute (optionnal : 0 if missing)
430    value = getIntValue(reader,"usenic", &ok);
431    if ( ok )
432    {
433#if XML_PARSER_DEBUG
434printf("      usenic = %x\n", value);
435#endif
436        task[task_index]->use_nic = value;
437    } 
438    else
439    {
440        task[task_index]->use_nic = 0;
441    }
442
443    /////////// get usetimer attribute (optionnal : 0 if missing)
444    value = getIntValue(reader,"usetimer", &ok);
445    if ( ok )
446    {
447#if XML_PARSER_DEBUG
448printf("      usetimer = %x\n", value);
449#endif
450        task[task_index]->use_timer = value;
451    } 
452    else
453    {
454        task[task_index]->use_timer = 0;
455    }
456
457    /////////// get usefbdma  attribute (optionnal : 0 if missing)
458    value = getIntValue(reader,"usefbdma", &ok);
459    if ( ok )
460    {
461#if XML_PARSER_DEBUG
462printf("      usefbdma = %x\n", value);
463#endif
464        task[task_index]->use_fbdma = value;
465    } 
466    else
467    {
468        task[task_index]->use_fbdma = 0;
469    }
470
471    task_index++;
472    task_loc_index++;
473} // end taskNode()
474
475//////////////////////////////////////////
476void  vobjNode ( xmlTextReaderPtr reader )
477//////////////////////////////////////////
478{
479    unsigned int        ok;
480    unsigned int        value;
481    char*               str;
482
483    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
484
485    if ( vobj_index >= MAX_VOBJS )
486    {
487        printf("[XML ERROR] The number of vobjs is larger than %d\n", MAX_VSEGS);
488        exit(1);
489    }
490
491#if XML_PARSER_DEBUG
492printf("      vobj %d\n", vobj_loc_index);
493#endif
494
495    vobj[vobj_index] = (mapping_vobj_t*)malloc(sizeof(mapping_vobj_t));
496
497    ///////// get name attribute
498    str = getStringValue(reader, "name", &ok);
499    if ( ok )
500    {
501#if XML_PARSER_DEBUG
502printf("        name = %s\n", str);
503#endif
504        strncpy( vobj[vobj_index]->name, str, 31);
505    }
506    else
507    {
508        printf("[XML ERROR] illegal or missing <name> attribute for vobj (%d,%d)\n", 
509                vseg_index, vobj_loc_index);
510        exit(1);
511    }
512
513    //////// get type attribute
514    str = getStringValue(reader, "type", &ok);
515#if XML_PARSER_DEBUG
516printf("        type = %s\n", str);
517#endif
518    if (ok && (strcmp(str, "ELF") == 0))
519    {
520        vobj[vobj_index]->type = VOBJ_TYPE_ELF;
521
522        //check that this vobj is the first in vseg
523        if(vobj_count != 0) 
524        {
525            printf("[XML ERROR] an ELF vobj must be alone in a vseg (%d,%d)\n", 
526                    vspace_index, vobj_loc_index);
527            exit(1);
528        }
529    }
530    else if (ok && (strcmp(str, "BLOB")    == 0)) vobj[vobj_index]->type = VOBJ_TYPE_BLOB;
531    else if (ok && (strcmp(str, "PTAB")    == 0)) vobj[vobj_index]->type = VOBJ_TYPE_PTAB;
532    else if (ok && (strcmp(str, "PERI")    == 0)) vobj[vobj_index]->type = VOBJ_TYPE_PERI;
533    else if (ok && (strcmp(str, "MWMR")    == 0)) vobj[vobj_index]->type = VOBJ_TYPE_MWMR;
534    else if (ok && (strcmp(str, "LOCK")    == 0)) vobj[vobj_index]->type = VOBJ_TYPE_LOCK;
535    else if (ok && (strcmp(str, "BUFFER")  == 0)) vobj[vobj_index]->type = VOBJ_TYPE_BUFFER;
536    else if (ok && (strcmp(str, "BARRIER") == 0)) vobj[vobj_index]->type = VOBJ_TYPE_BARRIER;
537    else
538    {
539        printf("[XML ERROR] illegal or missing <type> attribute for vobj (%d,%d)\n", 
540                vspace_index, vobj_loc_index);
541        exit(1);
542    }
543
544    ////////// get length attribute
545    value = getIntValue(reader,"length", &ok);
546    if ( ok )
547    {
548#if XML_PARSER_DEBUG
549printf("        length = %d\n", value);
550#endif
551        vobj[vobj_index]->length = value;
552    } 
553    else
554    {
555        printf("[XML ERROR] illegal or missing <length> attribute for vobj (%d,%d)\n", 
556                vspace_index, vobj_loc_index);
557        exit(1);
558    }
559
560    ////////// get align attribute (optional : 0 if missing)
561    value = getIntValue(reader,"align", &ok);
562    if ( ok )
563    {
564#if XML_PARSER_DEBUG
565printf("        align = %d\n", value);
566#endif
567        vobj[vobj_index]->align = value;
568    } 
569    else
570    {
571        vobj[vobj_index]->align = 0;
572    }
573
574    ////////// get binpath attribute (optional : '\0' if missing)
575    str = getStringValue(reader, "binpath", &ok);
576    if ( ok )
577    {
578#if XML_PARSER_DEBUG
579printf("        binpath = %s\n", str);
580#endif
581        strncpy(vobj[vobj_index]->binpath, str, 63);
582    } 
583    else
584    {
585        vobj[vobj_index]->binpath[0] = '\0';
586    }
587   
588    ////////// get init attribute (mandatory for mwmr and barrier)
589    value = getIntValue(reader,"init", &ok);
590    if ( ok )
591    {
592#if XML_PARSER_DEBUG
593printf("        init  = %d\n", value);
594#endif
595        vobj[vobj_index]->init = value;
596    } 
597    else
598    {
599        if( (vobj[vobj_index]->type == VOBJ_TYPE_MWMR) || 
600            (vobj[vobj_index]->type == VOBJ_TYPE_BARRIER) )
601        {
602            printf("[XML ERROR] illegal or missing <value> attribute for vobj (%d,%d). \
603                                        All MWMR or BARRIER vobj must have a init value \n", 
604                    vspace_index, vobj_loc_index);
605            exit(1);
606        }
607        vobj[vobj_index]->init = 0;
608    }
609
610    vobj_index++;
611    vobj_count++;
612    vobj_loc_index++;
613} // end vobjNode()
614
615//////////////////////////////////////////
616void  vsegNode ( xmlTextReaderPtr reader )
617//////////////////////////////////////////
618{
619    unsigned int        ok;
620    unsigned int        value;
621    char*               str;
622
623        vobj_count = 0;
624
625    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
626
627    if ( vseg_index >= MAX_VSEGS )
628    {
629        printf("[XML ERROR] The number of vsegs is larger than %d\n", MAX_VSEGS);
630        exit(1);
631    }
632
633#if XML_PARSER_DEBUG
634printf("    vseg %d\n", vseg_loc_index);
635#endif
636
637    vseg[vseg_index] = (mapping_vseg_t*)malloc(sizeof(mapping_vseg_t));
638   
639    ////////// set vobj_offset attributes
640    vseg[vseg_index]->vobj_offset = vobj_index;
641#if XML_PARSER_DEBUG
642printf("      vobj_offset = %d\n", vobj_index);
643#endif
644
645    ///////// get name attribute
646    str = getStringValue(reader, "name", &ok);
647    if ( ok )
648    {
649#if XML_PARSER_DEBUG
650printf("      name = %s\n", str);
651#endif
652        strncpy( vseg[vseg_index]->name, str, 31);
653    }
654    else
655    {
656        printf("[XML ERROR] illegal or missing <name> attribute for vseg (%d,%d)\n", 
657                vspace_index, vseg_loc_index);
658        exit(1);
659    }
660
661    ////////// get ident attribute (optional : 0 if missing)
662    value = getIntValue(reader,"ident", &ok);
663    if ( ok )
664    {
665#if XML_PARSER_DEBUG
666printf("      ident = %d\n", value);
667#endif
668        vseg[vseg_index]->ident = value;
669    } 
670    else
671    {
672        vseg[vseg_index]->ident = 0;
673    }
674
675    /////////// get vbase attribute
676    value = getIntValue(reader,"vbase", &ok);
677    if ( ok ) 
678    {
679#if XML_PARSER_DEBUG
680printf("      vbase = 0x%x\n", value);
681#endif
682        vseg[vseg_index]->vbase = value;
683    }
684    else
685    {
686        printf("[XML ERROR] illegal or missing <vbase> attribute for vseg (%d,%d)\n", 
687                vspace_index, vseg_loc_index);
688        exit(1);
689    }
690
691    ////////// get clusterid and psegname attributes
692    value = getIntValue(reader,"clusterid", &ok);
693    if ( ok == 0 )
694    {
695        printf("[XML ERROR] illegal or missing <clusterid> for vseg %d\n", 
696                 vseg_loc_index);
697        exit(1);
698    }
699    str = getStringValue(reader,"psegname", &ok);
700    if ( ok == 0 )
701    {
702        printf("[XML ERROR] illegal or missing <psegname> for vseg %d\n", 
703                 vseg_loc_index);
704        exit(1);
705    }
706
707    /////////// set psegid field
708    int index = getPsegId( value, str );
709    if ( index >= 0 ) 
710    {
711#if XML_PARSER_DEBUG
712printf("      clusterid = %d\n", value);
713printf("      psegname  = %s\n", str);
714printf("      psegid    = %d\n", index);
715#endif
716        vseg[vseg_index]->psegid = index;
717    }
718    else             
719    {
720        printf("[XML ERROR] pseg not found for vseg %d / clusterid = %d / psegname = %s\n", 
721                   vseg_loc_index, value, str );
722        exit(1);
723    } 
724
725    //////// get mode attribute
726    str = getStringValue(reader,"mode", &ok);
727#if XML_PARSER_DEBUG
728printf("      mode = %s\n", str);
729#endif
730    if      (ok && (strcmp(str, "CXWU") == 0)) vseg[vseg_index]->mode = 0xF;
731    else if (ok && (strcmp(str, "CXW_") == 0)) vseg[vseg_index]->mode = 0xE;
732    else if (ok && (strcmp(str, "CX_U") == 0)) vseg[vseg_index]->mode = 0xD;
733    else if (ok && (strcmp(str, "CX__") == 0)) vseg[vseg_index]->mode = 0xC;
734    else if (ok && (strcmp(str, "C_WU") == 0)) vseg[vseg_index]->mode = 0xB;
735    else if (ok && (strcmp(str, "C_W_") == 0)) vseg[vseg_index]->mode = 0xA;
736    else if (ok && (strcmp(str, "C__U") == 0)) vseg[vseg_index]->mode = 0x9;
737    else if (ok && (strcmp(str, "C___") == 0)) vseg[vseg_index]->mode = 0x8;
738    else if (ok && (strcmp(str, "_XWU") == 0)) vseg[vseg_index]->mode = 0x7;
739    else if (ok && (strcmp(str, "_XW_") == 0)) vseg[vseg_index]->mode = 0x6;
740    else if (ok && (strcmp(str, "_X_U") == 0)) vseg[vseg_index]->mode = 0x5;
741    else if (ok && (strcmp(str, "_X__") == 0)) vseg[vseg_index]->mode = 0x4;
742    else if (ok && (strcmp(str, "__WU") == 0)) vseg[vseg_index]->mode = 0x3;
743    else if (ok && (strcmp(str, "__W_") == 0)) vseg[vseg_index]->mode = 0x2;
744    else if (ok && (strcmp(str, "___U") == 0)) vseg[vseg_index]->mode = 0x1;
745    else if (ok && (strcmp(str, "____") == 0)) vseg[vseg_index]->mode = 0x0;
746    else
747    {
748        printf("[XML ERROR] illegal or missing <mode> attribute for vseg (%d,%d)\n", 
749                vspace_index, vseg_loc_index);
750        exit(1);
751    }
752   
753    ////////// get vobjs in vseg
754    int status = xmlTextReaderRead(reader);
755    while ( status == 1 ) 
756    {
757        const char* tag = (const char*)xmlTextReaderConstName(reader);
758
759        if      ( strcmp(tag, "vobj")     == 0 ) vobjNode(reader);
760        else if ( strcmp(tag, "#text"  )  == 0 ) { }
761        else if ( strcmp(tag, "#comment") == 0 ) { }
762        else if ( strcmp(tag, "vseg")     == 0 ) 
763        {
764            vseg[vseg_index]->vobjs = vobj_count;
765            vseg_index++;
766            vseg_loc_index++;
767            return;
768        }
769        else 
770        {
771            printf("[XML ERROR] Unknown tag %s",tag);
772            exit(1);
773        }
774        status = xmlTextReaderRead ( reader );
775    }
776} // end vsegNode()
777
778//////////////////////////////////////////
779void vspaceNode( xmlTextReaderPtr reader )
780//////////////////////////////////////////
781{
782    char*               str;
783    unsigned int        ok;
784
785    vobj_loc_index = 0;
786        vseg_loc_index = 0;
787        task_loc_index = 0;
788
789    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
790
791    // checking source file consistency
792    if ( vspace_index >= header->vspaces )
793    {
794        printf("[XML ERROR] The vspace index is too large : %d\n", 
795                 vspace_index);
796        exit(1);
797    }
798
799#if XML_PARSER_DEBUG
800printf("\n  vspace %d\n", vspace_index);
801#endif
802
803    vspace[vspace_index] = (mapping_vspace_t*)malloc(sizeof(mapping_vspace_t));
804
805    ////////// get name attribute
806    str = getStringValue(reader, "name", &ok);
807    if ( ok )
808    {
809#if XML_PARSER_DEBUG
810printf("  name = %s\n", str);
811#endif
812        strncpy(vspace[vspace_index]->name, str, 31);
813    }
814    else
815    {
816        printf("[XML ERROR] illegal or missing <name> attribute for vspace %d\n", 
817                 vspace_index);
818        exit(1);
819    }
820
821    ////////// set vseg_offset and task_offset attributes
822    vspace[vspace_index]->vseg_offset = vseg_index;
823    vspace[vspace_index]->vobj_offset = vobj_index;
824    vspace[vspace_index]->task_offset = task_index;
825   
826#if XML_PARSER_DEBUG
827printf("  vseg_offset = %d\n", vseg_index);
828printf("  vobj_offset = %d\n", vobj_index);
829printf("  task_offset = %d\n", task_index);
830#endif
831
832    ////////// get startname attribute
833    str = getStringValue(reader, "startname", &ok);
834    if ( ok )
835    {
836        //used after parsing the vobjs
837    }
838    else
839    {
840        printf("[XML ERROR] illegal or missing <startname> attribute for vspace %s\n", 
841                 vspace[vspace_index]->name);
842        exit(1);
843    }
844
845    int status = xmlTextReaderRead(reader);
846    while ( status == 1 ) 
847    {
848        const char* tag = (const char*)xmlTextReaderConstName(reader);
849
850        if      ( strcmp(tag, "vseg")     == 0 ) vsegNode(reader);
851        else if ( strcmp(tag, "task")     == 0 ) taskNode(reader);
852        else if ( strcmp(tag, "#text")    == 0 ) { }
853        else if ( strcmp(tag, "#comment") == 0 ) { }
854        else if ( strcmp(tag, "vspace")   == 0 ) 
855        {
856            vspace[vspace_index]->vobjs = vobj_loc_index; 
857            vspace[vspace_index]->tasks = task_loc_index ;
858            vspace[vspace_index]->vsegs = vseg_loc_index ;
859
860            // get index of the vobj containing the start vector
861            int index =  getVobjLocId( vspace_index, str , vobj_loc_index );
862            if(index == -1)
863            {
864                printf("[XML ERROR] vobj containing start vector not found in vspace %s\n",
865                        vspace[vspace_index]->name);
866                exit(-1);
867            }
868            else
869            {
870                vspace[vspace_index]->start_offset = index;
871#if XML_PARSER_DEBUG
872printf("      startname = %s\n", str);
873printf("      startid   = %d\n", index);
874printf("  end vspace %d\n\n", vspace_index);
875#endif
876            }
877
878            // checking startid values for all tasks in vspace
879            int task_id;
880            int task_min = vspace[vspace_index]->task_offset;
881            int task_max = task_min + vspace[vspace_index]->tasks; 
882            for ( task_id = task_min ; task_id < task_max ; task_id++ )
883            {
884                if ( task[task_id]->startid >= vspace[vspace_index]->tasks )
885                {
886                    printf("[XML ERROR] <startid> too large for task (%d,%d)\n", 
887                           vspace_index, task_id );
888                    exit(1);
889                }
890            }
891
892            vspace_index++;
893            return;
894        }
895        else 
896        {
897            printf("[XML ERROR] Unknown tag %s",tag);
898            exit(1);
899        }
900        status = xmlTextReaderRead ( reader );
901    }
902} // end vspaceNode()
903
904///////////////////////////////////////////
905void cpPortNode ( xmlTextReaderPtr reader )
906///////////////////////////////////////////
907{
908    char*           str;
909    unsigned int    ok;
910
911    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
912
913    if ( cp_port_index >= MAX_CP_PORTS )
914    {
915        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", MAX_CP_PORTS);
916    }
917
918#if XML_PARSER_DEBUG
919printf("\n  port %d\n", cp_port_index);
920#endif
921
922    cp_port[cp_port_index] = (mapping_cp_port_t*)malloc(sizeof(mapping_cp_port_t));
923    cp_port_vobj_ref[cp_port_index] = (vobj_ref_t*)malloc(sizeof(vobj_ref_t));
924
925
926   
927    ///////// get direction attribute
928    str = getStringValue( reader, "direction", &ok );
929    if ( ok )
930    {
931#if XML_PARSER_DEBUG
932printf("      direction = %s\n", str);
933#endif
934        if      ( strcmp(str, "TO_COPROC")   ==  0 ) cp_port[cp_port_index]->direction = PORT_TO_COPROC;
935        else if ( strcmp(str, "FROM_COPROC") ==  0 ) cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
936        else
937        {
938            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
939                   cp_port_index, cluster_index);
940            exit(1);
941        }
942    } 
943    else
944    {
945        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
946               cp_port_index, cluster_index);
947        exit(1);
948    }
949   
950    /////////// get vspacename attribute
951    str = getStringValue( reader, "vspacename", &ok );
952#if XML_PARSER_DEBUG
953printf("      vspacename = %s\n", str);
954#endif
955    if ( ok ) 
956    {
957        strncpy(cp_port_vobj_ref[cp_port_index]->vspace_name, str, 31);
958    }
959    else
960    {
961        printf("[XML ERROR] missing <vspacename> for cp_port %d in cluster %d\n",
962               cp_port_index, cluster_index);
963        exit(1);
964    }
965
966    /////////// get vobjname attribute
967    str = getStringValue( reader, "vobjname", &ok );
968#if XML_PARSER_DEBUG
969printf("      vobjname = %s\n", str);
970#endif
971    if ( ok ) 
972    {
973        strncpy(cp_port_vobj_ref[cp_port_index]->vobj_name, str, 31);
974    }
975    else
976    {
977        printf("[XML ERROR] missing <vobjname> for cp_port %d in cluster %d\n",
978               cp_port_index, cluster_index);
979        exit(1);
980    }
981   
982    cp_port_index++;
983    cp_port_loc_index++;
984
985} // end cpPortNode()
986
987///////////////////////////////////////////
988void periphNode ( xmlTextReaderPtr reader )
989///////////////////////////////////////////
990{
991    char*           str;
992    unsigned int    value;
993    unsigned int        ok;
994
995    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
996
997    if ( periph_index >= MAX_PERIPHS )
998    {
999        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
1000    }
1001
1002#if XML_PARSER_DEBUG
1003printf("\n  periph %d\n", periph_index);
1004#endif
1005
1006    periph[periph_index] = (mapping_periph_t*)malloc(sizeof(mapping_periph_t));
1007
1008
1009    ///////// get channels attribute (optionnal : 1 if missing)
1010    value = getIntValue( reader, "channels", &ok );
1011    if ( ok )
1012    {
1013#if XML_PARSER_DEBUG
1014printf("      channels = %d\n", value);
1015#endif
1016        periph[periph_index]->channels = value;
1017    }
1018    else
1019    {
1020        periph[periph_index]->channels = 1;
1021    }
1022
1023    /////////// get psegname attribute
1024    str = getStringValue(reader,"psegname", &ok);
1025    if ( ok == 0 )
1026    {
1027        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1028                 coproc_index, cluster_index);
1029        exit(1);
1030    }
1031
1032    /////////// set psegid attribute
1033    int index = getPsegId( cluster_index, str );
1034    if ( index >= 0 ) 
1035    {
1036#if XML_PARSER_DEBUG
1037printf("      clusterid = %d\n", cluster_index);
1038printf("      psegname  = %s\n", str);
1039printf("      psegid    = %d\n", index);
1040#endif
1041        periph[periph_index]->psegid = index;
1042        assert(pseg[index]->type == PSEG_TYPE_PERI && 
1043        "peripheral psegname attribute must refer to a pseg of type PERI" );
1044    }
1045    else             
1046    {
1047        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
1048                   periph_loc_index, cluster_index, str );
1049        exit(1);
1050    } 
1051
1052
1053    /////////// get type attribute
1054    str = getStringValue( reader, "type", &ok );
1055    if ( ok ) 
1056    {
1057#if XML_PARSER_DEBUG
1058printf("      type     = %s\n", str);
1059#endif
1060        unsigned int error = 0;
1061 
1062        // The TTY, IOC, NIC, FBF and IOB peripherals cannot be replicated
1063        // one per architecture
1064        if      ( strcmp( str, "IOC" ) == 0 ) 
1065        {
1066            periph[periph_index]->type = PERIPH_TYPE_IOC;
1067            if ( header->ioc_clusterid == 0xFFFFFFFF) header->ioc_clusterid = cluster_index;
1068            else  error = 1;
1069
1070            ioc_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1071            nb_ioc_channel = periph[periph_index]->channels;
1072        } 
1073        else if ( strcmp( str, "TTY" ) == 0 )
1074        {
1075            periph[periph_index]->type = PERIPH_TYPE_TTY;
1076            if ( header->tty_clusterid == 0xFFFFFFFF) header->tty_clusterid = cluster_index;
1077            else  error = 1;
1078
1079            tty_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1080            nb_tty_channel = periph[periph_index]->channels;
1081        }
1082        else if ( strcmp( str, "FBF" ) == 0 ) 
1083        {
1084            periph[periph_index]->type = PERIPH_TYPE_FBF;
1085            if ( header->fbf_clusterid == 0xFFFFFFFF) header->fbf_clusterid = cluster_index;
1086            else  error = 1;
1087
1088            fbf_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1089        }
1090        else if ( strcmp( str, "NIC" ) == 0 ) 
1091        {
1092            periph[periph_index]->type = PERIPH_TYPE_NIC;
1093            if ( header->nic_clusterid == 0xFFFFFFFF) header->nic_clusterid = cluster_index;
1094            else  error = 1;
1095
1096            //nic_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1097            nb_nic_channel = periph[periph_index]->channels;
1098        }
1099        else if ( strcmp( str, "IOB" ) == 0 ) 
1100        {
1101            periph[periph_index]->type = PERIPH_TYPE_IOB;
1102            iob_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1103
1104            if(io_mmu_active) error = 1;
1105            io_mmu_active = 1;
1106        }
1107        // The TIM, ICU, XICU, DMA and IOB peripherals can be replicated in several clusters
1108        // one per cluster
1109        else if ( strcmp( str, "TIM" ) == 0 ) 
1110        {
1111            periph[periph_index]->type = PERIPH_TYPE_TIM;
1112            if(found_timer) error = 1;
1113            found_timer = 1;
1114
1115            if(tim_base_offset == 0xFFFFFFFF)
1116                tim_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1117
1118            if(nb_timer_channel_max < periph[periph_index]->channels)
1119                nb_timer_channel_max = periph[periph_index]->channels;
1120        }
1121        else if ( strcmp( str, "ICU" ) == 0 ) 
1122        {
1123            periph[periph_index]->type = PERIPH_TYPE_ICU;
1124            if(found_icu) error = 1;
1125            found_icu = 1;
1126
1127            if(icu_base_offset == 0xFFFFFFFF)
1128                icu_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1129        }
1130        else if ( strcmp( str, "XICU" ) == 0 ) 
1131        {
1132            periph[periph_index]->type = PERIPH_TYPE_XICU;
1133            if(found_xicu) error = 1;
1134            found_xicu = 1;
1135
1136            //'icu' since we can't have both xicu and icu in an arch
1137            if(icu_base_offset == 0xFFFFFFFF)
1138                icu_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1139
1140            if(nb_timer_channel_max == 0)
1141                nb_timer_channel_max = 32;
1142        }
1143        else if ( strcmp( str, "DMA" ) == 0 ) 
1144        {
1145            periph[periph_index]->type = PERIPH_TYPE_DMA;
1146            if(found_dma) error = 1;
1147            found_dma = 1;
1148
1149            if(dma_base_offset == 0xFFFFFFFF)
1150                dma_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1151            if(nb_dma_channel_max < periph[periph_index]->channels)
1152                nb_dma_channel_max = periph[periph_index]->channels;
1153        }
1154        else
1155        {
1156            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1157                   periph_loc_index, cluster_index);
1158            exit(1);
1159        }
1160
1161        if(error)
1162        {
1163            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1164                   periph_loc_index, cluster_index);
1165            exit(1);
1166        }
1167    }
1168    else
1169    {
1170        printf("[XML ERROR] missing <type> for peripheral  %d in cluster %d\n",
1171               periph_loc_index, cluster_index);
1172        exit(1);
1173    }
1174
1175   
1176    periph_index++;
1177    periph_loc_index++;
1178    cluster[cluster_index]->periphs++;
1179
1180} // end periphNode
1181
1182/////////////////////////////////////////
1183void coprocNode ( xmlTextReaderPtr reader )
1184/////////////////////////////////////////
1185{
1186    char*           str;
1187    unsigned int        ok;
1188
1189    cp_port_loc_index = 0;
1190
1191    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1192
1193    if ( coproc_index >= MAX_COPROCS )
1194    {
1195        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
1196    }
1197
1198#if XML_PARSER_DEBUG
1199printf("\n  coproc %d\n", coproc_index);
1200#endif
1201
1202    coproc[coproc_index] = (mapping_coproc_t*)malloc(sizeof(mapping_coproc_t));
1203
1204    /////////// get name attribute
1205    str = getStringValue( reader, "name", &ok );
1206    if ( ok ) 
1207    {
1208#if XML_PARSER_DEBUG
1209printf("      name = %s\n", str);
1210#endif
1211        strncpy(coproc[coproc_index]->name, str, 31);
1212    }
1213    else
1214    {
1215        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
1216               coproc_index, cluster_index);
1217        exit(1);
1218    }
1219
1220    /////////// get psegname attribute
1221    str = getStringValue(reader,"psegname", &ok);
1222    if ( ok == 0 )
1223    {
1224        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1225                 coproc_index, cluster_index);
1226        exit(1);
1227    }
1228
1229    /////////// set psegid attribute
1230    int index = getPsegId( cluster_index, str );
1231    if ( index >= 0 ) 
1232    {
1233#if XML_PARSER_DEBUG
1234printf("      clusterid = %d\n", cluster_index);
1235printf("      psegname  = %s\n", str);
1236printf("      psegid    = %d\n", index);
1237#endif
1238        coproc[coproc_index]->psegid = index;
1239        assert(pseg[index]->type == PSEG_TYPE_PERI && "coproc psegname attribute must refer to a pseg of type PERI" );
1240    }
1241    else             
1242    {
1243        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
1244                   coproc_index, cluster_index, str );
1245        exit(1);
1246    } 
1247
1248    ////////// set port_offset
1249    coproc[coproc_index]->port_offset = cp_port_index;
1250
1251#if XML_PARSER_DEBUG
1252printf("      port_offset = %d\n", cp_port_index);
1253#endif
1254
1255    int status = xmlTextReaderRead(reader);
1256    while ( status == 1 ) 
1257    {
1258        const char* tag = (const char*)xmlTextReaderConstName(reader);
1259
1260        if      ( strcmp(tag, "port")     == 0 ) cpPortNode(reader);
1261        else if ( strcmp(tag, "#text")    == 0 ) { }
1262        else if ( strcmp(tag, "#comment") == 0 ) { }
1263        else if ( strcmp(tag, "coproc")   == 0 ) 
1264        {
1265            coproc[coproc_index]->ports = cp_port_loc_index;
1266            cluster[cluster_index]->coprocs++;
1267            coproc_loc_index++;
1268            coproc_index++;
1269            return;
1270        }
1271        else 
1272        {
1273            printf("[XML ERROR] Unknown tag %s",tag);
1274            exit(1);
1275        }
1276        status = xmlTextReaderRead ( reader );
1277    }
1278} // end coprocNode()
1279
1280///////////////////////////////////////
1281void irqNode( xmlTextReaderPtr reader )
1282///////////////////////////////////////
1283{
1284    unsigned int        ok;
1285    unsigned int        value;
1286    char*               str;
1287
1288    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1289
1290    if ( irq_index >= MAX_IRQS )
1291    {
1292        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
1293    }
1294
1295#if XML_PARSER_DEBUG
1296printf("     irq %d\n", irq_loc_index);
1297#endif
1298
1299    irq[irq_index] = (mapping_irq_t*)malloc(sizeof(mapping_irq_t));
1300
1301    ///////// get type attribute
1302    str = getStringValue(reader,"type", &ok);
1303    if ( ok )
1304    {
1305#if XML_PARSER_DEBUG
1306printf("        type    = %s\n", str);
1307#endif
1308        if      ( strcmp(str, "HARD") == 0 ) irq[irq_index]->type = 0;
1309        else if ( strcmp(str, "SOFT") == 0 ) irq[irq_index]->type = 1;
1310        else
1311        {
1312            printf("[XML ERROR] undefined IRQ  <type> for processor %d in cluster %d\n",
1313                   cluster_index, proc_loc_index );
1314            exit(1);
1315        }
1316    } 
1317    else
1318    {
1319        printf("[XML ERROR] missing IRQ <type> for processor %d in cluster %d\n",
1320               cluster_index, proc_loc_index );
1321        exit(1);
1322    }
1323
1324    ///////// get icuid attribute
1325    value = getIntValue(reader, "icuid", &ok);
1326    if ( ok )
1327    {
1328#if XML_PARSER_DEBUG
1329printf("        icuid   = %d\n", value);
1330#endif
1331        irq[irq_index]->icuid = value;
1332        if ( value >= 32 )
1333        {
1334            printf("[XML ERROR] IRQ <icuid> too large for processor %d in cluster %d\n",
1335                   cluster_index, proc_loc_index );
1336            exit(1);
1337        }
1338    }
1339    else
1340    {
1341        printf("[XML ERROR] missing IRQ <icuid> for processor %d in cluster %d\n",
1342               cluster_index, proc_loc_index );
1343        exit(1);
1344    }
1345
1346    ///////// get isr attribute
1347    str = getStringValue(reader,"isr", &ok);
1348    if ( ok )
1349    {
1350#if XML_PARSER_DEBUG
1351printf("        isr     = %s\n", str);
1352#endif
1353        if      ( strcmp(str, "ISR_SWITCH" ) == 0 ) irq[irq_index]->isr = ISR_SWITCH;
1354        else if ( strcmp(str, "ISR_IOC"    ) == 0 ) irq[irq_index]->isr = ISR_IOC;
1355        else if ( strcmp(str, "ISR_DMA"    ) == 0 ) irq[irq_index]->isr = ISR_DMA;
1356        else if ( strcmp(str, "ISR_TTY"    ) == 0 ) irq[irq_index]->isr = ISR_TTY;
1357        else if ( strcmp(str, "ISR_TIMER"  ) == 0 ) irq[irq_index]->isr = ISR_TIMER;
1358        else
1359        {
1360            printf("[XML ERROR] illegal IRQ <isr> for processor %d in cluster %d\n",
1361                   cluster_index, proc_loc_index );
1362            exit(1);
1363        }
1364#if XML_PARSER_DEBUG
1365printf("        isrnum  = %d\n", irq[irq_index]->isr);
1366#endif
1367    } 
1368    else
1369    {
1370        printf("[XML ERROR] missing IRQ <isr> for processor %d in cluster %d\n",
1371               cluster_index, proc_loc_index );
1372        exit(1);
1373    }
1374
1375    ///////// get channel attribute (optionnal : 0 if missing)
1376    value = getIntValue(reader, "channel", &ok);
1377    if ( ok )
1378    {
1379#if XML_PARSER_DEBUG
1380printf("        channel = %d\n", value);
1381#endif
1382        irq[irq_index]->channel = value;
1383    }
1384    else
1385    {
1386        irq[irq_index]->channel = 0;
1387    }
1388
1389    irq_index++;
1390    irq_loc_index++;
1391
1392} // end irqNode
1393
1394/////////////////////////////////////////
1395void procNode ( xmlTextReaderPtr reader )
1396/////////////////////////////////////////
1397{
1398    unsigned int        ok;
1399    unsigned int    value;
1400
1401    irq_loc_index = 0;
1402
1403    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1404
1405    if ( proc_index >= MAX_PROCS )
1406    {
1407        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1408    }
1409
1410#if XML_PARSER_DEBUG
1411printf("\n  proc %d\n", proc_index);
1412#endif
1413
1414    proc[proc_index] = (mapping_proc_t*)malloc(sizeof(mapping_proc_t));
1415
1416
1417    /////////// get index attribute (optional)
1418    value = getIntValue(reader,"index",&ok);
1419    if ( ok && (value != proc_loc_index) )
1420    {
1421            printf("[XML ERROR] wrong proc index / expected value is %d", 
1422                proc_loc_index);
1423            exit(1);
1424    }
1425
1426    ////////// set irq_offset attribute
1427    proc[proc_index]->irq_offset = irq_index;
1428
1429#if XML_PARSER_DEBUG
1430printf("    irq_offset = %d\n", irq_index);
1431#endif
1432
1433    int status = xmlTextReaderRead(reader);
1434    while ( status == 1 ) 
1435    {
1436        const char* tag = (const char*)xmlTextReaderConstName(reader);
1437
1438        if      ( strcmp(tag, "irq")      == 0 ) irqNode(reader);
1439        else if ( strcmp(tag, "#text")    == 0 ) { }
1440        else if ( strcmp(tag, "#comment") == 0 ) { }
1441        else if ( strcmp(tag, "proc")     == 0 ) 
1442        {
1443            proc[proc_index]->irqs = irq_loc_index;
1444            cluster[cluster_index]->procs++;
1445            proc_loc_index++;
1446            proc_index++;
1447            return;
1448        }
1449        else 
1450        {
1451            printf("[XML ERROR] Unknown tag %s",tag);
1452            exit(1);
1453        }
1454        status = xmlTextReaderRead ( reader );
1455    }
1456} // end procNode()
1457
1458
1459//////////////////////////////////////////
1460void  psegNode ( xmlTextReaderPtr reader )
1461//////////////////////////////////////////
1462{
1463    unsigned int        ok;
1464    unsigned int        value;
1465    char*               str;
1466
1467    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1468
1469    if ( pseg_index >= MAX_PSEGS )
1470    {
1471        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1472        exit(1);
1473    }
1474
1475#if XML_PARSER_DEBUG
1476printf("    pseg %d\n", pseg_index);
1477#endif
1478
1479    pseg[pseg_index] = (mapping_pseg_t*)malloc(sizeof(mapping_pseg_t));
1480
1481    /////// get name attribute
1482    str = getStringValue( reader, "name", &ok );
1483#if XML_PARSER_DEBUG
1484printf("      name = %s\n", str);
1485#endif
1486    if ( ok ) 
1487    {
1488        strncpy(pseg[pseg_index]->name, str, 31);
1489    }
1490    else
1491    {
1492        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1493               pseg_index, cluster_index);
1494        exit(1);
1495    }
1496
1497    //////// get type attribute
1498    str = getStringValue(reader, "type", &ok);
1499#if XML_PARSER_DEBUG
1500printf("      type = %s\n", str);
1501#endif
1502    if      (ok && (strcmp(str, "RAM" ) == 0)) pseg[pseg_index]->type = PSEG_TYPE_RAM;
1503    else if (ok && (strcmp(str, "ROM" ) == 0)) pseg[pseg_index]->type = PSEG_TYPE_ROM;
1504    else if (ok && (strcmp(str, "PERI") == 0)) pseg[pseg_index]->type = PSEG_TYPE_PERI;
1505    else
1506    {
1507        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1508        pseg[pseg_index]->name, cluster_index);
1509        exit(1);
1510    }
1511
1512    //////// get base attribute
1513    value = getIntValue( reader, "base", &ok );
1514#if XML_PARSER_DEBUG
1515printf("      base = 0x%x\n", value);
1516#endif
1517    if ( ok ) 
1518    {
1519        pseg[pseg_index]->base = value;
1520    }
1521    else
1522    {
1523        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1524        pseg[pseg_index]->name, cluster_index);
1525        exit(1);
1526    }
1527
1528    //////// get length attribute
1529    value = getIntValue( reader, "length", &ok );
1530#if XML_PARSER_DEBUG
1531printf("      length = 0x%x\n", value);
1532#endif
1533    if ( ok )
1534    {
1535        pseg[pseg_index]->length = value;
1536    } 
1537    else
1538    {
1539        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1540        pseg[pseg_index]->name, cluster_index);
1541        exit(1);
1542    }
1543
1544    //////// set cluster attribute
1545    pseg[pseg_index]->cluster = cluster_index;
1546
1547    pseg_index++;
1548    cluster[cluster_index]->psegs++;
1549} // end psegNode()
1550
1551/////////////////////////////////////////////
1552void  clusterNode ( xmlTextReaderPtr reader )
1553/////////////////////////////////////////////
1554{
1555    unsigned int ok;
1556    unsigned int value;
1557
1558    cluster[cluster_index] = (mapping_cluster_t*)malloc(sizeof(mapping_cluster_t));
1559   
1560    //initialise all variables
1561    //they will be incremented by *Node() functions
1562    //FIXME: calloc?
1563    cluster[cluster_index]->psegs = 0;
1564    cluster[cluster_index]->procs = 0;
1565    cluster[cluster_index]->coprocs = 0;
1566    cluster[cluster_index]->periphs = 0;
1567
1568
1569    //initialise global variables
1570    //TODO: delete those three
1571    proc_loc_index = 0;
1572    coproc_loc_index = 0;
1573    periph_loc_index = 0;
1574
1575    // for replicated periph
1576    found_timer = 0;
1577    found_icu = 0;
1578    found_xicu = 0;
1579    found_dma = 0;
1580
1581    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1582
1583    // checking source file consistency
1584    if ( cluster_index >= header->clusters )
1585    {
1586        printf("[XML ERROR] The cluster index is too large : %d\n", cluster_index);
1587        exit(1);
1588    }
1589
1590#if XML_PARSER_DEBUG
1591printf("  cluster %d\n", cluster_index);
1592#endif
1593
1594
1595    /////////// check cluster index attribute (optional)
1596    value = getIntValue(reader,"index",&ok);
1597    if ( ok && (value != cluster_index) )
1598    {
1599            printf("[XML ERROR] wrong cluster index / expected value is %d", 
1600                cluster_index);
1601            exit(1);
1602    }
1603
1604    ////////// set offsets
1605    cluster[cluster_index]->pseg_offset   = pseg_index;
1606    cluster[cluster_index]->proc_offset   = proc_index;
1607    cluster[cluster_index]->coproc_offset = coproc_index;
1608    cluster[cluster_index]->periph_offset = periph_index;
1609
1610#if XML_PARSER_DEBUG
1611printf("    pseg_offset   = %d\n", pseg_index);
1612printf("    proc_offset   = %d\n", proc_index);
1613printf("    coproc_offset = %d\n", coproc_index);
1614printf("    periph_offset = %d\n", coproc_index);
1615#endif
1616
1617    ////////// get psegs, procs, coprocs and periphs
1618    int status = xmlTextReaderRead(reader);
1619
1620    while ( status == 1 )
1621    {
1622        const char* tag = (const char*)xmlTextReaderConstName(reader);
1623
1624        if      ( strcmp(tag, "pseg")     == 0 ) psegNode(reader);
1625        else if ( strcmp(tag, "proc")     == 0 ) procNode(reader);
1626        else if ( strcmp(tag, "coproc")   == 0 ) coprocNode(reader);
1627        else if ( strcmp(tag, "periph")   == 0 ) periphNode(reader);
1628        else if ( strcmp(tag, "#text")    == 0 ) { }
1629        else if ( strcmp(tag, "#comment") == 0 ) { }
1630        else if ( strcmp(tag, "cluster")  == 0 ) 
1631        {
1632
1633            if(use_xicu == 0xFFFFFFFF)
1634                use_xicu = found_xicu;
1635
1636            ////////////////// peripherals checks ////////////////////
1637            if( (found_timer  && use_xicu) || (!found_timer  && !use_xicu) )
1638            {
1639                printf("[XML ERROR] illegal or missing timer peripheral in cluster %d\n", cluster_index);
1640                exit(1);
1641            }
1642
1643            if( (found_icu  && use_xicu) || (!found_icu  && !use_xicu) )
1644            {
1645                printf("[XML ERROR] illegal or missing icu peripheral in cluster %d\n", cluster_index);
1646                exit(1);
1647            }
1648
1649            if( !found_xicu && use_xicu)
1650            {
1651                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1652                exit(1);
1653            }
1654
1655            if(!found_dma)
1656            {
1657                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1658                exit(1);
1659            }
1660
1661               
1662            if(nb_proc_max < cluster[cluster_index]->procs)
1663                nb_proc_max = cluster[cluster_index]->procs;
1664
1665#if XML_PARSER_DEBUG
1666printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1667printf("    procs   = %d\n", cluster[cluster_index]->procs);
1668printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
1669printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1670printf("    end cluster %d\n", cluster_index);
1671#endif
1672            cluster_index++;
1673            return;
1674        }
1675        status = xmlTextReaderRead(reader);
1676    }
1677} // end clusterNode()
1678
1679//////////////////////////////////////////////
1680void clusterSetNode( xmlTextReaderPtr reader )
1681//////////////////////////////////////////////
1682{
1683    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1684
1685#if XML_PARSER_DEBUG
1686printf("\n  clusters set\n");
1687#endif
1688
1689    int status = xmlTextReaderRead(reader);
1690    while ( status == 1 ) 
1691    {
1692        const char* tag = (const char*)xmlTextReaderConstName(reader);
1693
1694        if      ( strcmp(tag, "cluster")    == 0 ) clusterNode(reader);
1695        else if ( strcmp(tag, "#text")      == 0 ) { }
1696        else if ( strcmp(tag, "#comment")   == 0 ) { }
1697        else if ( strcmp(tag, "clusterset") == 0 ) 
1698        {
1699            // checking source file consistency
1700            if ( cluster_index != header->clusters )
1701            {
1702                printf("[XML ERROR] Wrong number of clusters\n");
1703                exit(1);
1704            }
1705
1706            if(header->tty_clusterid == 0xFFFFFFFF)
1707            {
1708                printf("[XML ERROR] illegal or missing tty peripheral");
1709                exit(1);
1710            }
1711
1712#if XML_PARSER_DEBUG
1713printf("  end cluster set\n\n");
1714#endif
1715            header->psegs    = pseg_index;
1716            header->procs    = proc_index;
1717            header->irqs     = irq_index;
1718            header->coprocs  = coproc_index;
1719            header->cp_ports = cp_port_index;
1720            return;
1721        }
1722        else 
1723        {
1724            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1725            exit(1);
1726        }
1727        status = xmlTextReaderRead(reader);
1728    }
1729} // end clusterSetNode()
1730
1731/////////////////////////////////////////////
1732void globalSetNode( xmlTextReaderPtr reader )
1733/////////////////////////////////////////////
1734{
1735    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1736
1737#if XML_PARSER_DEBUG
1738printf("  globals set\n");
1739#endif
1740
1741    int status = xmlTextReaderRead(reader);
1742    while ( status == 1 ) 
1743    {
1744        const char* tag = (const char*)xmlTextReaderConstName(reader);
1745
1746        if      ( strcmp(tag, "vseg")      == 0 ) vsegNode(reader);
1747        else if ( strcmp(tag, "#text"  )   == 0 ) { }
1748        else if ( strcmp(tag, "#comment")  == 0 ) { }
1749        else if ( strcmp(tag, "globalset") == 0 ) 
1750        {
1751#if XML_PARSER_DEBUG
1752printf("  end global set\n\n");
1753#endif
1754            header->globals = vseg_index;
1755            vseg_loc_index = 0;
1756            return;
1757        }
1758        else 
1759        {
1760            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1761            exit(1);
1762        }
1763        status = xmlTextReaderRead ( reader );
1764    }
1765} // end globalSetNode()
1766
1767/////////////////////////////////////////////
1768void vspaceSetNode( xmlTextReaderPtr reader )
1769/////////////////////////////////////////////
1770{
1771    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1772
1773#if XML_PARSER_DEBUG
1774printf("\n  vspaces set\n");
1775#endif
1776
1777    int status = xmlTextReaderRead ( reader );
1778    while ( status == 1 ) 
1779    {
1780        const char* tag = (const char*)xmlTextReaderConstName(reader);
1781
1782        if      ( strcmp(tag, "vspace"   ) == 0 ) vspaceNode(reader);
1783        else if ( strcmp(tag, "#text"    ) == 0 ) { }
1784        else if ( strcmp(tag, "#comment" ) == 0 ) { }
1785        else if ( strcmp(tag, "vspaceset") == 0 ) 
1786        {
1787            // checking source file consistency
1788            if ( vspace_index != header->vspaces )
1789            {
1790                printf("[XML ERROR] Wrong number of vspaces\n");
1791                exit(1);
1792            }
1793            else
1794            {
1795                header->vsegs = vseg_index;
1796                header->vobjs = vobj_index;
1797                header->tasks = task_index;
1798                return;
1799            }
1800        }
1801        else 
1802        {
1803            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1804            exit(1);
1805        }
1806        status = xmlTextReaderRead ( reader );
1807    }
1808} // end globalSetNode()
1809
1810//////////////////////////////////////////
1811void headerNode(xmlTextReaderPtr reader )
1812//////////////////////////////////////////
1813{
1814    char*               name;
1815    unsigned int        value;
1816    unsigned int        ok;
1817
1818    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1819
1820#if XML_PARSER_DEBUG
1821printf("mapping_info\n");
1822#endif
1823
1824    header = (mapping_header_t*)malloc(sizeof(mapping_header_t));
1825
1826    ////////// get name attribute
1827    name = getStringValue(reader, "name", &ok);
1828    if ( ok )
1829    {
1830#if XML_PARSER_DEBUG
1831printf("  name = %s\n", name);
1832#endif
1833        strncpy( header->name, name, 31);
1834    }     
1835    else
1836    {
1837        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1838        exit(1);
1839    }
1840
1841    /////////// get cluster_x attribute
1842    cluster_x = getIntValue(reader, "cluster_x", &ok);
1843    if ( ok )
1844    {
1845#if XML_PARSER_DEBUG
1846printf("  cluster_x = %d\n", cluster_x);
1847#endif
1848        header->cluster_x = cluster_x;
1849    }
1850    else
1851    {
1852        printf("[XML ERROR] illegal or missing <cluster_x> attribute in header\n");
1853        exit(1);
1854    }
1855
1856    /////////// get cluster_y attribute
1857    cluster_y = getIntValue(reader, "cluster_y", &ok);
1858    if ( ok )
1859    {
1860#if XML_PARSER_DEBUG
1861printf("  cluster_y = %d\n", cluster_y);
1862#endif
1863        header->cluster_y = cluster_y;
1864    }
1865    else
1866    {
1867        printf("[XML ERROR] illegal or missing <cluster_y> attribute in header\n");
1868        exit(1);
1869    }
1870
1871    //check the number of cluster
1872    value = cluster_x*cluster_y;
1873    if ( value >= MAX_CLUSTERS )
1874    {
1875        printf("[XML ERROR] The number of clusters is larger than %d\n", MAX_CLUSTERS);
1876        exit(1);
1877    }
1878       
1879    header->clusters  = value;
1880
1881#if XML_PARSER_DEBUG
1882printf("  clusters = %d\n", value);
1883#endif
1884
1885    ///////// get vspaces attribute
1886    value = getIntValue(reader, "vspaces", &ok);
1887    if ( ok )
1888    {
1889        if ( value >= MAX_VSPACES )
1890        {
1891            printf("[XML ERROR] The number of vspaces is larger than %d\n", MAX_VSPACES);
1892            exit(1);
1893        }
1894#if XML_PARSER_DEBUG
1895printf("  vspaces = %d\n", value);
1896#endif
1897        header->vspaces  = value;
1898    }
1899    else
1900    {
1901        printf("[XML ERROR] illegal or missing <vspaces> attribute in mapping\n");
1902        exit(1);
1903    }
1904
1905    //////// initialise non replicated peripherals cluster_id
1906    header->tty_clusterid = 0xFFFFFFFF;
1907    header->nic_clusterid = 0xFFFFFFFF;
1908    header->ioc_clusterid = 0xFFFFFFFF;
1909    header->fbf_clusterid = 0xFFFFFFFF;
1910
1911    ///////// set signature
1912    header->signature = IN_MAPPING_SIGNATURE;
1913
1914    int status = xmlTextReaderRead(reader);
1915    while ( status == 1 ) 
1916    {
1917        const char* tag = (const char*)xmlTextReaderConstName(reader);
1918
1919        if      ( strcmp(tag, "clusterset")   == 0 ) clusterSetNode(reader);
1920        else if ( strcmp(tag, "globalset")    == 0 ) globalSetNode(reader);
1921        else if ( strcmp(tag, "vspaceset")    == 0 ) vspaceSetNode(reader);
1922        else if ( strcmp(tag, "#text")        == 0 ) { }
1923        else if ( strcmp(tag, "#comment")     == 0 ) { }
1924        else if ( strcmp(tag, "mapping_info") == 0 ) 
1925        {
1926#if XML_PARSER_DEBUG
1927printf("end mapping_info\n");
1928#endif
1929            return;
1930        }
1931        else 
1932        {
1933            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1934            exit(1);
1935        }
1936        status = xmlTextReaderRead(reader);
1937    }
1938} // end headerNode()
1939   
1940///////////////////////////////////////
1941void BuildTable( int fdout,
1942               const char* type,
1943               unsigned int nb_elem,
1944               unsigned int elem_size,
1945               char** table)       
1946////////////////////////////////////////
1947{
1948    unsigned int i;
1949    // write element
1950    for ( i = 0 ; i < nb_elem ; i++ )
1951    {
1952        if(elem_size != write(fdout, table[i], elem_size))
1953        {
1954            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
1955            exit(1);
1956        }
1957
1958#if XML_PARSER_DEBUG
1959printf("Building binary: writing %s %d\n", type, i);
1960#endif
1961    }
1962}
1963
1964int open_file(const char* file_path)
1965{
1966
1967    //open file
1968    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
1969    if ( fdout < 0)
1970    {
1971        perror("open");
1972        exit(1);
1973    }
1974
1975    //reinitialise the file
1976    if( ftruncate(fdout, 0) )
1977    {
1978        perror("truncate");
1979        exit(1);
1980    }
1981
1982//#if XML_PARSER_DEBUG
1983    printf("%s\n",file_path);
1984//#endif
1985
1986    return fdout;
1987}
1988
1989
1990///////////////////////////
1991void  buildBin( const char* file_path )
1992///////////////////////////
1993{
1994    unsigned int    length;
1995
1996    int fdout = open_file(file_path);
1997
1998    // write header to binary file
1999    length = write(fdout, (char*)header, sizeof(mapping_header_t));
2000    if ( length != sizeof(mapping_header_t) )
2001    {
2002        printf("write header error : length = %d \n", length);
2003        exit(1);
2004    }
2005
2006    // write clusters
2007    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char**) cluster);
2008    // write psegs
2009    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char**) pseg);
2010    // write vspaces
2011    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char**) vspace);
2012    // write vsegs
2013    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char**) vseg);
2014    // write vobjs
2015    BuildTable(fdout, "vobj", vobj_index, sizeof(mapping_vobj_t), (char**) vobj);
2016    // write tasks array
2017    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char**) task);
2018    //building procs array
2019    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char**) proc);
2020    //building irqs array
2021    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char**)irq);
2022    //building coprocs array
2023    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char**)coproc);
2024    //building cp_ports array
2025    BuildTable(fdout, "cp_port", cp_port_index, sizeof(mapping_cp_port_t),(char**) cp_port);
2026    //building periphs array
2027    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char**)periph);
2028   
2029    close(fdout);
2030
2031} // end buildBin()
2032
2033///////////////////////////////////////////////////////////////////////
2034// this function set the value the vobj_id fiels of all cp_ports
2035///////////////////////////////////////////////////////////////////////
2036void prepareBuild()
2037{
2038    unsigned int i;
2039    //asign for all cp_ports the correct vspaceid and vobjid
2040    for(i=0; i< cp_port_index; i++)
2041    {
2042        int vspace_id = getVspaceId( cp_port_vobj_ref[i]->vspace_name );
2043        if ( vspace_id < 0 ) 
2044        {
2045            printf("[XML ERROR] illegal  <vspacename> for cp_port %d,\n", 
2046                    i);
2047            exit(1);
2048        }
2049        cp_port[i]->vspaceid = vspace_id;
2050       
2051        int vobj_id = getVobjLocId( vspace_id, 
2052                                    cp_port_vobj_ref[i]->vobj_name, 
2053                                    vspace[vspace_id]->vobjs );
2054        if ( vobj_id >= 0 ) 
2055        {
2056
2057#if XML_PARSER_DEBUG
2058printf("\ncp_port = %d\n", i);
2059printf("      vspace_name  = %s\n", cp_port_vobj_ref[i]->vspace_name);
2060printf("      vobj_name    = %s\n", cp_port_vobj_ref[i]->vobj_name);
2061printf("      vobj_index   = %d\n", vobj_id);
2062#endif
2063            cp_port[i]->vobjlocid = vobj_id;
2064
2065            assert((vobj[ vspace[vspace_id]->vobj_offset + vobj_id]->type == VOBJ_TYPE_MWMR)
2066            && "coproc ports has to refere to a vobj of type MWMR");
2067        }
2068        else             
2069        {
2070            printf("[XML ERROR] illegal  <vobjname> for cp_port %d,\n", 
2071                    i);
2072            exit(1);
2073        }
2074    }
2075}
2076
2077//////////////////////////////////////////
2078void file_write(int fdout, char* towrite)
2079{
2080    unsigned int size = strlen(towrite);
2081    if(size != write(fdout, towrite, size))
2082    {
2083        printf("file_write error");
2084        exit(1);
2085    }
2086}
2087
2088//////////////////////////////////////////////////
2089void def_int_write(int fdout, char* def, int num)
2090{
2091    char  buf[64];
2092    sprintf(buf, "#define\t %s  %d\n", def, num);
2093    file_write(fdout, buf);
2094}
2095
2096/////////////////////////////////////////////////
2097void def_hex_write(int fdout, char* def, int num)
2098{
2099    char  buf[64];
2100    sprintf(buf, "#define\t %s  0x%x\n", def, num);
2101    file_write(fdout, buf);
2102}
2103
2104///////////////////////////////////////
2105void  genHd( const char* file_path )
2106///////////////////////////////////////
2107{
2108    int fdout = open_file(file_path);
2109
2110    char* prol = " /* Generated from the mapping_info file */\n\n#ifndef _HD_CONFIG_H\n#define _HD_CONFIG_H\n\n";
2111    file_write(fdout, prol);
2112
2113    def_int_write(fdout, "CLUSTER_X"       , cluster_x);
2114    def_int_write(fdout, "CLUSTER_Y"       , cluster_y);
2115    def_int_write(fdout, "NB_CLUSTERS"     , cluster_index);
2116    def_hex_write(fdout, "CLUSTER_SIZE"    , ( ((unsigned long)1) << 32)/cluster_index);
2117    def_int_write(fdout, "NB_PROCS_MAX"    , nb_proc_max);
2118    def_int_write(fdout, "NB_TIMERS_MAX"   , nb_timer_channel_max);
2119    def_int_write(fdout, "NB_DMAS_MAX"     , nb_dma_channel_max);
2120    def_int_write(fdout, "NB_TTYS"         , nb_tty_channel);
2121    def_int_write(fdout, "NB_IOCS"         , nb_ioc_channel);
2122    def_int_write(fdout, "NB_NICS"         , nb_nic_channel);
2123   
2124    file_write(fdout, "\n");
2125    def_int_write(fdout, "USE_XICU"         , use_xicu);
2126    def_int_write(fdout, "IOMMU_ACTIVE "    , io_mmu_active);
2127
2128    char* epil = "\n#endif //_HD_CONFIG_H";
2129    file_write(fdout, epil);
2130
2131    close(fdout);
2132}
2133
2134////////////////////////////////////////////////////////
2135void ld_write(int fdout, char* seg, unsigned int addr)
2136{
2137    char  buf[64];
2138    sprintf(buf, "%s = 0x%x;\n", seg, addr);
2139    file_write(fdout, buf);
2140
2141}
2142
2143///////////////////////////////////////
2144void  genLd( const char* file_path )
2145///////////////////////////////////////
2146{
2147    int fdout = open_file(file_path);
2148
2149    char* prol = "/* Generated from the mapping_info file */\n\n";
2150    file_write(fdout, prol);
2151
2152    //boot
2153    ld_write(fdout, "seg_boot_code_base"           , boot_code_base);
2154    ld_write(fdout, "seg_boot_stack_base"          , boot_stack_base);
2155    ld_write(fdout, "seg_mapping_base"        , boot_mapping_base );
2156
2157    //kernel
2158    ld_write(fdout, "\nseg_kernel_code_base"         , kernel_code_base);
2159    ld_write(fdout, "seg_kernel_data_base"         , kernel_data_base);
2160    ld_write(fdout, "seg_kernel_uncdata_base"      , kernel_uncdata_base);
2161    ld_write(fdout, "seg_kernel_init_base"         , kernel_init_base);
2162
2163    //peripherals
2164    ld_write(fdout, "\nseg_fbf_base" ,  fbf_base_offset);
2165    ld_write(fdout, "seg_icu_base" ,  icu_base_offset);
2166    ld_write(fdout, "seg_ioc_base" ,  ioc_base_offset);
2167    ld_write(fdout, "seg_tty_base" ,  tty_base_offset);
2168    ld_write(fdout, "seg_dma_base" ,  dma_base_offset);
2169    ld_write(fdout, "seg_tim_base" ,  tim_base_offset);
2170    ld_write(fdout, "seg_gcd_base" ,  gcd_base_offset);
2171    ld_write(fdout, "seg_iob_base" ,  iob_base_offset);
2172
2173    close(fdout);
2174}
2175
2176char* buildPath(const char* path, const char*name)
2177{
2178    char *res = calloc(strlen(path) + strlen(name) + 1, 1);
2179    strcat(res, path);
2180    strcat(res, "/");
2181    strcat(res, name);
2182    return res; 
2183}
2184
2185/////////////////////////////////////
2186int  main ( int argc, char* argv[] )
2187/////////////////////////////////////
2188{
2189    if ( argc < 3 ) 
2190    {
2191        printf("Usage: xml2bin <input_file_path> <output_path>\n");
2192        return 1;
2193    }
2194
2195    struct stat dir_st;
2196    if(stat( argv[2], &dir_st ))
2197    {
2198        perror("bad path");
2199        exit(1);
2200    }
2201
2202    if((dir_st.st_mode & S_IFDIR) == 0)
2203    {
2204        printf("path is not a dir: %s", argv[2] );
2205        exit(1);
2206    }
2207
2208   
2209    char *map_path = buildPath(argv[2], "map.bin"); 
2210    char *ld_path = buildPath(argv[2],  "giet_vsegs.ld"); 
2211    char *hd_path = buildPath(argv[2],  "hard_config.h"); 
2212
2213    LIBXML_TEST_VERSION;
2214
2215    int status;
2216    xmlTextReaderPtr reader = xmlReaderForFile ( argv[1], NULL, 0 );
2217
2218    if ( reader != NULL ) 
2219    {
2220        status = xmlTextReaderRead ( reader );
2221        while ( status == 1 ) 
2222        {
2223            const char* tag = (const char*)xmlTextReaderConstName(reader);
2224
2225            if ( strcmp(tag,"mapping_info") == 0 )
2226            { 
2227                headerNode( reader );
2228                prepareBuild();
2229                buildBin( map_path );
2230                genHd(hd_path);
2231                genLd(ld_path);
2232            }
2233            else
2234            {
2235                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
2236                return 1;
2237            }
2238            status = xmlTextReaderRead ( reader );
2239        }
2240        xmlFreeTextReader ( reader );
2241
2242        if ( status != 0 ) 
2243        {
2244            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
2245            return 1;
2246        }
2247    }
2248    return 0;
2249} // end main()
Note: See TracBrowser for help on using the repository browser.