Index: soft/giet_vm/xml/xml_parser.c
===================================================================
--- soft/giet_vm/xml/xml_parser.c	(revision 206)
+++ soft/giet_vm/xml/xml_parser.c	(revision 215)
@@ -86,10 +86,60 @@
 unsigned int		vobj_count     = 0;
 
-///////////////////////////////////////////////////////////////////////
+
+/////////////////////////
+// found peripheral
+/////////////////////////
+char found_timer = 0;
+char found_icu = 0;
+char found_xicu = 0;
+char found_dma = 0;
+
+
+//////////////////////////////////
+//needed to generate map_config.ld
+//////////////////////////////////
+unsigned int cluster_y              = 0;
+unsigned int cluster_x              = 0; 
+unsigned int nb_proc_max            = 0; // max number of processors per cluster 
+unsigned int nb_timer_channel_max   = 0; // max number of user timer
+unsigned int nb_dma_channel_max     = 0;
+unsigned int nb_tty_channel         = 0;
+unsigned int nb_ioc_channel         = 0;
+unsigned int nb_nic_channel         = 0;
+unsigned int io_mmu_active          = 0;
+unsigned int use_xicu               = 0xFFFFFFFF;
+
+
+//////////////////////////////////
+//needed to generate giet_vseg.ld
+//////////////////////////////////
+
+//kernel and boot code
+unsigned int kernel_code_base    = 0x80000000;	/* kernel code */
+unsigned int kernel_data_base    = 0x80010000;	/* system cacheable data */
+unsigned int kernel_uncdata_base = 0x80080000;	/* system uncacheable data */
+unsigned int kernel_init_base    = 0x80090000;	/* system init entry */ 
+
+unsigned int boot_code_base      = 0xBFC00000;   /* boot code */
+unsigned int boot_stack_base     = 0xBFC08000;	/* boot temporary stack */ 
+unsigned int boot_mapping_base   = 0xBFC0C000;	/* mapping_info blob */
+
+//periphs
+unsigned int tim_base_offset = 0xFFFFFFFF;
+unsigned int tty_base_offset = 0xFFFFFFFF;
+unsigned int dma_base_offset = 0xFFFFFFFF;
+unsigned int ioc_base_offset = 0xFFFFFFFF;
+unsigned int fbf_base_offset = 0xFFFFFFFF;
+unsigned int icu_base_offset = 0xFFFFFFFF;
+unsigned int gcd_base_offset = 0xFFFFFFFF;
+unsigned int iob_base_offset = 0xFFFFFFFF;
+
+
+//////////////////////////////////////////////////////////////////////
 // This arrray is useful to build a temporary list of vobj references. 
 // The struct vobj_ref_s is formed by a vspace_name and a vobj_name.
 // This array is used to set the attribute vobj_id of a cp_port
 // once all the vspace have been parsed.
