Index: soft/giet_vm/libs/mwmr_channel.c
===================================================================
--- soft/giet_vm/libs/mwmr_channel.c	(revision 207)
+++ soft/giet_vm/libs/mwmr_channel.c	(revision 228)
@@ -19,5 +19,5 @@
 // can be used to get the virtual base address of the channel from it's name.
 //
-// An MWMR transaction transfer	an integer number of items, and an item is
+// An MWMR transaction transfer    an integer number of items, and an item is
 // an integer number of unsigned int (32 bits words).
 // The max number of words that can be stored in a MWMR channel is defined by the
@@ -39,29 +39,30 @@
 // If the lock is already taken a fixed delay is introduced before retry.
 //////////////////////////////////////////////////////////////////////////////
-void mwmr_lock_acquire(unsigned int* lock_address)
-{
-    register unsigned int*	plock = lock_address;
-    register unsigned int	delay = 100;
+void mwmr_lock_acquire(unsigned int * lock_address) {
+    register unsigned int * plock = lock_address;
+    register unsigned int delay = 100;
     asm volatile (
-            "mwmr_lock_try:					\n"
-            "ll   $2,    0(%0)				\n" /* $2 <= lock current value */
-            "bnez $2,    mwmr_lock_delay	\n" /* retry after delay if lock busy */
-            "li   $3,    1					\n" /* $3 <= argument for sc */
-            "sc   $3,    0(%0)				\n" /* try to get lock */
-            "bnez $3,    mwmr_lock_ok		\n" /* exit if atomic */
-            "mwmr_lock_delay:				\n"
+            "mwmr_lock_try:                    \n"
+            "ll   $2,    0(%0)                \n" /* $2 <= lock current value */
+            "bnez $2,    mwmr_lock_delay    \n" /* retry after delay if lock busy */
+            "li   $3,    1                    \n" /* $3 <= argument for sc */
+            "sc   $3,    0(%0)                \n" /* try to get lock */
+            "bnez $3,    mwmr_lock_ok        \n" /* exit if atomic */
+            "mwmr_lock_delay:                \n"
             "move $4,    %1                 \n" /* $4 <= delay */
             "mwmr_lock_loop:                \n"
-            "beqz $4,    mwmr_lock_loop		\n" /* test end delay */
-            "addi $4,    $4,  -1			\n" /* $4 <= $4 - 1 */
-            "j           mwmr_lock_try		\n" /* retry ll */
-            "nop							\n"
-            "mwmr_lock_ok:					\n"
+            "beqz $4,    mwmr_lock_loop        \n" /* test end delay */
+            "addi $4,    $4,  -1            \n" /* $4 <= $4 - 1 */
+            "j           mwmr_lock_try        \n" /* retry ll */
+            "nop                            \n"
+            "mwmr_lock_ok:                    \n"
             :
             :"r"(plock), "r"(delay)
             :"$2", "$3", "$4");
 } 
-//////////////////////////////////////////////////////////////////////////////
-//	   nb_mwmr_write()
+
+
+//////////////////////////////////////////////////////////////////////////////
+//       nb_mwmr_write()
 // This is a non-blocking function.
 // The nitems parameter is the number of items to be transfered.
@@ -72,66 +73,70 @@
 // the number of read items (it can be 0).
 //////////////////////////////////////////////////////////////////////////////
-unsigned int nb_mwmr_write( mwmr_channel_t* 	mwmr, 
-                            unsigned int*		buffer,
-                            unsigned int 		nitems )
-{
-    unsigned int	x;
-    unsigned int	spaces;		// number of empty slots (in words)
-    unsigned int	nwords;		// requested transfer length (in words)
-    unsigned int    depth;		// channel depth (in words)
-    unsigned int    width;		// channel width (in words)
-    unsigned int    sts;        // channel sts
-    unsigned int    ptw;        // channel ptw
-
-    if(nitems == 0) return 0;
+unsigned int nb_mwmr_write(mwmr_channel_t * mwmr, unsigned int * buffer, unsigned int nitems) {
+    unsigned int x;
+    unsigned int spaces; // number of empty slots (in words)
+    unsigned int nwords; // requested transfer length (in words)
+    unsigned int depth;  // channel depth (in words)
+    unsigned int width;  // channel width (in words)
+    unsigned int sts;    // channel sts
+    unsigned int ptw;    // channel ptw
+
+    if (nitems == 0) {
+        return 0;
+    }
 
     // get the lock
-    mwmr_lock_acquire( &mwmr->lock );
+    mwmr_lock_acquire(&mwmr->lock);
 
     // access fifo status
-    depth  = mwmr->depth;
-    width  = mwmr->width;
-    sts    = mwmr->sts;
-    ptw    = mwmr->ptw;
+    depth = mwmr->depth;
+    width = mwmr->width;
+    sts = mwmr->sts;
+    ptw = mwmr->ptw;
     spaces = depth - sts;
     nwords = width * nitems;
 
-    if( spaces >= nwords ) 	// transfer nitems, release lock and return 
-    {
-        for ( x = 0 ; x < nwords ; x++ )  
-        {
+    if (spaces >= nwords) { // transfer nitems, release lock and return 
+        for (x = 0; x < nwords; x++) {
             mwmr->data[ptw] = buffer[x];
-            if ( (ptw + 1) == depth ) ptw = 0;
-            else                      ptw = ptw + 1;
-        }
-        mwmr->sts  = mwmr->sts + nwords;
-        mwmr->ptw  = ptw;
+            if ((ptw + 1) == depth) {
+                ptw = 0;
+            }
+            else {
+                ptw = ptw + 1;
+            }
+        }
+        mwmr->sts = mwmr->sts + nwords;
+        mwmr->ptw = ptw;
         mwmr->lock = 0;
         return nitems;
     }
-   
-    else if ( spaces < width )	// release lock and return 
-    {
+    else if (spaces < width) {
+        // release lock and return 
         mwmr->lock = 0;
         return 0;
     }
-    else	// transfer as many items as possible, release lock and return 
-    {
-        nwords = (spaces/width) * width;	// integer number of items
-        for ( x = 0 ; x < nwords ; x++ )  
-        {
+    else {
+        // transfer as many items as possible, release lock and return 
+        nwords = (spaces / width) * width;    // integer number of items
+        for (x = 0; x < nwords; x++) {
             mwmr->data[ptw] = buffer[x];
-            if ( (ptw + 1) == depth ) ptw = 0;
-            else                      ptw = ptw + 1;
-        }
-        mwmr->sts  = sts + nwords;
-        mwmr->ptw  = ptw;
-        mwmr->lock = 0;
-        return (nwords/width);
+            if ((ptw + 1) == depth) {
+                ptw = 0;
+            }
+            else {
+                ptw = ptw + 1;
+            }
+        }
+        mwmr->sts = sts + nwords;
+        mwmr->ptw = ptw;
+        mwmr->lock = 0;
+        return (nwords / width);
     }
 } // end nb_mwmr_write()
 
-//////////////////////////////////////////////////////////////////////////////
-//	mwmr_write()
+
+//////////////////////////////////////////////////////////////////////////////
+//    mwmr_write()
 // This blocking function returns only when the transfer is completed.
 // The nitems parameter is the number of items to be transfered.
@@ -142,71 +147,74 @@
 // after a random delay.
 //////////////////////////////////////////////////////////////////////////////
-void mwmr_write( mwmr_channel_t* 	mwmr, 
-                 unsigned int*		buffer,
-                 unsigned int 		nitems )
-{
-    unsigned int	x;
-    unsigned int	spaces;		// number of empty slots (in words)
-    unsigned int	nwords;		// requested transfer length (in words)
-    unsigned int    depth;		// channel depth (in words)
-    unsigned int    width;		// channel width (in words)
-    unsigned int    sts;        // channel sts
-    unsigned int    ptw;        // channel ptw
-
-    if(nitems == 0) return;
-
-    while(1)
-    {
+void mwmr_write(mwmr_channel_t * mwmr, unsigned int * buffer, unsigned int nitems) {
+    unsigned int x;
+    unsigned int spaces; // number of empty slots (in words)
+    unsigned int nwords; // requested transfer length (in words)
+    unsigned int depth;  // channel depth (in words)
+    unsigned int width;  // channel width (in words)
+    unsigned int sts;    // channel sts
+    unsigned int ptw;    // channel ptw
+
+    if (nitems == 0) {
+        return;
+    }
+
+    while (1) {
         // get the lock
         mwmr_lock_acquire(&mwmr->lock);
 
         // compute spaces and nwords
-        depth  = mwmr->depth;
-        width  = mwmr->width;
-        sts    = mwmr->sts;
-        ptw    = mwmr->ptw;
+        depth = mwmr->depth;
+        width = mwmr->width;
+        sts  = mwmr->sts;
+        ptw  = mwmr->ptw;
         spaces = depth - sts;
         nwords = width * nitems;
 
-        if( spaces >= nwords ) 	// write nwords, release lock and return
-        {
-            for ( x = 0 ; x < nwords ; x++ )  
-            {
+        if (spaces >= nwords) {
+            // write nwords, release lock and return
+            for (x = 0; x < nwords; x++) {
                 mwmr->data[ptw] = buffer[x];
-                if ( (ptw + 1) == depth ) ptw = 0;
-                else                      ptw = ptw + 1;
-            }
-            mwmr->ptw  = ptw;
-            mwmr->sts  = sts + nwords;
+                if ((ptw + 1) == depth) {
+                    ptw = 0;
+                }
+                else {
+                    ptw = ptw + 1;
+                }
+            }
+            mwmr->ptw = ptw;
+            mwmr->sts = sts + nwords;
             mwmr->lock = 0;
             return;
         }
-        else if ( spaces < width )	// release lock and retry after delay
-        {
-            mwmr->lock = 0;
-            for ( x = giet_rand()>>8 ; x > 0 ; x-- ) asm volatile ( "nop" );
-        }
-        else	// write as many items as possible, release lock and retry after delay
-        {
-            nwords = (spaces/width) * width;  // integer number of items
-            for ( x = 0 ; x < nwords ; x++ )  
-            {
+        else if (spaces < width) {
+            // release lock and retry after delay
+            mwmr->lock = 0;
+        }
+        else {
+            // write as many items as possible, release lock and retry after delay
+            nwords = (spaces / width) * width;  // integer number of items
+            for (x = 0; x < nwords; x++) {
                 mwmr->data[ptw] = buffer[x];
-                if ( (ptw + 1) == depth ) ptw = 0;
-                else                      ptw = ptw + 1;
-            }
-            mwmr->sts  = sts + nwords;
-            mwmr->ptw  = ptw;
-            buffer     = buffer + nwords;
-            nitems     = nitems - (nwords/width);
-            mwmr->lock = 0;
-        }
-        // random delay before retry
-        for ( x = giet_rand()>>6 ; x > 0 ; x-- ) asm volatile ( "nop" );
+                if ((ptw + 1) == depth) {
+                    ptw = 0;
+                }
+                else {
+                    ptw = ptw + 1;
+                }
+            }
+            mwmr->sts = sts + nwords;
+            mwmr->ptw = ptw;
+            buffer = buffer + nwords;
+            nitems = nitems - (nwords/width);
+            mwmr->lock = 0;
+        }
+        giet_context_switch();
     }
 } // end mwmr_write()
 
-//////////////////////////////////////////////////////////////////////////////
-//	   nb_mwmr_read()
+
+//////////////////////////////////////////////////////////////////////////////
+//       nb_mwmr_read()
 // This is a non-blocking function.
 // The nitems parameter is the number of items to be transfered.
@@ -217,64 +225,69 @@
 // the number of read items (it can be 0).
 //////////////////////////////////////////////////////////////////////////////
-unsigned int nb_mwmr_read( mwmr_channel_t* 	mwmr, 
-                           unsigned int*		buffer,
-                           unsigned int 		nitems )
-{
-    unsigned int	x;
-    unsigned int	nwords;		// requested transfer length (in words)
-    unsigned int    depth;		// channel depth (in words)
-    unsigned int    width;		// channel width (in words)
-    unsigned int    sts;        // channel sts
-    unsigned int    ptr;        // channel ptr
-
-    if(nitems == 0) return 0;
+unsigned int nb_mwmr_read(mwmr_channel_t * mwmr, unsigned int * buffer, unsigned int nitems) {
+    unsigned int x;
+    unsigned int nwords; // requested transfer length (in words)
+    unsigned int depth;  // channel depth (in words)
+    unsigned int width;  // channel width (in words)
+    unsigned int sts;    // channel sts
+    unsigned int ptr;    // channel ptr
+
+    if (nitems == 0) {
+        return 0;
+    }
 
     // get the lock
-    mwmr_lock_acquire( &mwmr->lock );
+    mwmr_lock_acquire(&mwmr->lock);
 
     // access fifo status
-    depth  = mwmr->depth;
-    width  = mwmr->width;
-    sts    = mwmr->sts;
-    ptr    = mwmr->ptr;
+    depth = mwmr->depth;
+    width = mwmr->width;
+    sts = mwmr->sts;
+    ptr = mwmr->ptr;
     nwords = width * nitems;
 
-    if( sts >= nwords ) 	// transfer nitems, release lock and return 
-    {
-        for ( x = 0 ; x < nwords ; x++ )  
-        {
+    if (sts >= nwords) {
+        // transfer nitems, release lock and return 
+        for (x = 0; x < nwords; x++) {
             buffer[x] = mwmr->data[ptr];
-            if ( (ptr + 1) == depth ) ptr = 0;
-            else                      ptr = ptr + 1;
-        }
-        mwmr->sts  = mwmr->sts - nwords;
-        mwmr->ptr  = ptr;
+            if ((ptr + 1) == depth) {
+                ptr = 0;
+            }
+            else {
+                ptr = ptr + 1;
+            }
+        }
+        mwmr->sts = mwmr->sts - nwords;
+        mwmr->ptr = ptr;
         mwmr->lock = 0;
         return nitems;
     }
-   
-    else if ( sts < width )	// release lock and return 
-    {
+    else if (sts < width) {
+        // release lock and return 
         mwmr->lock = 0;
         return 0;
     }
-    else	// transfer as many items as possible, release lock and return 
-    {
-        nwords = (sts/width) * width;	// integer number of items
-        for ( x = 0 ; x < nwords ; x++ )  
-        {
+    else {
+        // transfer as many items as possible, release lock and return 
+        nwords = (sts / width) * width; // integer number of items
+        for (x = 0 ; x < nwords ; x++) {
             buffer[x] = mwmr->data[ptr];
-            if ( (ptr + 1) == depth ) ptr = 0;
-            else                      ptr = ptr + 1;
-        }
-        mwmr->sts  = sts - nwords;
-        mwmr->ptr  = ptr;
-        mwmr->lock = 0;
-        return (nwords/width);
-    }
-} // nb_mwmr_read() 
-
-//////////////////////////////////////////////////////////////////////////////
-//	mwmr_read()
+            if ((ptr + 1) == depth) {
+                ptr = 0;
+            }
+            else {
+                ptr = ptr + 1;
+            }
+        }
+        mwmr->sts = sts - nwords;
+        mwmr->ptr = ptr;
+        mwmr->lock = 0;
+        return (nwords / width);
+    }
+} // nb_mwmr_read()
+
+
+//////////////////////////////////////////////////////////////////////////////
+//    mwmr_read()
 // This blocking function returns only when the transfer is completed.
 // The nitems parameter is the number of items to be transfered.
@@ -285,66 +298,75 @@
 // after a random delay.
 //////////////////////////////////////////////////////////////////////////////
-void mwmr_read( mwmr_channel_t* 	mwmr, 
-                unsigned int*		buffer,
-                unsigned int 		nitems )
-{
-    unsigned int	x;
-    unsigned int	nwords;		// requested transfer length (in words)
-    unsigned int    depth;		// channel depth (in words)
-    unsigned int    width;		// channel width (in words)
-    unsigned int    sts;        // channel sts
-    unsigned int    ptr;        // channel ptr
-
-    if(nitems == 0) return;
-
-    while(1)
-    {
+void mwmr_read( mwmr_channel_t * mwmr, unsigned int * buffer, unsigned int nitems) {
+    unsigned int x;
+    unsigned int nwords; // requested transfer length (in words)
+    unsigned int depth;  // channel depth (in words)
+    unsigned int width;  // channel width (in words)
+    unsigned int sts;    // channel sts
+    unsigned int ptr;    // channel ptr
+
+    if (nitems == 0) {
+        return;
+    }
+
+    while (1) {
         // get the lock
-        mwmr_lock_acquire( &mwmr->lock );
+        mwmr_lock_acquire(&mwmr->lock);
 
         // compute nwords
-        depth  = mwmr->depth;
-        width  = mwmr->width;
-        sts    = mwmr->sts;
-        ptr    = mwmr->ptr;
+        depth = mwmr->depth;
+        width = mwmr->width;
+        sts = mwmr->sts;
+        ptr = mwmr->ptr;
         nwords = width * nitems;
 
-        if( sts >= nwords ) 	// read nwords, release lock and return
-        {
-            for ( x = 0 ; x < nwords ; x++ )  
-            {
+        if (sts >= nwords) {
+            // read nwords, release lock and return
+            for (x = 0; x < nwords; x++) {
                 buffer[x] = mwmr->data[ptr];
-                if ( (ptr + 1) == depth ) ptr = 0;
-                else                      ptr = ptr + 1;
-            }
-            mwmr->sts  = mwmr->sts - nwords;
-            mwmr->ptr  = ptr;
+                if ((ptr + 1) == depth) {
+                    ptr = 0;
+                }
+                else {
+                    ptr = ptr + 1;
+                }
+            }
+            mwmr->sts = mwmr->sts - nwords;
+            mwmr->ptr = ptr;
             mwmr->lock = 0;
             return;
         }
-        else if ( sts < width )	// release lock and retry after delay
-        {
-            mwmr->lock = 0;
-            for ( x = giet_rand()>>8 ; x > 0 ; x-- ) asm volatile ( "nop" );
-        }
-        else	// read as many items as possible, release lock and retry after delay
-        {
-            nwords = (sts/width) * width;	// integer number of items
-            for ( x = 0 ; x < nwords ; x++ )  
-            {
+        else if (sts < width) {
+            // release lock and retry after delay
+            mwmr->lock = 0;
+        }
+        else {   // read as many items as possible, release lock and retry after delay
+            nwords = (sts / width) * width; // integer number of items
+            for (x = 0; x < nwords; x++) {
                 buffer[x] = mwmr->data[ptr];
-                if ( (ptr + 1) == depth ) ptr = 0;
-                else                      ptr = ptr + 1;
-            }
-            mwmr->sts  = sts - nwords;
-            mwmr->ptr  = ptr;
-            buffer     = buffer + nwords;
-            nitems     = nitems - (nwords/width);
-            mwmr->lock = 0;
-        }
-        // random delay before retry
-        for ( x = giet_rand()>>6 ; x > 0 ; x-- ) asm volatile ( "nop" );
+                if ((ptr + 1) == depth) {
+                    ptr = 0;
+                }
+                else {
+                    ptr = ptr + 1;
+                }
+            }
+            mwmr->sts = sts - nwords;
+            mwmr->ptr = ptr;
+            buffer = buffer + nwords;
+            nitems = nitems - (nwords/width);
+            mwmr->lock = 0;
+        }
+        giet_context_switch();
     }
 } // end mwmr_read() 
 
 
+// Local Variables:
+// tab-width: 4
+// c-basic-offset: 4
+// c-file-offsets:((innamespace . 0)(inline-open . 0))
+// indent-tabs-mode: nil
+// End:
+// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
+
