47
DSP C5000 DSP C5000 Chapter 12 Chapter 12 C and Assembly C and Assembly Interface Interface Copyright © 2003 Texas Instruments. All rights reserve Copyright © 2003 Texas Instruments. All rights reserve

Q15 Math TI

  • Upload
    sl10

  • View
    137

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Q15 Math TI

DSP C5000DSP C5000

Chapter 12Chapter 12

C and Assembly InterfaceC and Assembly Interface

Copyright © 2003 Texas Instruments. All rights reserved.Copyright © 2003 Texas Instruments. All rights reserved.

Page 2: Q15 Math TI

ESIEE, Slide 2 Copyright © 2003 Texas Instruments. All rights reserved.

Using C and assembly Using C and assembly

C54C54xx

C5C55x5x

Page 3: Q15 Math TI

ESIEE, Slide 3 Copyright © 2003 Texas Instruments. All rights reserved.

Objectives – C54Objectives – C54

Understand the Understand the C EnvironmentC Environment

Run the compilerRun the compiler

Describe how to Describe how to Mix C and Assembly Mix C and Assembly LanguageLanguage

Page 4: Q15 Math TI

ESIEE, Slide 4 Copyright © 2003 Texas Instruments. All rights reserved.

C Run-time EnvironmentC Run-time Environment

Func.C

int func(int a,int b,int c,int d,int e){ return(a + b + c + d + e);}

Main.C

int func(int,int,int,int,int);

int y = 0;

void main (void){ y = func(1,2,3,4,5);}

What other sectionsget created?...

y (global) .bss

0 (init val) .cinit

code .text

Page 5: Q15 Math TI

ESIEE, Slide 5 Copyright © 2003 Texas Instruments. All rights reserved.

C sectionsC sections

.text: .text: codecode Program ROMProgram ROM

.cinit: .cinit: global initsglobal inits Program ROMProgram ROM

.bss: .bss: variables variables Data RAMData RAM

.stack: .stack: for SPfor SP Data RAMData RAM

vectors vectors vectorsvectors Program ROM (0xFF80)Program ROM (0xFF80)

.const .const const int x=25;const int x=25; Data ROMData ROM

.switch .switch for case stmtsfor case stmts Program ROMProgram ROM

.sysmem .sysmem heap, dynamic memheap, dynamic mem Data RAMData RAM

Section NameSection Name Used for Used for Type of Memory Type of Memory

Page 6: Q15 Math TI

ESIEE, Slide 6 Copyright © 2003 Texas Instruments. All rights reserved.

CCS Compile & Link ProcessCCS Compile & Link Process

file1.cfile1.c

file.objfile.map

file.asm

Run-timeLibrary(rts.lib)

Compiler

Assembler

file.outfile.outVL

Debug: Symbolic Debug, Level 1 Optimization

Full opt: Level 3 Optimization

file2.asmfile2.asm

Lnk.rcp

Optimizer

Page 7: Q15 Math TI

ESIEE, Slide 7 Copyright © 2003 Texas Instruments. All rights reserved.

Initializing the C Environment ...Initializing the C Environment ...Boot.c in rts.lib

_c_int00: Initialize global and static variablesInitialize global and static variables Initialize C environment variablesInitialize C environment variables Setup stack (SP)Setup stack (SP) Call _mainCall _main

All symbols accessed by C require an underscore

On reset, how do you tellthe CPU to begin

execution at _c_int00?

;cvectors.asm

rsv: B _c_int00.sect “vectors”

.ref _c_int00

Page 8: Q15 Math TI

ESIEE, Slide 8 Copyright © 2003 Texas Instruments. All rights reserved.

Run-time EnvironmentRun-time Environment

ARP Auxiliary Reg Ptr 0 YesASM ACC shift mode YesBRAF Block Rpt Active Flag NoC Carry bit YesC16 Dual 16-bit math 0 NoCMPT Compatibility mode 0 NoCPL Compiler mode 1 NoFRCT Fractional mode 0 NoOVA/B ACC Overflow flags YesOVM Overflow mode 0 *SXM Sign-extension mode YesSMUL Saturate/multiply *TC Test Control flag Yes

Presumed ModifiedSTx Bits Name Value by C?

If the user modifies a “presumed value”, this value must berestored by the function

* - for intrinsics only

Page 9: Q15 Math TI

ESIEE, Slide 9 Copyright © 2003 Texas Instruments. All rights reserved.

Writing Func.ASMWriting Func.ASM

Func.C

int func(int a,int b,int c){ return(a + b + c);}

Main.C

int func(int,int,int);

int y = 0;

void main (void){ y = func(1,2,3);}

Main.C:

- prototypes called function

- calls function

How are the parameterspassed to func( ) ?

Page 10: Q15 Math TI

ESIEE, Slide 10 Copyright © 2003 Texas Instruments. All rights reserved.