-
+/////////////////////////////////////////////////////////////////////
 typedef struct vobj_ref_s
 {
@@ -159,4 +209,5 @@
     char* string = (char*)xmlTextReaderGetAttribute(reader, (const xmlChar*)attributeName);
 
+    
     if ( string == NULL )  // missing argument
     {
@@ -166,4 +217,11 @@
     else
     {
+        //we read only string smaller than 32 byte
+        if(strlen(string) > 32)
+        {
+            printf("[XML ERROR] all strings must be less than 32 bytes\n");
+            exit(1);
+        }
+
         *ok = 1;
         return string;
@@ -181,5 +239,8 @@
     for ( pseg_id = pseg_min ; pseg_id < pseg_max ; pseg_id++ )
     {
-        if ( strcmp(pseg[pseg_id]->name, pseg_name) == 0 ) return pseg_id;
+        if ( strcmp(pseg[pseg_id]->name, pseg_name) == 0 ) 
+        {
+            return pseg_id;
+        }
     }
     return -1;
@@ -219,492 +280,4 @@
     return -1;
 }
-
-///////////////////////////////////////////
-void cpPortNode ( xmlTextReaderPtr reader )
-///////////////////////////////////////////
-{
-    char*           str;
-    unsigned int    ok;
-
-    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
-
-    if ( cp_port_index >= MAX_CP_PORTS )
-    {
-        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", MAX_CP_PORTS);
-    }
-
-#if XML_PARSER_DEBUG
-printf("\n  port %d\n", cp_port_index);
-#endif
-
-    cp_port[cp_port_index] = (mapping_cp_port_t*)malloc(sizeof(mapping_cp_port_t));
-    cp_port_vobj_ref[cp_port_index] = (vobj_ref_t*)malloc(sizeof(vobj_ref_t));
-
-
-    
-    ///////// get direction attribute
-    str = getStringValue( reader, "direction", &ok );
-    if ( ok )
-    {
-#if XML_PARSER_DEBUG
-printf("      direction = %s\n", str);
-#endif
-        if      ( strcmp(str, "TO_COPROC")   ==  0 ) cp_port[cp_port_index]->direction = PORT_TO_COPROC;
-        else if ( strcmp(str, "FROM_COPROC") ==  0 ) cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
-        else
-        {
-            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
-                   cp_port_index, cluster_index);
-            exit(1);
-        }
-    }  
-    else
-    {
-        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
-               cp_port_index, cluster_index);
-        exit(1);
-    }
-    
-    /////////// get vspacename attribute 
-    str = getStringValue( reader, "vspacename", &ok );
-#if XML_PARSER_DEBUG
-printf("      vspacename = %s\n", str);
-#endif
-    if ( ok ) 
-    {
-        strncpy(cp_port_vobj_ref[cp_port_index]->vspace_name, str, 31);
-    }
-    else
-    {
-        printf("[XML ERROR] missing <vspacename> for cp_port %d in cluster %d\n",
-               cp_port_index, cluster_index);
-        exit(1);
-    }
-
-    /////////// get vobjname attribute 
-    str = getStringValue( reader, "vobjname", &ok );
-#if XML_PARSER_DEBUG
-printf("      vobjname = %s\n", str);
-#endif
-    if ( ok ) 
-    {
-        strncpy(cp_port_vobj_ref[cp_port_index]->vobj_name, str, 31);
-    }
-    else
-    {
-        printf("[XML ERROR] missing <vobjname> for cp_port %d in cluster %d\n",
-               cp_port_index, cluster_index);
-        exit(1);
-    }
-    
-    cp_port_index++;
-    cp_port_loc_index++;
-
-} // end cpPortNode()
-
-///////////////////////////////////////////
-void periphNode ( xmlTextReaderPtr reader )
-///////////////////////////////////////////
-{
-    char*           str;
-    unsigned int    value;
-    unsigned int 	ok;
-
-    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
-
-    if ( periph_index >= MAX_PERIPHS )
-    {
-        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
-    }
-
-#if XML_PARSER_DEBUG
-printf("\n  periph %d\n", periph_index);
-#endif
-
-    periph[periph_index] = (mapping_periph_t*)malloc(sizeof(mapping_periph_t));
-
-    /////////// get type attribute 
-    str = getStringValue( reader, "type", &ok );
-    if ( ok ) 
-    {
-#if XML_PARSER_DEBUG
-printf("      type     = %s\n", str);
-#endif
-        unsigned int error = 0;
-  
-        // The TTY, IOC, NIC, FBF and IOB peripherals cannot be replicated
-        if      ( strcmp( str, "IOC" ) == 0 ) 
-        {
-            periph[periph_index]->type = PERIPH_TYPE_IOC;
-            if ( header->ioc_clusterid == 0xFFFFFFFF) header->ioc_clusterid = cluster_index;
-            else  error = 1;
-        } 
-        else if ( strcmp( str, "TTY" ) == 0 )
-        {
-            periph[periph_index]->type = PERIPH_TYPE_TTY;
-            if ( header->ioc_clusterid == 0xFFFFFFFF) header->ioc_clusterid = cluster_index;
-            else  error = 1;
-        }
-        else if ( strcmp( str, "FBF" ) == 0 ) 
-        {
-            periph[periph_index]->type = PERIPH_TYPE_FBF;
-            if ( header->ioc_clusterid == 0xFFFFFFFF) header->ioc_clusterid = cluster_index;
-            else  error = 1;
-        }
-        else if ( strcmp( str, "NIC" ) == 0 ) 
-        {
-            periph[periph_index]->type = PERIPH_TYPE_NIC;
-            if ( header->ioc_clusterid == 0xFFFFFFFF) header->ioc_clusterid = cluster_index;
-            else  error = 1;
-        }
-        // The TIM, DMA and IOB peripherals can be replicated in several clusters
-        else if ( strcmp( str, "TIM" ) == 0 ) 
-        {
-            periph[periph_index]->type = PERIPH_TYPE_TIM;
-        }
-        else if ( strcmp( str, "DMA" ) == 0 ) 
-        {
-            periph[periph_index]->type = PERIPH_TYPE_DMA;
-        }
-        else if ( strcmp( str, "IOB" ) == 0 ) 
-        {
-            periph[periph_index]->type = PERIPH_TYPE_IOB;
-        }
-        else
-        {
-            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
-                   periph_loc_index, cluster_index);
-            exit(1);
-        }
-    }
-    else
-    {
-        printf("[XML ERROR] missing <type> for peripheral  %d in cluster %d\n",
-               periph_loc_index, cluster_index);
-        exit(1);
-    }
-
-    ///////// get channels attribute (optionnal : 1 if missing)
-    value = getIntValue( reader, "channels", &ok );
-    if ( ok )
-    {
-#if XML_PARSER_DEBUG
-printf("      channels = %d\n", value);
-#endif
-        periph[periph_index]->channels = value;
-    }
-    else
-    {
-        periph[periph_index]->channels = 1;
-    }
-
-    /////////// get psegname attribute 
-    str = getStringValue(reader,"psegname", &ok);
-    if ( ok == 0 )
-    {
-        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
-                 coproc_index, cluster_index);
-        exit(1);
-    }
-
-    /////////// set psegid attribute
-    int index = getPsegId( cluster_index, str );
-    if ( index >= 0 ) 
-    {
-#if XML_PARSER_DEBUG
-printf("      clusterid = %d\n", cluster_index);
-printf("      psegname  = %s\n", str);
-printf("      psegid    = %d\n", index);
-#endif
-        periph[periph_index]->psegid = index;
-        assert(pseg[index]->type == PSEG_TYPE_PERI && 
-        "peripheral psegname attribute must refer to a pseg of type PERI" );
-    }
-    else              
-    {
-        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
-                   periph_loc_index, cluster_index, str );
-        exit(1);
-    }  
-    
-    periph_index++;
-    periph_loc_index++;
-
-} // end periphNode
-
-/////////////////////////////////////////
-void coprocNode ( xmlTextReaderPtr reader )
-/////////////////////////////////////////
-{
-    char*           str;
-    unsigned int 	ok;
-
-    cp_port_loc_index = 0;
-
-    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
-
-    if ( coproc_index >= MAX_COPROCS )
-    {
-        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
-    }
-
-#if XML_PARSER_DEBUG
-printf("\n  coproc %d\n", coproc_index);
-#endif
-
-    coproc[coproc_index] = (mapping_coproc_t*)malloc(sizeof(mapping_coproc_t));
-
-    /////////// get name attribute 
-    str = getStringValue( reader, "name", &ok );
-    if ( ok ) 
-    {
-#if XML_PARSER_DEBUG
-printf("      name = %s\n", str);
-#endif
-        strncpy(coproc[coproc_index]->name, str, 31);
-    }
-    else
-    {
-        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
-               coproc_index, cluster_index);
-        exit(1);
-    }
-
-    /////////// get psegname attribute 
-    str = getStringValue(reader,"psegname", &ok);
-    if ( ok == 0 )
-    {
-        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
-                 coproc_index, cluster_index);
-        exit(1);
-    }
-
-    /////////// set psegid attribute
-    int index = getPsegId( cluster_index, str );
-    if ( index >= 0 ) 
-    {
-#if XML_PARSER_DEBUG
-printf("      clusterid = %d\n", cluster_index);
-printf("      psegname  = %s\n", str);
-printf("      psegid    = %d\n", index);
-#endif
-        coproc[coproc_index]->psegid = index;
-        assert(pseg[index]->type == PSEG_TYPE_PERI && "coproc psegname attribute must refer to a pseg of type PERI" );
-    }
-    else              
-    {
-        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
-                   coproc_index, cluster_index, str );
-        exit(1);
-    }  
-
-    ////////// set port_offset 
-    coproc[coproc_index]->port_offset = cp_port_index;
-
-#if XML_PARSER_DEBUG
-printf("      port_offset = %d\n", cp_port_index);
-#endif
-
-    int status = xmlTextReaderRead(reader);
-    while ( status == 1 ) 
-    {
-        const char* tag = (const char*)xmlTextReaderConstName(reader);
-
-        if      ( strcmp(tag, "port")     == 0 ) cpPortNode(reader);
-        else if ( strcmp(tag, "#text")    == 0 ) { }
-        else if ( strcmp(tag, "#comment") == 0 ) { }
-        else if ( strcmp(tag, "coproc")   == 0 ) 
-        {
-            coproc[coproc_index]->ports = cp_port_loc_index;
-            coproc_loc_index++;
-            coproc_index++;
-            return;
-        }
-        else 
-        {
-            printf("[XML ERROR] Unknown tag %s",tag);
-            exit(1);
-        }
-        status = xmlTextReaderRead ( reader );
-    }
-} // end coprocNode()
-
-///////////////////////////////////////
-void irqNode( xmlTextReaderPtr reader )
-///////////////////////////////////////
-{
-    unsigned int 	ok;
-    unsigned int 	value;
-    char*    		str;
-
-    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
-
-    if ( irq_index >= MAX_IRQS )
-    {
-        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
-    }
-
-#if XML_PARSER_DEBUG
-printf("     irq %d\n", irq_loc_index);
-#endif
-
-    irq[irq_index] = (mapping_irq_t*)malloc(sizeof(mapping_irq_t));
-
-    ///////// get type attribute
-    str = getStringValue(reader,"type", &ok);
-    if ( ok )
-    {
-#if XML_PARSER_DEBUG
-printf("        type    = %s\n", str);
-#endif
-        if      ( strcmp(str, "HARD") == 0 ) irq[irq_index]->type = 0;
-        else if ( strcmp(str, "SOFT") == 0 ) irq[irq_index]->type = 1;
-        else
-        {
-            printf("[XML ERROR] undefined IRQ  <type> for processor %d in cluster %d\n",
-                   cluster_index, proc_loc_index );
-            exit(1);
-        }
-    }  
-    else
-    {
-        printf("[XML ERROR] missing IRQ <type> for processor %d in cluster %d\n",
-               cluster_index, proc_loc_index );
-        exit(1);
-    }
-
-    ///////// get icuid attribute
-    value = getIntValue(reader, "icuid", &ok);
-    if ( ok )
-    {
-#if XML_PARSER_DEBUG
-printf("        icuid   = %d\n", value);
-#endif
-        irq[irq_index]->icuid = value;
-        if ( value >= 32 )
-        {
-            printf("[XML ERROR] IRQ <icuid> too large for processor %d in cluster %d\n",
-                   cluster_index, proc_loc_index );
-            exit(1);
-        }
-    }
-    else
-    {
-        printf("[XML ERROR] missing IRQ <icuid> for processor %d in cluster %d\n",
-               cluster_index, proc_loc_index );
-        exit(1);
-    }
-
-    ///////// get isr attribute
-    str = getStringValue(reader,"isr", &ok);
-    if ( ok )
-    {
-#if XML_PARSER_DEBUG
-printf("        isr     = %s\n", str);
-#endif
-        if      ( strcmp(str, "ISR_SWITCH" ) == 0 ) irq[irq_index]->isr = ISR_SWITCH;
-        else if ( strcmp(str, "ISR_IOC"    ) == 0 ) irq[irq_index]->isr = ISR_IOC;
-        else if ( strcmp(str, "ISR_DMA"    ) == 0 ) irq[irq_index]->isr = ISR_DMA;
-        else if ( strcmp(str, "ISR_TTY"    ) == 0 ) irq[irq_index]->isr = ISR_TTY;
-        else if ( strcmp(str, "ISR_TIMER"  ) == 0 ) irq[irq_index]->isr = ISR_TIMER;
-        else
-        {
-            printf("[XML ERROR] illegal IRQ <isr> for processor %d in cluster %d\n",
-                   cluster_index, proc_loc_index );
-            exit(1);
-        }
-#if XML_PARSER_DEBUG
-printf("        isrnum  = %d\n", irq[irq_index]->isr);
-#endif
-    }  
-    else
-    {
-        printf("[XML ERROR] missing IRQ <isr> for processor %d in cluster %d\n",
-               cluster_index, proc_loc_index );
-        exit(1);
-    }
-
-    ///////// get channel attribute (optionnal : 0 if missing)
-    value = getIntValue(reader, "channel", &ok);
-    if ( ok )
-    {
-#if XML_PARSER_DEBUG
-printf("        channel = %d\n", value);
-#endif
-        irq[irq_index]->channel = value;
-    }
-    else
-    {
-        irq[irq_index]->channel = 0;
-    }
-
-    irq_index++;
-    irq_loc_index++;
-
-} // end irqNode
-
-/////////////////////////////////////////
-void procNode ( xmlTextReaderPtr reader )
-/////////////////////////////////////////
-{
-    unsigned int 	ok;
-    unsigned int    value;
-
-    irq_loc_index = 0;
-
-    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
-
-    if ( proc_index >= MAX_PROCS )
-    {
-        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
-    }
-
-#if XML_PARSER_DEBUG
-printf("\n  proc %d\n", proc_index);
-#endif
-
-    proc[proc_index] = (mapping_proc_t*)malloc(sizeof(mapping_proc_t));
-
-
-    /////////// get index attribute (optional)
-    value = getIntValue(reader,"index",&ok);
-    if ( ok && (value != proc_loc_index) )
-    {
-            printf("[XML ERROR] wrong proc index / expected value is %d", 
-                proc_loc_index);
-            exit(1);
-    }
-
-    ////////// set irq_offset attribute
-    proc[proc_index]->irq_offset = irq_index;
-
-#if XML_PARSER_DEBUG
-printf("    irq_offset = %d\n", irq_index);
-#endif
-
-    int status = xmlTextReaderRead(reader);
-    while ( status == 1 ) 
-    {
-        const char* tag = (const char*)xmlTextReaderConstName(reader);
-
-        if      ( strcmp(tag, "irq")      == 0 ) irqNode(reader);
-        else if ( strcmp(tag, "#text")    == 0 ) { }
-        else if ( strcmp(tag, "#comment") == 0 ) { }
-        else if ( strcmp(tag, "proc")     == 0 ) 
-        {
-            proc[proc_index]->irqs = irq_loc_index;
-            proc_loc_index++;
-            proc_index++;
-            return;
-        }
-        else 
-        {
-            printf("[XML ERROR] Unknown tag %s",tag);
-            exit(1);
-        }
-        status = xmlTextReaderRead ( reader );
-    }
-} // end procNode()
-
 
 /////////////////////////////////////////
