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

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