Knowledge Base

Semaphores: Simplified addressing rules

There may be times when you decide to use some of the more sophisticated techniques with semaphores, and the automatic memory allocation won’t work. In those cases you will need to manually allocate them to known RAM bytes and bits. Examples of when you would need this are:

  • Forcing semaphores to live in Permanent Memory
  • Locating them in known addresses for the sake of external communications with SPLatLink
  • Using indexed addressing of semaphores
  • Using various advanced techniques where the actual addresses must be known.

We have made the semaphore feature in SPLat as flexible as possible, so it is able to use every byte of RAM for semaphores, and they can be used in sophisticated ways. However, for a vast majority of applications you would only need a handful of semaphores, used in a straightforward manner, so you can ignore some of the complexity that invariably comes with the flexibility. This page gives you a simple strategy to use in such cases.

All the semaphore instructions have an argument field that includes the semaphore number ss and a base address bb. In our simple strategy the base address is ignored, and defaults to 0. That means the first 32 bytes of RAM are potentially available as semaphores. In this strategy, though, we actually only use 8 bytes (0 through 7) for semaphores. That will give you 64 semaphores, numbered 0 to 63. As it happens (in fact, it was deliberate planning from day one!), RAM locations 6 and 7 are part of the permanent memory range, so two bytes (16 semaphores) of the designated semaphore area can be saved with PermStore.

The strategy is now defined by the following rules:

  1. Never allocate RAM locations 0-7 to any other variables.
  2. Define any semaphores you want in the range 0-63
  3. Use semaphore numbers 48-63 for any semaphores you want in permanent memory (you still have to do PermStore and PermRecall instructions at the appropriate times!)
  4. When you use semaphore instructions, simply leave out the bb part of the argument.

If you will never need more than 64 semaphores in your application, that’s it, no need to read on