@@ -1321,4 +894,556 @@
 } // end vspaceNode()
 
+///////////////////////////////////////////
+void cpPortNode ( xmlTextReaderPtr reader )
+///////////////////////////////////////////
+{
+    char*           str;
+    unsigned int    ok;
+
+    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
+
+    if ( cp_port_index >= MAX_CP_PORTS )
+    {
+        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", MAX_CP_PORTS);
+    }
+
+#if XML_PARSER_DEBUG
+printf("\n  port %d\n", cp_port_index);
+#endif
+
+    cp_port[cp_port_index] = (mapping_cp_port_t*)malloc(sizeof(mapping_cp_port_t));
+    cp_port_vobj_ref[cp_port_index] = (vobj_ref_t*)malloc(sizeof(vobj_ref_t));
+
+
+    
+    ///////// get direction attribute
+    str = getStringValue( reader, "direction", &ok );
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("      direction = %s\n", str);
+#endif
+        if      ( strcmp(str, "TO_COPROC")   ==  0 ) cp_port[cp_port_index]->direction = PORT_TO_COPROC;
+        else if ( strcmp(str, "FROM_COPROC") ==  0 ) cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
+        else
+        {
+            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
+                   cp_port_index, cluster_index);
+            exit(1);
+        }
+    }  
+    else
+    {
+        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
+               cp_port_index, cluster_index);
+        exit(1);
+    }
+    
+    /////////// get vspacename attribute 
+    str = getStringValue( reader, "vspacename", &ok );
+#if XML_PARSER_DEBUG
+printf("      vspacename = %s\n", str);
+#endif
+    if ( ok ) 
+    {
+        strncpy(cp_port_vobj_ref[cp_port_index]->vspace_name, str, 31);
+    }
+    else
+    {
+        printf("[XML ERROR] missing <vspacename> for cp_port %d in cluster %d\n",
+               cp_port_index, cluster_index);
+        exit(1);
+    }
+
+    /////////// get vobjname attribute 
+    str = getStringValue( reader, "vobjname", &ok );
+#if XML_PARSER_DEBUG
+printf("      vobjname = %s\n", str);
+#endif
+    if ( ok ) 
+    {
+        strncpy(cp_port_vobj_ref[cp_port_index]->vobj_name, str, 31);
+    }
+    else
+    {
+        printf("[XML ERROR] missing <vobjname> for cp_port %d in cluster %d\n",
+               cp_port_index, cluster_index);
+        exit(1);
+    }
+    
+    cp_port_index++;
+    cp_port_loc_index++;
+
+} // end cpPortNode()
+
+///////////////////////////////////////////
+void periphNode ( xmlTextReaderPtr reader )
+///////////////////////////////////////////
+{
+    char*           str;
+    unsigned int    value;
+    unsigned int 	ok;
+
+    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
+
+    if ( periph_index >= MAX_PERIPHS )
+    {
+        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
+    }
+
+#if XML_PARSER_DEBUG
+printf("\n  periph %d\n", periph_index);
+#endif
+
+    periph[periph_index] = (mapping_periph_t*)malloc(sizeof(mapping_periph_t));
+
+
+    ///////// get channels attribute (optionnal : 1 if missing)
+    value = getIntValue( reader, "channels", &ok );
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("      channels = %d\n", value);
+#endif
+        periph[periph_index]->channels = value;
+    }
+    else
+    {
+        periph[periph_index]->channels = 1;
+    }
+
+    /////////// get psegname attribute 
+    str = getStringValue(reader,"psegname", &ok);
+    if ( ok == 0 )
+    {
+        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
+                 coproc_index, cluster_index);
+        exit(1);
+    }
+
+    /////////// set psegid attribute
+    int index = getPsegId( cluster_index, str );
+    if ( index >= 0 ) 
+    {
+#if XML_PARSER_DEBUG
+printf("      clusterid = %d\n", cluster_index);
+printf("      psegname  = %s\n", str);
+printf("      psegid    = %d\n", index);
+#endif
+        periph[periph_index]->psegid = index;
+        assert(pseg[index]->type == PSEG_TYPE_PERI && 
+        "peripheral psegname attribute must refer to a pseg of type PERI" );
+    }
+    else              
+    {
+        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
+                   periph_loc_index, cluster_index, str );
+        exit(1);
+    }  
+
+
+    /////////// get type attribute 
+    str = getStringValue( reader, "type", &ok );
+    if ( ok ) 
+    {
+#if XML_PARSER_DEBUG
+printf("      type     = %s\n", str);
+#endif
+        unsigned int error = 0;
+  
+        // The TTY, IOC, NIC, FBF and IOB peripherals cannot be replicated
+        // one per architecture
+        if      ( strcmp( str, "IOC" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_IOC;
+            if ( header->ioc_clusterid == 0xFFFFFFFF) header->ioc_clusterid = cluster_index;
+            else  error = 1;
+
+            ioc_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+            nb_ioc_channel = periph[periph_index]->channels;
+        } 
+        else if ( strcmp( str, "TTY" ) == 0 )
+        {
+            periph[periph_index]->type = PERIPH_TYPE_TTY;
+            if ( header->tty_clusterid == 0xFFFFFFFF) header->tty_clusterid = cluster_index;
+            else  error = 1;
+
+            tty_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+            nb_tty_channel = periph[periph_index]->channels;
+        }
+        else if ( strcmp( str, "FBF" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_FBF;
+            if ( header->fbf_clusterid == 0xFFFFFFFF) header->fbf_clusterid = cluster_index;
+            else  error = 1;
+
+            fbf_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+        }
+        else if ( strcmp( str, "NIC" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_NIC;
+            if ( header->nic_clusterid == 0xFFFFFFFF) header->nic_clusterid = cluster_index;
+            else  error = 1;
+
+            //nic_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+            nb_nic_channel = periph[periph_index]->channels;
+        }
+        else if ( strcmp( str, "IOB" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_IOB;
+            iob_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+
+            if(io_mmu_active) error = 1;
+            io_mmu_active = 1;
+        }
+        // The TIM, ICU, XICU, DMA and IOB peripherals can be replicated in several clusters
+        // one per cluster
+        else if ( strcmp( str, "TIM" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_TIM;
+            if(found_timer) error = 1;
+            found_timer = 1;
+
+            if(tim_base_offset == 0xFFFFFFFF)
+                tim_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+
+            if(nb_timer_channel_max < periph[periph_index]->channels)
+                nb_timer_channel_max = periph[periph_index]->channels;
+        }
+        else if ( strcmp( str, "ICU" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_ICU;
+            if(found_icu) error = 1;
+            found_icu = 1;
+
+            if(icu_base_offset == 0xFFFFFFFF)
+                icu_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+        }
+        else if ( strcmp( str, "XICU" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_XICU;
+            if(found_xicu) error = 1;
+            found_xicu = 1;
+
+            //'icu' since we can't have both xicu and icu in an arch
+            if(icu_base_offset == 0xFFFFFFFF)
+                icu_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+        }
+        else if ( strcmp( str, "DMA" ) == 0 ) 
+        {
+            periph[periph_index]->type = PERIPH_TYPE_DMA;
+            if(found_dma) error = 1;
+            found_dma = 1;
+
+            if(dma_base_offset == 0xFFFFFFFF)
+                dma_base_offset = pseg[ periph[periph_index]->psegid ]->base;
+            if(nb_dma_channel_max < periph[periph_index]->channels)
+                nb_dma_channel_max = periph[periph_index]->channels;
+        }
+        else
+        {
+            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
+                   periph_loc_index, cluster_index);
+            exit(1);
+        }
+
+        if(error)
+        {
+            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
+                   periph_loc_index, cluster_index);
+            exit(1);
+        }
+    }
+    else
+    {
+        printf("[XML ERROR] missing <type> for peripheral  %d in cluster %d\n",
+               periph_loc_index, cluster_index);
+        exit(1);
+    }
+
+    
+    periph_index++;
+    periph_loc_index++;
+    cluster[cluster_index]->periphs++;
+
+} // end periphNode
+
+/////////////////////////////////////////
+void coprocNode ( xmlTextReaderPtr reader )
+/////////////////////////////////////////
+{
+    char*           str;
+    unsigned int 	ok;
+
+    cp_port_loc_index = 0;
+
+    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
+
+    if ( coproc_index >= MAX_COPROCS )
+    {
+        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
+    }
+
+#if XML_PARSER_DEBUG
+printf("\n  coproc %d\n", coproc_index);
+#endif
+
+    coproc[coproc_index] = (mapping_coproc_t*)malloc(sizeof(mapping_coproc_t));
+
+    /////////// get name attribute 
+    str = getStringValue( reader, "name", &ok );
+    if ( ok ) 
+    {
+#if XML_PARSER_DEBUG
+printf("      name = %s\n", str);
+#endif
+        strncpy(coproc[coproc_index]->name, str, 31);
+    }
+    else
+    {
+        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
+               coproc_index, cluster_index);
+        exit(1);
+    }
+
+    /////////// get psegname attribute 
+    str = getStringValue(reader,"psegname", &ok);
+    if ( ok == 0 )
+    {
+        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
+                 coproc_index, cluster_index);
+        exit(1);
+    }
+
+    /////////// set psegid attribute
+    int index = getPsegId( cluster_index, str );
+    if ( index >= 0 ) 
+    {
+#if XML_PARSER_DEBUG
+printf("      clusterid = %d\n", cluster_index);
+printf("      psegname  = %s\n", str);
+printf("      psegid    = %d\n", index);
+#endif
+        coproc[coproc_index]->psegid = index;
+        assert(pseg[index]->type == PSEG_TYPE_PERI && "coproc psegname attribute must refer to a pseg of type PERI" );
+    }
+    else              
+    {
+        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
+                   coproc_index, cluster_index, str );
+        exit(1);
+    }  
+
+    ////////// set port_offset 
+    coproc[coproc_index]->port_offset = cp_port_index;
+
+#if XML_PARSER_DEBUG
+printf("      port_offset = %d\n", cp_port_index);
+#endif
+
+    int status = xmlTextReaderRead(reader);
+    while ( status == 1 ) 
+    {
+        const char* tag = (const char*)xmlTextReaderConstName(reader);
+
+        if      ( strcmp(tag, "port")     == 0 ) cpPortNode(reader);
+        else if ( strcmp(tag, "#text")    == 0 ) { }
+        else if ( strcmp(tag, "#comment") == 0 ) { }
+        else if ( strcmp(tag, "coproc")   == 0 ) 
+        {
+            coproc[coproc_index]->ports = cp_port_loc_index;
+            cluster[cluster_index]->coprocs++;
+            coproc_loc_index++;
+            coproc_index++;
+            return;
+        }
+        else 
+        {
+            printf("[XML ERROR] Unknown tag %s",tag);
+            exit(1);
+        }
+        status = xmlTextReaderRead ( reader );
+    }
+} // end coprocNode()
+
+///////////////////////////////////////
+void irqNode( xmlTextReaderPtr reader )
+///////////////////////////////////////
+{
+    unsigned int 	ok;
+    unsigned int 	value;
+    char*    		str;
+
+    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
+
+    if ( irq_index >= MAX_IRQS )
+    {
+        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
+    }
+
+#if XML_PARSER_DEBUG
+printf("     irq %d\n", irq_loc_index);
+#endif
+
+    irq[irq_index] = (mapping_irq_t*)malloc(sizeof(mapping_irq_t));
+
+    ///////// get type attribute
+    str = getStringValue(reader,"type", &ok);
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("        type    = %s\n", str);
+#endif
+        if      ( strcmp(str, "HARD") == 0 ) irq[irq_index]->type = 0;
+        else if ( strcmp(str, "SOFT") == 0 ) irq[irq_index]->type = 1;
+        else
+        {
+            printf("[XML ERROR] undefined IRQ  <type> for processor %d in cluster %d\n",
+                   cluster_index, proc_loc_index );
+            exit(1);
+        }
+    }  
+    else
+    {
+        printf("[XML ERROR] missing IRQ <type> for processor %d in cluster %d\n",
+               cluster_index, proc_loc_index );
+        exit(1);
+    }
+
+    ///////// get icuid attribute
+    value = getIntValue(reader, "icuid", &ok);
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("        icuid   = %d\n", value);
+#endif
+        irq[irq_index]->icuid = value;
+        if ( value >= 32 )
+        {
+            printf("[XML ERROR] IRQ <icuid> too large for processor %d in cluster %d\n",
+                   cluster_index, proc_loc_index );
+            exit(1);
+        }
+    }
+    else
+    {
+        printf("[XML ERROR] missing IRQ <icuid> for processor %d in cluster %d\n",
+               cluster_index, proc_loc_index );
+        exit(1);
+    }
+
+    ///////// get isr attribute
+    str = getStringValue(reader,"isr", &ok);
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("        isr     = %s\n", str);
+#endif
+        if      ( strcmp(str, "ISR_SWITCH" ) == 0 ) irq[irq_index]->isr = ISR_SWITCH;
+        else if ( strcmp(str, "ISR_IOC"    ) == 0 ) irq[irq_index]->isr = ISR_IOC;
+        else if ( strcmp(str, "ISR_DMA"    ) == 0 ) irq[irq_index]->isr = ISR_DMA;
+        else if ( strcmp(str, "ISR_TTY"    ) == 0 ) irq[irq_index]->isr = ISR_TTY;
+        else if ( strcmp(str, "ISR_TIMER"  ) == 0 ) irq[irq_index]->isr = ISR_TIMER;
+        else
+        {
+            printf("[XML ERROR] illegal IRQ <isr> for processor %d in cluster %d\n",
+                   cluster_index, proc_loc_index );
+            exit(1);
+        }
+#if XML_PARSER_DEBUG
+printf("        isrnum  = %d\n", irq[irq_index]->isr);
+#endif
+    }  
+    else
+    {
+        printf("[XML ERROR] missing IRQ <isr> for processor %d in cluster %d\n",
+               cluster_index, proc_loc_index );
+        exit(1);
+    }
+
+    ///////// get channel attribute (optionnal : 0 if missing)
+    value = getIntValue(reader, "channel", &ok);
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("        channel = %d\n", value);
+#endif
+        irq[irq_index]->channel = value;
+    }
+    else
+    {
+        irq[irq_index]->channel = 0;
+    }
+
+    irq_index++;
+    irq_loc_index++;
+
+} // end irqNode
+
+/////////////////////////////////////////
+void procNode ( xmlTextReaderPtr reader )
+/////////////////////////////////////////
+{
+    unsigned int 	ok;
+    unsigned int    value;
+
+    irq_loc_index = 0;
+
+    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
+
+    if ( proc_index >= MAX_PROCS )
+    {
+        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
+    }
+
+#if XML_PARSER_DEBUG
+printf("\n  proc %d\n", proc_index);
+#endif
+
+    proc[proc_index] = (mapping_proc_t*)malloc(sizeof(mapping_proc_t));
+
+
+    /////////// get index attribute (optional)
+    value = getIntValue(reader,"index",&ok);
+    if ( ok && (value != proc_loc_index) )
+    {
+            printf("[XML ERROR] wrong proc index / expected value is %d", 
+                proc_loc_index);
+            exit(1);
+    }
+
+    ////////// set irq_offset attribute
+    proc[proc_index]->irq_offset = irq_index;
+
+#if XML_PARSER_DEBUG
+printf("    irq_offset = %d\n", irq_index);
+#endif
+
+    int status = xmlTextReaderRead(reader);
+    while ( status == 1 ) 
+    {
+        const char* tag = (const char*)xmlTextReaderConstName(reader);
+
+        if      ( strcmp(tag, "irq")      == 0 ) irqNode(reader);
+        else if ( strcmp(tag, "#text")    == 0 ) { }
+        else if ( strcmp(tag, "#comment") == 0 ) { }
+        else if ( strcmp(tag, "proc")     == 0 ) 
+        {
+            proc[proc_index]->irqs = irq_loc_index;
+            cluster[cluster_index]->procs++;
+            proc_loc_index++;
+            proc_index++;
+            return;
+        }
+        else 
+        {
+            printf("[XML ERROR] Unknown tag %s",tag);
+            exit(1);
+        }
+        status = xmlTextReaderRead ( reader );
+    }
+} // end procNode()
+
+
 //////////////////////////////////////////
 void  psegNode ( xmlTextReaderPtr reader )