Parameter PassingParameter Passing

used

used

Save on entry (SOE) - child must save if used

AR0AR1AR2AR3AR4AR5AR6AR7

PCarg2 = 2

arg3 = 3

SP

AB

arg1, ret value

y = func(1,2,3);

Return value placed in A accumulator

Argument 1 is passed in A accumulator Arguments 2,3… passed in reverse order via stack PC placed on stack

Ex: LD *SP(1),B ;arg2 loaded to accumulator B, i.e. *(SP + 1 )

Arguments on the stack can be accessed using compiler mode (CPL=1):

SPSP

Context save/restore: PSHM AR6, POPM AR6

Page 11: Q15 Math TI

ESIEE, Slide 11 Copyright © 2003 Texas Instruments. All rights reserved.

Func.ASMFunc.ASM .def _func

_func:

ADD *SP(1),A ;a + b

ADD *SP(2),A ;+ c

;pop SOE registers

Algorithm

Entry

ExitPC

2

3

used

SP

- declare func as global- define entry point (label)

- save SOE registers

;push SOE registers

- restore SOE registers

- return to calling routine

- execute the algorithm return(a + b + c);- place result in return reg RET

With maximum optimization, func is deleted and main simply does: ST #6,*(y)

Page 12: Q15 Math TI

ESIEE, Slide 12 Copyright © 2003 Texas Instruments. All rights reserved.

Accessing MMRs from CAccessing MMRs from CUsing pointers to access Memory-Mapped Registers :

Declare the necessary MMR component : volatile unsigned int *SWWSR = (volatile unsigned int *) 0x28;

Read and write to the register as desired :

*SWWSR = 0x8244;

Volatile modifier :

Especially important when using the optimizer

Tells compiler to always recheck actual memory whenever encountered

Otherwise, optimizer might register-base value, or eliminate construct

The regs54xx.h header file includes most MMR definitions

Page 13: Q15 Math TI

ESIEE, Slide 13 Copyright © 2003 Texas Instruments. All rights reserved.

Interrupts in CInterrupts in C

Interrupt Service Routine C function to run when interrupt occurs

All necessary context save/restore performed automatically

Interrupt Initialization Code Should be called prior to run-time process

Interrupt status may be modified during run-time

Interrupt Vector Table Written in ASM

Page 14: Q15 Math TI

ESIEE, Slide 14 Copyright © 2003 Texas Instruments. All rights reserved.

Writing ISRs in CWriting ISRs in C

int x[100] ;int *p = x ;

main { … } ;

interrupt void name(void) { static int y = 0 ; y += 1 ; if y < 100 *p++ = port0001; else asm(“ intr 17 “); }

Global variables allowsharing of data between main functions & ISR

KeywordName of ISR function

Void input and return values

Locals are lost across callsStatics persist across calls

ISRs should not include calls Return is with enable (RETE)Some compiler options delete

“dead” code

Page 15: Q15 Math TI

ESIEE, Slide 15 Copyright © 2003 Texas Instruments. All rights reserved.

Initializing Interrupts in CInitializing Interrupts in CSetup pointers to IMR & IFR. Initialize IMR, IFR, INTM :

volatile unsigned int *IMR = (volatile unsigned int *) 0x0000;

volatile unsigned int *IFR = (volatile unsigned int *) 0x0001;

*IFR = 0xFFFF;

*IMR = 0xFFFF;

asm(“ RSBX INTM “);

Create Vector Table :.sect “.vectors”…B _ISR1nopnop…

Compiled ISR Sequence : I$$SAVE performs context

save (from RTS.LIB) ISR function runs I$$RESTORE performs

context restore (RTS.LIB) RETE - Return with Enable

Page 16: Q15 Math TI

ESIEE, Slide 16 Copyright © 2003 Texas Instruments. All rights reserved.

Numerical Types in CNumerical Types in C

xxxx xxxx xxxx xxxx

yyyy yyyy yyyy yyyy

16-bit int

16-bit int*zzzz zzzz zzzz zzzzzzzz zzzz zzzz zzzz 32-bit prod

z= (int)(((long)x * (long)y )>>15); z= x * y;

z(Q15)z(Q15) z(Q0)z(Q0)

an integer is defined as the low portion of the accumulator

short, char, etc, occupy full 16-bits of memory

float operations supported via rts.lib (multicycle)

Q15 math in C is accomplished by shifting the result:

Page 17: Q15 Math TI

ESIEE, Slide 17 Copyright © 2003 Texas Instruments. All rights reserved.

C Optimization LevelsC Optimization Levels- allocates variables to registers

- simplifies expressions

- eliminates unused code

- removes unused assignments and common expressions

- single function (local) optimizations

- performs loop optimizations/unrolling

- multi-function (global) optimizations

- removes unused functions

- in-lines calls to small functions

- can perform multi-file optimizations using project mode (assertions)

- other options available with Level 3

