![Xilinx Virtex-II Pro PPC405 Скачать руководство пользователя страница 532](http://html1.mh-extra.com/html/xilinx/virtex-ii-pro-ppc405/virtex-ii-pro-ppc405_user-manual_3410279532.webp)
840
March 2002 Release
1-800-255-7778
Virtex-II Pro™ Platform FPGA Documentation
Appendix D:
Programming Considerations
R
lock:
li
r
4,1
#obtain new lock
loop:
bl
test_and_set
#test and set
bne-
loop
#retry until old lock = 0
isync
#synchronize context
blr
#return
The unlock procedure writes a zero to the lock location. If access to the shared resource
includes write operations, most applications require a
sync
instruction to make the shared
resource modifications visible to all processors before releasing the lock.
unlock:
sync
#delay until prior stores finish
li
r
1,0
stw
r
1,0(
r
3) #store zero to lock location
blr
#return
List Insertion
The following example shows how the
lwarx
and
stwcx.
instructions are used to
implement simple LIFO (last-in-first-out) insertion into a singly linked list. If multiple
values must be changed atomically or the correct order of insertion depends on the
element contents, insertion cannot be implemented as shown below and instead requires a
more complicated strategy (such as lock synchronization).
In this example, list elements are data structures that contain pointers to the next element
in the list. A new element is inserted after an existing (parent) element. The next element
pointer in the parent element is copied (stored) unconditionally into the new element. A
pointer to the new element is stored conditionally into the parent element.
In this example, it is assumed that the parent element address is in
r
3, the new element
address is in
r
4, and the next element pointers are at offset zero in the respective element
data structure. It is also assumed that the next element pointer of each list element is in a
reservation granule separate from that of the next element pointer of all other list elements.
loop:
lwarx
r
2,0,
r
3
#get next pointer
stw
r
2,0(
r
4) #store in new element
sync
#synchronize memory (can omit if not MP)
stwcx. r
4,0,
r
3
#add new element to list
bne-
loop
#loop if reservation lost
In the preceding example, livelock can occur in a multiprocessor system if two list
elements have next element pointers within the same reservation granule. If it is not
possible to allocate list elements such that next element pointers are in different reservation
granules, livelock can be avoided by using the following sequence:
lwz
r
2,0(
r
3) #get next pointer
loopl:
mr
r
5,
r
2
#keep a copy
stw
r
2,0(
r
4) #store in new element
sync
#synchronize memory
loop2:
lwarx
r
2,0,
r
3
#get next pointer again
cmpw
r
2,
r
5
#loop if changed
bne-
loopl
#(updated by another processor)
stwcx. r
4,0,
r
3
#add new element to list
bne-
loop2
#loop if reservation lost
Multiple-Precision Shifts
Following are programming examples for multiple-precision shifts. A multiple-precision
shift is a shift of an
n
-word quantity, where
n
>
1. The quantity to be shifted is contained in
n
registers. The shift amount is specified either by an immediate value in the instruction or
by bits 27:31 of a register.
The following examples distinguish between the cases
n
=
2 and
n
>
2. If
n
>
2, the examples
yield the desired result only when the shift amount is restricted to the range 0–31. When
n
>
2, the number of instructions required is 2
n
−
1 (immediate shifts) or 3
n
−
1 (non-
Содержание Virtex-II Pro PPC405
Страница 1: ...R Volume 2 a PPC405 User Manual Virtex II Pro Platform FPGA Developer s Kit March 2002 Release...
Страница 14: ...322 www xilinx com March 2002 Release 1 800 255 7778 Virtex II Pro Platform FPGA Documentation Preface R...
Страница 252: ...560 www xilinx com March 2002 Release 1 800 255 7778 Virtex II Pro Platform FPGA Documentation R...
Страница 260: ...568 www xilinx com March 2002 Release 1 800 255 7778 Virtex II Pro Platform FPGA Documentation R...
Страница 562: ...870 www xilinx com March 2002 Release 1 800 255 7778 Virtex II Pro Platform FPGA Documentation R...