@@ -1420,8 +1545,27 @@
     unsigned int value;
 
+    cluster[cluster_index] = (mapping_cluster_t*)malloc(sizeof(mapping_cluster_t));
+    
+    //initialise all variables
+    //they will be incremented by *Node() functions
+    //FIXME: calloc?
+    cluster[cluster_index]->psegs = 0;
+    cluster[cluster_index]->procs = 0;
+    cluster[cluster_index]->coprocs = 0;
+    cluster[cluster_index]->periphs = 0;
+
+
+    //initialise global variables
+    //TODO: delete those three
     proc_loc_index = 0;
     coproc_loc_index = 0;
     periph_loc_index = 0;
 
+    // for replicated periph
+    found_timer = 0;
+    found_icu = 0;
+    found_xicu = 0;
+    found_dma = 0;
+
     if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
 
@@ -1437,9 +1581,4 @@
 #endif
 
-    cluster[cluster_index] = (mapping_cluster_t*)malloc(sizeof(mapping_cluster_t));
-    
-    //initialise the number of pseg 
-    //it will be incremented by PsegNode
-    cluster[cluster_index]->psegs = 0;
 
     /////////// check cluster index attribute (optional)
@@ -1480,10 +1619,36 @@
         else if ( strcmp(tag, "cluster")  == 0 ) 
         {
-            cluster[cluster_index]->procs   = proc_loc_index;
-            cluster[cluster_index]->coprocs = coproc_loc_index;
-            cluster[cluster_index]->periphs = periph_loc_index;
-
-            // cluster[cluster_index]psegs update is done in psegNode(),
-            // because the coprocNode() call to getPsegId() need it
+
+            if(use_xicu == 0xFFFFFFFF)
+                use_xicu = found_xicu;
+
+            ////////////////// peripherals checks ////////////////////
+            if( (found_timer  && use_xicu) || (!found_timer  && !use_xicu) )
+            {
+                printf("[XML ERROR] illegal or missing timer peripheral in cluster %d\n", cluster_index);
+                exit(1);
+            }
+
+            if( (found_icu  && use_xicu) || (!found_icu  && !use_xicu) )
+            {
+                printf("[XML ERROR] illegal or missing icu peripheral in cluster %d\n", cluster_index);
+                exit(1);
+            }
+
+            if( !found_xicu && use_xicu)
+            {
+                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
+                exit(1);
+            }
+
+            if(!found_dma)
+            {
+                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
+                exit(1);
+            }
+
+               
+            if(nb_proc_max < cluster[cluster_index]->procs)
+                nb_proc_max = cluster[cluster_index]->procs;
 
 #if XML_PARSER_DEBUG
@@ -1492,5 +1657,5 @@
 printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
 printf("    periphs = %d\n", cluster[cluster_index]->periphs);
-printf("  end cluster %d\n", cluster_index);
+printf("    end cluster %d\n", cluster_index);
 #endif
             cluster_index++;
@@ -1527,16 +1692,20 @@
                 exit(1);
             }