Level 0

Level 1

“0” + ...

Level 2

“1” + ...

Level 3

“2” + ...

optimization levels are set via CCS build options

Page 18: Q15 Math TI

ESIEE, Slide 18 Copyright © 2003 Texas Instruments. All rights reserved.

Other C Stuff...Other C Stuff... In-Line Assembly

- can disrupt C Intrinsics

- ASM instructions in C- see C Compiler guide

Data/Program Sections

VL allows you to easily change the default stack and heap sizes

Volatile Keyword- compiler may remove code without volatile keyword

asm(“ IDLE 1”);

#include <intrindefs.h>y = _smacr(x1, x2, x3);

#pragma Data_Section(y,“Var”);int y = 0;

volatile unsigned int *ctrl;while (*ctrl != 0xFF);

Page 19: Q15 Math TI

ESIEE, Slide 19 Copyright © 2003 Texas Instruments. All rights reserved.

LAB11A - Mixing C and ASMLAB11A - Mixing C and ASM

1. Review the given file: 1. Review the given file: MAIN11A.CMAIN11A.C

2. Modify block FIR routine to be C callable2. Modify block FIR routine to be C callable

3. Create a Visual Linker recipe3. Create a Visual Linker recipe

4. Build, profile and verify operations 4. Build, profile and verify operations

Time: 75 minutes

Lab11b demonstrates calling a DSPLIB function from C. If you have time, run the lab.

Page 20: Q15 Math TI

ESIEE, Slide 20 Copyright © 2003 Texas Instruments. All rights reserved.

MAIN11A.C - SolutionMAIN11A.C - Solution#define RESULTS 185#define TAPS 16

// Initialize Coefficient Table#pragma DATA_SECTION (a,"coeffs");int a[TAPS] = {0x7FC, 0x7FD, 0x7FE, 0x7FF, 0x800, 0x801, 0x802, 0x803, 0x803, 0x802, 0x801, 0x800, 0x7FF, 0x7FE, 0x7FD, 0x7FC};

// Specify specific address for the result: y#pragma DATA_SECTION (y,"yloc");int y[RESULTS];

// include initialized x array#include "in11.h"

extern void fir(int taps,int results,int *y);

