������� ����� ���������� �� ����������
ApplicationReport
1998 Digital Signal Processing Solutions
Printed in U.S.A., July 1998 SPRA179
1998
ReportApplication
������������������
������������� �������
Digital Sine-Wave GenerationUsing the TMS320C2xx
SPRA179July 1998
Printed on Recycled Paper
IMPORTANT NOTICE
Texas Instruments and its subsidiaries (TI) reserve the right to make changes to their productsor to discontinue any product or service without notice, and advise customers to obtain the latestversion of relevant information to verify, before placing orders, that information being relied onis current and complete. All products are sold subject to the terms and conditions of sale suppliedat the time of order acknowledgement, including those pertaining to warranty, patentinfringement, and limitation of liability.
TI warrants performance of its semiconductor products to the specifications applicable at thetime of sale in accordance with TI’s standard warranty. Testing and other quality controltechniques are utilized to the extent TI deems necessary to support this warranty. Specific testingof all parameters of each device is not necessarily performed, except those mandated bygovernment requirements.
CERTAIN APPLICATIONS USING SEMICONDUCTOR PRODUCTS MAY INVOLVEPOTENTIAL RISKS OF DEATH, PERSONAL INJURY, OR SEVERE PROPERTY ORENVIRONMENTAL DAMAGE (“CRITICAL APPLICATIONS”). TI SEMICONDUCTORPRODUCTS ARE NOT DESIGNED, AUTHORIZED, OR WARRANTED TO BE SUITABLE FORUSE IN LIFE-SUPPORT DEVICES OR SYSTEMS OR OTHER CRITICAL APPLICATIONS.INCLUSION OF TI PRODUCTS IN SUCH APPLICATIONS IS UNDERSTOOD TO BE FULLYAT THE CUSTOMER’S RISK.
In order to minimize risks associated with the customer’s applications, adequate design andoperating safeguards must be provided by the customer to minimize inherent or proceduralhazards.
TI assumes no liability for applications assistance or customer product design. TI does notwarrant 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 anycombination, machine, or process in which such semiconductor products or services might beor are used. TI’s publication of information regarding any third party’s products or services doesnot constitute TI’s approval, warranty or endorsement thereof.
Copyright 1998, Texas Instruments Incorporated
iii Digital Sine-Wave Generation Using the TMS320C2xx
Contents1 Introduction 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Direct Look-Up and Linear Interpolation Method 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1 C- and Assembly-Compatible Functions 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1.1 C-Compatible Functions 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.2 Assembly-Compatible Functions 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Interpolation 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Functions 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 System Utilization 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Sine-Generation Precision 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5.1 One-Sine-Period Analysis 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.2 Quarter-Sine-Period Analysis 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Sine/Cosine With Mathematical Series 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1 C- and Assembly-Compatible Functions 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1.1 C-Compatible Functions 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.2 Assembly-Compatible Functions 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Sine/Cosine Mathematical Series 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Functions 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 System Utilization 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5 Sine-Generation Precision 12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.1 One-Sine-Period Analysis 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5.2 Quarter-Sine-Period Analysis 14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Appendix A Examples of a C Program and an Assembly Program Calling Sine andCosine Functions A-1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1 Main C Program to Call Sine or Cosine Functions A-1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2 Main Assembly Program to Call Sine or Cosine Functions A-2. . . . . . . . . . . . . . . . . . . . . . . A.3 COS Function With Direct Look-Up and Linear Interpolation for Assembly Program A-4. A.4 COS Function With Direct Look-Up and Linear Interpolation for C Program A-7. . . . . . . . A.5 SIN Function With Direct Look-Up and Linear Interpolation for
Assembly Program A-10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.6 SIN Function With Direct Look-Up and Linear Interpolation for C Program A-13. . . . . . . . A.7 Table for COS or SIN Function With Direct Look-Up A-16. . . . . . . . . . . . . . . . . . . . . . . . . . . . A.8 COS_SIN Function With Direct Look-Up and Linear Interpolation for
Assembly Program A-19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.9 Table for COS_SIN Function With Direct Look-Up A-23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.10 SIN With Mathematical Series for Assembly Program A-27. . . . . . . . . . . . . . . . . . . . . . . . . A.11 SIN With Mathematical Series for C Program A-30. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.12 COS With Mathematical Series for Assembly Program A-34. . . . . . . . . . . . . . . . . . . . . . . . A.13 COS With Mathematical Series for C Program A-38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Figures
SPRA179iv
List of Figures
1 The Formula and the Resulting Graph for Calculating the Table InterpolationValue COS of an Angle 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Direct Look-Up With Interpolation of Sine Calculation From 0 to 2*π With a 0.015-Radian Step 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Error Between the C Floating-Point Result and Direct Look-Up Calculation From 0 to 2*π With a 0.015-Radian Step 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Results of Direct Look-Up SIN From 0 to π/2 With a 0.0039-Radian Step 7. . . . . . . . . . . . . . .
5 Error Between the C Floating-Point Result and Direct Look-Up SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in Q15 Format 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 Error Between the C Floating-Point Result and the Direct Look-Up SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in % 9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7 Formula for Calculating the Value SIN of an Angle 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Formula for Calculating the Value COS of an Angle 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9 Mathematical Series Sine Calculation From 0 to 2*π With a 0.015-Radian Step 13. . . . . . . .
10 Error Between the C Floating-Point Result and the Mathematical Series SINE Calculation From 0 to 2*π With a 0.015-Radian Step 14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11 Results of Mathematical Series Sine From 0 to π/2 With a 0.0039-Radian Step 15. . . . . . . . .
12 Error Between the C Floating-Point Result and a Mathematical Series SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in Q15 Format 16. . . . . . . . . . . . . . . . .
13 Error Between the C Floating-Point Result and a Mathematical Series SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in % 17. . . . . . . . . . . . . . . . . . . . . . . . . .
Tables
v Digital Sine-Wave Generation Using the TMS320C2xx
List of Tables1 Processor Utilization (Maximum) When Using Direct Look-up and Interpolation 4. . . . . . . . . 2 Memory Utilization When Using Direct Look-up and Interpolation 4. . . . . . . . . . . . . . . . . . . . . . 3 Processor Utilization (Maximum) When Using Mathematical Series 11. . . . . . . . . . . . . . . . . . . 4 Memory Utilization When Using Mathematical Series 12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SPRA179vi
1 Digital Sine-Wave Generation Using the TMS320C2xx
Digital Sine-Wave Generation Using the TMS320C2xx
ABSTRACTThis application report describes two methods for implementing a digital sine-wavegenerator using the TMS320C2xx: the use of a direct look-up table with linearinterpolation, and the use of mathematical series. Both methods are explained interms of C-compatible and assembly-compatible functions. Formulas used forcalculating and graphing the results are included for each method to demonstratethe differences.
1 IntroductionSine-wave generators are fundamental building blocks of signal processingsystems for use in control applications. The following two methodsdescribed in this application report are for implementing a digital sine-wavegenerator using the TMS320C2xx:
• Direct look-up with linear interpolation to provide sine waves with aminimum of harmonic distortion
• Realization of the sine/cosine functions with a mathematical series
The two methods respond to different constraints. The direct look-upmethod is fast with minimal errors and is significant in ROM programmingrequirements. The mathematical series method is fast and requiresminimum (ROM) programming but results in less precision.
Direct Look-Up and Linear Interpolation Method
SPRA1792
2 Direct Look-Up and Linear Interpolation MethodThis section describes the Direct Look-Up and Linear Interpolation methodof implementing a digital sine-wave generator using the TMS320C2xx.
2.1 C- and Assembly-Compatible Functions
The Direct Look-Up method, which uses 100 sine-period values in a table,reveals the sine/cosine functions. To provide minimum harmonic distortion,the method includes linear interpolation with a fixed-step table. An extendedtable is used for the cosine + sine function and is located in program memory.
Two versions of each function are presented:
• C-compatible functions
• Assembly-compatible functions
Examples of a C program and an assembly program (that call sine andcosine functions) are in the appendix.
2.1.1 C-Compatible Functions
Fully C-compatible functions employ C conventions to use the stack forparameters passed to the functions. Parameters returned by the functionsare passed by a pointer. Responsibilities of called functions are managedin a postfix expression called the function designator. Stack pointer AR1 iswell-positioned, and the return address of the hardware stack is popped(retrieved) in case of C-interrupt runtime events using C-function features(stack). The frame pointer is not modified and registers AR6 and AR7 arenot used.
2.1.2 Assembly-Compatible Functions
Assembly-compatible functions do not use their own variables, but instead,rely on variables in a software stack where arguments are passed. The AR1register points to the stack just after the last argument. After the return fromthe function, the results are placed in the stack. Registers AR0, AR6, andAR7 are not modified.
Direct Look-Up and Linear Interpolation Method
3 Digital Sine-Wave Generation Using the TMS320C2xx
2.2 Interpolation
Figure 1 shows the formula and the resulting graph for calculating the tableinterpolation value COS(X) of an angle X.
ii+ 1x - x
Y = y + i+1 i
y - y )(
X – xi
i
r = ratiox x
i i+1
y
y
i
i+1
X
Y = ?
x
y
where:{xi} = {first coordinates of the table},{yi} = {second coordinates of the table},i chosen so that xi < X < xi+1.
Figure 1. The Formula and the Resulting Graph for Calculating the TableInterpolation Value COS of an Angle
Table interpolation is comprised of two steps:
1. Direct look-up: looking through the table to find the interval [xi, xi+1] atwhich the considered angle X is located, with xi < X < xi+1.
2. Interpolation: solving the above mentioned calculation to obtain Y.
2.3 Functions
Functions SINE, COSINE, and SINE + COSINE are illustrated by examplein the appendix. Conventions to interface with this function are:
• Input: ANGLE parameters are in the stack pointed by AR1. The valueof this angle is unsigned.
0° <-> 0000h
90° <-> 4000h
180° <-> 8000h
240° <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15format (–1 to 1–215).
2.4 System Utilization
Table 1 shows processor utilization when using the direct look-up and linearinterpolation methods.
Direct Look-Up and Linear Interpolation Method
SPRA1794
Table 1. Processor Utilization (Maximum) When Using Direct Look-Up and Interpolation
FUNCTION CYCLES EXECUTION TIME
SIN assembly call 35 1.75 µs
COS assembly call 38 1.90 µs
SIN fully C-compatible 42 2.10 µs
COS fully C-compatible 45 2.25 µs
SIN + COS of an angle 58 2.90 µs
Table 2 shows memory utilization when using the direct look-up and linearinterpolation method.
Table 2. Memory Utilization When Using Direct Look-Up and Interpolation
FUNCTIONROM
(WORDS) STACKLEVELSREGISTERS
USEDRAM
(WORDS)
SIN assembly call 32 + 100 3 1 In stack
COS assembly call 34 + 100 3 1 In stack
COS and SIN assembly call 66 + 100 3 1 In stack
SIN Fully C compatible 42 + 100 5 1 In stack
COS assembly call 44 + 100 5 1 In stack
COS + SIN in one function†
assembly call55 + 125 3 1 In stack
† In the case of Sine + Cosine, the same table is used.
2.5 Sine-Generation Precision
Figure 2 and Figure 3 are graphs that present the precision of the directlook-up and linear interpolation sine functions. The precision measurementis made by comparing the direct look-up and the floating-point C libraryresults.
2.5.1 One-Sine-Period Analysis
The sine period provides a general image of error calculation. In Figure 2and Figure 3, results of the sine calculation are in Q15 format.
Direct Look-Up and Linear Interpolation Method
5 Digital Sine-Wave Generation Using the TMS320C2xx
Figure 2. Direct Look-Up With Interpolation of Sine Calculation From 0 to 2*π With a 0.015-Radian Step
Direct Look-Up and Linear Interpolation Method
SPRA1796
Figure 3. Error Between the C Floating-Point Result and Direct Look-Up CalculationFrom 0 to 2*π With a 0.015-Radian Step
Direct Look-Up and Linear Interpolation Method
7 Digital Sine-Wave Generation Using the TMS320C2xx
2.5.2 Quarter-Sine-Period Analysis
Figure 4, Figure 5, and Figure 6 graph another precise analysis using aquarter of a half-sine period. In this case, the step is 0.0039 radian. Theinterpolation is made at 0.125-radian intervals. In this way, interpolationerror is exposed on the following graphs. Each interval (table approximation)is displayed with 32 pixels.
Figure 4. Results of Direct Look-Up SIN From 0 to π/2 With a 0.0039-Radian Step
Direct Look-Up and Linear Interpolation Method
SPRA1798
Figure 5. Error Between the C Floating-Point Result and the Direct Look-Up SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in Q15 Format
Direct Look-Up and Linear Interpolation Method
9 Digital Sine-Wave Generation Using the TMS320C2xx
Figure 6. Error Between the C Floating-Point Result and the Direct Look-Up SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in %
Sine/Cosine With Mathematical Series
SPRA17910
3 Sine/Cosine With Mathematical SeriesThis section explains the second of two methods of implementing a digitalsine-wave generator using the TMS320C2xx.
3.1 C- and Assembly-Compatible Functions
The sine/cosine method calculates the sine/cosine functions with amathematical series for the sine period and a mathematical series forcosine. Calculations are made with fixed-point instructions to optimize thetime calculation. The dynamics used in the calculation fit with maximumprecision without overflow.
Two versions of each function are presented:
• C-compatible functions
• Assembly-compatible functions
Examples of a C program and an assembly program (that call sine andcosine functions) are in the appendix.
3.1.1 C-Compatible Functions
Fully C-compatible functions employ C conventions to use the stack forparameters passed to the functions. Parameters returned by functions arepassed by a pointer. Responsibilities of called functions are managed in apostfix expression called the function designator. Stack pointer AR1 iswell-positioned, and the return address of the hardware stack is popped(retrieved) in case of C-interrupt runtime events using C-function features(stack). The frame pointer is not modified and registers AR6 and AR7 arenot used.
3.1.2 Assembly-Compatible Functions
Assembly-compatible functions do not use their own variables, but instead,rely on variables in a software stack where arguments are passed. The AR1register points to the stack just after the last argument. After the return fromthe function, the results are placed in the stack. Registers AR0, AR6, andAR7 are not modified.
Sine/Cosine With Mathematical Series
11 Digital Sine-Wave Generation Using the TMS320C2xx
3.2 Sine/Cosine Mathematical Series
Figure 7 shows the formula for calculating the value SIN(X) of an angle X.
X in radian,If (X>=0.0)
SIN(X)=((((0.0372*X)–0.2338)*X+0.0544)*X+0.9826)*X+0.0013elseSIN(X)=((((0.0372*X)+0.2338)*X+0.0544)*X–0.9826)*X+0.0013
Figure 7. Formula for Calculating the Value SIN of an Angle
Figure 8 shows the formula for calculating the value COS(X) of an angle X.
X in radian,If (X>=0.0)
COS(X)=(((((-0.0076*X)+0.0595)*X-0.0211)*X-0.4879)*X-0.0028)*X+1.0elseCOS(X)=(((((0.0076*X)+0.0595)*X+0.0211)*X-0.4879)*X+0.0028)*X+1.0
Figure 8. Formula for Calculating the Value COS of an Angle
3.3 Functions
Functions SINE and COSINE are illustrated by example in the appendix.Conventions to interface with these functions are:
• Input: ANGLE parameters are in the stack pointed to by AR1. The valueof this angle is unsigned.
0° <-> 0000h
90° <-> 4000h
180° <-> 8000h
240° or -90° <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15format (–1 to 1–215).
3.4 System Utilization
Table 3 shows processor utilization when using the mathematical seriesmethod.
Table 3. Processor Utilization (Maximum) When Using Mathematical Series
FUNCTION CYCLES EXECUTION TIME
SIN assembly main 27 1.35 µs
COS assembly main 37 1.85 µs
SIN fully C-compatible 34 1.70 µs
COS fully C-compatible 44 2.20 µs
SIN + COS of an angle – assembly main 64 3.20 µs
Sine/Cosine With Mathematical Series
SPRA17912
Table 4 shows memory utilization when using the mathematical seriesmethod.
Table 4. Memory Utilization When Using Mathematical Series
FUNCTIONROM
(WORDS)STACKLEVELS REGISTERS USED
RAM(WORDS)
SIN 54 3 1 In stack
COS 67 3 1 In stack
SIN 64 5 1 In stack
COS 77 5 1 In stack
COS and SIN 121 3 1 In stack
3.5 Sine-Generation Precision
The following graphs present the precision of the mathematical series ofsine functions. The precision measurement is calculated by comparing themathematical series and floating-point C library results.
Sine/Cosine With Mathematical Series
13 Digital Sine-Wave Generation Using the TMS320C2xx
3.5.1 One-Sine-Period Analysis
The sine period provides a general image of error calculation. In Figure 9and Figure 10, results of the sine calculation are in Q15 format.
Figure 9. Mathematical Series Sine Calculation From 0 to 2*π With a 0.015-Radian Step
Sine/Cosine With Mathematical Series
SPRA17914
Figure 10. Error Between the C Floating-Point Result and the Mathematical Series SINE Calculation From 0 to 2*π With a 0.015-Radian Step
3.5.2 Quarter-Sine-Period Analysis
Figure 11, Figure 12, and Figure 13 graph another precise analysis using aquarter of a half-sine period. In this case, the step is 0.0039 radian.
Sine/Cosine With Mathematical Series
15 Digital Sine-Wave Generation Using the TMS320C2xx
Figure 11. Results of Mathematical Series Sine From 0 to π/2 With a 0.0039-Radian Step
Sine/Cosine With Mathematical Series
SPRA17916
Figure 12. Error Between the C Floating-Point Result and a Mathematical SeriesSINE Calculation From 0 to π/4 With a 0.0039-Radian Step in Q15 Format
Sine/Cosine With Mathematical Series
17 Digital Sine-Wave Generation Using the TMS320C2xx
Figure 13. Error Between the C Floating-Point Result and a Mathematical SeriesSINE Calculation From 0 to π/4 With a 0.0039-Radian Step in %
SPRA17918
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-1 Digital Sine-Wave Generation Using the TMS320C2xx
Appendix A Examples of a C Program and an Assembly ProgramCalling Sine and Cosine Functions
A.1 Main C Program to Call Sine or Cosine Functions********************************************************************************
*File Name: Main_trig.c *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
*Description: Very simple main which calls COS or SIN function *
* *
* *
*Processor: C2xx *
* *
*Status: *
* *
*Last Update: 19 Oct 96 *
*______________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
********************************************************************************
extern ser_cos();
void main()
{
double rad;
double a;
int c;
.
.
.
if (rad!=0.0) /*–π < rad < +π*/ c=(int)(32767*rad/3.1415927);
else
c=0;
a=ser_cos(c);
.
}
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-2
A.2 Main Assembly Program to Call Sine or Cosine Functions**********************************************************************************
*File Name: Main_trig.asm *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: Very simple main which calls COS or SIN function *
* *
*Processor: C2xx *
*Status: *
*Last Update: 19 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
**********************************************************************************
.mmregs
.def _ser_sin
.sect “vectors”
b _c_int0
b $
*********************************************************************************** Main routine
**********************************************************************************
.text
_c_int0:
.
.
LAR AR1,#60h ;stack preparation
MAR *,AR1
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-3 Digital Sine-Wave Generation Using the TMS320C2xx
LACC #4000h ;π/2
SACL *+ ;parameters in stack
CALL _ser_sin
. ;result in ACCU
.
.end
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-4
A.3 COS Function With Direct Look-Up and Linear Interpolation for Assembly Program
**********************************************************************************
*Routine Name: COS *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Direct Look–up and *
* Interpolation Assembly calling function, *
* variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* *
**********************************************************************************
COS
MAR *–
LACC #4000h
ADDS * ;add 0.5 for COS
SACL *
LT *+ ;variable in size_sin
;*******stack : angle/X
LACC #64h
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-5 Digital Sine-Wave Generation Using the TMS320C2xx
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LACC *,15
AND #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LACC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LACC *+
;*******stack : y1/TEMP/remainder
ADD *,1 ;Y(Xdata) in ACCU
RET
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-6
equal_sin
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LACC *+
end_interp_sin
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
.end
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-7 Digital Sine-Wave Generation Using the TMS320C2xx
A.4 COS Function With Direct Look-Up and Linear Interpolation forC Program
**********************************************************************************
*Routine Name: _COS *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Direct Look–up and *
* Interpolation C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_COS
ADRK #2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-8
SBRK #3 ;C compatibility
SPM 0
LACC #4000h
ADDS * ;add 0.5 for COS
SACL *
LT *+ ;variable in size_sin
;*******stack : angle/X
LACC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LACC *,15
AND #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LACC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-9 Digital Sine-Wave Generation Using the TMS320C2xx
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LACC *+
;*******stack : y1/TEMP/remainder
ADD *,1,AR1 ;Y(Xdata) in ACCU
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
equal_sin
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LACC *+,0,AR1
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
.end
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-10
A.5 SIN Function With Direct Look-Up and Linear Interpolation forAssembly Program
**********************************************************************************
*Routine Name: SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Direct Look–up and Interpolation *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
SIN
MAR *+
LT *+ ;variable in size_sin
;*******stack : angle/X
LACC #64h
SACL *
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-11 Digital Sine-Wave Generation Using the TMS320C2xx
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LACC *,15
AND #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LACC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LACC *+
;*******stack : y1/TEMP/remainder
ADD *,1 ;Y(Xdata) in ACCU
RET
equal_sin
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-12
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LACC *+
;*******stack : result/X
end_interp_sin
RET
*********************************************************************************
* Table
*********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-13 Digital Sine-Wave Generation Using the TMS320C2xx
A.6 SIN Function With Direct Look-Up and Linear Interpolation forC Program
**********************************************************************************
*Routine Name: _SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Direct Look–up + Interpolation *
* C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2x *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 16 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_SIN
ADRK #2 ;C compatibility
POPD *+
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-14
SAR AR1,*
LAR AR2,*,AR2
SBRK #3 ;C compatibility
SPM 0
LT *+ ;variable in size_sin
;*******stack : angle/X
LACC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LACC *,15
AND #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LACC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-15 Digital Sine-Wave Generation Using the TMS320C2xx
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LACC *+
;*******stack : y1/TEMP/remainder
ADD *,1,AR1 ;Y(Xdata) in ACCU
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
equal_sin
LACC #Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/...
TBLR *
LACC *+,0,AR1
;*******stack : result/X
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-16
A.7 Table for COS or SIN Function With Direct Look-Up**********************************************************************************
* TABLE Sine and Cosine for separate functions COS and SIN *
**********************************************************************************
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
.word 32767
.word 32702
.word 32509
.word 32187
.word 31738
.word 31163
.word 30466
.word 29648
.word 28714
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-17 Digital Sine-Wave Generation Using the TMS320C2xx
.word 27666
.word 26509
.word 25247
.word 23886
.word 22431
.word 20886
.word 19260
.word 17557
.word 15786
.word 13952
.word 12062
.word 10126
.word 8149
.word 6140
.word 4107
.word 2057
.word 0
.word –2057
.word –4107
.word –6140
.word –8149
.word –10126
.word –12062
.word –13952
.word –15786
.word –17557
.word –19260
.word –20886
.word –22431
.word –23886
.word –25247
.word –26509
.word –27666
.word –28714
.word –29648
.word –30466
.word –31163
.word –31738
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-18
.word –32187
.word –32509
.word –32702
.word –32767
.word –32702
.word –32509
.word –32187
.word –31738
.word –31163
.word –30466
.word –29648
.word –28714
.word –27666
.word –26509
.word –25247
.word –23886
.word –22431
.word –20886
.word –19260
.word –17557
.word –15786
.word –13952
.word –12062
.word –10126
.word –8149
.word –6140
.word –4107
.word –2057
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-19 Digital Sine-Wave Generation Using the TMS320C2xx
A.8 COS_SIN Function With Direct Look-Up and Linear Interpolation forAssembly Program
**********************************************************************************
*Routine Name: COS_SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS+SIN function with Direct Look–up and Interpolation *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output: *
* SIN(angle)*32767, Q15, in stack pointed by AR1–1 *
* COS(angle)*32767,Q15, in stack pointed by AR1 *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
size_tab .set 64h ;this is not an error
COS_SIN
MAR *+
LT *+ ;variable in size_sin
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-20
;*stack: angle/X
LACC #size_tab
SACL *
;*stack: angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*stack: angle/indice/X
SACL * ;
;*stack: angle/indice/REMAINDER
LACC *,15
AND #0ffffh,15
SACH *–
;*stack: angle/INDICE/remainder
BCND equal_sin,EQ
LACC #Table_sin_cos ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*stack: ANGLE/indice/remainder
TBLR *+
;*stack: y1/INDICE/remainder
ADD #1h
TBLR *+ ;Load Y2 with the ordinate of the
;nearest second indice
;*stack: y1/y2/REMAINDER
ADRK #1
;*stack: y1/y2/remainder/X
ADD #18h
TBLR *+
;*stack: y1/y2/remainder/yy1/X
ADD #1h
TBLR * ;*stack: y1/y2/remainder/yy1/YY2
LACC *– ;calculate cos interpolate
;*stack: y1/y2/remainder/YY1/yy2
SUB *+ ;difference between the two Y value
;*stack: y1/y2/remainder/yy1/YY2
SACL * ;*stack: y1/y2/remainder/yy1/TEMP
LT *–
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-21 Digital Sine-Wave Generation Using the TMS320C2xx
;*stack: y1/y2/remainder/YY1
SBRK #1
;*stack: y1/y2/REMAINDER/yy1
MPY *
;*stack: y1/y2/REMAINDER/yy1
ADRK #2
;*stack: y1/y2/remainder/yy1/TEMP
SPH *– ;interpolation between Y1 and Y2
;*stack: Y1/y2/remainder/YY1/temp
LACC *+
;*stack: y1/y2/remainder/yy1/TEMP
ADD *–,1 ;Y(Xdata) in ACCU
;*stack: y1/y2/remainder/YY1
SACL *–
;*stack: y1/y2/REMAINDER/cos
;calculate sin interpolate
SBRK #1
;*stack: y1/Y2/remainder/cos
LACC *–
;*stack: Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*stack: y1/Y2/remainder/cos
SACL *+
;*stack: y1/temp/REMAINDER/cos
LT *–
;*stack: y1/TEMP/remainder/cos
MPY *
;*stack: y1/TEMP/remainder/cos
SPH *– ;interpolation between Y1 and Y2
;*stack: Y1/temp/remainder/cos
LACC *+
;*stack: y1/TEMP/remainder/cos
ADD *–,1 ;Y(Xdata) in ACCU
;*stack: y1/temp/remainder/cos
SACL * ;*stack: SIN/temp/remainder/cos
ADRK #3
;*stack: sin/temp/remainder/COS
LACC *
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-22
SBRK #2
;*stack: sin/TEMP/remainder/cos
SACL *
;*stack: sin/COS
RET
equal_sin
LALK Table_sin_cos;address of beginning of the table
;*stack: angle/INDICE/remainder
ADD *– ;address of the pointed indice
;*stack: ANGLE/indice
TBLR *+
;*stack: sin/INDICE/remainder
ADD #19h
TBLR *
;*stack: sin/COS
end_interp_sin
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin_cos .include sin_cos.tab
Table_sin_cos_end
.word 0
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-23 Digital Sine-Wave Generation Using the TMS320C2xx
A.9 Table for COS_SIN Function With Direct Look-Up**********************************************************************************
* TABLE Sine + Cosine for functions COS_SIN *
**********************************************************************************
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
.word 32767
.word 32702
.word 32509
.word 32187
.word 31738
.word 31163
.word 30466
.word 29648
.word 28714
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-24
.word 27666
.word 26509
.word 25247
.word 23886
.word 22431
.word 20886
.word 19260
.word 17557
.word 15786
.word 13952
.word 12062
.word 10126
.word 8149
.word 6140
.word 4107
.word 2057
.word 0
.word –2057
.word –4107
.word –6140
.word –8149
.word –10126
.word –12062
.word –13952
.word –15786
.word –17557
.word –19260
.word –20886
.word –22431
.word –23886
.word –25247
.word –26509
.word –27666
.word –28714
.word –29648
.word –30466
.word –31163
.word –31738
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-25 Digital Sine-Wave Generation Using the TMS320C2xx
.word –32187
.word –32509
.word –32702
.word –32767
.word –32702
.word –32509
.word –32187
.word –31738
.word –31163
.word –30466
.word –29648
.word –28714
.word –27666
.word –26509
.word –25247
.word –23886
.word –22431
.word –20886
.word –19260
.word –17557
.word –15786
.word –13952
.word –12062
.word –10126
.word –8149
.word –6140
.word –4107
.word –2057
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-26
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-27 Digital Sine-Wave Generation Using the TMS320C2xx
A.10 SIN With Mathematical Series for Assembly Program**********************************************************************************
*Routine Name: sin_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Mathematical Series *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 18 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
sin_ser
SETC SXM
MAR *–
LAC *
BCND angle_neg,LT
angle_pos
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-28
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscissa scale modify degree –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #985h ;0.0372
;
;PREG : angle * 0.0372
LACC #0C426h,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #7dc5h,13 ;0.9826
APAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LACC #05532,4 ;0.0013
APAC
SACH * ,3 ;result
LACC *–
;*stack : angle/SIN
RET
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-29 Digital Sine-Wave Generation Using the TMS320C2xx
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #985h ;0.0372
;
;PREG : angle * 0.0372
LACC #03bdah,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #7dc5h,13 ;0.9826
SPAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
MPY * ;rad * temp
;temp * angle
LACC #05532,4 ;0.0013
APAC
SACH *,3 ;result
LACC *–
;*stack : angle/SIN
RET
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-30
A.11 SIN With Mathematical Series for C Program**********************************************************************************
*Routine Name: _sin_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Mathematical Series *
* C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 18 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_sin_ser
ADRK #2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK #3 ;C compatibility
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-31 Digital Sine-Wave Generation Using the TMS320C2xx
SPM 0
SETC SXM
LACC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscissa scale modify degree –> rad
;in
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #985h ;0.0372
;
;PREG : angle * 0.0372
LACC #0C426h,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #7dc5h,13 ;0.9826
APAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
;temp
MPY * ;rad * temp
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-32
;temp * angle
LACC #05532,4 ;0.0013
APAC
SACH * ,3 ;result
LACC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #985h ;0.0372
;
;PREG : angle * 0.0372
LACC #03bdah,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-33 Digital Sine-Wave Generation Using the TMS320C2xx
LACC #7dc5h,13 ;0.9826
SPAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
MPY * ;rad * temp
;temp * angle
LACC #05532,4 ;0.0013
APAC
SACH *,3 ;result
LACC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-34
A.12 COS With Mathematical Series for Assembly Program**********************************************************************************
*Routine Name: cos_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Mathematical Series *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 18 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
cos_ser
SETC SXM
MAR *–
LACC *
BCND angle_neg,LT
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-35 Digital Sine-Wave Generation Using the TMS320C2xx
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #1070h ;0.0076 neg
;
;PREG : angle * 0.0076
LACC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #0f532h,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
MPY * ;rad * temp
;temp * angle
LACC #0a440h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LACC #1000h,15 ;1
APAC
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-36
SETC OVM
SACH *,3
LACC *,16
ADD *,16
SACH *
LACC *–
;*stack : angle/SIN
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #0f90h ;0.0076 neg
;
;PREG : angle * 0.0076
LACC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #00aceh,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-37 Digital Sine-Wave Generation Using the TMS320C2xx
SACH *,2 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LACC #05bc0h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LACC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LACC *,16
ADD *,16
SACH *
LACC *–
;*stack : angle/SIN
RET
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-38
A.13 COS With Mathematical Series for C Program**********************************************************************************
*Routine Name: _cos_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Mathematical Series *
* C calling function, *
* variables in C stack. Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 18 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_cos_ser
ADRK #2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK #3 ;C compatibility
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-39 Digital Sine-Wave Generation Using the TMS320C2xx
SPM 0
SETC SXM
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #1070h ;0.0076 neg
;
;PREG : angle * 0.0076
LACC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LACC #0f532h,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
;temp
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
SPRA179A-40
MPY * ;rad * temp
;temp * angle
LACC #0a440h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LACC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LACC *,16
ADD *,16
SACH *
LACC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPY #0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPY #0f90h ;0.0076 neg
;
;PREG : angle * 0.0076
LACC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions
A-41 Digital Sine-Wave Generation Using the TMS320C2xx
;temporary value
;*stack : angle/rad/TEMP
LACC #00aceh,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LACC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
MPY * ;rad * temp
;temp * angle in Q28
LACC #05bc0h,5 ;0.0028
SACH *,2 ;result
MPY * ;
LACC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LACC *,16
ADD *,16
SACH *
LACC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK #1
RET
SPRA179A-42
A-1 Digital Sine-Wave Generation Using the TMS320C2xx
Digital Sine-Wave Generation Using the TMS320C2xx
ABSTRACTThis application report describes two methods for implementing a digital sine-wavegenerator using the TMS320C2xx: the use of a direct look-up table with linearinterpolation, and the use of mathematical series. Both methods are explained interms of C-compatible and assembly-compatible functions. Formulas used forcalculating and graphing the results are included for each method to demonstratethe differences.
B IntroductionSine-wave generators are fundamental building blocks of signal processingsystems for use in control applications. The following two methods aredescribed in this application report for implementing a digital sine-wavegenerator using the TMS320C2xx:
• Direct look-up with linear interpolation to provide sine waves with aminimum of harmonic distortion
• Realization of the sine/cosine functions with a mathematical series
The two methods respond to different constraints. The direct look-upmethod is fast with minimum errors and is significant in ROM programmingrequirements. The mathematical series method is fast and requiresminimum (ROM) programming but results in less precision.
Direct Look-Up and Linear Interpolation Method
STDZ001AA-2
C Direct Look-Up and Linear Interpolation MethodThis section describes the Direct Look- Up and Linear Interpolation methodof implementing a digital sine-wave generator using the TMS320C2xx.
C.1 C- and Assembly-Compatible Functions
The Direct Look-Up method, which uses 100 sine-period values in a table,reveals the sine/cosine functions. To provide minimum harmonic distortion,the method includes linear interpolation with a fixed-step table. An extendedtable is used for the cosine + sine function and is located in program memory.
Two versions of each function are presented :
• C-compatible functions
• Assembly-compatible functions
Examples of a C program and an assembly program (that call sine andcosine functions) are in the appendix.
C.1.1 C-Compatible Functions
Fully C-compatible functions employ C conventions to use the stack forparameters passed to the functions. Parameters returned by the functionsare passed by a pointer. Responsibilities of called functions are managedin a postfix expression called the function designator. Stack pointer AR1 iswell-positioned, and the return address of the hardware stack is popped(retrieved) in case of C-interrupt runtime events using C-function features(stack). The frame pointer is not modified and registers AR6 and AR7 arenot used.
C.1.2 Assembly-Compatible Functions
Assembly-compatible functions do not use their own variables, but instead,rely on variables in a software stack where arguments are passed. The AR1register points to the stack just after the last argument. After the return fromthe function, the results are placed in the stack. Registers AR0, AR6, andAR7 are not modified.
Direct Look-Up and Linear Interpolation Method
A-3 Digital Sine-Wave Generation Using the TMS320C2xx
C.2 Interpolation
Figure B–1 shows the formula and the resulting graph for calculating thetable interpolation value COS(X) of an angle X.
ii+ 1x - x
Y = y + i+1 i
y - y )(
X – xi
i
r = ratiox x
i i+1
y
y
i
i+1
X
Y = ?
x
y
where:{xi} = {first coordinates of the table},{yi} = {second coordinates of the table},i chosen so that xi < X < xi+1.
Figure B–1. The Formula and the Resulting Graph for Calculating the TableInterpolation Value COS of an Angle
Table interpolation is comprised of two steps:
1. Direct look-up: looking through the table to find the interval [xi, xi+1] at which the considered angle X is located, with xi < X < xi+1.
• Interpolation: solving the above mentioned calculation to obtain Y.
C.3 Functions
Functions SINE, COSINE, and SINE + COSINE are illustrated by examplein the appendix. Conventions to interface with this function are:
• Input: ANGLE parameters are in the stack pointed by AR1. The valueof this angle is unsigned.
0° <-> 0000h
90° <-> 4000h
180° <-> 8000h
240° <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15format (–1 to 1–215).
C.4 System Utilization
Table E–1 shows processor utilization when using the direct look-up andlinear interpolation methods.
Direct Look-Up and Linear Interpolation Method
STDZ001AA-4
Table E–1. Processor Utilization (Maximum) When Using Table Look-Up and Interpolation
FUNCTION CYCLES EXECUTION TIME
SIN assembly call 35 1.75 µs
COS assembly call 38 1.90 µs
SIN fully C-compatible 42 2.10 µs
COS fully C-compatible 45 2.25 µs
SIN + COS of an angle 58 2.90 µs
Table E–2 shows memory utilization when using the direct look-up andlinear interpolation method.
Table E–2. Memory Utilization When Using Direct Look-Up and Interpolation
FUNCTIONROM
(WORDS) STACKLEVELSREGISTERS
USEDRAM
(WORDS)
SIN ssembly call 32 + 100 3 1 In stack
COS assembly call 34 + 100 3 1 In stack
COS and SINassembly call 66 + 100 3 1 In stack
SIN Fully C compatible 42 + 100 5 1 In stack
COS assembly call 44 + 100 5 1 In stack
COS + SIN in one function†
assembly call55 + 125 3 1 In stack
† In the case of Sine + Cosine, the same table is used.
C.5 Sine-Generation Precision
Figure B–2 and Figure B–3 are graphs that present the precision of thedirect look-up and linear interpolation sine functions. The precisionmeasurement is made by comparing the direct look-up and the floating-pointC library results.
C.5.1 One-Sine-Period Analysis
The sine period provides a general image of error calculation. In Figure B–2and Figure B–3, results of the sine calculation are in Q15 format.
Direct Look-Up and Linear Interpolation Method
A-5 Digital Sine-Wave Generation Using the TMS320C2xx
Figure B–2. Direct Look-Up With Interpolation of Sine Calculation From 0 to 2*π With a 0.015-Radian Step
Direct Look-Up and Linear Interpolation Method
STDZ001AA-6
Figure B–3. Error Between the C Floating-Point Result and Direct Look-UpCalculation From 0 to 2*π With a 0.015-Radian Step
Direct Look-Up and Linear Interpolation Method
A-7 Digital Sine-Wave Generation Using the TMS320C2xx
C.5.2 Quarter-Sine-Period Analysis
Figure B–4, Figure B–5, and Figure B–6 graph another precise analysisusing a quarter of a half-sine period. In this case, the step is 0.0039 radian.The interpolation is made at 0.125-radian intervals. In this way, interpolationerror is exposed on the following graphs. Each interval (table approximation)is displayed with 32 pixels.
Figure B–4. Results of Direct Look-Up SIN From 0 to π/2 With a 0.0039-RadianStep
Direct Look-Up and Linear Interpolation Method
STDZ001AA-8
Figure B–5. Error Between the C Floating-Point Result and Direct Look-Up SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in Q15 Format
Direct Look-Up and Linear Interpolation Method
A-9 Digital Sine-Wave Generation Using the TMS320C2xx
Figure B–6. Error Between the C Floating-Point Result and Direct Look-Up SINE Calculation From 0 to π/4 With a 0.0039-Radian Step in %
Sine/Cosine With Mathematical Series
STDZ001AA-10
D Sine/Cosine With Mathematical SeriesThis section explains the second of two methods of implementing a digitalsine-wave generator using the TMS320C2xx.
D.1 C- and Assembly-Compatible Functions
The sine/cosine method calculates the sine/cosine functions with amathematical series for the sine period and a mathematical series forcosine. Calculations are made with fixed-point instructions to optimize thetime calculation. The dynamics used in the calculation fit with maximumprecision without overflow.
Two versions of each function are presented :
• C-compatible functions
• Assembly-compatible functions
Examples of a C program and an assembly program (that call sine andcosine functions) are in the appendix.
D.1.1 C-Compatible Functions
Fully C-compatible functions employ C conventions to use the stack forparameters passed to the functions. Parameters returned by functions arepassed by a pointer. Responsibilities of called functions are managed in apostfix expression called the function designator. Stack pointer AR1 iswell-positioned, and the return address of the hardware stack is popped(retrieved) in case of C-interrupt runtime events using C-function features(stack). The frame pointer is not modified and registers AR6 and AR7 arenot used.
D.1.2 Assembly-Compatible Functions
Assembly-compatible functions do not use their own variables, but instead,rely on variables in a software stack where arguments are passed. The AR1register points to the stack just after the last argument. After the return fromthe function, the results are placed in the stack. Registers AR0, AR6, andAR7 are not modified.
Sine/Cosine With Mathematical Series
A-11 Digital Sine-Wave Generation Using the TMS320C2xx
D.2 Sine/Cosine Mathematical SeriesFigure B–7 shows the formula for calculating the value SIN(X) of an angleX.
X in radian,If (X>=0.0)
SIN(X)=((((0.0372*X)–0.2338)*X+0.0544)*X+0.9826)*X+0.0013elseSIN(X)=((((0.0372*X)+0.2338)*X+0.0544)*X–0.9826)*X+0.0013
Figure B–7. Formula for Calculating the Value SIN of an Angle
Figure B–8 shows the formula for calculating the value COS(X) of an angleX.
X in radian,If (X>=0.0)
COS(X)=(((((-0.0076*X)+0.0595)*X-0.0211)*X-0.4879)*X-0.0028)*X+1.0elseCOS(X)=(((((0.0076*X)+0.0595)*X+0.0211)*X-0.4879)*X+0.0028)*X+1.0
Figure B–8. Formula for Calculating the Value COS of an Angle
D.3 FunctionsFunctions SINE and COSINE are illustrated by example in the appendix.Conventions to interface with these functions are:• Input: ANGLE parameters are in the stack pointed toby AR1. The value
of this angle is unsigned.0 º <-> 0000h90 º <-> 4000h180 º <-> 8000h240 º or -90º <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15format (–1 to 1–215).
D.4 System UtilizationTable E–3 shows processor utilization when using the mathematical seriesmethod.
Table E–3. Processor Utilization (Maximum) When Using Mathematical Series
FUNCTION CYCLES EXECUTION TIME
SIN assembly main 27 1.35 µs
COS assembly main 37 1.85 µs
SIN fully C-compatible 34 1.70 µs
COS fully C-compatible 44 2.20 µs
SIN + COS of an angle – assembly main 64 3.20 µs
Sine/Cosine With Mathematical Series
STDZ001AA-12
Table E–4 shows memory utilization when using the mathematical seriesmethod.
Table E–4. Memory Utilization When Using Mathematical Series
FUNCTIONROM
(WORDS)STACKLEVELS REGISTERS USED
RAM(WORDS)
SIN 54 3 1 In stack
COS 67 3 1 In stack
SIN 64 5 1 In stack
COS 77 5 1 In stack
COS and SIN 121 3 1 In stack
D.5 Sine-Generation Precision
The following graphs present the precision of the mathematical series ofsine functions. The precision measurement is calculated by comparing themathematical series and floating-point C library results.
Sine/Cosine With Mathematical Series
A-13 Digital Sine-Wave Generation Using the TMS320C2xx
D.5.1 One-Sine-Period Analysis
The sine period provides a general image of error calculation. In Figure B–9and Figure B–10, results of the sine calculation are in Q15 format.
Figure B–9. Mathematical Series Sine Calculation From 0 to 2*π With a 0.015-Radian Step
Sine/Cosine With Mathematical Series
STDZ001AA-14
Figure B–10. Error Between the C Floating-Point Result and the MathematicalSeries
SINE Calculation From 0 to 2*π With a 0.015-Radian Step
D.5.2 Quarter-Sine-Period Analysis
Figure B–11, Figure B–12, and Figure B–13 graph another precise analysisusing a quarter of a half-sine period. In this case, the step is 0.0039 radian.
Sine/Cosine With Mathematical Series
A-15 Digital Sine-Wave Generation Using the TMS320C2xx
Figure B–11. Results of Mathematical Series Sine From 0 to π/2 With a 0.0039-Radian Step
Sine/Cosine With Mathematical Series
STDZ001AA-16
Figure B–12. Error Between the C Floating-Point Result and a MathematicalSeries Sine Calculation From 0 to π/4 With a 0.0039-Radian Step in Q15 Format
Sine/Cosine With Mathematical Series
A-17 Digital Sine-Wave Generation Using the TMS320C2xx
Figure B–13. Error Between the C Floating-Point Result and a MathematicalSeries Sine Calculation From 0 to π/4 With a 0.0039-Radian Step in %
A-1
********************************************************************************
*File Name: Main_trig.c *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
*Description: Very simple main which calls COS or SIN function *
* *
* *
*Processor: C2xx *
* *
*Status: *
* *
*Last Update: 19 Oct 96 *
*______________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
********************************************************************************
extern ser_cos();
void main()
{
double rad;
double a;
int c;
.
.
.
if (rad!=0.0) /*–π < rad < +π*/
c=(int)(32767*rad/3.1415927);
else
c=0;
a=ser_cos(c);
.
}
**********************************************************************************
*File Name: Main_trig.asm *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
A-2
* *
*Description: Very simple main which calls COS or SIN function *
* *
*Processor: C2xx *
*Status: *
*Last Update: 19 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
**********************************************************************************
.mmregs
.def _ser_sin
.sect “vectors”
b _c_int0
b $
*********************************************************************************** Main routine
**********************************************************************************
.text
_c_int0:
.
.
LAR AR1,#60h ;stack preparation
MAR *,AR1
A-3
LAC #4000h ;π/2
SACL *+ ;parameters in stack
CALL _ser_sin
. ;result in ACCU
.
.end
**********************************************************************************
*Routine Name: COS *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Direct Look–up and *
* Interpolation Assembly calling function, *
* variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* *
**********************************************************************************
COS
MAR *–
LAC #4000h
ADDS * ;add 0.5 for COS
A-4
SACL *
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
A-5
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1 ;Y(Xdata) in ACCU
RET
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LAC *+
end_interp_sin
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
.end
**********************************************************************************
*Routine Name: _COS *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Direct Look–up and *
* Interpolation C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
A-6
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_COS
ADRK 2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
SPM 0
LAC #4000h
ADDS * ;add 0.5 for COS
SACL *
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
A-7
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1,AR1 ;Y(Xdata) in ACCU
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LAC *+,0,AR1
A-8
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
.end
**********************************************************************************
*Routine Name: SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Direct Look–up and Interpolation *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
A-9
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
SIN
MAR *+
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
A-10
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1 ;Y(Xdata) in ACCU
RET
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LAC *+
;*******stack : result/X
end_interp_sin
RET
*********************************************************************************
* Table
*********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
**********************************************************************************
*Routine Name: _SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Direct Look–up + Interpolation *
* C calling function, variables in C stack. *
* Fully C compatible *
A-11
* *
*Status: *
* *
*Processor: C2x *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 16 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_SIN
ADRK 2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
SPM 0
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
A-12
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1,AR1 ;Y(Xdata) in ACCU
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
A-13
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/...
TBLR *
LAC *+,0,AR1
;*******stack : result/X
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
**********************************************************************************
* TABLE Sine and Cosine for separate functions COS and SIN *
**********************************************************************************
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
A-14
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
.word 32767
.word 32702
.word 32509
.word 32187
.word 31738
.word 31163
.word 30466
.word 29648
.word 28714
.word 27666
.word 26509
.word 25247
.word 23886
.word 22431
.word 20886
.word 19260
.word 17557
.word 15786
.word 13952
.word 12062
.word 10126
.word 8149
.word 6140
.word 4107
.word 2057
.word 0
.word –2057
.word –4107
.word –6140
.word –8149
.word –10126
A-15
.word –12062
.word –13952
.word –15786
.word –17557
.word –19260
.word –20886
.word –22431
.word –23886
.word –25247
.word –26509
.word –27666
.word –28714
.word –29648
.word –30466
.word –31163
.word –31738
.word –32187
.word –32509
.word –32702
.word –32767
.word –32702
.word –32509
.word –32187
.word –31738
.word –31163
.word –30466
.word –29648
.word –28714
.word –27666
.word –26509
.word –25247
.word –23886
.word –22431
.word –20886
.word –19260
.word –17557
.word –15786
.word –13952
A-16
.word –12062
.word –10126
.word –8149
.word –6140
.word –4107
.word –2057
**********************************************************************************
*Routine Name: COS_SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS+SIN function with Direct Look–up and Interpolation *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output: *
* SIN(angle)*32767, Q15, in stack pointed by AR1–1 *
* COS(angle)*32767,Q15, in stack pointed by AR1 *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
size_tab .set 64h ;this is not an error
A-17
COS_SIN
MAR *+
LT *+ ;variable in size_sin
;*stack: angle/X
LAC #size_tab
SACL *
;*stack: angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*stack: angle/indice/X
SACL * ;
;*stack: angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*stack: angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin_cos;address of beginning of the table
ADD *– ;address of the nearest first indice
;*stack: ANGLE/indice/remainder
TBLR *+
;*stack: y1/INDICE/remainder
ADD #1h
TBLR *+ ;Load Y2 with the ordinate of the
;nearest second indice
;*stack: y1/y2/REMAINDER
ADRK 1
;*stack: y1/y2/remainder/X
ADD #18h
TBLR *+
;*stack: y1/y2/remainder/yy1/X
ADD #1h
TBLR * ;*stack: y1/y2/remainder/yy1/YY2
LAC *– ;calculate cos interpolate
;*stack: y1/y2/remainder/YY1/yy2
A-18
SUB *+ ;difference between the two Y value
;*stack: y1/y2/remainder/yy1/YY2
SACL * ;*stack: y1/y2/remainder/yy1/TEMP
LT *–
;*stack: y1/y2/remainder/YY1
SBRK 1
;*stack: y1/y2/REMAINDER/yy1
MPY *
;*stack: y1/y2/REMAINDER/yy1
ADRK 2
;*stack: y1/y2/remainder/yy1/TEMP
SPH *– ;interpolation between Y1 and Y2
;*stack: Y1/y2/remainder/YY1/temp
LAC *+
;*stack: y1/y2/remainder/yy1/TEMP
ADD *–,1 ;Y(Xdata) in ACCU
;*stack: y1/y2/remainder/YY1
SACL *–
;*stack: y1/y2/REMAINDER/cos
;calculate sin interpolate
SBRK 1
;*stack: y1/Y2/remainder/cos
LAC *–
;*stack: Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*stack: y1/Y2/remainder/cos
SACL *+
;*stack: y1/temp/REMAINDER/cos
LT *–
;*stack: y1/TEMP/remainder/cos
MPY *
;*stack: y1/TEMP/remainder/cos
SPH *– ;interpolation between Y1 and Y2
;*stack: Y1/temp/remainder/cos
LAC *+
;*stack: y1/TEMP/remainder/cos
ADD *–,1 ;Y(Xdata) in ACCU
;*stack: y1/temp/remainder/cos
A-19
SACL * ;*stack: SIN/temp/remainder/cos
ADRK 3
;*stack: sin/temp/remainder/COS
LAC *
SBRK 2
;*stack: sin/TEMP/remainder/cos
SACL *
;*stack: sin/COS
RET
equal_sin
LALK Table_sin_cos;address of beginning of the table
;*stack: angle/INDICE/remainder
ADD *– ;address of the pointed indice
;*stack: ANGLE/indice
TBLR *+
;*stack: sin/INDICE/remainder
ADDK 19h
TBLR *
;*stack: sin/COS
end_interp_sin
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin_cos .include sin_cos.tab
Table_sin_cos_end
.word 0
**********************************************************************************
* TABLE Sine + Cosine for functions COS_SIN *
**********************************************************************************
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
A-20
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
.word 32767
.word 32702
.word 32509
.word 32187
.word 31738
.word 31163
.word 30466
.word 29648
.word 28714
.word 27666
.word 26509
.word 25247
.word 23886
.word 22431
.word 20886
.word 19260
.word 17557
.word 15786
A-21
.word 13952
.word 12062
.word 10126
.word 8149
.word 6140
.word 4107
.word 2057
.word 0
.word –2057
.word –4107
.word –6140
.word –8149
.word –10126
.word –12062
.word –13952
.word –15786
.word –17557
.word –19260
.word –20886
.word –22431
.word –23886
.word –25247
.word –26509
.word –27666
.word –28714
.word –29648
.word –30466
.word –31163
.word –31738
.word –32187
.word –32509
.word –32702
.word –32767
.word –32702
.word –32509
.word –32187
.word –31738
.word –31163
A-22
.word –30466
.word –29648
.word –28714
.word –27666
.word –26509
.word –25247
.word –23886
.word –22431
.word –20886
.word –19260
.word –17557
.word –15786
.word –13952
.word –12062
.word –10126
.word –8149
.word –6140
.word –4107
.word –2057
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
A-23
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
**********************************************************************************
*Routine Name: sin_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Mathematical Series *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 18 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
sin_ser
SETC SXM
A-24
MAR *–
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscissa scale modify degree –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #0C426h,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
APAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
APAC
A-25
SACH * ,3 ;result
LAC *–
;*stack : angle/SIN
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #03bdah,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
SPAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
A-26
APAC
SACH *,3 ;result
LAC *–
;*stack : angle/SIN
RET
**********************************************************************************
*Routine Name: _sin_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Mathematical Series *
* C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 18 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_sin_ser
ADRK 2 ;C compatibility
A-27
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
SPM 0
SETC SXM
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscissa scale modify degree –> rad
;in
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #0C426h,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
A-28
APAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
APAC
SACH * ,3 ;result
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #03bdah,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
A-29
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
SPAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
APAC
SACH *,3 ;result
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
**********************************************************************************
*Routine Name: cos_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Mathematical Series *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
A-30
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 18 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
cos_ser
SETC SXM
MAR *–
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 1070h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
A-31
;*stack : angle/rad/TEMP
LAC #0f532h,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
MPY * ;rad * temp
;temp * angle
LAC #0a440h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–
;*stack : angle/SIN
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
A-32
MPYK 0f90h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #00aceh,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LAC #05bc0h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–
;*stack : angle/SIN
RET
A-33
**********************************************************************************
*Routine Name: _cos_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Mathematical Series *
* C calling function, *
* variables in C stack. Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 18 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_cos_ser
ADRK 2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
A-34
SPM 0
SETC SXM
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 1070h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #0f532h,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
;temp
MPY * ;rad * temp
A-35
;temp * angle
LAC #0a440h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 0f90h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
A-36
;*stack : angle/rad/TEMP
LAC #00aceh,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
MPY * ;rad * temp
;temp * angle in Q28
LAC #05bc0h,5 ;0.0028
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
A-37
A-1
********************************************************************************
*File Name: Main_trig.c *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
*Description: Very simple main which calls COS or SIN function *
* *
* *
*Processor: C2xx *
* *
*Status: *
* *
*Last Update: 19 Oct 96 *
*______________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
********************************************************************************
extern ser_cos();
void main()
{
double rad;
double a;
int c;
.
.
.
if (rad!=0.0) /*–π < rad < +π*/
c=(int)(32767*rad/3.1415927);
else
c=0;
a=ser_cos(c);
.
}
**********************************************************************************
*File Name: Main_trig.asm *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
A-2
* *
*Description: Very simple main which calls COS or SIN function *
* *
*Processor: C2xx *
*Status: *
*Last Update: 19 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
**********************************************************************************
.mmregs
.def _ser_sin
.sect “vectors”
b _c_int0
b $
*********************************************************************************** Main routine
**********************************************************************************
.text
_c_int0:
.
.
LAR AR1,#60h ;stack preparation
MAR *,AR1
A-3
LAC #4000h ;π/2
SACL *+ ;parameters in stack
CALL _ser_sin
. ;result in ACCU
.
.end
**********************************************************************************
*Routine Name: COS *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Direct Look–up and *
* Interpolation Assembly calling function, *
* variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* *
**********************************************************************************
COS
MAR *–
LAC #4000h
ADDS * ;add 0.5 for COS
A-4
SACL *
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
A-5
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1 ;Y(Xdata) in ACCU
RET
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LAC *+
end_interp_sin
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
.end
**********************************************************************************
*Routine Name: _COS *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Direct Look–up and *
* Interpolation C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
A-6
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_COS
ADRK 2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
SPM 0
LAC #4000h
ADDS * ;add 0.5 for COS
SACL *
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
A-7
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1,AR1 ;Y(Xdata) in ACCU
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LAC *+,0,AR1
A-8
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
.end
**********************************************************************************
*Routine Name: SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Direct Look–up and Interpolation *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
A-9
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
SIN
MAR *+
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
A-10
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1 ;Y(Xdata) in ACCU
RET
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/
TBLR *
LAC *+
;*******stack : result/X
end_interp_sin
RET
*********************************************************************************
* Table
*********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
**********************************************************************************
*Routine Name: _SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Direct Look–up + Interpolation *
* C calling function, variables in C stack. *
* Fully C compatible *
A-11
* *
*Status: *
* *
*Processor: C2x *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 16 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_SIN
ADRK 2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
SPM 0
LT *+ ;variable in size_sin
;*******stack : angle/X
LAC #64h
SACL *
;*******stack : angle/TEMP
MPYU * ;integer position in the table
A-12
PAC ;in High part of Acc
SACH *+ ;remainder
;*******stack : angle/indice/X
SACL * ;
;*******stack : angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*******stack : angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the nearest first indice
;*******stack : ANGLE/indice/remainder
TBLR *+
;*******stack : y1/INDICE/remainder
ADD #1h
TBLR * ;Load Y2 with the ordinate of the
;*******stack : y1/Y2/remainder
;nearest second indice
LAC *–
;*******stack : Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*******stack : y1/Y2/remainder
SACL *+
;*******stack : y1/temp/REMAINDER
LT *–
;*******stack : y1/TEMP/remainder
MPY *
;*******stack : y1/TEMP/remainder
SPH *– ;interpolation between Y1 and Y2
;*******stack : Y1/temp/remainder
LAC *+
;*******stack : y1/TEMP/remainder
ADD *,1,AR1 ;Y(Xdata) in ACCU
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
A-13
equal_sin
LALK Table_sin ;address of beginning of the table
ADD *– ;address of the pointed indice
;*******stack : RESULT/...
TBLR *
LAC *+,0,AR1
;*******stack : result/X
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin .include sine.tab
Table_sin_end
.word 0
**********************************************************************************
* TABLE Sine and Cosine for separate functions COS and SIN *
**********************************************************************************
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
A-14
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
.word 32767
.word 32702
.word 32509
.word 32187
.word 31738
.word 31163
.word 30466
.word 29648
.word 28714
.word 27666
.word 26509
.word 25247
.word 23886
.word 22431
.word 20886
.word 19260
.word 17557
.word 15786
.word 13952
.word 12062
.word 10126
.word 8149
.word 6140
.word 4107
.word 2057
.word 0
.word –2057
.word –4107
.word –6140
.word –8149
.word –10126
A-15
.word –12062
.word –13952
.word –15786
.word –17557
.word –19260
.word –20886
.word –22431
.word –23886
.word –25247
.word –26509
.word –27666
.word –28714
.word –29648
.word –30466
.word –31163
.word –31738
.word –32187
.word –32509
.word –32702
.word –32767
.word –32702
.word –32509
.word –32187
.word –31738
.word –31163
.word –30466
.word –29648
.word –28714
.word –27666
.word –26509
.word –25247
.word –23886
.word –22431
.word –20886
.word –19260
.word –17557
.word –15786
.word –13952
A-16
.word –12062
.word –10126
.word –8149
.word –6140
.word –4107
.word –2057
**********************************************************************************
*Routine Name: COS_SIN *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS+SIN function with Direct Look–up and Interpolation *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output: *
* SIN(angle)*32767, Q15, in stack pointed by AR1–1 *
* COS(angle)*32767,Q15, in stack pointed by AR1 *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 16 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
size_tab .set 64h ;this is not an error
A-17
COS_SIN
MAR *+
LT *+ ;variable in size_sin
;*stack: angle/X
LAC #size_tab
SACL *
;*stack: angle/TEMP
MPYU * ;integer position in the table
PAC ;in High part of Acc
SACH *+ ;remainder
;*stack: angle/indice/X
SACL * ;
;*stack: angle/indice/REMAINDER
LAC *,15
ANDK #0ffffh,15
SACH *–
;*stack: angle/INDICE/remainder
BCND equal_sin,EQ
LALK Table_sin_cos;address of beginning of the table
ADD *– ;address of the nearest first indice
;*stack: ANGLE/indice/remainder
TBLR *+
;*stack: y1/INDICE/remainder
ADD #1h
TBLR *+ ;Load Y2 with the ordinate of the
;nearest second indice
;*stack: y1/y2/REMAINDER
ADRK 1
;*stack: y1/y2/remainder/X
ADD #18h
TBLR *+
;*stack: y1/y2/remainder/yy1/X
ADD #1h
TBLR * ;*stack: y1/y2/remainder/yy1/YY2
LAC *– ;calculate cos interpolate
;*stack: y1/y2/remainder/YY1/yy2
A-18
SUB *+ ;difference between the two Y value
;*stack: y1/y2/remainder/yy1/YY2
SACL * ;*stack: y1/y2/remainder/yy1/TEMP
LT *–
;*stack: y1/y2/remainder/YY1
SBRK 1
;*stack: y1/y2/REMAINDER/yy1
MPY *
;*stack: y1/y2/REMAINDER/yy1
ADRK 2
;*stack: y1/y2/remainder/yy1/TEMP
SPH *– ;interpolation between Y1 and Y2
;*stack: Y1/y2/remainder/YY1/temp
LAC *+
;*stack: y1/y2/remainder/yy1/TEMP
ADD *–,1 ;Y(Xdata) in ACCU
;*stack: y1/y2/remainder/YY1
SACL *–
;*stack: y1/y2/REMAINDER/cos
;calculate sin interpolate
SBRK 1
;*stack: y1/Y2/remainder/cos
LAC *–
;*stack: Y1/y2/remainder
SUB *+ ;difference between the two Y value
;*stack: y1/Y2/remainder/cos
SACL *+
;*stack: y1/temp/REMAINDER/cos
LT *–
;*stack: y1/TEMP/remainder/cos
MPY *
;*stack: y1/TEMP/remainder/cos
SPH *– ;interpolation between Y1 and Y2
;*stack: Y1/temp/remainder/cos
LAC *+
;*stack: y1/TEMP/remainder/cos
ADD *–,1 ;Y(Xdata) in ACCU
;*stack: y1/temp/remainder/cos
A-19
SACL * ;*stack: SIN/temp/remainder/cos
ADRK 3
;*stack: sin/temp/remainder/COS
LAC *
SBRK 2
;*stack: sin/TEMP/remainder/cos
SACL *
;*stack: sin/COS
RET
equal_sin
LALK Table_sin_cos;address of beginning of the table
;*stack: angle/INDICE/remainder
ADD *– ;address of the pointed indice
;*stack: ANGLE/indice
TBLR *+
;*stack: sin/INDICE/remainder
ADDK 19h
TBLR *
;*stack: sin/COS
end_interp_sin
RET
**********************************************************************************
* Table
**********************************************************************************
Table_sin_cos .include sin_cos.tab
Table_sin_cos_end
.word 0
**********************************************************************************
* TABLE Sine + Cosine for functions COS_SIN *
**********************************************************************************
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
A-20
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
.word 32767
.word 32702
.word 32509
.word 32187
.word 31738
.word 31163
.word 30466
.word 29648
.word 28714
.word 27666
.word 26509
.word 25247
.word 23886
.word 22431
.word 20886
.word 19260
.word 17557
.word 15786
A-21
.word 13952
.word 12062
.word 10126
.word 8149
.word 6140
.word 4107
.word 2057
.word 0
.word –2057
.word –4107
.word –6140
.word –8149
.word –10126
.word –12062
.word –13952
.word –15786
.word –17557
.word –19260
.word –20886
.word –22431
.word –23886
.word –25247
.word –26509
.word –27666
.word –28714
.word –29648
.word –30466
.word –31163
.word –31738
.word –32187
.word –32509
.word –32702
.word –32767
.word –32702
.word –32509
.word –32187
.word –31738
.word –31163
A-22
.word –30466
.word –29648
.word –28714
.word –27666
.word –26509
.word –25247
.word –23886
.word –22431
.word –20886
.word –19260
.word –17557
.word –15786
.word –13952
.word –12062
.word –10126
.word –8149
.word –6140
.word –4107
.word –2057
.word 0
.word 2057
.word 4107
.word 6140
.word 8149
.word 10126
.word 12062
.word 13952
.word 15786
.word 17557
.word 19260
.word 20886
.word 22431
.word 23886
.word 25247
.word 26509
.word 27666
.word 28714
.word 29648
A-23
.word 30466
.word 31163
.word 31738
.word 32187
.word 32509
.word 32702
**********************************************************************************
*Routine Name: sin_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Mathematical Series *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 18 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
sin_ser
SETC SXM
A-24
MAR *–
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscissa scale modify degree –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #0C426h,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
APAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
APAC
A-25
SACH * ,3 ;result
LAC *–
;*stack : angle/SIN
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #03bdah,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
SPAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
A-26
APAC
SACH *,3 ;result
LAC *–
;*stack : angle/SIN
RET
**********************************************************************************
*Routine Name: _sin_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: SIN function with Mathematical Series *
* C calling function, variables in C stack. *
* Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : SIN(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 18 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_sin_ser
ADRK 2 ;C compatibility
A-27
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
SPM 0
SETC SXM
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscissa scale modify degree –> rad
;in
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #0C426h,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
A-28
APAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
APAC
SACH * ,3 ;result
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 985h ;0.0372
;
;PREG : angle * 0.0372
LAC #03bdah,13 ; in high ACCU
APAC ;ACCU : –0.2338 + angle * 0.0372
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #6f69h,10 ;0.0544
APAC ;ACCU: temp + 0.0544
SACH *,2 ;temp
A-29
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #7dc5h,13 ;0.9826
SPAC ;ACCU=0.9826+temp
SACH *,3 ;treg = rad
MPY * ;rad * temp
;temp * angle
LAC #05532,4 ;0.0013
APAC
SACH *,3 ;result
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
**********************************************************************************
*Routine Name: cos_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Mathematical Series *
* Assembly calling function, variables in s/w stack. *
* *
*Status: *
* *
*Processor: C2xx *
* *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
A-30
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
* *
*Last Update: 18 Oct 96 *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
cos_ser
SETC SXM
MAR *–
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 1070h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
A-31
;*stack : angle/rad/TEMP
LAC #0f532h,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
MPY * ;rad * temp
;temp * angle
LAC #0a440h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–
;*stack : angle/SIN
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
A-32
MPYK 0f90h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #00aceh,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
;temp
MPY * ;rad * temp
;temp * angle
LAC #05bc0h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–
;*stack : angle/SIN
RET
A-33
**********************************************************************************
*Routine Name: _cos_ser *
*Project: DMC Mathematical Library *
*Originator: Pascal DORSTER (Texas Instruments) *
* *
*Description: COS function with Mathematical Series *
* C calling function, *
* variables in C stack. Fully C compatible *
* *
*Status: *
* *
*Processor: C2xx *
* *
*Calling convention: *
* Input : Angle in stack value 0–360 degrees <=>0h–FFFFh *
* Output : COS(angle)*32767, Q15, in ACCU *
* Pointed register AR1 *
* *
*Stack commentary: *
* Position of current register in Caps *
* Stack at beginning : angle/X *
* *
*Last Update: 18 Oct 96 *
* *
*________________________________________________________________________________*
*Date of Mod | DESCRIPTION *
*––––––––––––––|–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*
* | *
* | *
**********************************************************************************
_cos_ser
ADRK 2 ;C compatibility
POPD *+
SAR AR1,*
LAR AR2,*,AR2
SBRK 3 ;C compatibility
A-34
SPM 0
SETC SXM
LAC *
BCND angle_neg,LT
angle_pos
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 1070h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
;*stack : angle/rad/TEMP
LAC #0f532h,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
;temp
MPY * ;rad * temp
A-35
;temp * angle
LAC #0a440h,5 ;0.0028
APAC
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
angle_neg
LT *+ ;variable in size_sin
;*stack : angle/X
MPYK 0c90h ;abscisse scale modif degre –> rad
PAC
SACH *,4 ;angle in rad
;*stack : angle/RAD
LT *+
;*stack : angle/rad/X
MPYK 0f90h ;0.0076 neg
;
;PREG : angle * 0.0076
LAC #01e76h,15 ; in high ACCU
APAC ;ACCU : +0.0595 + angle * 0.0076
SACH * ,3 ;temporary value
;angle in TREG
MPY * ;multiply by temporary value
;temporary value
A-36
;*stack : angle/rad/TEMP
LAC #00aceh,15 ;0.0211
APAC ;ACCU: temp + 0.0211
SACH * ;temp
;*stack : angle/RAD/temp
;angle in TREG
;*stack : angle/rad/TEMP
MPY * ;temp
LAC #0e0c6h,15 ;0.4879
APAC ;ACCU=0.9826+temp
SACH *,2 ;treg = rad
MPY * ;rad * temp
;temp * angle in Q28
LAC #05bc0h,5 ;0.0028
SACH *,2 ;result
MPY * ;
LAC #1000h,15 ;1
APAC
SETC OVM
SACH *,3
LAC *,16
ADD *,16
SACH *
LAC *–,0,AR1
;*stack : angle/SIN
MAR *– ;C compatibility
PSHD *–
SBRK 1
RET
A-37
Top Related