Index: trunk/softs/giet_tsar/drivers.c
===================================================================
--- trunk/softs/giet_tsar/drivers.c	(revision 158)
+++ trunk/softs/giet_tsar/drivers.c	(revision 173)
@@ -128,6 +128,7 @@
 ////////////////////////////////////////////////////////////////////////////////////////
 
-in_unckdata int volatile    _barrier_initial_value[8] = { 0,0,0,0,0,0,0,0 };
-in_unckdata int volatile    _barrier_count[8]         = { 0,0,0,0,0,0,0,0 };
+in_unckdata int volatile    _barrier_initial_value[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
+in_unckdata int volatile    _barrier_count[16]         = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
+in_unckdata int volatile    _barrier_lock[16]          = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
 
 ////////////////////////////////////////////////////////////////////////////////////////
@@ -179,4 +180,5 @@
 //  _procid()
 // Access CP0 and returns processor ident
+// No more than 1024 processors...
 ////////////////////////////////////////////////////////////////////////////////////////
 in_drivers unsigned int _procid()
@@ -184,5 +186,5 @@
     unsigned int ret;
     asm volatile( "mfc0 %0, $15, 1": "=r"(ret) );
-    return (ret & 0xFF);
+    return (ret & 0x3FF);
 }
 ////////////////////////////////////////////////////////////////////////////////////////
@@ -359,13 +361,16 @@
     size_t  		ntasks 		= (size_t)&NB_TASKS;
     size_t  		nprocs 		= (size_t)&NB_PROCS;
-    size_t		nclusters	= (size_t)&NB_CLUSTERS;
-    unsigned int    	base		= (unsigned int)&seg_tty_base;
+    size_t		    nclusters	= (size_t)&NB_CLUSTERS;
+    unsigned int    base		= (unsigned int)&seg_tty_base;
     unsigned int	increment 	= _segment_increment(ntasks*TTY_SPAN*4);
     size_t  		pid 		= _procid();
-    size_t		tid		= _current_task_array[pid];
     int     		nwritten 	= 0;
+    size_t		    tid;
     int     		i;
 
-    if( tid >= ntasks )   		return -1;
+    if( ntasks == 0 )  tid = 0;
+    else               tid = _current_task_array[pid];
+
+    if( tid >= ntasks )   		        return -1;
     if( pid >= nprocs*nclusters )   	return -1;
 
@@ -400,12 +405,15 @@
     size_t  		nprocs 		= (size_t)&NB_PROCS;
     size_t  		nclusters	= (size_t)&NB_CLUSTERS;
-    unsigned int    	base		= (unsigned int)&seg_tty_base;
+    unsigned int    base		= (unsigned int)&seg_tty_base;
     unsigned int	increment 	= _segment_increment(ntasks*TTY_SPAN*4);
     size_t  		pid 		= _procid();
-    size_t		tid		= _current_task_array[pid];
-
-    if( length != 1)      		return -1;
-    if( pid >= nprocs*nclusters )   	return -1;
-    if( tid >= ntasks )   		return -1;
+    size_t		    tid;
+
+    if( pid > 7 )   tid = 0;
+    else            tid	= _current_task_array[pid];
+
+    if( length != 1)      		    return -1;
+    if( pid >= nprocs*nclusters )   return -1;
+    if( tid >= ntasks )   		    return -1;
     
     tty_address = (char*)(base + increment + tid*TTY_SPAN*4);
