Page 248 - Embedded Microprocessor Systems Real World Design
P. 248

even though the LOCK signal was supposed to prevent it. It was supposed to work
                like this:

                   CPU 1 reads the semaphore, asserting the LOCK signal
                   CPU 2 requests access to memory and is put in a wait state due to the LOCK
                   CPU 1 writes to the semaphore.
                   CPU 2 is released from the wait state, reads the semaphore, finds the value
                   that CPU 1 wrote.
                   It actually did this:

                   CPU  1 reads the semaphore, asserting the LOCK signal.
                   CPU 2 requests access to memory, gets memory, reads the semaphore, finds it
                   is 0.
                   CPU 1 writes to the semaphore.
                   CPU 2 writes to the semaphore, overwriting the CPU 1 value.
                   The problem showed up when it did because the firmware change altered the
                relative timing of  the two processors so that they occasionally conflicted in access-
                ing the memory. Although this problem occurred because of a flaw in handling the
                LOCK signal, a similar circumstance can occur any time that two  (or more) proces-
                sors try to write to a single RAM location. The following are some guidelines for
                using multiport RAM:

                  Wherever possible, do not have two processors that write to one memory loca-
                  tion. As mentioned earlier in the chapter, segregate buffers so that one proces-
                  sor writes to a buffer and the other one reads.
                  Never  have  a  situation  in  which  two  processors can  simultaneously check  a
                  memory location  (such as a semaphore) and then write to it. This is a sure way
                  to get contention. Have one CPU write a flag location to indicate that data are
                  in the buffer, have the other CPU write the location only when it has taken the
                  data. Then the two CPUs do not contend for the location at the same time.
                  If you must have a resource  (such as a buffer) that is shared between multiple
                  processors, have a two-step arbitration protocol. In this scheme, each CPU writes
                  a unique  code to a semaphore to indicate it wants the resource  (whatever the
                  resource is). Then each CPU checks the semaphore (preferably twice) to ensure
                  that its own code is written there. If  a conflict occurs, whichever CPU’s code is
                  left in the semaphore wins.
                   The last guideline works like this (in this example, a nonzero value in the flag
                location indicates that the buffer is in use; a zero value indicates that the buffer is
                free)  :



                Multipocesso-r Systems                                               229
   243   244   245   246   247   248   249   250   251   252   253