You are on page 1of 160

 





  


Application
Report

1998 Digital Signal Processing Solutions


Printed in U.S.A., July 1998 SPRA179






1998
Application
Report  

Digital Sine-Wave Generation
Using the TMS320C2xx

SPRA179
July 1998

Printed on Recycled Paper


IMPORTANT NOTICE

Texas Instruments and its subsidiaries (TI) reserve the right to make changes to their products
or to discontinue any product or service without notice, and advise customers to obtain the latest
version of relevant information to verify, before placing orders, that information being relied on
is current and complete. All products are sold subject to the terms and conditions of sale supplied
at the time of order acknowledgement, including those pertaining to warranty, patent
infringement, and limitation of liability.

TI warrants performance of its semiconductor products to the specifications applicable at the


time of sale in accordance with TI’s standard warranty. Testing and other quality control
techniques are utilized to the extent TI deems necessary to support this warranty. Specific testing
of all parameters of each device is not necessarily performed, except those mandated by
government requirements.

CERTAIN APPLICATIONS USING SEMICONDUCTOR PRODUCTS MAY INVOLVE


POTENTIAL RISKS OF DEATH, PERSONAL INJURY, OR SEVERE PROPERTY OR
ENVIRONMENTAL DAMAGE (“CRITICAL APPLICATIONS”). TI SEMICONDUCTOR
PRODUCTS ARE NOT DESIGNED, AUTHORIZED, OR WARRANTED TO BE SUITABLE FOR
USE IN LIFE-SUPPORT DEVICES OR SYSTEMS OR OTHER CRITICAL APPLICATIONS.
INCLUSION OF TI PRODUCTS IN SUCH APPLICATIONS IS UNDERSTOOD TO BE FULLY
AT THE CUSTOMER’S RISK.

In order to minimize risks associated with the customer’s applications, adequate design and
operating safeguards must be provided by the customer to minimize inherent or procedural
hazards.

TI assumes no liability for applications assistance or customer product design. TI does not
warrant or represent that any license, either express or implied, is granted under any patent right,
copyright, mask work right, or other intellectual property right of TI covering or relating to any
combination, machine, or process in which such semiconductor products or services might be
or are used. TI’s publication of information regarding any third party’s products or services does
not constitute TI’s approval, warranty or endorsement thereof.

Copyright  1998, Texas Instruments Incorporated


Contents
1 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 and
Cosine 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

Digital Sine-Wave Generation Using the TMS320C2xx iii


Figures

List of Figures
1 The Formula and the Resulting Graph for Calculating the Table Interpolation
Value 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

iv SPRA179
Tables

List of Tables
1 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

Digital Sine-Wave Generation Using the TMS320C2xx v


vi SPRA179
Digital Sine-Wave Generation Using the TMS320C2xx

ABSTRACT
This application report describes two methods for implementing a digital sine-wave
generator using the TMS320C2xx: the use of a direct look-up table with linear
interpolation, and the use of mathematical series. Both methods are explained in
terms of C-compatible and assembly-compatible functions. Formulas used for
calculating and graphing the results are included for each method to demonstrate
the differences.

1 Introduction
Sine-wave generators are fundamental building blocks of signal processing
systems for use in control applications. The following two methods
described in this application report are for implementing a digital sine-wave
generator using the TMS320C2xx:
• Direct look-up with linear interpolation to provide sine waves with a
minimum of harmonic distortion
• Realization of the sine/cosine functions with a mathematical series
The two methods respond to different constraints. The direct look-up
method is fast with minimal errors and is significant in ROM programming
requirements. The mathematical series method is fast and requires
minimum (ROM) programming but results in less precision.

Digital Sine-Wave Generation Using the TMS320C2xx 1


Direct Look-Up and Linear Interpolation Method

2 Direct Look-Up and Linear Interpolation Method


This section describes the Direct Look-Up and Linear Interpolation method
of 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 extended
table 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 and
cosine functions) are in the appendix.