main(){ // set wait states to zero using in-line assembly asm(" STM #0,SWWSR"); // call assembly FIR routine fir(TAPS,RESULTS,y);}

Page 21: Q15 Math TI

ESIEE, Slide 21 Copyright © 2003 Texas Instruments. All rights reserved.

LAB11A.ASM - SolutionLAB11A.ASM - Solution

; allocate label definition here

.mmregs

.def _fir .ref _a,_x ; allocate initialized data sections here; only the first 8 values are used in Labs 2a and 3a

; allocate code section here

.sect "code"_fir: STLM A,BK ;load BK with TAPS (16)

LD *SP(1),A ;load parameter RESULTS into ASUB #1,A ;subtract 1 from the numberSTLM A, BRC ;load BRC with RESULTS-1 (184)MVDK *SP(2),*(AR1) ;load ARn with &y

RSBX CPL ;turn off Compiler Mode

LD #0,DP ;set SST bit (saturate on store)ORM #1,@PMST

SSBX CPL ;turn on Compiler Mode

Page 22: Q15 Math TI

ESIEE, Slide 22 Copyright © 2003 Texas Instruments. All rights reserved.

LAB11A.ASM - Solution (continued)LAB11A.ASM - Solution (continued)SSBX FRCT ;set FRCT bit (fractional mode)RSBX OVM ;clr OVM bit (overflow mode)SSBX SXM ;set SXM bit (sign extension)

STM #1,AR0STM #_a,AR2 ;setup ARs for MACSTM #_x,AR3

RPTB done-1 MPY *AR2+0%,*AR3+,A ;1st product RPT #14 ;mult/acc 15 terms

MAC *AR2+0%,*AR3+,A MAR *+AR3(-15) STH A,*AR1+ ;store result

done: RSBX FRCTRET ;return

Page 23: Q15 Math TI

ESIEE, Slide 23 Copyright © 2003 Texas Instruments. All rights reserved.

MAIN11B.C - SolutionMAIN11B.C - Solution/* include header files */#include "math.h"#include "tms320.h"#include "dsplib.h"

/* Define Sample and Tap sizes for function */#define RESULTS 200#define NH 16#define NX 200

short i;

/* Initialize Coefficient Table ... MUST BE ALIGNED IN MEMORY */#pragma DATA_SECTION (h,"coeffs");DATA h[NH] = {0x7FC, 0x7FD, 0x7FE, 0x7FF, 0x800, 0x801, 0x802, 0x803, 0x803, 0x802, 0x801, 0x800, 0x7FF, 0x7FE, 0x7FD, 0x7FC};

/* Specify specific address for the result array r */#pragma DATA_SECTION (r,"yloc");DATA r[RESULTS];

/* include initialized x array */#pragma DATA_SECTION (x,"xloc");#include "in11b.h"

Page 24: Q15 Math TI

ESIEE, Slide 24 Copyright © 2003 Texas Instruments. All rights reserved.

MAIN11B.C - Solution (continued)MAIN11B.C - Solution (continued)/* Setup delay buffer ... MUST BE ALIGNED IN MEMORY */#pragma DATA_SECTION (db,"delaybuff")DATA db[NH];

/* Setup indirected delay buffer pointer */DATA *dbptr = &db[0];

main(){/* set wait states to zero using in-line assembly */ asm(" STM #0,SWWSR"); /* clear delay buffer */for (i=0; i<NH;i++) db[i] = 0; /* call DSPLIB BLOCK FIR routine *//*

x pointer to datah pointer to aligned coeffs&dbptr delay bufferNH # of data samplesNX # of coeffsoflag overflow error flag

*/fir(x, h, r, &dbptr, NH, NX); }

Page 25: Q15 Math TI

ESIEE, Slide 25 Copyright © 2003 Texas Instruments. All rights reserved.

Follow on Activities for C54xFollow on Activities for C54x

Laboratory 9 for the TMS320C5416 Laboratory 9 for the TMS320C5416 DSKDSK

Compares the performance of functions Compares the performance of functions written in C code and assembly written in C code and assembly language and monitors the execution language and monitors the execution speed.speed.

Will allow the following question to be Will allow the following question to be answered:answered:

Does assembly language always offer Does assembly language always offer and advantage in performance over C and advantage in performance over C code?code?

Page 26: Q15 Math TI

ESIEE, Slide 26 Copyright © 2003 Texas Instruments. All rights reserved.

Objectives – C55Objectives – C55

Understand the Understand the C EnvironmentC Environment

Setting Setting compiler optionscompiler options

Describe how to Describe how to Mix C and AssemblyMix C and Assembly

Page 27: Q15 Math TI

ESIEE, Slide 27 Copyright © 2003 Texas Instruments. All rights reserved.

C Run-time EnvironmentC Run-time Environment

Func.CFunc.C

int func(int a,int b,int c,int d,int e)int func(int a,int b,int c,int d,int e){{ return(a + b + c + d + e);return(a + b + c + d + e);}}

Main.CMain.C

int func(int,int,int,int,int);int func(int,int,int,int,int);

int y = 0;int y = 0;

void main (void)void main (void){ { y = func(1,2,3,4,5);y = func(1,2,3,4,5);}}

How are theseHow are thesesections linked?sections linked?

y (global)y (global) .bss.bss

0 (init val)0 (init val) .cinit.cinit

codecode .text .text

Page 28: Q15 Math TI

ESIEE, Slide 28 Copyright © 2003 Texas Instruments. All rights reserved.

C Linker Command FileC Linker Command File

vectors:vectors: > VECS > VECS ;vector table;vector table.text:.text: >> EPROMEPROM ;code;code.cinit:.cinit: >> CROMCROM ;global inits;global inits.bss:.bss: >> SARAMSARAM ;global vars;global vars.stack:.stack: >> DARAMDARAM ;for SP;for SP.sysstack:.sysstack: > DARAM> DARAM ;for SSP;for SSP.const:.const: >> CROMCROM ;constants;constants.sysmem:.sysmem: >> SARAMSARAM ;for heap;for heap.switch:.switch: >> CROMCROM ;for case stmts;for case stmts

SECTIONSSECTIONS{ {

}}

MEMORYMEMORY{ VECS: org = 0xFFFF00, len = 00100h{ VECS: org = 0xFFFF00, len = 00100h EPROM: org = 0xFF0000, len = 0FF00hEPROM: org = 0xFF0000, len = 0FF00h SARAM: org = 0x008000, len = 08000hSARAM: org = 0x008000, len = 08000h DARAM: org = 0x002000, len = 02000hDARAM: org = 0x002000, len = 02000h CROM: org = 0xFE0000, len = 10000h CROM: org = 0xFE0000, len = 10000h }}

Page 29: Q15 Math TI

ESIEE, Slide 29 Copyright © 2003 Texas Instruments. All rights reserved.

Run-time Environment (STx_55)Run-time Environment (STx_55)Compiler CompilerCompiler Compiler

ST0_55ST0_55 NameName ExpectsExpects Modifies Modifies

ACOV[0–3]ACOV[0–3] Overflow detectionOverflow detection YesYesCC CarryCarry YesYesTC[1–2]TC[1–2] Test controlTest control YesYesDP[7–15]DP[7–15] Local data page reg Local data page reg NoNo

BRAFBRAF Block-repeat activeBlock-repeat active NoNoCPLCPL Compiler modeCompiler mode 1 1 NoNoINTMINTM Interrupt modeInterrupt mode NoNoM40M40 32/40-bit (D unit)32/40-bit (D unit) 0 0 NoNoSATDSATD Saturation (D unit)Saturation (D unit) 0 0 YesYesSXMDSXMD Sign-exten. (D unit)Sign-exten. (D unit) 1 1 NoNoC16C16 Dual 16-bit math Dual 16-bit math NoNoFRCTFRCT Fractional modeFractional mode 0 0 YesYesC54CMC54CM Compatibility modeCompatibility mode 0 0 NoNoASMASM Accumulator shift Accumulator shift YesYes

Compiler CompilerCompiler CompilerST1_55ST1_55 NameName ExpectsExpects Modifies Modifies

Page 30: Q15 Math TI

ESIEE, Slide 30 Copyright © 2003 Texas Instruments. All rights reserved.

Run-time Environment (STx_55)Run-time Environment (STx_55)

Compiler CompilerCompiler CompilerST2_55ST2_55 NameName Expects Expects Modifies Modifies

ARMSARMS AR modeAR mode 11 NoNoRDMRDM Rounding modeRounding mode 00 NoNoCDPLCCDPLC CDP linear/circularCDP linear/circular 00 NoNoAR[0–7]LCAR[0–7]LC AR[0–7] lin/circularAR[0–7] lin/circular 00 NoNo

MPNMCMPNMC MPNMC modeMPNMC mode NoNoSATASATA Saturation (A unit)Saturation (A unit) 00 YesYesSMULSMUL Sat on multiplySat on multiply 00 YesYesSSTSST Sat on store Sat on store NoNo

Compiler CompilerCompiler CompilerST3_55ST3_55 NameName ExpectsExpects Modifies Modifies

If the user modifies a “presumed value”, this value must beIf the user modifies a “presumed value”, this value must berestored by the functionrestored by the function

Page 31: Q15 Math TI

ESIEE, Slide 31 Copyright © 2003 Texas Instruments. All rights reserved.

Boot.c in rts55.libBoot.c in rts55.lib

_c_int00:_c_int00:

Initializing the C Environment ...Initializing the C Environment ...

Initialize global and static variablesInitialize global and static variables Initialize C environment variables toInitialize C environment variables to

expected valuesexpected values Setup stacks (SP & SSP)Setup stacks (SP & SSP) Call _mainCall _main

All C symbols accessed in assembly require a leading underscoreAll C symbols accessed in assembly require a leading underscore

On reset, how do you tellOn reset, how do you tell

the CPU to beginthe CPU to begin

execution at _c_int00?execution at _c_int00?

;cvectors.asm;cvectors.asm

rsv:rsv: .ivec _c_int00.ivec _c_int00.sect “vectors”.sect “vectors”

.ref _c_int00.ref _c_int00

Page 32: Q15 Math TI

ESIEE, Slide 32 Copyright © 2003 Texas Instruments. All rights reserved.

ObjectivesObjectives

Understand the Understand the C EnvironmentC Environment

Setting Setting compiler optionscompiler options

Describe how to Describe how to Mix C and AssemblyMix C and Assembly

Page 33: Q15 Math TI

ESIEE, Slide 33 Copyright © 2003 Texas Instruments. All rights reserved.

Setting Up a C ProjectSetting Up a C Project

Add any necessary includeAdd any necessary includefiles (via your source code)files (via your source code)

Add the run-time support Add the run-time support library (rts55.lib)library (rts55.lib)

Add the C source filesAdd the C source files

In addition to what has beenIn addition to what has beendone before, you need to:done before, you need to:

Page 34: Q15 Math TI

ESIEE, Slide 34 Copyright © 2003 Texas Instruments. All rights reserved.

Selecting the Build/Optimization OptionsSelecting the Build/Optimization Options

1.1. Select: Project Select: Project Build Options Build Options Compiler Tab Compiler Tab

2.2. Choose the necessary compiler optionsChoose the necessary compiler options

3.3. Build and benchmark.Build and benchmark.

Page 35: Q15 Math TI

ESIEE, Slide 35 Copyright © 2003 Texas Instruments. All rights reserved.

ObjectivesObjectives

Understand the Understand the C EnvironmentC Environment

Setting Setting compiler optionscompiler options

Describe how to Describe how to Mix C and Assembly Mix C and Assembly LanguageLanguage

Page 36: Q15 Math TI

ESIEE, Slide 36 Copyright © 2003 Texas Instruments. All rights reserved.

Creating a C-Callable Assembly RoutineCreating a C-Callable Assembly Routine

Func.CFunc.C

int func(int a,int b,int c,int d,int e)int func(int a,int b,int c,int d,int e){{ return(a + b + c + d + e);return(a + b + c + d + e);}}

Main.CMain.C

int func(int,int,int,int,int);int func(int,int,int,int,int);

int y = 0;int y = 0;

void main (void)void main (void){ { y = func(1,2,3,4,5);y = func(1,2,3,4,5);}}

Main.C:Main.C:

- prototypes- prototypes called function called function

- calls function - calls function

How are the parametersHow are the parameterspassed to func( ) ?passed to func( ) ?

With -o3 enabled, func is deleted and main simply does: With -o3 enabled, func is deleted and main simply does: MOV #15,*(#_y)MOV #15,*(#_y)

Page 37: Q15 Math TI

ESIEE, Slide 37 Copyright © 2003 Texas Instruments. All rights reserved.

Parameter Passing ConventionsParameter Passing Conventions

y = func(1,2,3,4,5);y = func(1,2,3,4,5);

The compiler will scan the parameters (from left to right)The compiler will scan the parameters (from left to right)and place them into the following registers (from left to right):and place them into the following registers (from left to right):

The registers are filled in the order shown:The registers are filled in the order shown:- - T0T0 gets the first 16-bit int, gets the first 16-bit int, T1T1 gets the 2nd, etc. gets the 2nd, etc.- - XAR0XAR0 gets the first pointer if available, etc. gets the first pointer if available, etc.- 32-bit values are passed in - 32-bit values are passed in AC0-2AC0-2

All parameters that don’t find a home in a register, areAll parameters that don’t find a home in a register, areplaced on the placed on the stackstack (SP). (SP).

16-bit Integers16-bit Integers Pointers Pointers Longs Longs

T0-1, AR0-4T0-1, AR0-4 XAR0-4XAR0-4 AC0-2AC0-2

So, which registers will contain the parameters listed above? So, which registers will contain the parameters listed above?

Page 38: Q15 Math TI

ESIEE, Slide 38 Copyright © 2003 Texas Instruments. All rights reserved.

Exercise/RecommendationsExercise/Recommendationsvar = func(0x98765432,1,2,3,4,5,a,x,y,z);var = func(0x98765432,1,2,3,4,5,a,x,y,z);

a, x, y, and z are pointersa, x, y, and z are pointers Where are these parameters placed by the compiler?Where are these parameters placed by the compiler?

T0=T0=

T1=T1=

XAR0=XAR0=

XAR1=XAR1=

XAR2=XAR2=

XAR3=XAR3=

XAR4=XAR4=

AC0=AC0=

11

22

33

44

55

aa

xx

0x987654320x98765432

zzyy SPSP

last usedlast used

StackStack

Pass pointers firstPass pointers first Pass most-used parameters firstPass most-used parameters first

Recommend:Recommend:

(parameters are placed (parameters are placed on the stack as shown)on the stack as shown)

lolo

hihi

Page 39: Q15 Math TI

ESIEE, Slide 39 Copyright © 2003 Texas Instruments. All rights reserved.

Calling Convention, Accessing the StackCalling Convention, Accessing the Stack So, who’s responsible for saving registers? The parent or child?So, who’s responsible for saving registers? The parent or child?

Parent - SOC (save on call)Parent - SOC (save on call)

T0-1 XAR0-4 AC0-3T0-1 XAR0-4 AC0-3

Child - SOE (save on entry)Child - SOE (save on entry)

T2-3 XAR5-7T2-3 XAR5-7

If the child routine modifies the SOE registers, it must If the child routine modifies the SOE registers, it must preserve their contentspreserve their contents

Return values from a function are placed here:Return values from a function are placed here:

Return ValuesReturn Values

16/32-bit integer:16/32-bit integer: T0/AC0 T0/AC0,, Data/Func pointer:Data/Func pointer: XAR0/AC0 XAR0/AC0

Accessing parameters on the stack:Accessing parameters on the stack:

PSH AR6,AR7PSH AR6,AR7

MOV *SP(#3),AR6MOV *SP(#3),AR6

MOV *SP(#4),AR7MOV *SP(#4),AR7

SPSP

parent stkparent stk

StackStack

&z&z&y&yPCPC

AR7AR7AR6AR6

Page 40: Q15 Math TI

ESIEE, Slide 40 Copyright © 2003 Texas Instruments. All rights reserved.

Remember Func.C ?Remember Func.C ?

Func.CFunc.C

int func(int a,int b,int c,int d,int e)int func(int a,int b,int c,int d,int e){{ return(a + b + c + d + e);return(a + b + c + d + e);}}

Main.CMain.C

int func(int,int,int,int,int);int func(int,int,int,int,int);

int y = 0;int y = 0;

void main (void)void main (void){ { y = func(1,2,3,4,5);y = func(1,2,3,4,5);}}

Now let’s writeNow let’s writeFunc.C in assembly...Func.C in assembly...

Page 41: Q15 Math TI

ESIEE, Slide 41 Copyright © 2003 Texas Instruments. All rights reserved.

Writing Func.ASMWriting Func.ASM .def _func.def _func

_func:_func:

ADD T1,T0 ;a + bADD T1,T0 ;a + b ADD AR0,T0 ;+ c ADD AR0,T0 ;+ c ADD AR1,T0 ;+ d ADD AR1,T0 ;+ d ADD AR2,T0 ;+ e ADD AR2,T0 ;+ e ;restore STx_55 values;restore STx_55 values

;pop SOE registers;pop SOE registers

AlgorithmAlgorithm

EntryEntry

ExitExit

PCPC

usedused

SPSP

- declare func as global- declare func as global- define entry point (label)- define entry point (label)

- save SOE registers- save SOE registers

;push SOE registers;push SOE registers;adjust STx_55 values;adjust STx_55 values

- restore SOE registers - restore SOE registers

- return to calling routine- return to calling routine

- execute the algorithm- execute the algorithm return(a + b + c + d + e);return(a + b + c + d + e);- place result in return reg- place result in return reg

RETRET

Page 42: Q15 Math TI

ESIEE, Slide 42 Copyright © 2003 Texas Instruments. All rights reserved.

Other C Stuff...Other C Stuff... In-Line AssemblyIn-Line Assembly

- can disrupt C- can disrupt C Intrinsics Intrinsics

- ASM instructions in C- ASM instructions in C- see C Compiler guide- see C Compiler guide

Data/Code SectionsData/Code Sections

Linker Command OptionsLinker Command Options- stack, sysstack and heap- stack, sysstack and heap sizes can be changed via sizes can be changed via CCS CCS

Volatile KeywordVolatile Keyword- compiler may remove- compiler may remove code w/o volatile kywd code w/o volatile kywd

Interrupt KeywordInterrupt Keyword- context save/restore- context save/restore

asmasm(“ IDLE”);(“ IDLE”);

#include <intrindefs.h>#include <intrindefs.h>y = y = _sadd_sadd(x1, x2);(x1, x2);

#pragma Data_Section(y,“Var”);#pragma Data_Section(y,“Var”);int y = 0;int y = 0;

volatilevolatile unsigned int *ctrl; unsigned int *ctrl;while (*ctrl != 0xFF);while (*ctrl != 0xFF);

interrupt void my_ISR (void)interrupt void my_ISR (void)

Page 43: Q15 Math TI

ESIEE, Slide 43 Copyright © 2003 Texas Instruments. All rights reserved.

LAB12A - Mixing C and ASM LAB12A - Mixing C and ASM Exercise for C55Exercise for C55

1. Review the given file: 1. Review the given file: MAIN12A.CMAIN12A.C

2. Modify block FIR routine to be C callable2. Modify block FIR routine to be C callable

3. Review/modify 3. Review/modify givengiven linker command file linker command file

4. Compile, link, benchmark and verify4. Compile, link, benchmark and verify operations operations

Time: 90 minutesTime: 90 minutes

Page 44: Q15 Math TI

ESIEE, Slide 44 Copyright © 2003 Texas Instruments. All rights reserved.

1. Review the given file: 1. Review the given file: main12b.c main12b.c

2. Modify block FIR routine to be C callable2. Modify block FIR routine to be C callable

3. Compile, link, benchmark and verify operations 3. Compile, link, benchmark and verify operations

Time: 90 minutesTime: 90 minutes

LAB12B – Mixing C and AssemblyLAB12B – Mixing C and Assembly

LAB12A – Using the C Compiler/OptimizerLAB12A – Using the C Compiler/Optimizer

1. Review the given file: 1. Review the given file: lab12a.clab12a.c

2. Run the compiler, profile the results. Result? 54K cycles.2. Run the compiler, profile the results. Result? 54K cycles.

3. How do you use compiler options to decrease the 3. How do you use compiler options to decrease the benchmark to ~2000 cycles?benchmark to ~2000 cycles?

Time: 30 minutesTime: 30 minutes

Page 45: Q15 Math TI

ESIEE, Slide 45 Copyright © 2003 Texas Instruments. All rights reserved.

main12b.Cmain12b.C - Solution - Solution/* Define Sample, Taps, Counts for function *//* Define Sample, Taps, Counts for function */#define SAMPS 200#define SAMPS 200#define TAPS 16#define TAPS 16#define BLKRPT_CNT (SAMPS-TAPS)/2 /* generate 186 results */#define BLKRPT_CNT (SAMPS-TAPS)/2 /* generate 186 results */#define RPT_CNT TAPS-3 /* modify based on your asm routine */#define RPT_CNT TAPS-3 /* modify based on your asm routine */

/* Initialize Coefficient Table *//* Initialize Coefficient Table */int a0[TAPS] = {int a0[TAPS] = { 0x7FC, 0x7FD, 0x7FE, 0x7FF,0x7FC, 0x7FD, 0x7FE, 0x7FF, 0x800, 0x801, 0x802, 0x803,0x800, 0x801, 0x802, 0x803, 0x803, 0x802, 0x801, 0x800,0x803, 0x802, 0x801, 0x800,

0x7FF, 0x7FE, 0x7FD, 0x7FC};0x7FF, 0x7FE, 0x7FD, 0x7FC};

/* Specify specific address for the result: y *//* Specify specific address for the result: y */#pragma DATA_SECTION (y0,"yloc");#pragma DATA_SECTION (y0,"yloc");int y0[200];int y0[200];

/* include initialized x array *//* include initialized x array */#include "in12.h"#include "in12.h"

extern void fir(int *x0,int *a0,int *y0,int taps,int blkrpt,int rpt);extern void fir(int *x0,int *a0,int *y0,int taps,int blkrpt,int rpt);

main()main(){{ /* call assembly FIR routine *//* call assembly FIR routine */ fir(x0,a0,y0,TAPS,BLKRPT_CNT,RPT_CNT); fir(x0,a0,y0,TAPS,BLKRPT_CNT,RPT_CNT); for (;;);for (;;);}}

Page 46: Q15 Math TI

ESIEE, Slide 46 Copyright © 2003 Texas Instruments. All rights reserved.

lab12b.asmlab12b.asm - Solution - Solution .def _fir.def _fir

.cpl_on.cpl_on

.arms_on.arms_on

.c54cm_off.c54cm_off

; Parameter pass looks like this:; Parameter pass looks like this:; XAR0 = x0; XAR0 = x0; XAR1 = a0; XAR1 = a0; XAR2 = y0; XAR2 = y0; T0 = TAPS; T0 = TAPS; T1 = BLKRPT_CNT; T1 = BLKRPT_CNT; XAR3 = RPT_CNT ; XAR3 = RPT_CNT

.text.text

_fir: _fir: PSH mmap(@ST1) PSH mmap(@ST1) ;context save of affected;context save of affected PSH mmap(@ST2) PSH mmap(@ST2) ;status registers;status registers

BSET FRCT BSET FRCT ;turn on multiplier shift;turn on multiplier shiftBSET M40 BSET M40 ;turn on 40 bit math;turn on 40 bit mathBSET SXMD BSET SXMD ;turn on sign extension (C default);turn on sign extension (C default)BCLR C54CMBCLR C54CM ;go to C55 native mode (C default);go to C55 native mode (C default)

MOV T1,BRC0MOV T1,BRC0 ;block repeat count ;block repeat count MOV XAR0,XAR4MOV XAR0,XAR4 ;set upper bits of XAR4 ;set upper bits of XAR4

MOV XAR1,XCDPMOV XAR1,XCDP ;pointer for coefficients;pointer for coefficients

MOV #0, CDPMOV #0, CDP ;zero lower bits of XCDP;zero lower bits of XCDP MOV AR1,mmap(@BSAC)MOV AR1,mmap(@BSAC) ;buffer start address;buffer start address MOV T0,mmap(@BKC) MOV T0,mmap(@BKC) ;buffer size ;buffer size

Page 47: Q15 Math TI

ESIEE, Slide 47 Copyright © 2003 Texas Instruments. All rights reserved.

lab12b.asmlab12b.asm - Solution - SolutionADD #1,AR4 ADD #1,AR4 ;pointer setup for x1;pointer setup for x1MOV AR3,T0MOV AR3,T0 ;set up TO for ptr wrap;set up TO for ptr wrapMOV AR3,CSRMOV AR3,CSR ;Computed single repeat value;Computed single repeat valueBSET CDPLCBSET CDPLC ;turn on circ addressing for CDP;turn on circ addressing for CDP

RPTBlocal endRPTBlocal end

MPY *AR0+,*CDP+,AC0 ;AC0 gets 1st productMPY *AR0+,*CDP+,AC0 ;AC0 gets 1st product :::: MPY *AR4+,*CDP+,AC1 ;AC1 gets 2nd product MPY *AR4+,*CDP+,AC1 ;AC1 gets 2nd product

|||| RPT CSR ;RPT in parallel with MPYs RPT CSR ;RPT in parallel with MPYs MAC *AR0+,*CDP+,AC0 ;form 14 resultsMAC *AR0+,*CDP+,AC0 ;form 14 results :::: MAC *AR4+,*CDP+,AC1 MAC *AR4+,*CDP+,AC1 MAC *(AR0-T0),*CDP+,AC0 MAC *(AR0-T0),*CDP+,AC0 :::: MAC *(AR4-T0),*CDP+,AC1 MAC *(AR4-T0),*CDP+,AC1 ;form last result and wrap pointers ;form last result and wrap pointers end: end: MOV pair(hi(AC0)),dbl(*AR2+) MOV pair(hi(AC0)),dbl(*AR2+) ;store AC0 & AC1 results ;store AC0 & AC1 results

POP mmap(@ST2) POP mmap(@ST2) ;restore status registers;restore status registersPOP mmap(@ST1)POP mmap(@ST1)

RETRET