source: soft/giet_vm/giet_xml/xml_parser.c @ 511

Last change on this file since 511 was 511, checked in by alain, 9 years ago

Removing the vobj objects fro the mapping_info structure.

  • Property svn:executable set to *
File size: 57.8 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 memory 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
17#include  <stdlib.h>
18#include  <fcntl.h>
19#include  <sys/types.h>
20#include  <sys/stat.h>
21#include  <unistd.h>
22#include  <stdio.h>
23#include  <string.h>
24#include  <assert.h>
25#include  <libxml/xmlreader.h>
26#include  <mapping_info.h>
27#include  <irq_handler.h>
28
29#define MAX_CLUSTERS   1024
30#define MAX_PSEGS      4096
31#define MAX_VSPACES    1024
32#define MAX_TASKS      4096
33#define MAX_VSEGS      4096
34#define MAX_PROCS      1024
35#define MAX_IRQS       8192
36#define MAX_COPROCS    4096
37#define MAX_CP_PORTS   8192
38#define MAX_PERIPHS    8192
39
40#define XML_PARSER_DEBUG  1
41
42///////////////////////////////////////////////////////////////////////////////////
43//     global variables used to store and index the data structures
44///////////////////////////////////////////////////////////////////////////////////
45
46mapping_header_t *   header;
47mapping_cluster_t *  cluster[MAX_CLUSTERS];  // cluster array
48mapping_pseg_t *     pseg[MAX_PSEGS];        // pseg array
49mapping_vspace_t *   vspace[MAX_VSPACES];    // vspace array
50mapping_vseg_t *     vseg[MAX_VSEGS];        // vseg array
51mapping_task_t *     task[MAX_TASKS];        // task array
52mapping_proc_t *     proc[MAX_PROCS];        // proc array
53mapping_irq_t *      irq[MAX_IRQS];          // irq array
54mapping_coproc_t *   coproc[MAX_COPROCS];    // coproc array
55mapping_cp_port_t *  cp_port[MAX_CP_PORTS];  // coproc port array
56mapping_periph_t *   periph[MAX_PERIPHS];    // peripheral array
57
58// Index for the various arrays
59
60unsigned int cluster_index  = 0;
61unsigned int vspace_index = 0;
62unsigned int global_index = 0;
63unsigned int pseg_index = 0;       
64
65unsigned int proc_index = 0;
66unsigned int proc_loc_index = 0;
67
68unsigned int irq_index = 0;
69unsigned int irq_loc_index  = 0;
70
71unsigned int coproc_index = 0;
72unsigned int coproc_loc_index = 0;
73
74unsigned int cp_port_index = 0;
75unsigned int cp_port_loc_index = 0;
76
77unsigned int periph_index = 0;
78unsigned int periph_loc_index = 0;
79
80unsigned int vseg_index = 0;
81unsigned int vseg_loc_index = 0;
82
83unsigned int task_index = 0;
84unsigned int task_loc_index = 0;
85
86
87//////////////////////////////
88// for replicated peripheral
89//////////////////////////////
90char found_timer = 0;
91char found_icu   = 0;
92char found_xcu   = 0;
93char found_dma   = 0;
94char found_mmc   = 0;
95
96/*
97////////////////////////////////////////////////////////////////////////
98// These variables are used to generate the hard_config.h file.
99////////////////////////////////////////////////////////////////////////
100
101
102unsigned int tim_channels     = 0; // number of user timers  (per cluster)
103unsigned int dma_channels     = 0; // number of DMA channels (per cluster)
104
105unsigned int tty_channels     = 0; // number of TTY channels
106unsigned int ioc_channels     = 0; // number of HBA channels
107unsigned int nic_channels     = 0; // number of NIC channels
108unsigned int cma_channels     = 0; // number of CMA channels
109unsigned int pic_channels     = 0; // number of PIC channels
110
111unsigned int use_iob          = 0; // using IOB component
112unsigned int use_pic          = 0; // using PIC component
113unsigned int use_xcu          = 0; // using XCU (not ICU)
114unsigned int use_fbf          = 0; // using Frame Buffer
115
116// These variables define the IOC peripheral subtype
117
118unsigned int use_hba          = 0; // using SoClib AHCI controller
119unsigned int use_bdv          = 0; // using SoCLIB block device controller
120unsigned int use_spi          = 0; // using SDCard-SPI
121
122////////////////////////////////////////////////////////////////
123// These variables are used to generate the giet_vseg.ld file
124////////////////////////////////////////////////////////////////
125
126unsigned int periph_vbase_array[PERIPH_TYPE_MAX_VALUE]
127         = { [0 ... (PERIPH_TYPE_MAX_VALUE - 1)] = 0xFFFFFFFF };
128*/
129
130
131//////////////////////////////////////////////////
132unsigned int getIntValue( xmlTextReaderPtr reader, 
133                          const char * attributeName, 
134                          unsigned int * ok) 
135{
136    unsigned int value = 0;
137    unsigned int i;
138    char c;
139
140    char * string = (char *) xmlTextReaderGetAttribute(reader, 
141                                (const xmlChar *) attributeName);
142
143    if (string == NULL) 
144    {
145        // missing argument
146        *ok = 0;
147        return 0;
148    }
149    else 
150    {
151        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
152        {
153            // Hexa
154            for (i = 2 ; (string[i] != 0) && (i < 10) ; i++) 
155            {
156                c = string[i];
157                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
158                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
159                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
160                else 
161                {
162                    *ok = 0;
163                    return 0;
164                }
165            }
166        }
167        else 
168        {
169            // Decimal
170            for (i = 0; (string[i] != 0) && (i < 9); i++) 
171            {
172                c = string[i];
173                if ((c >= '0') && (c <= '9')) value = (value * 10) + string[i] - 48;
174                else 
175                {
176                    *ok = 0;
177                    return 0;
178                }
179            }
180        }
181        *ok = 1;
182        return value; 
183    }
184} // end getIntValue()
185
186////////////////////////////////////////////////
187paddr_t getPaddrValue( xmlTextReaderPtr reader, 
188                       const char * attributeName, 
189                       unsigned int * ok) 
190{
191    paddr_t value = 0;
192    unsigned int i;
193    char c;
194
195    char * string = (char *) xmlTextReaderGetAttribute(reader, 
196                                (const xmlChar *) attributeName);
197
198    if (string == NULL) 
199    {
200        // missing argument
201        *ok = 0;
202        return 0;
203    }
204    else 
205    {
206        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
207        {
208            // Hexa
209            for (i = 2 ; (string[i] != 0) && (i < 18) ; i++) 
210            {
211                c = string[i];
212                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
213                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
214                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
215                else 
216                {
217                    *ok = 0;
218                    return 0;
219                }
220            }
221        }
222        else 
223        {
224            // Decimal not supported for paddr_t
225            *ok = 0;
226            return 0;
227        }
228        *ok = 1;
229        return value; 
230    }
231} // end getPaddrValue()
232
233////////////////////////////////////////////////
234char * getStringValue( xmlTextReaderPtr reader, 
235                       const char * attributeName, 
236                       unsigned int * ok ) 
237{
238    char * string = (char *) xmlTextReaderGetAttribute(reader, 
239                               (const xmlChar *) attributeName);
240
241
242    if (string == NULL) 
243    {
244        // missing argument
245        *ok = 0;
246        return NULL;
247    }
248    else 
249    {
250        //we read only string smaller than 32 byte
251        if (strlen(string) > 32) 
252        {
253            printf("[XML ERROR] all strings must be less than 32 bytes\n");
254            exit(1);
255        }
256
257        *ok = 1;
258        return string;
259    }
260} // end getStringValue()
261
262///////////////////////////////////////////////////////////////
263int getClusterId( unsigned int x, unsigned int y )
264{
265    // associative search of cluster index
266    unsigned int cluster_id;
267
268    for( cluster_id = 0 ; cluster_id < (header->x_size * header->y_size) ; cluster_id++ )
269    {
270        if( (cluster[cluster_id]->x == x) && (cluster[cluster_id]->y == y) )
271        {
272            return cluster_id;
273        }
274    }
275    return -1;
276}  // end getClusterId()
277
278///////////////////////////////////////////////////////////////
279int getPsegId(unsigned int x, unsigned int y, char * pseg_name) 
280{
281    int cluster_id = getClusterId( x, y );
282
283    if ( cluster_id == -1 ) return -1;
284
285    // associative search for pseg index
286    unsigned int pseg_id;
287    unsigned int pseg_min = cluster[cluster_id]->pseg_offset;
288    unsigned int pseg_max = pseg_min + cluster[cluster_id]->psegs;
289
290    for (pseg_id = pseg_min; pseg_id < pseg_max; pseg_id++) 
291    {
292        if (strcmp(pseg[pseg_id]->name, pseg_name) == 0) 
293        {
294            return pseg_id;
295        }
296    }
297    return -1;
298}  // end getPsegId()
299
300///////////////////////////////////
301int getVspaceId(char * vspace_name) 
302{
303    unsigned int vspace_id;
304
305    for (vspace_id = 0; vspace_id < vspace_index; vspace_id++) 
306    {
307        if (strcmp(vspace[vspace_id]->name, vspace_name) == 0) 
308        {
309            return vspace_id;
310        }
311    }
312    return -1;
313}
314
315///////////////////////////////////////////////////
316int getVsegId( unsigned int vspace_id, char *name )
317{
318    unsigned int vseg_id;
319    unsigned int vseg_min = vspace[vspace_id]->vseg_offset;
320    unsigned int vseg_max = vseg_min + vspace[vspace_id]->vsegs;
321
322    for (vseg_id = vseg_min ; vseg_id < vseg_max ; vseg_id++) 
323    {
324        if (strcmp(vseg[vseg_id]->name, name) == 0) return vseg_id;
325    }
326    return -1;
327}
328
329
330//////////////////////////////////////
331void taskNode(xmlTextReaderPtr reader) 
332{
333    unsigned int ok;
334    unsigned int value;
335    unsigned int x,y;
336    char * str;
337
338    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
339
340    if (task_index >= MAX_TASKS) 
341    {
342        printf("[XML ERROR] The number of tasks is larger than %d\n", MAX_TASKS);
343        exit(1);
344    }
345
346#if XML_PARSER_DEBUG
347printf("   task %d\n", task_loc_index);
348#endif
349
350    task[task_index] = (mapping_task_t *) malloc(sizeof(mapping_task_t));
351
352    ////////// get name attribute
353    str = getStringValue(reader, "name", &ok);
354    if (ok) 
355    {
356#if XML_PARSER_DEBUG
357printf("      name      = %s\n", str);
358#endif
359        strncpy( task[task_index]->name, str, 31 );
360    }
361    else 
362    {
363        printf("[XML ERROR] illegal or missing <name> attribute for task (%d,%d)\n", 
364                vspace_index, task_loc_index);
365        exit(1);
366    }
367
368    ///////// get trdid attribute (optional)
369    task[task_index]->trdid = getIntValue(reader, "trdid", &ok);
370#if XML_PARSER_DEBUG
371printf("      trdid     = %d\n", x);
372#endif
373    if ( !ok ) 
374    {
375        task[task_index]->trdid = task_loc_index;
376    } 
377
378    ///////// get x coordinate
379    x = getIntValue(reader, "x", &ok);
380#if XML_PARSER_DEBUG
381printf("      x         = %d\n", x);
382#endif
383    if ( !(ok && (x < header->x_size)) ) 
384    {
385        printf("[XML ERROR] illegal or missing < x > attribute for task (%d,%d)\n",
386                vspace_index, task_loc_index);
387        exit(1);
388    } 
389
390    ///////// get y coordinate
391    y = getIntValue(reader, "y", &ok);
392#if XML_PARSER_DEBUG
393printf("      y         = %d\n", y);
394#endif
395    if ( !(ok && (y < header->y_size)) ) 
396    {
397        printf("[XML ERROR] illegal or missing < y > attribute for task (%d,%d)\n",
398                vspace_index, task_loc_index);
399        exit(1);
400    } 
401
402    ///////// set clusterid attribute
403    int index = getClusterId( x, y );
404#if XML_PARSER_DEBUG
405printf("      clusterid = %d\n", index);
406#endif
407    if( index >= 0 )
408    {
409        task[task_index]->clusterid = index;
410    }
411    else
412    {
413        printf("[XML ERROR] <clusterid> not found for task (%d,%d)\n",
414                vspace_index, task_loc_index);
415        exit(1);
416    }
417
418    ////////// get p attribute
419    value = getIntValue(reader, "p", &ok);
420    if (ok) 
421    {
422#if XML_PARSER_DEBUG
423printf("      proclocid = %x\n", value);
424#endif
425        if (value >= cluster[task[task_index]->clusterid]->procs) 
426        {
427            printf("[XML ERROR] <proclocid> too large for task (%d,%d)\n",
428                    vspace_index, task_loc_index);
429            exit(1);
430        }
431        task[task_index]->proclocid = value;
432    } 
433    else 
434    {
435        printf("[XML ERROR] illegal or missing <p> attribute for task (%d,%d)\n", 
436                vspace_index, task_loc_index);
437        exit(1);
438    }
439
440    ////////// get stackname attribute
441    char* stack_name = getStringValue(reader, "stackname" , &ok);
442    if (ok) 
443    {
444#if XML_PARSER_DEBUG
445printf("      stackname = %s\n", str);
446#endif
447        int index = getVsegId( vspace_index , stack_name );
448        if (index >= 0) 
449        {
450#if XML_PARSER_DEBUG
451printf("      stack_id  = %d\n", index);
452#endif
453            task[task_index]->stack_vseg_id = index;
454        }
455        else 
456        {
457            printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
458                    vspace_index, task_loc_index);
459            exit(1);
460        }
461    } 
462    else 
463    {
464        printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
465                vspace_index, task_loc_index);
466        exit(1);
467    }
468
469    ////////// get heap attribute
470    char* heap_name = getStringValue(reader, "heapname", &ok);
471    if (ok) 
472    {
473#if XML_PARSER_DEBUG
474printf("      heapname  = %s\n", str);
475#endif
476        int index = getVsegId( vspace_index , heap_name );
477        if (index >= 0) 
478        {
479#if XML_PARSER_DEBUG
480printf("      heap_id   = %d\n", index );
481#endif
482            task[task_index]->heap_vseg_id = index;
483        }
484        else 
485        {
486            printf("[XML ERROR] illegal or missing <heapname> for task (%d,%d)\n", 
487                   vspace_index, task_loc_index);
488            exit(1);
489        }
490    } 
491    else 
492    {
493        task[task_index]->heap_vseg_id = -1;
494    }
495
496    ////////// get startid  attribute
497    value = getIntValue(reader, "startid", &ok);
498    if (ok) 
499    {
500#if XML_PARSER_DEBUG
501printf("      startid   = %x\n", value);
502#endif
503        task[task_index]->startid = value;
504    } 
505    else 
506    {
507        printf("[XML ERROR] illegal or missing <startid> attribute for task (%d,%d)\n", 
508                vspace_index, task_loc_index);
509        exit(1);
510    }
511
512    task_index++;
513    task_loc_index++;
514} // end taskNode()
515
516
517//////////////////////////////////////
518void vsegNode(xmlTextReaderPtr reader) 
519{
520    unsigned int ok;
521    unsigned int value;
522    unsigned int x,y;
523    char * str;
524
525    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
526
527    if (vseg_index >= MAX_VSEGS) 
528    {
529        printf("[XML ERROR] The number of vsegs is larger than %d\n", MAX_VSEGS);
530        exit(1);
531    }
532
533#if XML_PARSER_DEBUG
534printf("    vseg %d\n", vseg_loc_index);
535#endif
536
537    vseg[vseg_index] = (mapping_vseg_t *) malloc(sizeof(mapping_vseg_t));
538
539    ///////// set mapped attribute
540    vseg[vseg_index]->mapped = 0;
541
542    ///////// get name attribute
543    str = getStringValue(reader, "name", &ok);
544    if (ok) 
545    {
546#if XML_PARSER_DEBUG
547printf("      name        = %s\n", str);
548#endif
549        strncpy( vseg[vseg_index]->name, str, 31);
550    }
551    else 
552    {
553        printf("[XML ERROR] illegal or missing <name> attribute for vseg (%d,%d)\n", 
554                vspace_index, vseg_loc_index);
555        exit(1);
556    }
557
558    ////////// get ident attribute (optional : 0 if missing)
559    value = getIntValue(reader, "ident", &ok);
560    if (ok) 
561    {
562#if XML_PARSER_DEBUG
563printf("      ident       = %d\n", value);
564#endif
565        vseg[vseg_index]->ident = value;
566    } 
567    else 
568    {
569        vseg[vseg_index]->ident = 0;
570    }
571
572    ////////// get local attribute (optional : 0 if missing)
573    value = getIntValue(reader, "local", &ok);
574    if (ok) 
575    {
576#if XML_PARSER_DEBUG
577printf("      local       = %d\n", value);
578#endif
579        vseg[vseg_index]->local = value;
580    } 
581    else 
582    {
583        vseg[vseg_index]->local = 0;
584    }
585
586    ////////// get big attribute (optional : 0 if missing)
587    value = getIntValue(reader, "big", &ok);
588    if (ok) 
589    {
590#if XML_PARSER_DEBUG
591printf("      big         = %d\n", value);
592#endif
593        vseg[vseg_index]->big = value;
594    } 
595    else 
596    {
597        vseg[vseg_index]->big = 0;
598    }
599
600    /////////// get vbase attribute
601    value = getIntValue(reader, "vbase", &ok);
602    if (ok) 
603    {
604#if XML_PARSER_DEBUG
605printf("      vbase       = 0x%x\n", value);
606#endif
607        vseg[vseg_index]->vbase = value;
608    }
609    else 
610    {
611        printf("[XML ERROR] illegal or missing <vbase> attribute for vseg (%d,%d)\n", 
612                vspace_index, vseg_loc_index);
613        exit(1);
614    }
615
616    ////////// get length attribute
617    value = getIntValue(reader, "length", &ok);
618    if (ok)
619    {
620#if XML_PARSER_DEBUG
621printf("      length      = %x\n", value);
622#endif
623        vseg[vseg_index]->length = value;
624    }
625    else 
626    {
627        printf("[XML ERROR] illegal or missing <length> attribute for vseg (%d,%d)\n",
628                vspace_index, vseg_loc_index);
629        exit(1);
630    }
631
632    //////// get type attribute
633    str = getStringValue(reader, "type", &ok);
634
635#if XML_PARSER_DEBUG
636printf("      type        = %s\n", str);
637#endif
638
639    if      (ok && (strcmp(str, "ELF")    == 0)) vseg[vseg_index]->type = VSEG_TYPE_ELF;
640    else if (ok && (strcmp(str, "PERI")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_PERI;
641    else if (ok && (strcmp(str, "BLOB")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_BLOB;
642    else if (ok && (strcmp(str, "PTAB")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_PTAB;
643    else if (ok && (strcmp(str, "BUFFER") == 0)) vseg[vseg_index]->type = VSEG_TYPE_BUFFER;
644    else if (ok && (strcmp(str, "SCHED")  == 0)) vseg[vseg_index]->type = VSEG_TYPE_SCHED;
645    else if (ok && (strcmp(str, "HEAP")   == 0)) vseg[vseg_index]->type = VSEG_TYPE_HEAP;
646    else
647    {
648        printf("[XML ERROR] illegal or missing <type> attribute for vseg (%d,%d)\n",
649                vspace_index, vseg_loc_index);
650        exit(1);
651    }
652
653    ////////// get x coordinate
654    x = getIntValue(reader, "x", &ok);
655#if XML_PARSER_DEBUG
656printf("      x           = %d\n", x);
657#endif
658    if ( !(ok && (x < header->x_size)) ) 
659    {
660        printf("[XML ERROR] illegal or missing < x > attribute for vseg %d\n", 
661                vseg_loc_index);
662        exit(1);
663    }
664
665    ////////// get y coordinate
666    y = getIntValue(reader, "y", &ok);
667#if XML_PARSER_DEBUG
668printf("      y           = %d\n", y);
669#endif
670    if ( !(ok && (y < header->y_size)) ) 
671    {
672        printf("[XML ERROR] illegal or missing < y > attribute for vseg %d\n", 
673                vseg_loc_index);
674        exit(1);
675    }
676
677    ///////// get psegname attribute
678    str = getStringValue(reader, "psegname", &ok);
679#if XML_PARSER_DEBUG
680printf("      psegname    = %s\n", str);
681#endif
682    if (ok == 0) 
683    {
684        printf("[XML ERROR] illegal or missing <psegname> for vseg %d\n", 
685                vseg_loc_index);
686        exit(1);
687    }
688
689    /////////// set psegid field
690    int psegid = getPsegId( x, y, str );
691#if XML_PARSER_DEBUG
692printf("      psegid      = %d\n", psegid);
693#endif
694    if (psegid >= 0) 
695    {
696        vseg[vseg_index]->psegid = psegid;
697    }
698    else 
699    {
700        printf("[XML ERROR] pseg not found for vseg %d / x = %d / y = %d / psegname = %s\n", 
701                vseg_loc_index, x, y, str );
702        exit(1);
703    } 
704
705    //////// get mode attribute
706    str = getStringValue(reader, "mode", &ok);
707#if XML_PARSER_DEBUG
708printf("      mode        = %s\n", str);
709#endif
710    if      (ok && (strcmp(str, "CXWU") == 0)) { vseg[vseg_index]->mode = 0xF; }
711    else if (ok && (strcmp(str, "CXW_") == 0)) { vseg[vseg_index]->mode = 0xE; }
712    else if (ok && (strcmp(str, "CX_U") == 0)) { vseg[vseg_index]->mode = 0xD; }
713    else if (ok && (strcmp(str, "CX__") == 0)) { vseg[vseg_index]->mode = 0xC; }
714    else if (ok && (strcmp(str, "C_WU") == 0)) { vseg[vseg_index]->mode = 0xB; }
715    else if (ok && (strcmp(str, "C_W_") == 0)) { vseg[vseg_index]->mode = 0xA; }
716    else if (ok && (strcmp(str, "C__U") == 0)) { vseg[vseg_index]->mode = 0x9; }
717    else if (ok && (strcmp(str, "C___") == 0)) { vseg[vseg_index]->mode = 0x8; }
718    else if (ok && (strcmp(str, "_XWU") == 0)) { vseg[vseg_index]->mode = 0x7; }
719    else if (ok && (strcmp(str, "_XW_") == 0)) { vseg[vseg_index]->mode = 0x6; }
720    else if (ok && (strcmp(str, "_X_U") == 0)) { vseg[vseg_index]->mode = 0x5; }
721    else if (ok && (strcmp(str, "_X__") == 0)) { vseg[vseg_index]->mode = 0x4; }
722    else if (ok && (strcmp(str, "__WU") == 0)) { vseg[vseg_index]->mode = 0x3; }
723    else if (ok && (strcmp(str, "__W_") == 0)) { vseg[vseg_index]->mode = 0x2; }
724    else if (ok && (strcmp(str, "___U") == 0)) { vseg[vseg_index]->mode = 0x1; }
725    else if (ok && (strcmp(str, "____") == 0)) { vseg[vseg_index]->mode = 0x0; }
726    else {
727        printf("[XML ERROR] illegal or missing <mode> attribute for vseg (%d,%d)\n", 
728                vspace_index, vseg_loc_index);
729        exit(1);
730    }
731
732    ////////// get binpath attribute (optional : "" if missing)
733    str = getStringValue(reader, "binpath", &ok);
734    if (ok)
735    {
736#if XML_PARSER_DEBUG
737printf("      binpath = %s\n", str);
738#endif
739        strncpy(vseg[vseg_index]->binpath, str, 63);
740    }
741    else
742    {
743        vseg[vseg_index]->binpath[0] = 0;
744    }
745
746    vseg_index++;
747    vseg_loc_index++;
748} // end vsegNode()
749
750////////////////////////////////////////
751void vspaceNode(xmlTextReaderPtr reader) 
752{
753    unsigned int ok;
754
755    vseg_loc_index = 0;
756    task_loc_index = 0;
757
758    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
759
760    vspace[vspace_index] = (mapping_vspace_t *) malloc(sizeof(mapping_vspace_t));
761    header->vspaces      = header->vspaces + 1;
762
763    ////////// get name attribute
764    char* vspace_name = getStringValue(reader, "name", &ok);
765    if (ok) 
766    {
767#if XML_PARSER_DEBUG
768printf("\n  vspace = %s\n", vspace_name );
769#endif
770        strncpy( vspace[vspace_index]->name, vspace_name , 31 );
771    }
772    else 
773    {
774        printf("[XML ERROR] illegal or missing <name> attribute for vspace %d\n", 
775                vspace_index);
776        exit(1);
777    }
778
779    ////////// set vseg_offset and task_offset attributes
780    vspace[vspace_index]->vseg_offset = vseg_index;
781    vspace[vspace_index]->task_offset = task_index;
782
783    ////////// initialise vsegs and tasks attributes
784    vspace[vspace_index]->vsegs = 0;
785    vspace[vspace_index]->tasks = 0;
786
787    ////////// get startname attribute
788    char* start_name = getStringValue(reader, "startname", &ok);
789    if (ok == 0) 
790    {
791        printf("[XML ERROR] illegal or missing <startname> attribute for vspace %s\n", 
792                vspace[vspace_index]->name);
793        exit(1);
794    }
795
796    int status = xmlTextReaderRead(reader);
797    while (status == 1) 
798    {
799        const char * tag = (const char *) xmlTextReaderConstName(reader);
800
801        if (strcmp(tag, "vseg") == 0) 
802        {
803            vsegNode(reader);
804            vspace[vspace_index]->vsegs += 1;
805        }
806        else if (strcmp(tag, "task") == 0) 
807        {
808            taskNode(reader);
809            vspace[vspace_index]->tasks += 1;
810        }
811        else if (strcmp(tag, "#text")    == 0) { }
812        else if (strcmp(tag, "#comment") == 0) { }
813        else if (strcmp(tag, "vspace")   == 0) 
814        {
815            // get index of the vseg containing the start vector
816            int index = getVsegId( vspace_index, start_name );
817            if (index == -1) 
818            {
819                printf("[XML ERROR] vseg containing start vector not found in vspace %s\n",
820                        vspace[vspace_index]->name);
821                exit(1);
822            }
823            else 
824            {
825                vspace[vspace_index]->start_vseg_id = index;
826            }
827
828#if XML_PARSER_DEBUG
829printf("      vsegs       = %d\n", vspace[vspace_index]->vsegs );
830printf("      tasks       = %d\n", vspace[vspace_index]->tasks );
831printf("      vseg_offset = %d\n", vspace[vspace_index]->vseg_offset );
832printf("      task_offset = %d\n", vspace[vspace_index]->task_offset );
833printf("      startname   = %s\n", start_name);
834printf("      start_id    = %d\n", index);
835printf("  end vspace %d\n\n", vspace_index);
836#endif
837            vspace_index++;
838            return;
839        }
840        else 
841        {
842            printf("[XML ERROR] Unknown tag %s", tag);
843            exit(1);
844        }
845        status = xmlTextReaderRead(reader);
846    }
847} // end vspaceNode()
848
849/////////////////////////////////////
850void irqNode(xmlTextReaderPtr reader) 
851{
852    unsigned int ok;
853    unsigned int value;
854    char * str;
855
856    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
857
858    if (irq_index >= MAX_IRQS) 
859    {
860        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
861    }
862
863#if XML_PARSER_DEBUG
864printf("      irq %d\n", irq_loc_index);
865#endif
866
867    irq[irq_index] = (mapping_irq_t *) malloc(sizeof(mapping_irq_t));
868
869    ///////// get srcid attribute
870    value = getIntValue(reader, "srcid", &ok);
871    if (ok) 
872    {
873#if XML_PARSER_DEBUG
874printf("        srcid   = %d\n", value);
875#endif
876        irq[irq_index]->srcid = value;
877        if (value >= 32) 
878        {
879            printf("[XML ERROR] IRQ <srcid> too large for periph %d in cluster %d\n",
880                    cluster_index, periph_loc_index);
881            exit(1);
882        }
883    }
884    else 
885    {
886        printf("[XML ERROR] missing IRQ <srcid> for periph %d in cluster %d\n",
887                cluster_index, periph_loc_index);
888        exit(1);
889    }
890
891    ///////// get srctype attribute
892    str = getStringValue(reader, "srctype", &ok);
893    if (ok) 
894    {
895#if XML_PARSER_DEBUG
896printf("        srctype = %s\n", str);
897#endif
898        if      ( strcmp(str, "HWI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_HWI;
899        else if ( strcmp(str, "WTI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_WTI;
900        else if ( strcmp(str, "PTI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_PTI;
901        else   
902        {
903            printf("[XML ERROR] illegal IRQ <srctype> for periph %d in cluster %d\n",
904                   cluster_index, periph_loc_index);
905            exit(1);
906        }
907    }
908    else 
909    {
910        printf("[XML ERROR] missing IRQ <srctype> for periph %d in cluster %d\n",
911               cluster_index, periph_loc_index);
912        exit(1);
913    }
914
915    ///////// get isr attribute
916    str = getStringValue(reader, "isr", &ok);
917    if (ok) 
918    {
919#if XML_PARSER_DEBUG
920printf("        isr     = %s\n", str);
921#endif
922        if      (strcmp(str, "ISR_TICK"   ) == 0)  irq[irq_index]->isr = ISR_TICK;
923        else if (strcmp(str, "ISR_BDV"    ) == 0)  irq[irq_index]->isr = ISR_BDV;
924        else if (strcmp(str, "ISR_CMA"    ) == 0)  irq[irq_index]->isr = ISR_CMA;
925        else if (strcmp(str, "ISR_TTY_RX" ) == 0)  irq[irq_index]->isr = ISR_TTY_RX;
926        else if (strcmp(str, "ISR_TTY_TX" ) == 0)  irq[irq_index]->isr = ISR_TTY_TX;
927        else if (strcmp(str, "ISR_TIMER"  ) == 0)  irq[irq_index]->isr = ISR_TIMER;
928        else if (strcmp(str, "ISR_WAKUP"  ) == 0)  irq[irq_index]->isr = ISR_WAKUP;
929        else if (strcmp(str, "ISR_NIC_RX" ) == 0)  irq[irq_index]->isr = ISR_NIC_RX;
930        else if (strcmp(str, "ISR_NIC_TX" ) == 0)  irq[irq_index]->isr = ISR_NIC_TX;
931        else if (strcmp(str, "ISR_MMC"    ) == 0)  irq[irq_index]->isr = ISR_MMC;
932        else if (strcmp(str, "ISR_DMA"    ) == 0)  irq[irq_index]->isr = ISR_DMA;
933        else if (strcmp(str, "ISR_SPI"    ) == 0)  irq[irq_index]->isr = ISR_SPI;
934        else if (strcmp(str, "ISR_DEFAULT") == 0)  irq[irq_index]->isr = ISR_DEFAULT;
935        else 
936        {
937            printf("[XML ERROR] illegal IRQ <isr> for periph %d in cluster %d\n",
938                   periph_loc_index, cluster_index );
939            exit(1);
940        }
941    } 
942    else 
943    {
944        printf("[XML ERROR] missing IRQ <isr> for periph %d in cluster %d\n",
945                cluster_index, periph_loc_index);
946        exit(1);
947    }
948
949    ///////// get channel attribute (optionnal : 0 if missing)
950    value = getIntValue(reader, "channel", &ok);
951    if (ok) 
952    {
953#if XML_PARSER_DEBUG
954printf("        channel = %d\n", value);
955#endif
956        irq[irq_index]->channel = value;
957    }
958    else 
959    {
960        irq[irq_index]->channel = 0;
961    }
962
963    irq_index++;
964    irq_loc_index++;
965
966} // end irqNode
967
968
969
970////////////////////////////////////////
971void cpPortNode(xmlTextReaderPtr reader) 
972{
973    char * str;
974    unsigned int ok;
975
976    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
977
978    if (cp_port_index >= MAX_CP_PORTS) 
979    {
980        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", 
981                 MAX_CP_PORTS);
982        exit(1);
983    }
984
985#if XML_PARSER_DEBUG
986printf("\n  port %d\n", cp_port_index);
987#endif
988
989    cp_port[cp_port_index] = (mapping_cp_port_t *) malloc(sizeof(mapping_cp_port_t));
990
991    ///////// get direction attribute
992    str = getStringValue(reader, "direction", &ok);
993    if (ok) 
994    {
995#if XML_PARSER_DEBUG
996printf("      direction = %s\n", str);
997#endif
998        if (strcmp(str, "TO_COPROC")   ==  0) 
999        {
1000            cp_port[cp_port_index]->direction = PORT_TO_COPROC;
1001        }
1002        else if (strcmp(str, "FROM_COPROC") ==  0) 
1003        {
1004            cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
1005        }
1006        else 
1007        {
1008            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
1009                    cp_port_index, cluster_index);
1010            exit(1);
1011        }
1012    } 
1013    else 
1014    {
1015        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
1016                cp_port_index, cluster_index);
1017        exit(1);
1018    }
1019
1020    cp_port_index++;
1021    cp_port_loc_index++;
1022} // end cpPortNode()
1023
1024////////////////////////////////////////
1025void periphNode(xmlTextReaderPtr reader) 
1026{
1027    char * str;
1028    unsigned int value;
1029    unsigned int ok;
1030
1031    irq_loc_index = 0;
1032
1033    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1034
1035    if (periph_index >= MAX_PERIPHS) 
1036    {
1037        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
1038        exit(1);
1039    }
1040
1041#if XML_PARSER_DEBUG
1042printf("\n    periph %d\n", periph_index);
1043#endif
1044
1045    periph[periph_index] = (mapping_periph_t *) malloc(sizeof(mapping_periph_t));
1046
1047    ///////// get channels attribute (optionnal : 1 if missing)
1048    value = getIntValue(reader, "channels", &ok);
1049    if (ok) 
1050    {
1051#if XML_PARSER_DEBUG
1052printf("      channels    = %d\n", value);
1053#endif
1054        periph[periph_index]->channels = value;
1055    }
1056    else 
1057    {
1058        periph[periph_index]->channels = 1;
1059    }
1060
1061    ///////// get arg attribute (optionnal : 0 if missing)
1062    value = getIntValue(reader, "arg", &ok);
1063    if (ok) 
1064    {
1065#if XML_PARSER_DEBUG
1066printf("      arg         = %d\n", value);
1067#endif
1068        periph[periph_index]->arg = value;
1069    }
1070    else 
1071    {
1072        periph[periph_index]->arg = 1;
1073    }
1074
1075    /////////// get psegname attribute
1076    str = getStringValue(reader, "psegname", &ok);
1077    if (ok == 0) 
1078    {
1079        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1080                coproc_index, cluster_index);
1081        exit(1);
1082    }
1083
1084    /////////// set psegid attribute
1085    int index = getPsegId( cluster[cluster_index]->x, cluster[cluster_index]->y, str);
1086    if (index >= 0) 
1087    {
1088#if XML_PARSER_DEBUG
1089printf("      clusterid   = %d\n", cluster_index);
1090printf("      psegname    = %s\n", str);
1091printf("      psegid      = %d\n", index);
1092#endif
1093        periph[periph_index]->psegid = index;
1094    }
1095    else 
1096    {
1097        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
1098                periph_loc_index, cluster_index, str );
1099        exit(1);
1100    } 
1101
1102    /////////// get type attribute
1103    str = getStringValue(reader, "type", &ok);
1104    if (ok) 
1105    {
1106#if XML_PARSER_DEBUG
1107printf("      type        = %s\n", str);
1108#endif
1109        if      (strcmp(str, "CMA" ) == 0) periph[periph_index]->type = PERIPH_TYPE_CMA;
1110        else if (strcmp(str, "FBF" ) == 0) periph[periph_index]->type = PERIPH_TYPE_FBF;
1111        else if (strcmp(str, "IOB" ) == 0) periph[periph_index]->type = PERIPH_TYPE_IOB;
1112        else if (strcmp(str, "IOC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_IOC;
1113        else if (strcmp(str, "NIC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_NIC;
1114        else if (strcmp(str, "ROM" ) == 0) periph[periph_index]->type = PERIPH_TYPE_ROM;
1115        else if (strcmp(str, "SIM" ) == 0) periph[periph_index]->type = PERIPH_TYPE_SIM;
1116        else if (strcmp(str, "TTY" ) == 0) periph[periph_index]->type = PERIPH_TYPE_TTY;
1117        else if (strcmp(str, "PIC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_PIC;
1118        else if (strcmp(str, "DMA" ) == 0) periph[periph_index]->type = PERIPH_TYPE_DMA;
1119        else if (strcmp(str, "MMC" ) == 0) periph[periph_index]->type = PERIPH_TYPE_MMC;
1120        else if (strcmp(str, "XCU" ) == 0) periph[periph_index]->type = PERIPH_TYPE_XCU;
1121        else if (strcmp(str, "DROM") == 0) periph[periph_index]->type = PERIPH_TYPE_DROM;
1122        else
1123        {
1124            printf("[XML ERROR] illegal peripheral type: %s in cluster %d\n",
1125                    str, cluster_index);
1126            exit(1);
1127        }
1128    }
1129
1130    ////////// get subtype if IOC
1131    if (periph[periph_index]->type == PERIPH_TYPE_IOC )
1132    {
1133        char* subtype = getStringValue(reader, "subtype", &ok);
1134        if (ok)
1135        {
1136#if XML_PARSER_DEBUG
1137printf("      subtype     = %s\n", str);
1138#endif
1139            if      (strcmp(subtype, "BDV") == 0) 
1140            periph[periph_index]->subtype = PERIPH_SUBTYPE_BDV;
1141            else if (strcmp(subtype, "HBA") == 0) 
1142            periph[periph_index]->subtype = PERIPH_SUBTYPE_HBA;
1143            else if (strcmp(subtype, "SPI") == 0) 
1144            periph[periph_index]->subtype = PERIPH_SUBTYPE_SPI;
1145        }
1146        else
1147        {
1148            printf("[XML ERROR] illegal subtype for IOC peripheral\n");
1149            exit(1);
1150        }
1151    }
1152    else
1153    {
1154        periph[periph_index]->subtype = 0XFFFFFFFF;
1155    }
1156
1157    ////////////// set irq_offset attribute
1158    periph[periph_index]->irq_offset = irq_index;
1159
1160    ///////////// get IRQs
1161    int status = xmlTextReaderRead(reader);
1162    while (status == 1) 
1163    {
1164        const char * tag = (const char *) xmlTextReaderConstName(reader);
1165
1166        if (strcmp(tag, "irq") == 0) 
1167        {
1168            if ( (periph[periph_index]->type != PERIPH_TYPE_XCU) &&
1169                 (periph[periph_index]->type != PERIPH_TYPE_PIC) )
1170            {
1171                printf("[XML ERROR] periph %d in cluster(%d,%d) "
1172                       " only XCU and PIC can contain IRQs",
1173                periph_loc_index, cluster[cluster_index]->x, cluster[cluster_index]->y);
1174                exit(1);
1175            }
1176            else
1177            {
1178                  irqNode(reader);
1179            }
1180        }
1181        else if (strcmp(tag, "#text")    == 0) { }
1182        else if (strcmp(tag, "#comment") == 0) { }
1183        else if (strcmp(tag, "periph")   == 0) 
1184        {
1185            periph[periph_index]->irqs = irq_loc_index;
1186            cluster[cluster_index]->periphs++;
1187            periph_loc_index++;
1188            periph_index++;
1189
1190#if XML_PARSER_DEBUG
1191printf("      irqs        = %d\n", irq_loc_index);
1192printf("      irq_offset  = %d\n", irq_index);
1193#endif
1194            return;
1195        }
1196        else 
1197        {
1198            printf("[XML ERROR] Unknown tag %s", tag);
1199            exit(1);
1200        }
1201        status = xmlTextReaderRead(reader);
1202    }
1203} // end periphNode
1204
1205////////////////////////////////////////
1206void coprocNode(xmlTextReaderPtr reader) 
1207{
1208    char * str;
1209    unsigned int ok;
1210
1211    cp_port_loc_index = 0;
1212
1213    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1214
1215    if (coproc_index >= MAX_COPROCS) 
1216    {
1217        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
1218        exit(1);
1219    }
1220
1221#if XML_PARSER_DEBUG
1222printf("\n  coproc %d\n", coproc_index);
1223#endif
1224
1225    coproc[coproc_index] = (mapping_coproc_t *) malloc(sizeof(mapping_coproc_t));
1226
1227    /////////// get name attribute
1228    str = getStringValue(reader, "name", &ok);
1229    if (ok) 
1230    {
1231#if XML_PARSER_DEBUG
1232printf("      name = %s\n", str);
1233#endif
1234        strncpy(coproc[coproc_index]->name, str, 31);
1235    }
1236    else 
1237    {
1238        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
1239                coproc_index, cluster_index);
1240        exit(1);
1241    }
1242
1243    /////////// get psegname attribute
1244    str = getStringValue(reader, "psegname", &ok);
1245    if (ok == 0) 
1246    {
1247        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1248                coproc_index, cluster_index);
1249        exit(1);
1250    }
1251
1252    /////////// set psegid attribute
1253    int index = getPsegId( cluster[cluster_index]->x, cluster[cluster_index]->y, str);
1254    if (index >= 0) 
1255    {
1256#if XML_PARSER_DEBUG
1257printf("      clusterid = %d\n", cluster_index);
1258printf("      psegname  = %s\n", str);
1259printf("      psegid    = %d\n", index);
1260#endif
1261        coproc[coproc_index]->psegid = index;
1262        assert(pseg[index]->type == PSEG_TYPE_PERI && 
1263        "coproc psegname attribute must refer to a pseg of type PERI" );
1264    }
1265    else 
1266    {
1267        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
1268                coproc_index, cluster_index, str );
1269        exit(1);
1270    } 
1271
1272    ////////// set port_offset
1273    coproc[coproc_index]->port_offset = cp_port_index;
1274
1275#if XML_PARSER_DEBUG
1276printf("      port_offset = %d\n", cp_port_index);
1277#endif
1278
1279    int status = xmlTextReaderRead(reader);
1280    while (status == 1) 
1281    {
1282        const char * tag = (const char *) xmlTextReaderConstName(reader);
1283
1284        if (strcmp(tag, "port") == 0 ) 
1285        {
1286            cpPortNode(reader);
1287        }
1288        else if (strcmp(tag, "#text")    == 0 ) { }
1289        else if (strcmp(tag, "#comment") == 0 ) { }
1290        else if (strcmp(tag, "coproc") == 0 ) 
1291        {
1292            coproc[coproc_index]->ports = cp_port_loc_index;
1293            cluster[cluster_index]->coprocs++;
1294            coproc_loc_index++;
1295            coproc_index++;
1296            return;
1297        }
1298        else 
1299        {
1300            printf("[XML ERROR] Unknown tag %s", tag);
1301            exit(1);
1302        }
1303        status = xmlTextReaderRead(reader);
1304    }
1305} // end coprocNode()
1306
1307
1308//////////////////////////////////////
1309void procNode(xmlTextReaderPtr reader) 
1310{
1311    unsigned int ok;
1312    unsigned int value;
1313
1314    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1315
1316    if (proc_index >= MAX_PROCS) 
1317    {
1318        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1319        exit(1);
1320    }
1321
1322#if XML_PARSER_DEBUG
1323printf("\n    proc %d\n", proc_index);
1324#endif
1325
1326    proc[proc_index] = (mapping_proc_t *) malloc(sizeof(mapping_proc_t));
1327
1328    /////////// get index attribute (optional)
1329    value = getIntValue(reader, "index", &ok);
1330    if (ok && (value != proc_loc_index)) 
1331    {
1332        printf("[XML ERROR] wrong local proc index / expected value is %d", 
1333                proc_loc_index);
1334        exit(1);
1335    }
1336    proc[proc_index]->index = proc_loc_index;
1337
1338    cluster[cluster_index]->procs++;
1339    proc_loc_index++;
1340    proc_index++;
1341} // end procNode()
1342
1343
1344//////////////////////////////////////
1345void psegNode(xmlTextReaderPtr reader) 
1346{
1347    unsigned int ok;
1348    paddr_t      ll_value;
1349    char * str;
1350
1351    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1352
1353    if (pseg_index >= MAX_PSEGS) 
1354    {
1355        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1356        exit(1);
1357    }
1358
1359#if XML_PARSER_DEBUG
1360printf("    pseg %d\n", pseg_index);
1361#endif
1362
1363    pseg[pseg_index] = (mapping_pseg_t *) malloc(sizeof(mapping_pseg_t));
1364
1365    /////// get name attribute
1366    str = getStringValue(reader, "name", &ok);
1367#if XML_PARSER_DEBUG
1368printf("      name = %s\n", str);
1369#endif
1370    if (ok) 
1371    {
1372        strncpy(pseg[pseg_index]->name, str, 31);
1373    }
1374    else 
1375    {
1376        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1377                pseg_index, cluster_index);
1378        exit(1);
1379    }
1380
1381    //////// get type attribute
1382    str = getStringValue(reader, "type", &ok);
1383#if XML_PARSER_DEBUG
1384printf("      type = %s\n", str);
1385#endif
1386    if      (ok && (strcmp(str, "RAM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_RAM; }
1387    else if (ok && (strcmp(str, "ROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1388    else if (ok && (strcmp(str, "DROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1389    else if (ok && (strcmp(str, "PERI") == 0)) { pseg[pseg_index]->type = PSEG_TYPE_PERI; }
1390    else 
1391    {
1392        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1393                pseg[pseg_index]->name, cluster_index);
1394        exit(1);
1395    }
1396
1397    //////// get base attribute
1398    ll_value = getPaddrValue(reader, "base", &ok);
1399#if XML_PARSER_DEBUG
1400printf("      base = 0x%llx\n", ll_value);
1401#endif
1402    if (ok) 
1403    {
1404        pseg[pseg_index]->base = ll_value;
1405    }
1406    else {
1407        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1408                pseg[pseg_index]->name, cluster_index);
1409        exit(1);
1410    }
1411
1412    //////// get length attribute
1413    ll_value = getPaddrValue(reader, "length", &ok);
1414#if XML_PARSER_DEBUG
1415printf("      length = 0x%llx\n", ll_value);
1416#endif
1417    if (ok) 
1418    {
1419        pseg[pseg_index]->length = ll_value;
1420    } 
1421    else 
1422    {
1423        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1424                pseg[pseg_index]->name, cluster_index);
1425        exit(1);
1426    }
1427
1428    //////// set cluster attribute
1429    pseg[pseg_index]->clusterid = cluster_index;
1430
1431    //////// set next_vseg attribute
1432    pseg[pseg_index]->next_vseg = 0;
1433
1434    pseg_index++;
1435    cluster[cluster_index]->psegs++;
1436} // end psegNode()
1437
1438
1439/////////////////////////////////////////
1440void clusterNode(xmlTextReaderPtr reader) 
1441{
1442    unsigned int ok;
1443    unsigned int value;
1444
1445    cluster[cluster_index] = (mapping_cluster_t *) malloc(sizeof(mapping_cluster_t));
1446
1447    //initialise variables that will be incremented by *Node() functions
1448    cluster[cluster_index]->psegs = 0;
1449    cluster[cluster_index]->procs = 0;
1450    cluster[cluster_index]->coprocs = 0;
1451    cluster[cluster_index]->periphs = 0;
1452
1453    //initialise global variables
1454    proc_loc_index = 0;
1455    coproc_loc_index = 0;
1456    periph_loc_index = 0;
1457
1458    // for replicated periph
1459    found_timer = 0;
1460    found_icu   = 0;
1461    found_xcu   = 0;
1462    found_dma   = 0;
1463    found_mmc   = 0;
1464
1465    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)  return;
1466
1467#if XML_PARSER_DEBUG
1468printf("\n  cluster %d\n", cluster_index);
1469#endif
1470
1471    /////////// get x coordinate
1472    value = getIntValue(reader, "x", &ok);
1473#if XML_PARSER_DEBUG
1474printf("    x             = %d\n", value);
1475#endif
1476    if (ok && (value < header->x_size) ) 
1477    {
1478        cluster[cluster_index]->x = value;
1479    }
1480    else
1481    {
1482        printf("[XML ERROR] Illegal or missing < x > attribute for cluster %d", 
1483                cluster_index);
1484        exit(1);
1485    }
1486
1487    /////////// get y coordinate
1488    value = getIntValue(reader, "y", &ok);
1489#if XML_PARSER_DEBUG
1490printf("    y             = %d\n", value);
1491#endif
1492    if (ok && (value < header->y_size) ) 
1493    {
1494        cluster[cluster_index]->y = value;
1495    }
1496    else
1497    {
1498        printf("[XML ERROR] Illegal or missing < y > attribute for cluster %d", 
1499                cluster_index);
1500        exit(1);
1501    }
1502
1503    ////////// set offsets
1504    cluster[cluster_index]->pseg_offset = pseg_index;
1505    cluster[cluster_index]->proc_offset = proc_index;
1506    cluster[cluster_index]->coproc_offset = coproc_index;
1507    cluster[cluster_index]->periph_offset = periph_index;
1508
1509#if XML_PARSER_DEBUG
1510printf("    pseg_offset   = %d\n", pseg_index);
1511printf("    proc_offset   = %d\n", proc_index);
1512printf("    coproc_offset = %d\n", coproc_index);
1513printf("    periph_offset = %d\n", coproc_index);
1514#endif
1515
1516    ////////// get psegs, procs, coprocs and periphs
1517    int status = xmlTextReaderRead(reader);
1518
1519    while (status == 1) 
1520    {
1521        const char * tag = (const char *) xmlTextReaderConstName(reader);
1522
1523        if      (strcmp(tag, "pseg")     == 0) psegNode(reader);
1524        else if (strcmp(tag, "proc")     == 0) procNode(reader);
1525        else if (strcmp(tag, "coproc")   == 0) coprocNode(reader);
1526        else if (strcmp(tag, "periph")   == 0) periphNode(reader);
1527        else if (strcmp(tag, "#text")    == 0) { }
1528        else if (strcmp(tag, "#comment") == 0) { }
1529        else if (strcmp(tag, "cluster")  == 0) 
1530        {
1531
1532#if XML_PARSER_DEBUG
1533printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1534printf("    procs   = %d\n", cluster[cluster_index]->procs);
1535printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
1536printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1537printf("    end cluster %d\n", cluster_index);
1538#endif
1539            cluster_index++;
1540            return;
1541        }
1542        status = xmlTextReaderRead(reader);
1543    }
1544} // end clusterNode()
1545
1546
1547//////////////////////////////////////////////
1548void clusterSetNode(xmlTextReaderPtr reader) 
1549{
1550    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1551
1552#if XML_PARSER_DEBUG
1553printf("\n  clusters set\n");
1554#endif
1555
1556    int status = xmlTextReaderRead(reader);
1557    while (status == 1) 
1558    {
1559        const char * tag = (const char *) xmlTextReaderConstName(reader);
1560
1561        if      (strcmp(tag, "cluster")    == 0) { clusterNode(reader); }
1562        else if (strcmp(tag, "#text")      == 0) { }
1563        else if (strcmp(tag, "#comment")   == 0) { }
1564        else if (strcmp(tag, "clusterset") == 0) 
1565        {
1566            // checking number of clusters
1567            if ( cluster_index != (header->x_size * header->y_size) ) 
1568            {
1569                printf("[XML ERROR] Wrong number of clusters\n");
1570                exit(1);
1571            }
1572
1573#if XML_PARSER_DEBUG
1574            printf("  end cluster set\n\n");
1575#endif
1576            header->psegs = pseg_index;
1577            header->procs = proc_index;
1578            header->irqs = irq_index;
1579            header->coprocs = coproc_index;
1580            header->cp_ports = cp_port_index;
1581            header->periphs = periph_index;
1582            return;
1583        }
1584        else 
1585        {
1586            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1587            exit(1);
1588        }
1589        status = xmlTextReaderRead(reader);
1590    }
1591} // end clusterSetNode()
1592
1593
1594///////////////////////////////////////////
1595void globalSetNode(xmlTextReaderPtr reader) 
1596{
1597    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1598
1599#if XML_PARSER_DEBUG
1600    printf("  globals set\n");
1601#endif
1602
1603    int status = xmlTextReaderRead(reader);
1604    while (status == 1) 
1605    {
1606        const char * tag = (const char *) xmlTextReaderConstName(reader);
1607
1608        if      (strcmp(tag, "vseg")      == 0) 
1609        { 
1610            vsegNode( reader ); 
1611            header->globals = header->globals + 1;
1612        }
1613        else if (strcmp(tag, "#text")     == 0) { }
1614        else if (strcmp(tag, "#comment")  == 0) { }
1615        else if (strcmp(tag, "globalset") == 0) 
1616        {
1617#if XML_PARSER_DEBUG
1618            printf("  end global set\n\n");
1619#endif
1620            vseg_loc_index = 0;
1621            return;
1622        }
1623        else 
1624        {
1625            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1626            exit(1);
1627        }
1628        status = xmlTextReaderRead(reader);
1629    }
1630} // end globalSetNode()
1631
1632
1633///////////////////////////////////////////
1634void vspaceSetNode(xmlTextReaderPtr reader)
1635{
1636    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1637        return;
1638    }
1639
1640#if XML_PARSER_DEBUG
1641    printf("\n  vspaces 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, "vspace") == 0) {
1649            vspaceNode(reader);
1650        }
1651        else if (strcmp(tag, "#text"    ) == 0 ) { }
1652        else if (strcmp(tag, "#comment" ) == 0 ) { }
1653        else if (strcmp(tag, "vspaceset") == 0 ) 
1654        {
1655            header->vsegs = vseg_index;
1656            header->tasks = task_index;
1657            return;
1658        }
1659        else 
1660        {
1661            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1662            exit(1);
1663        }
1664        status = xmlTextReaderRead(reader);
1665    }
1666} // end globalSetNode()
1667
1668
1669////////////////////////////////////////
1670void headerNode(xmlTextReaderPtr reader) 
1671{
1672    char * name;
1673    unsigned int value;
1674    unsigned int ok;
1675
1676    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1677
1678#if XML_PARSER_DEBUG
1679    printf("mapping_info\n");
1680#endif
1681
1682    header = (mapping_header_t *) malloc(sizeof(mapping_header_t));
1683
1684    ////////// get name attribute
1685    name = getStringValue(reader, "name", &ok);
1686    if (ok) 
1687    {
1688#if XML_PARSER_DEBUG
1689        printf("  name = %s\n", name);
1690#endif
1691        strncpy( header->name, name, 31);
1692    }
1693    else 
1694    {
1695        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1696        exit(1);
1697    }
1698
1699    /////////// get signature attribute
1700    value = getIntValue(reader, "signature", &ok);
1701    if ( ok && (value == IN_MAPPING_SIGNATURE) )
1702    {
1703#if XML_PARSER_DEBUG
1704        printf("  signature = %x\n", value);
1705#endif
1706        header->signature = IN_MAPPING_SIGNATURE;
1707    }
1708    else
1709    {
1710        printf("[XML ERROR] illegal or missing <signature> for mapping %s\n",
1711        header->name );
1712        exit(1);
1713    }
1714
1715    /////////// get x_width attribute
1716    value = getIntValue(reader, "x_width", &ok);
1717    if (ok) 
1718    {
1719#if XML_PARSER_DEBUG
1720        printf("  x_width = %d\n", value);
1721#endif
1722        header->x_width = value;
1723    }
1724
1725    /////////// get y_width attribute
1726    value = getIntValue(reader, "y_width", &ok);
1727    if (ok) 
1728    {
1729#if XML_PARSER_DEBUG
1730        printf("  y_width = %d\n", value);
1731#endif
1732        header->y_width = value;
1733    }
1734
1735    /////////// get x_size attribute
1736    unsigned int x_size = getIntValue(reader, "x_size", &ok);
1737    if (ok) 
1738    {
1739#if XML_PARSER_DEBUG
1740        printf("  x_size  = %d\n", x_size);
1741#endif
1742        header->x_size = x_size;
1743    }
1744    else 
1745    {
1746        printf("[XML ERROR] illegal or missing <x_size> attribute in header\n");
1747        exit(1);
1748    }
1749
1750    /////////// get y_size attribute
1751    unsigned int y_size = getIntValue(reader, "y_size", &ok);
1752    if (ok) 
1753    {
1754#if XML_PARSER_DEBUG
1755        printf("  y_size  = %d\n", y_size);
1756#endif
1757        header->y_size = y_size;
1758    }
1759    else 
1760    {
1761        printf("[XML ERROR] illegal or missing <y_size> attribute in header\n");
1762        exit(1);
1763    }
1764
1765    /////////// get x_io attribute
1766    unsigned int x_io = getIntValue(reader, "x_io", &ok);
1767#if XML_PARSER_DEBUG
1768        printf("  x_io      = %d\n", x_io);
1769#endif
1770    if ( ok && (x_io < x_size) ) 
1771    {
1772        header->x_io = x_io;
1773    }
1774    else 
1775    {
1776        printf("[XML ERROR] illegal or missing <x_io> attribute in header\n");
1777        exit(1);
1778    }
1779
1780    /////////// get y_io attribute
1781    unsigned int y_io = getIntValue(reader, "y_io", &ok);
1782#if XML_PARSER_DEBUG
1783        printf("  y_io      = %d\n", y_io);
1784#endif
1785    if ( ok &&(y_io < y_size) ) 
1786    {
1787        header->y_io = y_io;
1788    }
1789    else 
1790    {
1791        printf("[XML ERROR] illegal or missing <y_io> attribute in header\n");
1792        exit(1);
1793    }
1794
1795    // check the number of cluster
1796    if ( (x_size * y_size) >= MAX_CLUSTERS )
1797    {
1798        printf("[XML ERROR] Number of clusters cannot be larger than %d\n", MAX_CLUSTERS);
1799        exit(1);
1800    }
1801
1802    ///////// get irq_per_proc attribute
1803    value = getIntValue(reader, "irq_per_proc", &ok);
1804    if (ok) 
1805    {
1806#if XML_PARSER_DEBUG
1807        printf("  irq_per_proc = %d\n", value);
1808#endif
1809        header->irq_per_proc = value;
1810    }
1811    else 
1812    {
1813        printf("[XML ERROR] illegal or missing <irq_per_proc> attribute in mapping\n");
1814        exit(1);
1815    }
1816
1817    ///////// get use_ram_disk attribute (default = 0)
1818    value = getIntValue(reader, "use_ram_disk", &ok);
1819    if (ok) 
1820    {
1821#if XML_PARSER_DEBUG
1822        printf("  use_ram_disk = %d\n", value);
1823#endif
1824        header->use_ram_disk = value;
1825    }
1826    else 
1827    {
1828        header->use_ram_disk = 0;
1829    }
1830
1831    ///////// set other header fields
1832    header->globals   = 0;
1833    header->vspaces   = 0;
1834    header->psegs     = 0;
1835    header->vsegs     = 0;
1836    header->tasks     = 0;
1837    header->procs     = 0;
1838    header->irqs      = 0;
1839    header->coprocs   = 0;
1840    header->cp_ports  = 0;
1841    header->periphs   = 0;
1842
1843
1844    int status = xmlTextReaderRead(reader);
1845    while (status == 1) 
1846    {
1847        const char * tag = (const char *) xmlTextReaderConstName(reader);
1848
1849        if      (strcmp(tag, "clusterset")   == 0) { clusterSetNode(reader); }
1850        else if (strcmp(tag, "globalset")    == 0) { globalSetNode(reader); }
1851        else if (strcmp(tag, "vspaceset")    == 0) { vspaceSetNode(reader); }
1852        else if (strcmp(tag, "#text")        == 0) { }
1853        else if (strcmp(tag, "#comment")     == 0) { }
1854        else if (strcmp(tag, "mapping_info") == 0) 
1855        {
1856#if XML_PARSER_DEBUG
1857            printf("end mapping_info\n");
1858#endif
1859            return;
1860        }
1861        else 
1862        {
1863            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1864            exit(1);
1865        }
1866        status = xmlTextReaderRead(reader);
1867    }
1868} // end headerNode()
1869
1870
1871////////////////////////////////////
1872void BuildTable( int          fdout, 
1873                 const char * type, 
1874                 unsigned int nb_elem,
1875                 unsigned int elem_size, 
1876                 char ** table ) 
1877{
1878    unsigned int i;
1879    for (i = 0; i < nb_elem; i++) 
1880    {
1881        if (elem_size != write(fdout, table[i], elem_size)) 
1882        {
1883            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
1884            exit(1);
1885        }
1886
1887#if XML_PARSER_DEBUG
1888        printf("Building binary: writing %s %d\n", type, i);
1889#endif
1890    }
1891}
1892
1893/////////////////////////////////////
1894int open_file(const char * file_path) 
1895{
1896    //open file
1897    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
1898    if (fdout < 0) 
1899    {
1900        perror("open");
1901        exit(1);
1902    }
1903
1904    //reinitialise the file
1905    if (ftruncate(fdout, 0)) 
1906    {
1907        perror("truncate");
1908        exit(1);
1909    }
1910
1911    //#if XML_PARSER_DEBUG
1912    printf("%s\n", file_path);
1913    //#endif
1914
1915    return fdout;
1916}
1917
1918
1919/////////////////////////////////////
1920void buildBin(const char * file_path) 
1921{
1922    unsigned int length;
1923
1924    int fdout = open_file(file_path);
1925
1926#if XML_PARSER_DEBUG
1927printf("Building map.bin for %s\n", header->name);
1928printf("signature = %x\n", header->signature);
1929printf("x_size    = %d\n", header->x_size);
1930printf("y_size    = %d\n", header->y_size);
1931printf("x_width   = %d\n", header->x_width);
1932printf("y_width   = %d\n", header->y_width);
1933printf("vspaces   = %d\n", header->vspaces);
1934printf("psegs     = %d\n", header->psegs);
1935printf("vsegs     = %d\n", header->vsegs);
1936printf("tasks     = %d\n", header->tasks);
1937printf("procs     = %d\n", header->procs);
1938printf("irqs      = %d\n", header->irqs);
1939printf("coprocs   = %d\n", header->coprocs);
1940printf("periphs   = %d\n", header->periphs);
1941#endif
1942
1943    // write header to binary file
1944    length = write(fdout, (char *) header, sizeof(mapping_header_t));
1945    if (length != sizeof(mapping_header_t)) 
1946    {
1947        printf("write header error : length = %d \n", length);
1948        exit(1);
1949    }
1950
1951    // write clusters
1952    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char **) cluster);
1953    // write psegs
1954    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char **) pseg);
1955    // write vspaces
1956    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char **) vspace);
1957    // write vsegs
1958    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char **) vseg);
1959    // write tasks array
1960    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char **) task);
1961    //building procs array
1962    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char **) proc);
1963    //building irqs array
1964    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char **) irq);
1965    //building coprocs array
1966    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char **) coproc);
1967    //building cp_ports array
1968    BuildTable(fdout, "cp_port", cp_port_index, sizeof(mapping_cp_port_t),(char **) cp_port);
1969    //building periphs array
1970    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char **) periph);
1971
1972    close(fdout);
1973
1974} // end buildBin()
1975
1976
1977//////////////////////////////////////////////////////
1978char * buildPath(const char * path, const char * name) 
1979{
1980    char * res = calloc(strlen(path) + strlen(name) + 1, 1);
1981    strcat(res, path);
1982    strcat(res, "/");
1983    strcat(res, name);
1984    return res; 
1985}
1986
1987
1988//////////////////////////////////
1989int main(int argc, char * argv[]) 
1990{
1991    if (argc < 3) 
1992    {
1993        printf("Usage: xml2bin <input_file_path> <output_path>\n");
1994        return 1;
1995    }
1996
1997    struct stat dir_st;
1998    if (stat( argv[2], &dir_st)) 
1999    {
2000        perror("bad path");
2001        exit(1);
2002    }
2003
2004    if ((dir_st.st_mode & S_IFDIR) == 0) 
2005    {
2006        printf("path is not a dir: %s", argv[2] );
2007        exit(1);
2008    }
2009
2010    char * map_path = buildPath(argv[2], "map.bin"); 
2011
2012    LIBXML_TEST_VERSION;
2013
2014    int status;
2015    xmlTextReaderPtr reader = xmlReaderForFile(argv[1], NULL, 0);
2016
2017    if (reader != NULL) 
2018    {
2019        status = xmlTextReaderRead (reader);
2020        while (status == 1) 
2021        {
2022            const char * tag = (const char *) xmlTextReaderConstName(reader);
2023
2024            if (strcmp(tag, "mapping_info") == 0) 
2025            { 
2026                headerNode(reader);
2027                buildBin(map_path);
2028            }
2029            else 
2030            {
2031                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
2032                return 1;
2033            }
2034            status = xmlTextReaderRead(reader);
2035        }
2036        xmlFreeTextReader(reader);
2037
2038        if (status != 0) 
2039        {
2040            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
2041            return 1;
2042        }
2043    }
2044    return 0;
2045} // end main()
2046
2047
2048// Local Variables:
2049// tab-width: 4
2050// c-basic-offset: 4
2051// c-file-offsets:((innamespace . 0)(inline-open . 0))
2052// indent-tabs-mode: nil
2053// End:
2054// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
2055
Note: See TracBrowser for help on using the repository browser.