2.1.1 C-Compatible Functions


Fully C-compatible functions employ C conventions to use the stack for
parameters passed to the functions. Parameters returned by the functions
are passed by a pointer. Responsibilities of called functions are managed
in a postfix expression called the function designator. Stack pointer AR1 is
well-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 are
not 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 AR1
register points to the stack just after the last argument. After the return from
the function, the results are placed in the stack. Registers AR0, AR6, and
AR7 are not modified.

2 SPRA179
Direct Look-Up and Linear Interpolation Method

2.2 Interpolation
Figure 1 shows the formula and the resulting graph for calculating the table
interpolation value COS(X) of an angle X.
y
y i+1

X –x Y=?
i ( y
Y = y + - y ) y
i x x i+ 1 i i
-
i+ 1 i

r = ratio x
x X x
i i+1

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 Table
Interpolation 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.
2. Interpolation: solving the above mentioned calculation to obtain Y.

2.3 Functions
Functions SINE, COSINE, and SINE + COSINE are illustrated by example
in the appendix. Conventions to interface with this function are:
• Input: ANGLE parameters are in the stack pointed by AR1. The value
of this angle is unsigned.
0° <-> 0000h
90° <-> 4000h
180° <-> 8000h
240° <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15
format (–1 to 1–215).

2.4 System Utilization


Table 1 shows processor utilization when using the direct look-up and linear
interpolation methods.

Digital Sine-Wave Generation Using the TMS320C2xx 3


Direct Look-Up and Linear Interpolation Method

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 linear
interpolation method.
Table 2. Memory Utilization When Using Direct Look-Up and Interpolation
ROM REGISTERS RAM
FUNCTION STACKLEVELS
(WORDS) USED (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†


55 + 125 3 1 In stack
assembly call
† 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 direct
look-up and linear interpolation sine functions. The precision measurement
is made by comparing the direct look-up and the floating-point C library
results.

2.5.1 One-Sine-Period Analysis


The sine period provides a general image of error calculation. In Figure 2
and Figure 3, results of the sine calculation are in Q15 format.

4 SPRA179
Direct Look-Up and Linear Interpolation Method

Figure 2. Direct Look-Up With Interpolation of Sine Calculation From 0 to 2*π


With a 0.015-Radian Step

Digital Sine-Wave Generation Using the TMS320C2xx 5


Direct Look-Up and Linear Interpolation Method

Figure 3. Error Between the C Floating-Point Result and Direct Look-Up Calculation
From 0 to 2*π With a 0.015-Radian Step

6 SPRA179
Direct Look-Up and Linear Interpolation Method

2.5.2 Quarter-Sine-Period Analysis


Figure 4, Figure 5, and Figure 6 graph another precise analysis using 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, interpolation
error 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

Digital Sine-Wave Generation Using the TMS320C2xx 7


Direct Look-Up and Linear Interpolation Method

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

8 SPRA179
Direct Look-Up and Linear Interpolation Method

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 %

Digital Sine-Wave Generation Using the TMS320C2xx 9


Sine/Cosine With Mathematical Series

3 Sine/Cosine With Mathematical Series


This section explains the second of two methods of implementing a digital
sine-wave generator using the TMS320C2xx.

3.1 C- and Assembly-Compatible Functions


The sine/cosine method calculates the sine/cosine functions with a
mathematical series for the sine period and a mathematical series for
cosine. Calculations are made with fixed-point instructions to optimize the
time calculation. The dynamics used in the calculation fit with maximum
precision 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 and
cosine functions) are in the appendix.

3.1.1 C-Compatible Functions


Fully C-compatible functions employ C conventions to use the stack for
parameters passed to the functions. Parameters returned by functions are
passed by a pointer. Responsibilities of called functions are managed in a
postfix expression called the function designator. Stack pointer AR1 is
well-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 are
not 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 AR1
register points to the stack just after the last argument. After the return from
the function, the results are placed in the stack. Registers AR0, AR6, and
AR7 are not modified.