-            else
+
+            if(header->tty_clusterid == 0xFFFFFFFF)
             {
+                printf("[XML ERROR] illegal or missing tty peripheral");
+                exit(1);
+            }
+
 #if XML_PARSER_DEBUG
 printf("  end cluster set\n\n");
 #endif
-                header->psegs    = pseg_index;
-                header->procs    = proc_index;
-                header->irqs     = irq_index;
-                header->coprocs  = coproc_index;
-                header->cp_ports = cp_port_index;
-                return;
-            }
+            header->psegs    = pseg_index;
+            header->procs    = proc_index;
+            header->irqs     = irq_index;
+            header->coprocs  = coproc_index;
+            header->cp_ports = cp_port_index;
+            return;
         }
         else 
@@ -1659,23 +1828,47 @@
     }
 
-    /////////// get clusters attribute
-    value = getIntValue(reader, "clusters", &ok);
-    if ( ok )
-    {
-        if ( value >= MAX_CLUSTERS )
-        {
-            printf("[XML ERROR] The number of clusters is larger than %d\n", MAX_CLUSTERS);
-            exit(1);
-        }
+    /////////// get cluster_x attribute
+    cluster_x = getIntValue(reader, "cluster_x", &ok);
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("  cluster_x = %d\n", cluster_x);
+#endif
+        header->cluster_x = cluster_x;
+    }
+    else
+    {
+        printf("[XML ERROR] illegal or missing <cluster_x> attribute in header\n");
+        exit(1);
+    }
+
+    /////////// get cluster_y attribute
+    cluster_y = getIntValue(reader, "cluster_y", &ok);
+    if ( ok )
+    {
+#if XML_PARSER_DEBUG
+printf("  cluster_y = %d\n", cluster_y);
+#endif
+        header->cluster_y = cluster_y;
+    }
+    else
+    {
+        printf("[XML ERROR] illegal or missing <cluster_y> attribute in header\n");
+        exit(1);
+    }
+
+    //check the number of cluster
+    value = cluster_x*cluster_y;
+    if ( value >= MAX_CLUSTERS )
+    {
+        printf("[XML ERROR] The number of clusters is larger than %d\n", MAX_CLUSTERS);
+        exit(1);
+    }
+	
+    header->clusters  = value;
+
 #if XML_PARSER_DEBUG
 printf("  clusters = %d\n", value);
 #endif