@@ -436,10 +444,13 @@
 {
     int     pid 	= _procid();
-    int     tid		= _current_task_array[pid];
     int     ntasks 	= (int)&NB_TASKS;
     int     nprocs 	= (int)&NB_PROCS;
     int     nclusters	= (int)&NB_CLUSTERS;
     int     tty_index;
-
+    int     tid;
+
+    if( pid > 7 )   tid = 0;
+    else            tid	= _current_task_array[pid];
+ 
     if( length != 1)      		return -1;
     if( pid >= nprocs*nclusters )   	return -1;
@@ -858,6 +869,10 @@
 // _barrier_init()
 // This function makes a cooperative initialisation of the barrier:
-// Several tasks can try to initialize the barrier, but the initialisation 
+// - barrier_count[index] <= N
+// - barrier_lock[index]  <= 0
+// All tasks try to initialize the barrier, but the initialisation 
 // is done by only one task, using LL/SC instructions.
+// This cooperative initialisation is questionnable, 
+// bcause the barrier can ony be initialised once...
 //////////////////////////////////////////////////////////////////////////////////////
 in_drivers int _barrier_init(unsigned int index, unsigned int value)
@@ -866,4 +881,5 @@
     register int* pinit 	= (int*)&_barrier_initial_value[index];
     register int* pcount 	= (int*)&_barrier_count[index];
+    register int* plock  	= (int*)&_barrier_lock[index];
 
     if ( index > 7 )	return 1;
@@ -871,36 +887,40 @@
     // parallel initialisation using atomic instructions LL/SC
     asm volatile ("_barrier_init_test:          	\n"
-                  "ll   $2,     0(%0)           	\n"	// read initial value
+                  "ll   $2,     0(%0)           	\n"	// read barrier_inital_value 
                   "bnez $2,     _barrier_init_done	\n"
-                  "move $3,     %2			\n"
-                  "sc   $3,     0(%0)    	      	\n"	// try to write initial value
+                  "move $3,     %3			        \n"
+                  "sc   $3,     0(%0)    	      	\n"	// try to write barrier_initial_value
                   "beqz $3,     _barrier_init_test	\n"
-                  "move $3,	%2			\n"
-                  "sw   $3,	0(%1)			\n"	// write count
+                  "move $3,	%3			            \n" 
+                  "sw   $3,	0(%1)			        \n"	// barrier_count <= barrier_initial_value
+                  "move $3, $0                      \n" // 
+                  "sw   $3,	0(%2)			        \n"	// barrier_lock <= 0
                   "_barrier_init_done:			\n"
-                  ::"r"(pinit),"r"(pcount),"r"(value):"$2","$3");
+                  ::"r"(pinit),"r"(pcount),"r"(plock),"r"(value):"$2","$3");
     return 0 ;
 }
 //////////////////////////////////////////////////////////////////////////////////////
 // 	_barrier_wait()
-// This blocking function uses a busy_wait technics (on the counter value), 
+// This blocking function uses a busy_wait technics (on the barrier_lock value), 
 // because the GIET does not support dynamic scheduling/descheduling of tasks. 
-// In the busy waiting state, each task uses a pseudo-random delay between 
-// two successive read of the barrier counter in order to avoid bus saturation.
-// the average delay is about 1000 cycles.
-// There is at most 8 independant barriers, and an error is returned
-// if the barrier index is larger than 7.
+// The barrier state is actually defined by two variables:
+// _barrier_count[index] define the number of particpants that are waiting
+// _barrier_lock[index] define the bool variable whose value is polled 
+// The last participant change the value of _barrier_lock[index] to release the barrier...
+// There is at most 16 independant barriers, and an error is returned
+// if the barrier index is larger than 15.
 //////////////////////////////////////////////////////////////////////////////////////
 in_drivers int _barrier_wait(unsigned int index)
 {
     register int* 	pcount 		= (int*)&_barrier_count[index];		
-    register int 	maxcount 	= _barrier_initial_value[index]; 
     register int  	count;
 
-    if ( index > 7 )	return 1;
-
-    // parallel decrement barrier counter using atomic instructions LL/SC
-    // input : pointer on the barrier counter 
-    // output : counter value 
+    int 	       lock 		= _barrier_lock[index];		
+
+    if ( index > 15 )	return 1;
+    
+    // parallel decrement _barrier_count[index] using atomic instructions LL/SC
+    // input : pointer on _barrier_count[index]
+    // output : count = _barrier_count[index] (before decrementation)
     asm volatile ("_barrier_decrement:          		\n"
                   "ll   %0,     0(%1)           		\n"
@@ -910,15 +930,17 @@
                   :"=r"(count):"r"(pcount):"$2","$3");
 
-    // the last task re-initializes the barrier counter 
-    // to the max value, waking up all other waiting tasks
+    // the last task re-initializes the barrier_ count variable
+    // and the barrier_lock variable, waking up all other waiting tasks
 
     if ( count == 1 ) 	 // last task
     {
-        *pcount = maxcount;
-        return 0;
+        _barrier_count[index] = _barrier_initial_value[index];
+        asm volatile( "sync" );
+        _barrier_lock[index]   = (lock == 0) ? 1 : 0;
+        return 0 ;
     }
     else 		// other tasks
     {
-        while ( *pcount != maxcount ) 	{ }	// busy waiting 
+        while ( lock == _barrier_lock[index] ) 	{ }	// busy waiting 
         return 0 ;
     }