10 SPRA179
Sine/Cosine With Mathematical Series

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.0013
else
SIN(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.0
else
COS(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 value
of this angle is unsigned.
0° <-> 0000h
90° <-> 4000h
180° <-> 8000h
240° or -90° <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15
format (–1 to 1–215).

3.4 System Utilization


Table 3 shows processor utilization when using the mathematical series
method.
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

Digital Sine-Wave Generation Using the TMS320C2xx 11


Sine/Cosine With Mathematical Series

Table 4 shows memory utilization when using the mathematical series


method.
Table 4. Memory Utilization When Using Mathematical Series
ROM STACK RAM
FUNCTION REGISTERS USED
(WORDS) LEVELS (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 of
sine functions. The precision measurement is calculated by comparing the
mathematical series and floating-point C library results.

12 SPRA179
Sine/Cosine With Mathematical Series

3.5.1 One-Sine-Period Analysis


The sine period provides a general image of error calculation. In Figure 9
and 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

Digital Sine-Wave Generation Using the TMS320C2xx 13


Sine/Cosine With Mathematical Series

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 a
quarter of a half-sine period. In this case, the step is 0.0039 radian.

14 SPRA179
Sine/Cosine With Mathematical Series

Figure 11. Results of Mathematical Series Sine From 0 to π/2


With a 0.0039-Radian Step

Digital Sine-Wave Generation Using the TMS320C2xx 15


Sine/Cosine With Mathematical Series

Figure 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 SPRA179
Sine/Cosine With Mathematical Series

Figure 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 %

Digital Sine-Wave Generation Using the TMS320C2xx 17


18 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

Appendix A Examples of a C Program and an Assembly Program


Calling 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);
.
}

Digital Sine-Wave Generation Using the TMS320C2xx A-1


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-2 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

LACC #4000h ;π/2


SACL *+ ;parameters in stack
CALL _ser_sin
. ;result in ACCU
.
.end

Digital Sine-Wave Generation Using the TMS320C2xx A-3


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-4 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-5


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-6 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

A.4 COS Function With Direct Look-Up and Linear Interpolation for
C 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

Digital Sine-Wave Generation Using the TMS320C2xx A-7


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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 *–

A-8 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

;*******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

Digital Sine-Wave Generation Using the TMS320C2xx A-9


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

A.5 SIN Function With Direct Look-Up and Linear Interpolation for
Assembly 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 *

A-10 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

;*******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

Digital Sine-Wave Generation Using the TMS320C2xx A-11


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-12 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

A.6 SIN Function With Direct Look-Up and Linear Interpolation for
C 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 *+

Digital Sine-Wave Generation Using the TMS320C2xx A-13


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-14 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-15


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-16 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

.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

Digital Sine-Wave Generation Using the TMS320C2xx A-17


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

.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

A-18 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

A.8 COS_SIN Function With Direct Look-Up and Linear Interpolation for
Assembly 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

Digital Sine-Wave Generation Using the TMS320C2xx A-19


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

;*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 *–

A-20 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

;*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 *

Digital Sine-Wave Generation Using the TMS320C2xx A-21


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-22 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-23


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

.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

A-24 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

.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

Digital Sine-Wave Generation Using the TMS320C2xx A-25


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

.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

A-26 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-27


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-28 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-29


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-30 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-31


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

;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

A-32 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-33


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-34 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-35


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-36 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-37


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-38 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

Digital Sine-Wave Generation Using the TMS320C2xx A-39


Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

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

A-40 SPRA179
Examples of a C Program and an Assembly Program Calling Sine and Cosine Functions

;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

Digital Sine-Wave Generation Using the TMS320C2xx A-41


A-42 SPRA179
Digital Sine-Wave Generation Using the TMS320C2xx

ABSTRACT
This application report describes two methods for implementing a digital sine-wave
generator using the TMS320C2xx: the use of a direct look-up table with linear
interpolation, and the use of mathematical series. Both methods are explained in
terms of C-compatible and assembly-compatible functions. Formulas used for
calculating and graphing the results are included for each method to demonstrate
the differences.

B Introduction
Sine-wave generators are fundamental building blocks of signal processing
systems for use in control applications. The following two methods are
described in this application report for implementing a digital sine-wave
generator using the TMS320C2xx:
• Direct look-up with linear interpolation to provide sine waves with a
minimum of harmonic distortion
• Realization of the sine/cosine functions with a mathematical series
The two methods respond to different constraints. The direct look-up
method is fast with minimum errors and is significant in ROM programming
requirements. The mathematical series method is fast and requires
minimum (ROM) programming but results in less precision.

Digital Sine-Wave Generation Using the TMS320C2xx A-1


Direct Look-Up and Linear Interpolation Method

C Direct Look-Up and Linear Interpolation Method


This section describes the Direct Look- Up and Linear Interpolation method
of 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 extended
table 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 and
cosine functions) are in the appendix.