-	header->clusters  = value;
-    }
-    else
-    {
-        printf("[XML ERROR] illegal or missing <clusters> attribute in header\n");
-        exit(1);
-    }
 
     ///////// get vspaces attribute
@@ -1743,38 +1936,55 @@
 {
     unsigned int i;
-    unsigned int length;
     // write element
     for ( i = 0 ; i < nb_elem ; i++ )
     {
-        length = write(fdout, table[i], elem_size);
-    
+        if(elem_size != write(fdout, table[i], elem_size))
+        {
+            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
+            exit(1);
+        }
+
 #if XML_PARSER_DEBUG
 printf("Building binary: writing %s %d\n", type, i);
 #endif
-
-        if ( length != elem_size )
-        {
-            printf("type: %s\n", type);
-            perror("error writing");
-            exit(1);
-        }
     }
 }
 
+int open_file(const char* file_path)
+{
+
+    //open file
+    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
+    if ( fdout < 0)
+    {
+        perror("open");
+        exit(1);
+    }
+
+    //reinitialise the file
+    if( ftruncate(fdout, 0) )
+    {
+        perror("truncate");
+        exit(1);
+    }
+
+//#if XML_PARSER_DEBUG
+    printf("%s\n",file_path);
+//#endif
+
+    return fdout;
+}
+
+
 ///////////////////////////
