Circular Buffer With Bit Reverse Addressing
Circular Buffer With Bit Reverse Addressing
IMPORTANT NOTICE
Texas Instruments (TI) reserves the right to make changes to its products or to discontinue any semiconductor product or service without notice, and advises its customers to obtain the latest version of relevant information to verify, before placing orders, that the information being relied on is current. TI warrants performance of its semiconductor products and related software to the specifications applicable at the time of sale in accordance with TIs standard warranty. Testing and other quality control techniques are utilized to the extent TI deems necessary to support this warranty. Specific testing of all parameters of each device is not necessarily performed, except those mandated by government requirements. Certain application using semiconductor products may involve potential risks of death, personal injury, or severe property or environmental damage (Critical Applications). TI SEMICONDUCTOR PRODUCTS ARE NOT DESIGNED, INTENDED, AUTHORIZED, OR WARRANTED TO BE SUITABLE FOR USE IN LIFE-SUPPORT APPLICATIONS, DEVICES OR SYSTEMS OR OTHER CRITICAL APPLICATIONS. Inclusion of TI products in such applications is understood to be fully at the risk of the customer. Use of TI products in such applications requires the written approval of an appropriate TI officer. Questions concerning potential risk applications should be directed to TI through a local SC sales office. In order to minimize risks associated with the customers applications, adequate design and operating safeguards should be provided by the customer to minimize inherent or procedural hazards. TI assumes no liability for applications assistance, customer product design, software performance, or infringement of patents or services described herein. Nor does TI warrant or represent that any license, either express or implied, is granted under any patent right, copyright, mask work right, or other intellectual property right of TI covering or relating to any combination, machine, or process in which such semiconductor products or services might be or are used.
TRADEMARKS
TI is a trademark of Texas Instruments Incorporated. Other brands and names are the property of their respective owners.
CONTACT INFORMATION
Contents
Abstract .........................................................................................................................7 Product Support............................................................................................................8 World Wide Web .......................................................................................................8 Linear Versus Circular Buffers.....................................................................................9 Linear Buffers ............................................................................................................9 Circular Buffers..........................................................................................................9 Theory..........................................................................................................................11 Limitations ...................................................................................................................12 Methodology................................................................................................................13 Pointer Manipulations.................................................................................................15 Method 1: Computing Offset Values ........................................................................15 Method 2: Saving the Original Value .......................................................................16 Method 3: Using Different Offset Values..................................................................17 Conclusion...................................................................................................................18 References...................................................................................................................19
Figures
Figure 1: Linear Buffer Requires Manual Data Movement..................................................9 Figure 2: Circular Buffer Moves Pointer Through Data.....................................................10
Tables
Table 1: Buffer Access Sequence....................................................................................11
Abstract
Delay lines are used in many DSP applications. Delay lines provide a buffer of the last N samples of data for a filter or other algorithm. While delay lines can be implemented as linear or circular buffers, buffers of the circular variety are often preferred due to their relative simplicity. Texas Instruments (TI) TMS320 family of DSPs can manipulate circular buffer pointers without penalty to code size or execution time through their bit-reversed addressing capabilities. Through consistent pointer manipulations, these buffers can be used anywhere traditional circular buffers are used. This application note describes:
Y Y Y Y
How linear and circular buffers handle data movement in a delay line. The properties and limitations of circular buffers. How to implement circular buffers with bit-reversed addressing. Three methods, with step-by-step examples, for handling pointer manipulations.
SPRA292
Product Support
World Wide Web
Our World Wide Web site at www.ti.com contains the most up to date product information, revisions, and additions. Users registering with TI&ME can build custom information pages and receive new product updates automatically via email.
SPRA292
Although TMS320 DSPs can perform this data movement in parallel with arithmetic processing, use of this feature is limited to the on-chip memory. Manual data movement in external memory is discouraged since it involves external data writes, which typically require at least 3 cycles. The parallel data moves are also limited in movement by only one data location and in only one direction. Some applications, such as a decimating filter, require the sample data to be accessed by an offset greater than one.
Circular Buffers
Circular buffers implement the delay line by moving a pointer through the data, rather than moving the data itself. New data is written one position above the previous sample. This requires that the pointer must be able to jump from the last location to the first, or vice-versa. In this manner, the buffer appears to be continuous, with the newest data overwriting the oldest data.
SPRA292
Since a great deal of software overhead is involved in checking the pointer values after every update, many DSPs provide circular buffering hardware that performs this function automatically. This hardware will wrap the pointer around once it goes past the end or start of the buffer. Although some of the TMS320 DSP family members (namely the C1x, C2x, and C2xx) do not provide this hardware, they can still provide zero-overhead circular buffering by using their bitreversed addressing hardware, normally used in FFT algorithms. This same method can also be used in DSPs with circular buffering hardware to provide additional circular buffers.
10
SPRA292
Theory
The only requirement for implementing a circular buffer is a method to keep a pointers value within the range of the buffer and wrap around from the highest to lowest address (or vice-versa). Using normal pointer arithmetic, carries are propagated to the left, causing the values to grow until the highest 16-bit number is reached. With bit-reversed addressing, the pointer arithmetic is done such that the carry bits propagate to the right. Carries from the rightmost bit are ignored, thus confining the pointer to a range determined solely by the value used to increment or decrement the pointer. As long as the pointers are updated in bit-reversed manner, only the specified lsbs are ever modified. The following example will illustrate this concept. Given a buffer size of 8, an index register set to 4, and an initial pointer set to 0x100, the sequence of accesses to the buffer is shown in Table 1.
000 + 100 = 100 100 + 100 = 010 010 + 100 = 110 110 + 100 = 001 001 + 100 = 101 101 + 100 = 011 011 + 100 = 111 111 + 100 = 000
11
SPRA292
Limitations
From this example we can see that a few restrictions are required to use this method. 1) The size of the buffer must be a power of two (2n). The filter length can be any size, however (see examples to follow). 2) The buffer must be aligned so that the starting address of the buffer has n lsb's equal to zero. 3) All pointer updates must use the bit-reversed update mode (*BR0+/-). This requires that the desired increment/decrement value be stored in the index register (AR0) in bit-reversed form. NOTE: All 2n data values in the buffer are accessed but not in linear order. This means that all accesses to the data in the buffer must comprehend the ordering of the data and never modify the circular buffer pointers using normal updates.
12
SPRA292
Methodology
Given the properties and limitations above, the following procedure should be used to implement a circular buffer: 1) Determine the required buffer size. This should be a number that is the smallest power of two that will hold all the data. For example, a buffer of 48 would require a buffer of size 64 (2n, where n=6). 2) Align the buffer such that the n lsbs are zero. This can be done manually or by using the align directive in the linker command file. 3) Initialize a pointer somewhere within the buffer. While typically you would set the pointer to the start of the buffer, it will work as long as it is anywhere within the buffer. 4) Set the index register (AR0 for the C2xx) to the desired offset value, in bit-reversed form. Typically an offset of 1 is used, which is half the buffer size in bit-reversed form. For example, a size 64 buffer would set AR0 = 32, or 10 0000 binary. 5) Step through the buffer using bit-reversed mode (*BR0+/-) to update the pointer. The following example shows code implementing a simple filter on a C203 in which the filter length is the same size as the circular buffer. Note that the data pointer ends up at the starting point of the buffer and must be manually moved back one position in order to implement the delay function.
13
SPRA292
COEF
.sect coef .word 3129h .word 7422h .text lar lar lar mar ldp
setup
ar0,#8 ar1,#cbuf ar2,#FRAME-1 *,ar1 #temp *,2 #0 #0 #15 COEF,*BR0+ temp,1 temp,5 *BR0-,ar2 filter,*-,ar1
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
= 1000b (bit-reversed 1) points into cbuf is loop counter auxiliary register pointer to ar1 data page pointer to temp page
filter in mpy lacl rpt mac apac sach out mar banz
get input from I/O port 2 clear P reg clear ACC do 16 taps ACC += x(n-k)*h(k) do last accumulate save result (Q15) output to I/O port 5 move pointer back 1 & change ARP to loop counter loop until done
14
SPRA292
Pointer Manipulations
There are often additional pointer manipulations to be made after the filtering or other function is complete. For instance, when using a filter length that is less than the buffer size, resetting the pointer to the location of the next input sample requires movement by a different value than used in the filter. Another example is access of an arbitrary sample in the delay line. Three different methods for manipulating bit-reversed pointers are shown below.
15
SPRA292
COEF
.sect coef .word 3129h .word 7422h .text lar lar lar mar ldp
setup
ar0,#20h ar1,#cbuf ar2,#FRAME-1 *,ar1 #temp *,2 #0 #0 #39 COEF,*BR0+ temp,1 temp,5 ar0,#25h *BR0-,ar2 ar0,#20h filter,*-,ar1
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
= 10 0000b (bit-reversed 1) points into cbuf is loop counter auxiliary register pointer to ar1 data page pointer to temp page
filter in mpy lacl rpt mac apac sach out lar mar lar banz
get input from I/O port 2 clear P reg clear ACC do 40 taps ACC += x(n-k)*h(k) do last accumulate save result (Q15) output to I/O port 5 ar0 = 10 0101b (bit-reversed 41) move pointer back 42 & change ARP to loop ctr ar0 = 10 0000b (bit-reversed 1) loop until done
16
SPRA292
COEF
.sect coef .word 3129h .word 7422h .text lar lar lar mar ldp
setup
ar0,#20h ar1,#cbuf ar2,#FRAME-1 *,ar1 #temp ar1,temp2 *,2 #0 #0 #39 COEF,*BR0+ temp,1 temp,5 ar1,temp2 *BR0-,ar2 filter,*-,ar1
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
= 10 0000b (bit-reversed 1) points into cbuf is loop counter auxiliary register pointer to ar1 data page pointer to temp page
filter sar in mpy lacl rpt mac apac sach out lar mar banz
save current data pointer value get input from I/O port 2 clear P reg clear ACC do 40 taps ACC += x(n-k)*h(k) do last accumulate save result (Q15) output to I/O port 5 restore data pointer move pointer back 1 & change ARP to loop ctr loop until done
17
SPRA292
Conclusion
Even when lacking dedicated circular buffering hardware, all members of the TMS320 family of DSPs can implement circular buffers through their bit-reversed addressing capabilities. This capability requires about the same overhead as dedicated circular buffers, both in code size and in execution time. It has been shown that use of these types of circular buffers have minimal limitations. Through consistent pointer manipulations, these buffers can be used wherever traditional circular buffers are used.
18
SPRA292
References
Randy Restle, Circular Buffer in Second Generation DSPs, TMS320 Designers Notebook Page #7, Texas Instruments, 1996.
TMS320C2xx Users Guide, Digital Signal Processing Solutions, Texas Instruments, 1995.
19