C.1.1 C-Compatible Functions


Fully C-compatible functions employ C conventions to use the stack for
parameters passed to the functions. Parameters returned by the functions
are passed by a pointer. Responsibilities of called functions are managed
in a postfix expression called the function designator. Stack pointer AR1 is
well-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 are
not 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 AR1
register points to the stack just after the last argument. After the return from
the function, the results are placed in the stack. Registers AR0, AR6, and
AR7 are not modified.

A-2 STDZ001A
Direct Look-Up and Linear Interpolation Method

C.2 Interpolation
Figure B–1 shows the formula and the resulting graph for calculating the
table interpolation value COS(X) of an angle X.
y
y i+1

X –x Y=?
i ( y
Y = y + - y ) y
i x x i+ 1 i i
-
i+ 1 i

r = ratio x
x X x
i i+1

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 Table
Interpolation 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 example
in the appendix. Conventions to interface with this function are:
• Input: ANGLE parameters are in the stack pointed by AR1. The value
of this angle is unsigned.
0° <-> 0000h
90° <-> 4000h
180° <-> 8000h
240° <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15
format (–1 to 1–215).

C.4 System Utilization


Table E–1 shows processor utilization when using the direct look-up and
linear interpolation methods.

Digital Sine-Wave Generation Using the TMS320C2xx A-3


Direct Look-Up and Linear Interpolation Method

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 and
linear interpolation method.
Table E–2. Memory Utilization When Using Direct Look-Up and Interpolation
ROM REGISTERS RAM
FUNCTION STACKLEVELS
(WORDS) USED (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†


55 + 125 3 1 In stack
assembly call
† 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 the
direct look-up and linear interpolation sine functions. The precision
measurement is made by comparing the direct look-up and the floating-point
C library results.

C.5.1 One-Sine-Period Analysis


The sine period provides a general image of error calculation. In Figure B–2
and Figure B–3, results of the sine calculation are in Q15 format.

A-4 STDZ001A
Direct Look-Up and Linear Interpolation Method

Figure B–2. Direct Look-Up With Interpolation of Sine Calculation From 0 to 2*π
With a 0.015-Radian Step

Digital Sine-Wave Generation Using the TMS320C2xx A-5


Direct Look-Up and Linear Interpolation Method

Figure B–3. Error Between the C Floating-Point Result and Direct Look-Up
Calculation From 0 to 2*π With a 0.015-Radian Step

A-6 STDZ001A
Direct Look-Up and Linear Interpolation Method

C.5.2 Quarter-Sine-Period Analysis


Figure B–4, Figure B–5, and Figure B–6 graph another precise analysis
using 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, interpolation
error 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-Radian
Step

Digital Sine-Wave Generation Using the TMS320C2xx A-7


Direct Look-Up and Linear Interpolation Method

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

A-8 STDZ001A
Direct Look-Up and Linear Interpolation Method

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 %

Digital Sine-Wave Generation Using the TMS320C2xx A-9


Sine/Cosine With Mathematical Series

D Sine/Cosine With Mathematical Series


This section explains the second of two methods of implementing a digital
sine-wave generator using the TMS320C2xx.

D.1 C- and Assembly-Compatible Functions


The sine/cosine method calculates the sine/cosine functions with a
mathematical series for the sine period and a mathematical series for
cosine. Calculations are made with fixed-point instructions to optimize the
time calculation. The dynamics used in the calculation fit with maximum
precision 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 and
cosine functions) are in the appendix.