-void  buildBin( int fdout )
+void  buildBin( const char* file_path )
 ///////////////////////////
 {
-    unsigned int    vspace_id;
-    unsigned int    vobj_id;
     unsigned int    length;
+
+    int fdout = open_file(file_path);
 
     // write header to binary file
     length = write(fdout, (char*)header, sizeof(mapping_header_t));
-    
-#if XML_PARSER_DEBUG
-printf("\n*** write header\n");
-#endif
-
     if ( length != sizeof(mapping_header_t) )
     {
@@ -1785,49 +1995,12 @@
     // write clusters
     BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char**) cluster);
-
     // write psegs
     BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char**) pseg);
-
     // write vspaces
-    for ( vspace_id = 0 ; vspace_id < header->vspaces ; vspace_id++ )
-    {
-        length = write(fdout, (char*)vspace[vspace_id], sizeof(mapping_vspace_t));
-    
-#if XML_PARSER_DEBUG
-printf("write vspace %d\n", vspace_id);
-printf("vspace->vobj_offset: %d\n", vspace[vspace_id]->vobj_offset);
-printf("vspace->vobjs: %d\n", vspace[vspace_id]->vobjs);
-printf("header->vobjs: %d\n", header->vobjs);
-#endif
-
-        if ( length != sizeof(mapping_vspace_t) )
-        {
-            perror("write vspace");
-            exit(1);
-        }
-    }
-
+    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char**) vspace);
     // write vsegs
     BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char**) vseg);
-    
     // write vobjs
