4
Data Transfer
4 – 5
4.2.4
Calculating The Base Address
The base address of a circular buffer of length L is 2
n
or
a multiple of 2
n
,
where n satisfies the condition:
2
n-1
< L
≤
2
n
In other words, the base address is L “rounded” upwards to the closest
power of 2 (or its multiple). This rule implies that a certain number of low-
order bits of the base address must be zeroes.
In practice, you do not need to calculate n yourself; the linker
automatically places circular buffers at a proper address.
4.2.4.1 Circular Buffer Base Address Example 1
For
example, let us assume that the buffer length is eight. The length of the
buffer must be less than or equal to some value 2
n
; n therefore, must be
three or greater. The left side of the inequality rule specifies that the buffer
length must be greater than the value 2
n-1
; n therefore must be three or less.
The only value of n that satisfies both inequalities is three. Valid base
addresses are multiples of 2
n
, so in this example valid base addresses are
multiples of eight: 0x0008, 0x0010, 0x0018, and so on.
4.2.4.2 Circular Buffer Base Address Example 2
As a second example, assume a buffer length of seven. The inequality
again yields the same value for n, namely, three. With a buffer length of
seven, therefore, the valid base addresses are multiples of eight:
0x0008,
0x0010, 0x0018, and so on.
4.2.4.3 Circular Buffer Operation Example 1
Suppose that I0 = 5, M0 = 1, L0 = 3, and the base address = 4. The next
address is calculated as:
(I0 + M0 - B) mod L0 +B = (5 + 1 - 4) mod 3 + 4 = 6
The successive address calculations using I0 for indirect addressing
produce the sequence: 5, 6, 4, 5, 6, 4, 5 …. For M0 = –1 (0x3FFF), I0 would
produce the sequence: 5, 4, 6, 5, 4, 6, 5, 4 ….