D.1.1 C-Compatible Functions


Fully C-compatible functions employ C conventions to use the stack for
parameters passed to the functions. Parameters returned by functions are
passed by a pointer. Responsibilities of called functions are managed in a
postfix expression called the function designator. Stack pointer AR1 is
well-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 are
not 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 AR1
register points to the stack just after the last argument. After the return from
the function, the results are placed in the stack. Registers AR0, AR6, and
AR7 are not modified.

A-10 STDZ001A
Sine/Cosine With Mathematical Series

D.2 Sine/Cosine Mathematical Series


Figure B–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.0013
else
SIN(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 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.0
else
COS(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 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 toby AR1. The value
of this angle is unsigned.
0º <-> 0000h
90 º <-> 4000h
180 º <-> 8000h
240 º or -90º <-> C000h
• Output: COS(angle) or SIN(angle) are in ACCUMULATOR in Q15
format (–1 to 1–215).

D.4 System Utilization


Table E–3 shows processor utilization when using the mathematical series
method.
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

Digital Sine-Wave Generation Using the TMS320C2xx A-11


Sine/Cosine With Mathematical Series

Table E–4 shows memory utilization when using the mathematical series
method.
Table E–4. Memory Utilization When Using Mathematical Series
ROM STACK RAM
FUNCTION REGISTERS USED
(WORDS) LEVELS (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 of
sine functions. The precision measurement is calculated by comparing the
mathematical series and floating-point C library results.

A-12 STDZ001A
Sine/Cosine With Mathematical Series

D.5.1 One-Sine-Period Analysis


The sine period provides a general image of error calculation. In Figure B–9
and 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

Digital Sine-Wave Generation Using the TMS320C2xx A-13


Sine/Cosine With Mathematical Series

Figure B–10. Error Between the C Floating-Point Result and the Mathematical
Series
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 analysis
using a quarter of a half-sine period. In this case, the step is 0.0039 radian.

A-14 STDZ001A
Sine/Cosine With Mathematical Series

Figure B–11. Results of Mathematical Series Sine From 0 to π/2


With a 0.0039-Radian Step

Digital Sine-Wave Generation Using the TMS320C2xx A-15


Sine/Cosine With Mathematical Series

Figure B–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

A-16 STDZ001A
Sine/Cosine With Mathematical Series

Figure B–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 %

Digital Sine-Wave Generation Using the TMS320C2xx A-17


********************************************************************************
*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-1
* *
*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-2
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-3
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-4
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-5
* *
*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-6
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-7
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-8
*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-9
;*******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-10
* *
*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-11
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-12
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-13
.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-14
.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-15
.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-16
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-17
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-18
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-19
.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-20
.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-21
.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-22
.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-23
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-24
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-25
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-26
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-27
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-28
;*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-29
* *
*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-30
;*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-31
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-32
**********************************************************************************
*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-33
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-34
;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-35
;*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-36
A-37
********************************************************************************
*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-1
* *
*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-2
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-3
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-4
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-5
* *
*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-6
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-7
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-8
*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-9
;*******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-10
* *
*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-11
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-12
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-13
.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-14
.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-15
.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-16
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-17
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-18
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-19
.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-20
.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-21
.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-22
.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-23
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-24
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-25
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-26
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-27
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-28
;*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-29
* *
*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-30
;*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-31
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-32
**********************************************************************************
*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-33
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-34
;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-35
;*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-36
A-37

You might also like