Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
46
Добавлен:
27.04.2015
Размер:
4.26 Mб
Скачать

Software Techniques

Another use of the PUSH instruction is for temporary storage. Sometimes a temporary variable is required, such as in swapping two registers. There are two techniques for doing this, the first using an unused register and the second using a location on the stack. The second technique uses the PUSH instruction macro and works whenever there are no other registers available. The two techniques are shown in the following code:

;Swapping two registers (X0, R0) using an Available Register (N)

;3 Icyc, 3 Instruction Words

MOVE

X0,N

; X0

->

TEMP

MOVE

R0,X0

;

R0

->

X0

MOVE

N,R0

;

TEMP -> R0

;Swapping two registers (X0, R0) using a Stack Location

;4 Icyc, 4 Instruction Words

PUSH

X0

; X0

->

TEMP

MOVE

R0,X0

;

R0

->

X0

POP

R0

;

TEMP -> R0

The operation is faster using an unused register if one is available. Often, the N register is a good choice for temporary storage, as in the preceding example.

8.6 Loops

The DSP56800 core contains a powerful and flexible hardware DO loop mechanism. It allows for loop counts up to 8,192, it allows a large number of instructions (maximum of 64K) to reside within the body of the loop, and hardware DO loops can be interrupted. In addition, loops execute correctly from both on-chip and off-chip program memory, and it is possible to single step through the instructions in the loop using the OnCE port for emulation.

The DSP56800 core also contains a useful hardware REP loop mechanism, which is very useful for very simple, fast looping on a single instruction. It is very useful for simple nesting when the inner loop only contains a single instruction. For a REP loop, the instruction to be repeated is only fetched once from program memory, reducing activity on the buses. This is very useful when executing code from off-chip program memory. However, REP loops are not interruptible.

8.6.1 Large Loops (Count Greater Than 63)

Currently, the DO instruction allows an immediate value up to the value 63 to be specified for the loop count. When necessary, specifying an immediate value larger than 63 is done using one of the registers on the DSP56800 core to specify the loop count. Since registers are a precious resource, it is desirable not to use any important registers that may contain valid data. The following code shows a technique for specifying loop counts greater than 63 without destroying any register values.

 

MOVE

#2048,LC

; Specify a loop count greater than 63

 

 

 

; using the LC register

 

DO

LC,LABEL

; (LC register used to avoid destroying

;

(instructions)

; another register)

 

LABEL

 

 

 

Since the LC register is already a dedicated register used for looping and is always loaded by the DO instruction, no information is lost when this register is used to specify a larger loop count. Note that this technique will also work with the LC register for nested loops, as long as the loading of the LC register with immediate data occurs after the LC register is pushed for nested loops.

8-20

DSP56800 Family Manual

 

Loops

NOTE:

This technique should not be used for the REP instruction because it will destroy the value of the LC register if done by a REP instruction nested within a hardware DO loop.

8.6.2 Variable Count Loops

There are cases where it is useful to loop for a variable number of times instead of a constant number of times. For these cases the loop count is specified using a register. This allows a variable number of loop iterations from 1 to 2k times (where k is the number of bits in the LC register, or 13). It is important to consider what takes place if this variable is zero or negative. Whenever a DO loop is executed and the loop count is zero, the loop will execute 213 times. For the case where the number of iterations is negative, the number will simply be interpreted as an unsigned positive number and the loop will be entered. If there is a possibility that a register value may be less than or equal to zero, then it is necessary to insert extra code outside of the loop to detect this and branch over the loop. This is demonstrated in the following code.

; Hardware looping when the loop count can be negative or zero

TSTW

X0

; Skip over loop if loop count <= 0

BLE

LABEL

 

DO

X0,LABEL

 

ASL

A

 

LABEL

 

 

For the case of REP looping on a register value when the register contains the value 0, the instruction to be repeated is simply skipped as desired; no extra code is required. This is also true when an immediate value of 0 is specified. For the case where the number of iterations can be negative, the response is the same as for the DO loop and can be solved using the preceding technique presented for DO looping.

8.6.3 Software Loops

The DSP56800 provides the capability for implementing loops in either hardware or software. For non-nested loops in critical code sections, the hardware looping mechanism is always the fastest. However, there is a limitation when the hardware looping mechanism is used. The DSP56800 allows a maximum of two nested hardware DO loops. Any looping beyond this generates a HWS overflow interrupt.

Software looping techniques are also efficiently implemented on the DSP core. Software looping simply uses a register or memory location and decrements this value until it reaches zero. A branch instruction conditionally branches to the top of the loop.

There are three different techniques for implementing a loop in software: one using a data ALU register, one using an AGU register, and one using a memory location to hold the loop count. Each of these is shown in the following code.

 

Software Techniques

8-21

Соседние файлы в папке DSP568xx