-    for ( vobj_id = 0 ; vobj_id < header->vobjs ; vobj_id++ )
-    {
-        length = write(fdout, (char*)vobj[vobj_id], sizeof(mapping_vobj_t));
-    
-#if XML_PARSER_DEBUG
-printf("write vobj %d\n", vobj_id);
-printf("write vobj name %s\n", vobj[vobj_id]->name);
-printf("write vobj length %x\n", vobj[vobj_id]->length);
-printf("write vobj type %d\n", vobj[vobj_id]->type);
-#endif
-
-        if ( length != sizeof(mapping_vobj_t) )
-        {
-            perror("write vobj");
-            exit(1);
-        }
-    }
-
+    BuildTable(fdout, "vobj", vobj_index, sizeof(mapping_vobj_t), (char**) vobj);
     // write tasks array
     BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char**) task);
@@ -1842,4 +2015,6 @@
     //building periphs array
     BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char**)periph);
+    
+    close(fdout);
 
 } // end buildBin()
@@ -1889,4 +2064,110 @@
 }
 
+
+void file_write(int fdout, char* towrite)
+{
+    unsigned int size = strlen(towrite);
+    if(size != write(fdout, towrite, size))
+    {
+        printf("file_write error");
+        exit(1);
+    }
+}
+
+void def_int_write(int fdout, char* def, int num)
+{
+    char  buf[64];
+    sprintf(buf, "#define\t %s  %d\n", def, num);
+    file_write(fdout, buf);
+
+}
+
+void def_hex_write(int fdout, char* def, int num)
+{
+    char  buf[64];
+    sprintf(buf, "#define\t %s  0x%x\n", def, num);
+    file_write(fdout, buf);
+}
+
+///////////////////////////
+void  genHd( const char* file_path )
+///////////////////////////
+{
+    int fdout = open_file(file_path);
+
+    char* prol = " /* AUTO GENRATED FILE */\n\n#ifndef _HD_CONFIG_H\n#define _HD_CONFIG_H\n\n";
+    file_write(fdout, prol);
+
+    def_int_write(fdout, "CLUSTER_X"       , cluster_x);
+    def_int_write(fdout, "CLUSTER_Y"       , cluster_y);
+    def_int_write(fdout, "NB_CLUSTERS"      , cluster_index);
+    def_hex_write(fdout, "CLUSTER_SIZE"    , ( ((unsigned long)1) << 32)/cluster_index);
+    def_int_write(fdout, "NB_PROCS_MAX"     , nb_proc_max);
+    def_int_write(fdout, "NB_TIMERS_MAX"   , nb_timer_channel_max);
+    def_int_write(fdout, "NB_DMAS_MAX"     , nb_dma_channel_max);
+    def_int_write(fdout, "NB_TTYS"         , nb_tty_channel);
+    def_int_write(fdout, "NB_IOCS"         , nb_ioc_channel);
+    def_int_write(fdout, "NB_NICS"         , nb_nic_channel);
+    
+    file_write(fdout, "\n");
+    def_int_write(fdout, "USE_XICU"         , use_xicu);
+    def_int_write(fdout, "IOMMU_ACTIVE "    , io_mmu_active);
+
+    char* epil = "\n#endif //_HD_CONFIG_H";
+    file_write(fdout, epil);
+
+    close(fdout);
+}
+
+void ld_write(int fdout, char* seg, unsigned int addr)
+{
+    char  buf[64];
+    sprintf(buf, "%s = 0x%x;\n", seg, addr);
+    file_write(fdout, buf);
+
+}
+
+///////////////////////////
+void  genLd( const char* file_path )
+///////////////////////////
+{
+    int fdout = open_file(file_path);
+
+    char* prol = "/* AUTO GENRATED FILE */\n\n";
+    file_write(fdout, prol);
+
+    //boot
+    ld_write(fdout, "seg_boot_code_base"           , boot_code_base);
+    ld_write(fdout, "seg_boot_stack_base"          , boot_stack_base);
+    ld_write(fdout, "seg_mapping_base"        , boot_mapping_base );
+
+    //kernel
+    ld_write(fdout, "\nseg_kernel_code_base"         , kernel_code_base);
+    ld_write(fdout, "seg_kernel_data_base"         , kernel_data_base);
+    ld_write(fdout, "seg_kernel_uncdata_base"      , kernel_uncdata_base);
+    ld_write(fdout, "seg_kernel_init_base"         , kernel_init_base);
+
+    //peripherals
+    ld_write(fdout, "\nseg_fbf_base" ,  fbf_base_offset);
+    ld_write(fdout, "seg_icu_base" ,  icu_base_offset);
+    ld_write(fdout, "seg_ioc_base" ,  ioc_base_offset);
+    ld_write(fdout, "seg_tty_base" ,  tty_base_offset);
+    ld_write(fdout, "seg_dma_base" ,  dma_base_offset);
+    ld_write(fdout, "seg_tim_base" ,  tim_base_offset);
+    ld_write(fdout, "seg_gcd_base" ,  gcd_base_offset);
+    ld_write(fdout, "seg_iob_base" ,  iob_base_offset);
+
+    close(fdout);
+}
+
+char* buildPath(const char* path, const char*name)
+{
+    char *res = calloc(strlen(path) + strlen(name) + 1, 1);
+    strcat(res, path);
+    strcat(res, "/");
+    strcat(res, name);
+    return res; 
+}
+
 /////////////////////////////////////
 int  main ( int argc, char* argv[] )
@@ -1895,15 +2176,26 @@
     if ( argc < 3 ) 
     {
-        printf("Usage: xml2bin <input_file_path> <output_file_path>\n");
+        printf("Usage: xml2bin <input_file_path> <output_path>\n");
         return 1;
     }
 
-    int fdout = open( argv[2], (O_CREAT | O_RDWR), S_IRWXU );
-    if ( fdout < 0)
-    {
-        perror("open");
-        exit(1);
-    }
+    struct stat dir_st;
+    if(stat( argv[2], &dir_st ))
+    {
+        perror("bad path");
+        exit(1);
+    }
+
+    if((dir_st.st_mode & S_IFDIR) == 0)
+    {
+        printf("path is not a dir: %s", argv[2] );
+        exit(1);
+    }
+
     
+    char *map_path = buildPath(argv[2], "map.bin"); 
+    char *ld_path = buildPath(argv[2],  "giet_vsegs.ld"); 
+    char *hd_path = buildPath(argv[2],  "hard_config.h"); 
+
     LIBXML_TEST_VERSION;
 
@@ -1922,5 +2214,7 @@
                 headerNode( reader );
                 prepareBuild();
-                buildBin( fdout );
+                buildBin( map_path );
+                genHd(hd_path);
+                genLd(ld_path);
             }
             else
