 /****************************************************************************/  A    1 :V17
// Copyright 2004 MBARI.                                                      A    2 :
// Monterey Bay Aquarium Research Institute Proprietary Information.          A    3 :
// All rights reserved.                                                       A    4 :
/****************************************************************************/  A    5 :
#if defined(__PCM__)  A    6 :
#include <16f873.h> A    7 :
 //////// Standard Header file for the PIC16F873 device ////////////////  B    1 :I1
#device PIC16F873  B    2 :
#nolist  B    3N:X0
//////// Program memory: 4096x14  Data RAM: 190  Stack: 8  B    4N:
//////// I/O: 22   Analog Pins: 5  B    5N:
//////// Data EEPROM: 128  B    6N:
//////// C Scratch area: 20   ID Location: 2000  B    7N:
//////// Fuses: LP,XT,HS,RC,NOWDT,WDT,NOPUT,PUT,PROTECT,PROTECT_5%  B    8N:
//////// Fuses: PROTECT_50%,NOPROTECT,BROWNOUT,NOBROWNOUT,LVP,NOLVP,CPD  B    9N:
//////// Fuses: NOCPD,WRT,NOWRT,DEBUG,NODEBUG  B   10N:
////////   B   11N:
////////////////////////////////////////////////////////////////// I/O  B   12N:
// Discrete I/O Functions: SET_TRIS_x(), OUTPUT_x(), INPUT_x(),  B   13N:
//                         PORT_B_PULLUPS(), INPUT(),  B   14N:
//                         OUTPUT_LOW(), OUTPUT_HIGH(),  B   15N:
//                         OUTPUT_FLOAT(), OUTPUT_BIT()  B   16N:
// Constants used to identify pins in the above are:  B   17N:
  B   18N:
#define PIN_A0  40  B   19N:
#define PIN_A1  41  B   20N:
#define PIN_A2  42  B   21N:
#define PIN_A3  43  B   22N:
#define PIN_A4  44  B   23N:
#define PIN_A5  45  B   24N:
  B   25N:
#define PIN_B0  48  B   26N:
#define PIN_B1  49  B   27N:
#define PIN_B2  50  B   28N:
#define PIN_B3  51  B   29N:
#define PIN_B4  52  B   30N:
#define PIN_B5  53  B   31N:
#define PIN_B6  54  B   32N:
#define PIN_B7  55  B   33N:
  B   34N:
#define PIN_C0  56  B   35N:
#define PIN_C1  57  B   36N:
#define PIN_C2  58  B   37N:
#define PIN_C3  59  B   38N:
#define PIN_C4  60  B   39N:
#define PIN_C5  61  B   40N:
#define PIN_C6  62  B   41N:
#define PIN_C7  63  B   42N:
  B   43N:
////////////////////////////////////////////////////////////////// Useful defines  B   44N:
#define FALSE 0  B   45N:
#define TRUE 1  B   46N:
  B   47N:
#define BYTE int  B   48N:
#define BOOLEAN short int  B   49N:
  B   50N:
#define getc getch  B   51N:
#define fgetc getch  B   52N:
#define getchar getch  B   53N:
#define putc putchar  B   54N:
#define fputc putchar  B   55N:
#define fgets gets  B   56N:
#define fputs puts  B   57N:
  B   58N:
////////////////////////////////////////////////////////////////// Control  B   59N:
// Control Functions:  RESET_CPU(), SLEEP(), RESTART_CAUSE()  B   60N:
// Constants returned from RESTART_CAUSE() are:  B   61N:
#define WDT_FROM_SLEEP  0       B   62N:
#define WDT_TIMEOUT     8       B   63N:
#define MCLR_FROM_SLEEP 16      B   64N:
#define NORMAL_POWER_UP 24      B   65N:
  B   66N:
  B   67N:
////////////////////////////////////////////////////////////////// Timer 0  B   68N:
// Timer 0 (AKA RTCC)Functions: SETUP_COUNTERS() or SETUP_TIMER0(),  B   69N:
//                              SET_TIMER0() or SET_RTCC(),  B   70N:
//                              GET_TIMER0() or GET_RTCC()  B   71N:
// Constants used for SETUP_TIMER0() are:  B   72N:
#define RTCC_INTERNAL   0  B   73N:
#define RTCC_EXT_L_TO_H 32  B   74N:
#define RTCC_EXT_H_TO_L 48  B   75N:
  B   76N:
#define RTCC_DIV_1      8  B   77N:
#define RTCC_DIV_2      0  B   78N:
#define RTCC_DIV_4      1  B   79N:
#define RTCC_DIV_8      2  B   80N:
#define RTCC_DIV_16     3  B   81N:
#define RTCC_DIV_32     4  B   82N:
#define RTCC_DIV_64     5  B   83N:
#define RTCC_DIV_128    6  B   84N:
#define RTCC_DIV_256    7  B   85N:
  B   86N:
  B   87N:
#define RTCC_8_BIT      0       B   88N:
  B   89N:
// Constants used for SETUP_COUNTERS() are the above  B   90N:
// constants for the 1st param and the following for  B   91N:
// the 2nd param:  B   92N:
  B   93N:
////////////////////////////////////////////////////////////////// WDT  B   94N:
// Watch Dog Timer Functions: SETUP_WDT() or SETUP_COUNTERS() (see above)  B   95N:
//                            RESTART_WDT()  B   96N:
//  B   97N:
#define WDT_18MS        8     B   98N:
#define WDT_36MS        9     B   99N:
#define WDT_72MS       10     B  100N:
#define WDT_144MS      11     B  101N:
#define WDT_288MS      12     B  102N:
#define WDT_576MS      13     B  103N:
#define WDT_1152MS     14     B  104N:
#define WDT_2304MS     15     B  105N:
  B  106N:
////////////////////////////////////////////////////////////////// Timer 1  B  107N:
// Timer 1 Functions: SETUP_TIMER_1, GET_TIMER1, SET_TIMER1  B  108N:
// Constants used for SETUP_TIMER_1() are:  B  109N:
//      (or (via |) together constants from each group)  B  110N:
#define T1_DISABLED         0  B  111N:
#define T1_INTERNAL         0x85  B  112N:
#define T1_EXTERNAL         0x87  B  113N:
#define T1_EXTERNAL_SYNC    0x83  B  114N:
  B  115N:
#define T1_CLK_OUT          8  B  116N:
  B  117N:
#define T1_DIV_BY_1         0  B  118N:
#define T1_DIV_BY_2         0x10  B  119N:
#define T1_DIV_BY_4         0x20  B  120N:
#define T1_DIV_BY_8         0x30  B  121N:
  B  122N:
////////////////////////////////////////////////////////////////// Timer 2  B  123N:
// Timer 2 Functions: SETUP_TIMER_2, GET_TIMER2, SET_TIMER2  B  124N:
// Constants used for SETUP_TIMER_2() are:  B  125N:
#define T2_DISABLED         0  B  126N:
#define T2_DIV_BY_1         4  B  127N:
#define T2_DIV_BY_4         5  B  128N:
#define T2_DIV_BY_16        6  B  129N:
  B  130N:
////////////////////////////////////////////////////////////////// CCP  B  131N:
// CCP Functions: SETUP_CCPx, SET_PWMx_DUTY  B  132N:
// CCP Variables: CCP_x, CCP_x_LOW, CCP_x_HIGH  B  133N:
// Constants used for SETUP_CCPx() are:  B  134N:
#define CCP_OFF                         0  B  135N:
#define CCP_CAPTURE_FE                  4  B  136N:
#define CCP_CAPTURE_RE                  5  B  137N:
#define CCP_CAPTURE_DIV_4               6  B  138N:
#define CCP_CAPTURE_DIV_16              7  B  139N:
#define CCP_COMPARE_SET_ON_MATCH        8  B  140N:
#define CCP_COMPARE_CLR_ON_MATCH        9  B  141N:
#define CCP_COMPARE_INT                 0xA  B  142N:
#define CCP_COMPARE_RESET_TIMER         0xB  B  143N:
#define CCP_PWM                         0xC  B  144N:
#define CCP_PWM_PLUS_1                  0x1c  B  145N:
#define CCP_PWM_PLUS_2                  0x2c  B  146N:
#define CCP_PWM_PLUS_3                  0x3c  B  147N:
long CCP_1;  B  148N:V18
#byte   CCP_1    =                      0x15          B  149N:
#byte   CCP_1_LOW=                      0x15          B  150N:
#byte   CCP_1_HIGH=                     0x16          B  151N:V19
long CCP_2;  B  152N:V20V21
#byte   CCP_2    =                      0x1B          B  153N:
#byte   CCP_2_LOW=                      0x1B          B  154N:
#byte   CCP_2_HIGH=                     0x1C          B  155N:V22
////////////////////////////////////////////////////////////////// SPI  B  156N:
// SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN  B  157N:
// Constants used in SETUP_SSP() are:  B  158N:
#define SPI_MASTER       0x20  B  159N:
#define SPI_SLAVE        0x24  B  160N:
#define SPI_L_TO_H       0  B  161N:
#define SPI_H_TO_L       0x10  B  162N:
#define SPI_CLK_DIV_4    0  B  163N:
#define SPI_CLK_DIV_16   1  B  164N:
#define SPI_CLK_DIV_64   2  B  165N:
#define SPI_CLK_T2       3  B  166N:
#define SPI_SS_DISABLED  1  B  167N:
  B  168N:
#define SPI_SAMPLE_AT_END 0x8000  B  169N:
#define SPI_XMIT_L_TO_H  0x4000  B  170N:
  B  171N:
////////////////////////////////////////////////////////////////// UART  B  172N:
// Constants used in setup_uart() are:  B  173N:
// FALSE - Turn UART off  B  174N:
// TRUE  - Turn UART on  B  175N:
#define UART_ADDRESS           2  B  176N:
#define UART_DATA              4  B  177N:
  B  178N:
////////////////////////////////////////////////////////////////// ADC  B  179N:
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),  B  180N:
//                SET_ADC_CHANNEL(), READ_ADC()  B  181N:
// Constants used for SETUP_ADC() are:  B  182N:
#define ADC_OFF                0          // ADC Off  B  183N:
#define ADC_CLOCK_DIV_2    0x100  B  184N:
#define ADC_CLOCK_DIV_8     0x40  B  185N:
#define ADC_CLOCK_DIV_32    0x80  B  186N:
#define ADC_CLOCK_INTERNAL  0xc0          // Internal 2-6us  B  187N:
  B  188N:
// Constants used in SETUP_ADC_PORTS() are:  B  189N:
#define NO_ANALOGS                           7    // None  B  190N:
#define ALL_ANALOG                           0    // A0 A1 A2 A3 A5           B  191N:
#define AN0_AN1_AN2_AN4_VSS_VREF             1    // A0 A1 A2 A5 VRefh=A3                B  192N:
#define AN0_AN1_AN3                          4    // A0 A1 A3  B  193N:
#define AN0_AN1_VSS_VREF                     5    // A0 A1 VRefh=A3  B  194N:
#define AN0_AN1_AN4_VREF_VREF             0x08    // A0 A1 A5 VRefh=A3 VRefl=A2                B  195N:
#define AN0_AN1_VREF_VREF                 0x0D    // A0 A1 VRefh=A3 VRefl=A2  B  196N:
#define AN0                               0x0E    // A0  B  197N:
#define AN0_VREF_VREF                     0x0F    // A0 VRefh=A3 VRefl=A2  B  198N:
#define ANALOG_RA3_REF         0x1         //!old only provided for compatibility  B  199N:
#define RA0_RA1_RA3_ANALOG     0x4         //!old only provided for compatibility  B  200N:
#define RA0_RA1_ANALOG_RA3_REF 0x5         //!old only provided for compatibility  B  201N:
#define ANALOG_RA3_RA2_REF              0x8   //!old only provided for compatibility  B  202N:
#define RA0_RA1_ANALOG_RA3_RA2_REF      0xD   //!old only provided for compatibility  B  203N:
#define RA0_ANALOG                      0xE   //!old only provided for compatibility  B  204N:
#define RA0_ANALOG_RA3_RA2_REF          0xF   //!old only provided for compatibility  B  205N:
  B  206N:
  B  207N:
// Constants used in READ_ADC() are:  B  208N:
#define ADC_START_AND_READ     7   // This is the default if nothing is specified  B  209N:
#define ADC_START_ONLY         1  B  210N:
#define ADC_READ_ONLY          6  B  211N:
  B  212N:
  B  213N:
  B  214N:
////////////////////////////////////////////////////////////////// INT  B  215N:
// Interrupt Functions: ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),  B  216N:
//                      EXT_INT_EDGE()  B  217N:
//  B  218N:
// Constants used in EXT_INT_EDGE() are:  B  219N:
#define L_TO_H              0x40  B  220N:
#define H_TO_L                 0  B  221N:
// Constants used in ENABLE/DISABLE_INTERRUPTS() are:  B  222N:
#define GLOBAL                    0x0BC0  B  223N:
#define INT_RTCC                  0x0B20  B  224N:
#define INT_RB                    0x0B08  B  225N:
#define INT_EXT                   0x0B10  B  226N:
#define INT_AD                    0x8C40  B  227N:
#define INT_TBE                   0x8C10  B  228N:
#define INT_RDA                   0x8C20  B  229N:
#define INT_TIMER1                0x8C01  B  230N:
#define INT_TIMER2                0x8C02  B  231N:
#define INT_CCP1                  0x8C04  B  232N:
#define INT_CCP2                  0x8D01  B  233N:
#define INT_SSP                   0x8C08  B  234N:
#define INT_BUSCOL                0x8D08  B  235N:
#define INT_EEPROM                0x8D10  B  236N:
#define INT_TIMER0                0x0B20  B  237N:
  B  238N:
#list  B  239 :X1E
 B  239 :
#fuses XT,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP  A    8 :
#use delay(clock=3580000)  A    9 :V24V26
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)  A   10 :V28
#use fast_io(C)  A   11 :
#endif  A   12 :
  A   13 :
#include <stdlib.h> A   14 :
 ///////////////////////////////////////////////////////////////////////////  C    1 :I2
////        (C) Copyright 1996,2003 Custom Computer Services           ////  C    2 :
//// This source code may only be used by licensed users of the CCS C  ////  C    3 :
//// compiler.  This source code may only be distributed to other      ////  C    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction ////  C    5 :
//// or distribution is permitted without written permission.          ////  C    6 :
//// Derivative programs created using this software in object code    ////  C    7 :
//// form are not restricted in any way.                               ////  C    8 :
///////////////////////////////////////////////////////////////////////////  C    9 :
  C   10 :
#ifndef _STDLIB  C   11 :
#define _STDLIB  C   12 :
  C   13 :
//---------------------------------------------------------------------------  C   14 :
// Definitions and types  C   15 :
//---------------------------------------------------------------------------  C   16 :
  C   17 :
#ifndef RAND_MAX  C   18 :
#define RAND_MAX  32767    // The value of which is the maximum value  C   19 :
                           // ... returned by the rand function  C   20 :
#endif  C   21 :
  C   22 :
typedef struct {  C   23 :V32
   signed int quot;  C   24 :T33
   signed int rem;  C   25 :
} div_t;  C   26 :
  C   27 :
typedef struct {  C   28 :
   signed long quot;  C   29 :T37
   signed long rem;  C   30 :
} ldiv_t;  C   31 :
  C   32 :
#include <stddef.h> C   33 :
 ///////////////////////////////////////////////////////////////////////////  D    1 :I3
////        (C) Copyright 1996,2003 Custom Computer Services           ////  D    2 :
//// This source code may only be used by licensed users of the CCS C  ////  D    3 :
//// compiler.  This source code may only be distributed to other      ////  D    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction ////  D    5 :
//// or distribution is permitted without written permission.          ////  D    6 :
//// Derivative programs created using this software in object code    ////  D    7 :
//// form are not restricted in any way.                               ////  D    8 :
///////////////////////////////////////////////////////////////////////////  D    9 :
  D   10 :
#ifndef _STDDEF  D   11 :
  D   12 :
#define _STDDEF  D   13 :
  D   14 :
#if sizeof(int *)==1  D   15 :
#define ptrdiff_t int  D   16 :
#else  D   17 :
#define ptrdiff_t long  D   18 :
#endif  D   19 :
  D   20 :
#define size_t int  D   21 :
#define wchar_t char  D   22 :
#define NULL 0  D   23 :
  D   24 :
#define offsetof(s,f) (offsetofbit(s,f)/8)  D   25 :
  D   26 :
#endif  D   27 :E
 D   27 :
  C   34 :
//---------------------------------------------------------------------------  C   35 :
// String conversion functions  C   36 :
//---------------------------------------------------------------------------  C   37 :
  C   38 :
/* Standard template: float atof(char * s)  C   39 :
 * converts the initial portion of the string s to a float.  C   40 :
 * returns the converted value if any, 0 otherwise  C   41 :
 */  C   42 :
float atof(char * s);  C   43 :f266V42P266
  C   44 :
/* Standard template: signed int  atoi(char * s)  C   45 :
 * converts the initial portion of the string s to a signed int  C   46 :
 * returns the converted value if any, 0 otherwise  C   47 :
 */  C   48 :
signed int atoi(char *s);  C   49 :f268V44P268
  C   50 :
/* Syntax: signed int32  atoi32(char * s)  C   51 :
   converts the initial portion of the string s to a signed int32  C   52 :
   returns the converted value if any, 0 otherwise*/  C   53 :
signed int32 atoi32(char *s);  C   54 :f269V46P269
  C   55 :
/* Standard template: signed long  atol(char * s)  C   56 :
 * converts the initial portion of the string s to a signed long  C   57 :
 * returns the converted value if any, 0 otherwise  C   58 :
 */  C   59 :
signed long atol(char *s);  C   60 :f270V48P270
  C   61 :
/* Standard template: float strtol(char * s,char *endptr)  C   62 :
 * converts the initial portion of the string s to a float  C   63 :
 * returns the converted value if any, 0 otherwise  C   64 :
 * the final string is returned in the endptr, if endptr is not null  C   65 :
 */  C   66 :
float strtod(char *s,char *endptr);  C   67 :f271V50V51P271
  C   68 :
/* Standard template: long strtoul(char * s,char *endptr,signed int base)  C   69 :
 * converts the initial portion of the string s, represented as an  C   70 :
 * integral value of radix base  to a signed long.  C   71 :
 * Returns the converted value if any, 0 otherwise  C   72 :
 * the final string is returned in the endptr, if endptr is not null  C   73 :
 */  C   74 :
signed long strtol(char *s,char *endptr,signed int base);  C   75 :f273V53V54V55P273
  C   76 :
/* Standard template: long strtoul(char * s,char *endptr,signed int base)  C   77 :
 * converts the initial portion of the string s, represented as an  C   78 :
 * integral value of radix base to a unsigned long.  C   79 :
 * returns the converted value if any, 0 otherwise  C   80 :
 * the final string is returned in the endptr, if endptr is not null  C   81 :
 */  C   82 :
long strtoul(char *s,char *endptr,signed int base);  C   83 :f275V57V58V59P275
  C   84 :
//---------------------------------------------------------------------------  C   85 :
// Pseudo-random sequence generation functions  C   86 :
//---------------------------------------------------------------------------  C   87 :
  C   88 :
/* The rand function computes a sequence of pseudo-random integers in  C   89 :
 * the range 0 to RAND_MAX  C   90 :
 *  C   91 :
 * Parameters:  C   92 :
 *       (none)  C   93 :
 *  C   94 :
 * Returns:  C   95 :
 *       The pseudo-random integer  C   96 :
 */  C   97 :
long rand(void);  C   98 :f276P276
  C   99 :
/* The srand function uses the argument as a seed for a new sequence of  C  100 :
 * pseudo-random numbers to be returned by subsequent calls to rand.  C  101 :
 *  C  102 :
 * Parameters:  C  103 :
 *       [in] seed: The seed value to start from. You might need to pass  C  104 :
 *  C  105 :
 * Returns:  C  106 :
 *       (none)  C  107 :
 *  C  108 :
 * Remarks  C  109 :
 *          The srand function sets the starting point for generating  C  110 :
 *       a series of pseudorandom integers. To reinitialize the  C  111 :
 *       generator, use 1 as the seed argument. Any other value for  C  112 :
 *       seed sets the generator to a random starting point. rand  C  113 :
 *       retrieves the pseudorandom numbers that are generated.  C  114 :
 *       Calling rand before any call to srand generates the same  C  115 :
 *       sequence as calling srand with seed passed as 1.  C  116 :
 *          Usually, you need to pass a time here from outer source  C  117 :
 *       so that the numbers will be different every time you run.  C  118 :
 */  C  119 :
void srand(unsigned int32 seed);  C  120 :f277V62P277
  C  121 :
//---------------------------------------------------------------------------  C  122 :
// Memory management functions  C  123 :
//---------------------------------------------------------------------------  C  124 :
  C  125 :
// Comming soon  C  126 :
  C  127 :
//---------------------------------------------------------------------------  C  128 :
// Communication with the environment  C  129 :
//---------------------------------------------------------------------------  C  130 :
  C  131 :
/* The function returns 0 always  C  132 :
 */  C  133 :
signed int system(char *string);  C  134 :f279V64P279
  C  135 :
//---------------------------------------------------------------------------  C  136 :
// Searching and sorting utilities  C  137 :
//---------------------------------------------------------------------------  C  138 :
  C  139 :
/* Performs a binary search of a sorted array..  C  140 :
 *  C  141 :
 * Parameters:  C  142 :
 *       [in] key: Object to search for  C  143 :
 *       [in] base: Pointer to base of search data  C  144 :
 *       [in] num: Number of elements  C  145 :
 *       [in] width: Width of elements  C  146 :
 *       [in] compare: Function that compares two elements  C  147 :
 *  C  148 :
 * Returns:  C  149 :
 *       bsearch returns a pointer to an occurrence of key in the array pointed  C  150 :
 *       to by base. If key is not found, the function returns NULL. If the  C  151 :
 *       array is not in order or contains duplicate records with identical keys,  C  152 :
 *       the result is unpredictable.  C  153 :
 */  C  154 :
//void *bsearch(const void *key, const void *base, size_t num, size_t width,  C  155 :
//              int (*compare)(const void *, const void *));  C  156 :
  C  157 :
/* Performs the shell-metzner sort (not the quick sort algorithm). The contents  C  158 :
 * of the array are sorted into ascending order according to a comparison  C  159 :
 * function pointed to by compar.  C  160 :
 *  C  161 :
 * Parameters:  C  162 :
 *       [in] base: Pointer to base of search data  C  163 :
 *       [in] num: Number of elements  C  164 :
 *       [in] width: Width of elements  C  165 :
 *       [in] compare: Function that compares two elements  C  166 :
 *  C  167 :
 * Returns:  C  168 :
 *       (none)  C  169 :
 */  C  170 :
//void *qsort(const void *base, size_t num, size_t width,  C  171 :
//              int (*compare)(const void *, const void *));  C  172 :
  C  173 :
//---------------------------------------------------------------------------  C  174 :
// Integer arithmetic functions  C  175 :
//---------------------------------------------------------------------------  C  176 :
  C  177 :
#define labs abs  C  178 :
  C  179 :
div_t div(signed int numer,signed int denom);  C  180 :f282V66V67P282
ldiv_t ldiv(signed long numer,signed long denom);  C  181 :f285V69V70P285
  C  182 :
//---------------------------------------------------------------------------  C  183 :
// Multibyte character functions  C  184 :
//---------------------------------------------------------------------------  C  185 :
  C  186 :
// Not supported  C  187 :
  C  188 :
//---------------------------------------------------------------------------  C  189 :
// Multibyte string functions  C  190 :
//---------------------------------------------------------------------------  C  191 :
  C  192 :
// Not supported  C  193 :
  C  194 :
  C  195 :
//---------------------------------------------------------------------------  C  196 :
// Internal implementation  C  197 :
//---------------------------------------------------------------------------  C  198 :
  C  199 :
#include <stddef.h> C  200 :
 ///////////////////////////////////////////////////////////////////////////  D    1 :I3
////        (C) Copyright 1996,2003 Custom Computer Services           ////  D    2 :
//// This source code may only be used by licensed users of the CCS C  ////  D    3 :
//// compiler.  This source code may only be distributed to other      ////  D    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction ////  D    5 :
//// or distribution is permitted without written permission.          ////  D    6 :
//// Derivative programs created using this software in object code    ////  D    7 :
//// form are not restricted in any way.                               ////  D    8 :
///////////////////////////////////////////////////////////////////////////  D    9 :
  D   10 :
#ifndef _STDDEF  D   11 :
  D   12 :
#define _STDDEF  D   13 :
  D   14 :
#if sizeof(int *)==1  D   15 :
#define ptrdiff_t int  D   16 :
#else  D   17 :
#define ptrdiff_t long  D   18 :
#endif  D   19 :
  D   20 :
#define size_t int  D   21 :
#define wchar_t char  D   22 :
#define NULL 0  D   23 :
  D   24 :
#define offsetof(s,f) (offsetofbit(s,f)/8)  D   25 :
  D   26 :
#endif  D   27 :E
 D   27 :
#include <string.h> C  201 :
 ////////////////////////////////////////////////////////////////////////////  E    1 :I4
////        (C) Copyright 1996,2003 Custom Computer Services            ////  E    2 :
//// This source code may only be used by licensed users of the CCS C   ////  E    3 :
//// compiler.  This source code may only be distributed to other       ////  E    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction  ////  E    5 :
//// or distribution is permitted without written permission.           ////  E    6 :
//// Derivative programs created using this software in object code     ////  E    7 :
//// form are not restricted in any way.                                ////  E    8 :
////////////////////////////////////////////////////////////////////////////  E    9 :
  E   10 :
#ifndef _STRING  E   11 :
#define _STRING  E   12 :
#include <stddef.h> E   13 :
 ///////////////////////////////////////////////////////////////////////////  D    1 :I3
////        (C) Copyright 1996,2003 Custom Computer Services           ////  D    2 :
//// This source code may only be used by licensed users of the CCS C  ////  D    3 :
//// compiler.  This source code may only be distributed to other      ////  D    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction ////  D    5 :
//// or distribution is permitted without written permission.          ////  D    6 :
//// Derivative programs created using this software in object code    ////  D    7 :
//// form are not restricted in any way.                               ////  D    8 :
///////////////////////////////////////////////////////////////////////////  D    9 :
  D   10 :
#ifndef _STDDEF  D   11 :
  D   12 :
#define _STDDEF  D   13 :
  D   14 :
#if sizeof(int *)==1  D   15 :
#define ptrdiff_t int  D   16 :
#else  D   17 :
#define ptrdiff_t long  D   18 :
#endif  D   19 :
  D   20 :
#define size_t int  D   21 :
#define wchar_t char  D   22 :
#define NULL 0  D   23 :
  D   24 :
#define offsetof(s,f) (offsetofbit(s,f)/8)  D   25 :
  D   26 :
#endif  D   27 :E
 D   27 :
#include <ctype.h> E   14 :
 ////////////////////////////////////////////////////////////////////////////  F    1 :I5
////        (C) Copyright 1996,2003 Custom Computer Services            ////  F    2 :
//// This source code may only be used by licensed users of the CCS C   ////  F    3 :
//// compiler.  This source code may only be distributed to other       ////  F    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction  ////  F    5 :
//// or distribution is permitted without written permission.           ////  F    6 :
//// Derivative programs created using this software in object code     ////  F    7 :
//// form are not restricted in any way.                                ////  F    8 :
////////////////////////////////////////////////////////////////////////////  F    9 :
  F   10 :
#ifndef _CTYPE  F   11 :
#define _CTYPE  F   12 :
  F   13 :
#define islower(x)  isamong(x,"abcdefghijklmnopqrstuvwxyz")  F   14 :
#define isupper(x)  isamong(x,"ABCDEFGHIJKLMNOPQRSTUVWXYZ")  F   15 :
#define isalnum(x)  isamong(x,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")  F   16 :
#define isalpha(x)  isamong(x,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")  F   17 :
#define isdigit(x)  isamong(x,"0123456789")  F   18 :
#define isspace(x)  (x==' ')  F   19 :
#define isxdigit(x) isamong(x,"0123456789ABCDEFabcdef")  F   20 :
#define iscntrl(x)  (x<' ')  F   21 :
#define isprint(x)  (x>=' ')  F   22 :
#define isgraph(x)  (x>' ')  F   23 :
#define ispunct(x)  ((x>' ')&&!isalnum(x))  F   24 :
  F   25 :
#endif  F   26 :
  F   27 :E
 F   27 :
  E   15 :
  E   16 :
  E   17 :
//////////////////////////////////////////////  E   18 :
//// Uncomment the following define to    ////  E   19 :
//// allow some functions to use a        ////  E   20 :
//// quicker algorithm, but use more ROM  ////  E   21 :
////                                      ////  E   22 :
//// #define FASTER_BUT_MORE_ROM          ////  E   23 :
//////////////////////////////////////////////  E   24 :
  E   25 :
  E   26 :
  E   27 :
/*Copying functions*/  E   28 :
/* standard template:  E   29 :
   void *memmove(void *s1, void *s2, size_t n).  E   30 :
   Copies max of n characters safely (not following ending '\0')  E   31 :
   from s2 in s1; if s2 has less than n characters, appends 0 */  E   32 :
  E   33 :
char *memmove(void *s1,char *s2,size_t n)  E   34 :f299V72V73V74
{  E   35 :{47
   char *sc1;  E   36 :V75
   char *sc2;  E   37 :V76
   sc1=s1;  E   38 :
   sc2=s2;  E   39 :
   if(sc2<sc1 && sc1 <sc2 +n)  E   40 :
      for(sc1+=n,sc2+=n;0<n;--n)  E   41 :
         *--sc1=*--sc2;  E   42 :
   else  E   43 :
      for(;0<n;--n)  E   44 :
         *sc1++=*sc2++;  E   45 :
  return s1;  E   46 :
  }  E   47 :}66
  E   48 :
/* compiler ignored the name 'strcpy()'; perhaps, it's reserved? E   49 :
   Standard template: char *strcpy(char *s1, const char *s2)  E   50 :
   copies the string s2 including the null character to s1*/  E   51 :
  E   52 :
char *strcopy(char *s1, char *s2)  E   53 :f305V78V79
{  E   54 :{68
  char *s;  E   55 :V80
  E   56 :
  for (s = s1; *s2 != 0; s++, s2++) {  E   57 :
	  *s = *s2;  E   58 :{76
  }  E   59 :}77
  *s = *s2;  E   60 :
  return(s1);  E   61 :
}  E   62 :}78
  E   63 :
/* standard template:  E   64 :
   char *strncpy(char *s1, const char *s2, size_t n).  E   65 :
   Copies max of n characters (not following ending '\0')  E   66 :
   from s2 in s1; if s2 has less than n characters, appends 0 */  E   67 :
  E   68 :
char *strncpy(char *s1, char *s2, size_t n)  E   69 :f306V82V83V84
{  E   70 :{80
  char *s;  E   71 :V85
  E   72 :
  for (s = s1; n > 0 && *s2 != '\0'; n--)  E   73 :
     *s++ = *s2++;  E   74 :
  for (; n > 0; n--)  E   75 :
     *s++ = '\0';  E   76 :
  E   77 :
  return(s1);  E   78 :
}  E   79 :}96
/***********************************************************/  E   80 :
  E   81 :
/*concatenation functions*/  E   82 :
/* standard template: char *strcat(char *s1, const char *s2)  E   83 :
appends s2 to s1*/  E   84 :
  E   85 :
char *strcat(char *s1, char *s2)  E   86 :f307V87V88
{  E   87 :{98
   char *s;  E   88 :V89
  E   89 :
   for (s = s1; *s != '\0'; ++s);  E   90 :
   while(*s2 != '\0')  E   91 :
   {  E   92 :
      *s = *s2;  E   93 :{109
      ++s;  E   94 :
      ++s2;  E   95 :
   }  E   96 :}110
  E   97 :
   *s = '\0';  E   98 :
   return(s1);  E   99 :
}  E  100 :}111
/* standard template: char *strncat(char *s1, char *s2,size_t n)  E  101 :
appends not more than n characters from s2 to s1*/  E  102 :
  E  103 :
char *strncat(char *s1, char *s2, size_t n)  E  104 :f308V91V92V93
{  E  105 :{113
   char *s;  E  106 :V94
  E  107 :
   for (s = s1; *s != '\0'; ++s);  E  108 :
   while(*s2 != '\0' && 0<n)  E  109 :
   {  E  110 :
      *s = *s2;  E  111 :{126
      ++s;  E  112 :
      ++s2;  E  113 :
      --n;  E  114 :
   }  E  115 :}127
  E  116 :
   *s = '\0';  E  117 :
   return(s1);  E  118 :
}  E  119 :}128
  E  120 :
/***********************************************************/  E  121 :
  E  122 :
  E  123 :
/*comparison functions*/  E  124 :
/* standard template: signed int memcmp(void *s1, void *s2).  E  125 :
   Compares s1 & s2; returns -1 if s1<s2, 0 if s1=s2, 1 if s1>s2 */  E  126 :
  E  127 :
signed int memcmp(void * s1,char *s2,size_t n)  E  128 :f309V96V97V98
{  E  129 :{130
char *su1, *su2;  E  130 :V99V100
for(su1=s1, su2=s2; 0<n; ++su1, ++su2, --n)  E  131 :
{  E  132 :
   if(*su1!=*su2)  E  133 :{138
      return ((*su1<*su2)?1:+1);  E  134 :
}  E  135 :}144
return 0;  E  136 :
}  E  137 :}145
  E  138 :
/* standard template: int strcmp(const char *s1, const char *s2).  E  139 :
   Compares s1 & s2; returns -1 if s1<s2, 0 if s1=s2, 1 if s1>s2 */  E  140 :
  E  141 :
signed int strcmp(char *s1, char *s2)  E  142 :f312V102V103
{  E  143 :{147
   for (; *s1 == *s2; s1++, s2++)  E  144 :
      if (*s1 == '\0')  E  145 :
         return(0);  E  146 :
   return((*s1 < *s2) ?-1: 1);  E  147 :
}  E  148 :}160
/* standard template: int strcoll(const char *s1, const char *s2).  E  149 :
   Compares s1 & s2; returns -1 if s1<s2, 0 if s1=s2, 1 if s1>s2 */  E  150 :
  E  151 :
signed int strcoll(char *s1, char *s2)  E  152 :f313V105V106
{  E  153 :{162
   for (; *s1 == *s2; s1++, s2++)  E  154 :
      if (*s1 == '\0')  E  155 :
         return(0);  E  156 :
   return((*s1 < *s2) ?-1: 1);  E  157 :
}  E  158 :}175
  E  159 :
/* standard template:  E  160 :
   int strncmp(const char *s1, const char *s2, size_t n).  E  161 :
   Compares max of n characters (not following 0) from s1 to s2;  E  162 :
   returns same as strcmp */  E  163 :
  E  164 :
signed int strncmp(char *s1, char *s2, size_t n)  E  165 :f314V108V109V110
{  E  166 :{177
   for (; n > 0; s1++, s2++, n--)  E  167 :
      if (*s1 != *s2)  E  168 :
         return((*s1 <*s2) ?-1: 1);  E  169 :
      else if (*s1 == '\0')  E  170 :
         return(0);  E  171 :
   return(0);  E  172 :
}  E  173 :}193
/* standard template:  E  174 :
   int strxfrm(const char *s1, const char *s2, size_t n).  E  175 :
   transforms maximum of n characters from s2 and places them into s1*/  E  176 :
size_t strxfrm(char *s1, char *s2, size_t n)  E  177 :f315V112V113V114
{  E  178 :{195
  char *s;  E  179 :V115
  int n1;  E  180 :V116
  n1=n;  E  181 :
  for (s = s1; n > 0 && *s2 != '\0'; n--)  E  182 :
     *s++ = *s2++;  E  183 :
  for (; n > 0; n--)  E  184 :
     *s++ = '\0';  E  185 :
  E  186 :
  return(n1);  E  187 :
}  E  188 :}211
  E  189 :
  E  190 :
  E  191 :
  E  192 :
  E  193 :
/***********************************************************/  E  194 :
/*Search functions*/  E  195 :
/* standard template: void *memchr(const char *s, int c).  E  196 :
   Finds first occurrence of c in n characters of s */  E  197 :
  E  198 :
char *memchr(void *s,int c,size_t n)  E  199 :f317V118V119V120
{  E  200 :{213
   char uc;  E  201 :V121
   char *su;  E  202 :V122
   uc=c;  E  203 :
   for(su=s;0<n;++su,--n)  E  204 :
      if(*su==uc)  E  205 :
      return su;  E  206 :
   return NULL;  E  207 :
}  E  208 :}223
  E  209 :
/* standard template: char *strchr(const char *s, int c).  E  210 :
   Finds first occurrence of c in s */  E  211 :
  E  212 :
char *strchr(char *s, int c)  E  213 :f321V124V125
{  E  214 :{225
   for (; *s != c; s++)  E  215 :
      if (*s == '\0')  E  216 :
         return(0);  E  217 :
   return(s);  E  218 :
}  E  219 :}235
/* standard template:  E  220 :
   size_t strcspn(const char *s1, const char *s2).  E  221 :
   Computes length of max initial segment of s1 that  E  222 :
   consists entirely of characters NOT from s2*/  E  223 :
  E  224 :
int *strcspn(char *s1, char *s2)  E  225 :f322V127V128
{  E  226 :{237
   char *sc1, *sc2;  E  227 :V129V130
  E  228 :
   for (sc1 = s1; *sc1 != 0; sc1++)  E  229 :
      for (sc2 = s2; *sc2 != 0; sc2++)  E  230 :
         if (*sc1 == *sc2)  E  231 :
            return(sc1 - s1);  E  232 :
   return(sc1 - s1);  E  233 :
}  E  234 :}253
/* standard template:  E  235 :
   char *strpbrk(const char *s1, const char *s2).  E  236 :
   Locates first occurence of any character from s2 in s1;  E  237 :
   returns s1 if s2 is empty string */  E  238 :
  E  239 :
char *strpbrk(char *s1, char *s2)  E  240 :f323V132V133
{  E  241 :{255
   char *sc1, *sc2;  E  242 :V134V135
  E  243 :
   for (sc1 = s1; *sc1 != 0; sc1++)  E  244 :
      for (sc2 = s2; *sc2 != 0; sc2++)  E  245 :
         if (*sc1 == *sc2)  E  246 :
            return(sc1);  E  247 :
   return(0);  E  248 :
}  E  249 :}271
  E  250 :
  E  251 :
/* standard template: char *strrchr(const char *s, int c).  E  252 :
   Finds last occurrence of c in s */  E  253 :
  E  254 :
char *strrchr(char *s, int c)  E  255 :f324V137V138
{  E  256 :{273
   char *p;  E  257 :V139
  E  258 :
   for (p = 0; ; s++)  E  259 :
   {  E  260 :
      if (*s == c)  E  261 :{279
         p = s;  E  262 :
      if (*s == '\0')  E  263 :
         return(p);  E  264 :
   }  E  265 :}284
}  E  266 :}285
/* computes length of max initial segment of s1 consisting  E  267 :
   entirely of characters from s2 */  E  268 :
  E  269 :
int *strspn(char *s1, char *s2)  E  270 :f326V141V142
{  E  271 :{287
   char *sc1, *sc2;  E  272 :V143V144
  E  273 :
   for (sc1 = s1; *sc1 != 0; sc1++)  E  274 :
      for (sc2 = s2; ; sc2++)  E  275 :
	 if (*sc2 == '\0')  E  276 :
	    return(sc1 - s1);  E  277 :
         else if (*sc1 == *sc2)  E  278 :
            break;  E  279 :
   return(sc1 - s1);  E  280 :
}  E  281 :}304
/* standard template:  E  282 :
   char *strstr(const char *s1, const char *s2);  E  283 :
   Locates first occurence of character sequence s2 in s1;  E  284 :
   returns 0 if s2 is empty string  E  285 :
  E  286 :
   Uncomment #define FASTER_BUT_MORE_ROM at the top of the  E  287 :
   file to use the faster algorithm */  E  288 :
char *strstr(char *s1, char *s2)  E  289 :f327V146V147
{  E  290 :{306
	char *s, *t;  E  291 :V148V149
  E  292 :
   #ifdef FASTER_BUT_MORE_ROM  E  293 :
   if (*s2 == '\0')  E  294 :
         return(s1);  E  295 :
   #endif  E  296 :
  E  297 :
	while (*s1)  E  298 :
   {  E  299 :
      for(s = s1, t = s2; *t && *s == *t; ++s, ++t);  E  300 :{311
  E  301 :
		if (*t == '\0')  E  302 :
			return s1;  E  303 :
      ++s1;  E  304 :
      #ifdef FASTER_BUT_MORE_ROM  E  305 :
         while(*s1 != '\0' && *s1 != *s2)  E  306 :
            ++s1;  E  307 :
      #endif  E  308 :
	}  E  309 :}322
	return 0;  E  310 :
}  E  311 :}323
  E  312 :
/* standard template: char *strtok(char *s1, const char *s2).  E  313 :
  E  314 :
   Finds next token in s1 delimited by a character from separator  E  315 :
   string s2 (which can be different from call to call).  First call  E  316 :
   starts at beginning of s1 searching for first character NOT  E  317 :
   contained in s2; returns 0 if none is found.  E  318 :
   If one is found, it is the start of first token (return value).  E  319 :
   Function then searches from there for a character contained in s2.  E  320 :
   If none is found, current token extends to end of s1, and subsequent  E  321 :
   searches for a token will return 0.  If one is found, it is  E  322 :
   overwritten by '\0', which terminates current token.  Function saves  E  323 :
   pointer to following character from which next search will start.  E  324 :
   Each subsequent call, with 0 as first argument, starts searching  E  325 :
   from saved pointer */  E  326 :
  E  327 :
char *strtok(char *s1, char *s2)  E  328 :f329V151V152
{  E  329 :{325
   char *beg, *end;  E  330 :V153V154
   static char *save;  E  331 :V155
  E  332 :
   beg = (s1)?s1: save;  E  333 :
   beg += strspn(beg, s2);  E  334 :
   if (*beg == '\0')  E  335 :
   {  E  336 :
      *save = ' ';  E  337 :{332
      return(0);  E  338 :
   }  E  339 :}333
   end = strpbrk(beg, s2);  E  340 :
   if (*end != '\0')  E  341 :
   {  E  342 :
      *end = '\0';  E  343 :{336
      end++;  E  344 :
   }  E  345 :}337
   save = end;  E  346 :
   return(beg);  E  347 :
}  E  348 :}338
  E  349 :
/*****************************************************************/  E  350 :
/*Miscellaneous functions*/  E  351 :
/* standard template  E  352 :
maps error number in errnum to an error message string  E  353 :
Returns: Pointer to string  E  354 :
*/  E  355 :
#ifdef _ERRNO  E  356 :
char * strerror(int errnum)  E  357 :
{  E  358 :
char s[15];  E  359 :
switch( errnum)  E  360 :
{  E  361 :
case 0:  E  362 :
   strcpy(s,"no errors");  E  363 :
   return s;  E  364 :
case EDOM :  E  365 :
   strcpy(s,"domain error");  E  366 :
   return s;  E  367 :
case ERANGE:  E  368 :
   strcpy(s,"range error");  E  369 :
   return s;  E  370 :
}  E  371 :
}  E  372 :
#ENDIF  E  373 :
/* standard template: size_t strlen(const char *s).  E  374 :
   Computes length of s1 (preceding terminating 0) */  E  375 :
  E  376 :
int *strlen(char *s)  E  377 :f333V157
{  E  378 :{340
   char *sc;  E  379 :V158
  E  380 :
   for (sc = s; *sc != 0; sc++);  E  381 :
   return(sc - s);  E  382 :
}  E  383 :}348
  E  384 :
/* standard template: size_t stricmp(const char *s1, const char *s2).  E  385 :
   Compares s1 to s2 ignoring case (upper vs. lower) */  E  386 :
  E  387 :
signed int stricmp(char *s1, char *s2)  E  388 :f335V160V161
{  E  389 :{350
 for(; *s1==*s2||(isalpha(*s1)&&isalpha(*s2)&&(*s1==*s2+32||*s2==*s1+32));  E  390 :
    s1++, s2++)  E  391 :
    if (*s1 == '\0')  E  392 :
       return(0);  E  393 :
 return((*s1 < *s2) ?-1: 1);  E  394 :
}  E  395 :}385
  E  396 :
  E  397 :
/* standard template: char *strlwr(char *s).  E  398 :
   Replaces uppercase letters by lowercase;  E  399 :
   returns pointer to new string s */  E  400 :
  E  401 :
char *strlwr(char *s)  E  402 :f338V165
{  E  403 :{387
   char *p;  E  404 :V166
  E  405 :
   for (p = s; *p != '\0'; p++)  E  406 :
      if (*p >= 'A' && *p <='Z')  E  407 :
         *p += 'a' - 'A';  E  408 :
   return(s);  E  409 :
}  E  410 :}399
  E  411 :
  E  412 :
/************************************************************/  E  413 :
  E  414 :
  E  415 :
#endif  E  416 :E
 E  416 :
  C  202 :
div_t div(signed int numer,signed int denom)  C  203 :f282V66V67
{  C  204 :{400
   div_t val;  C  205 :V167
   val.quot = numer / denom;  C  206 :V169V170
   val.rem = numer - (denom * val.quot);  C  207 :V172V173
   return (val);  C  208 :
}  C  209 :}413
  C  210 :
ldiv_t ldiv(signed long numer,signed long denom)  C  211 :f285V69V70
{  C  212 :{414
   ldiv_t val;  C  213 :V174
   val.quot = numer / denom;  C  214 :V176V177
   val.rem = numer - (denom * val.quot);  C  215 :V179V180
   return (val);  C  216 :
}  C  217 :}431
  C  218 :
float atof(char * s)  C  219 :f266V42
{  C  220 :{432
   float pow10 = 1.0;  C  221 :V181
   float result = 0.0;  C  222 :V182
   int sign = 0;  C  223 :V183
   char c;  C  224 :V184
   int ptr = 0;  C  225 :V185
  C  226 :
   c = s[ptr++];  C  227 :
  C  228 :
   if ((c>='0' && c<='9') || c=='+' || c=='-' || c=='.') {  C  229 :
      if(c == '-') {  C  230 :{444
         sign = 1;  C  231 :{447
         c = s[ptr++];  C  232 :
      }  C  233 :}448
      if(c == '+')  C  234 :
         c = s[ptr++];  C  235 :
  C  236 :
      while((c >= '0' && c <= '9')) {  C  237 :
         result = 10*result + c - '0';  C  238 :{456V187V188V190V192V193
         c = s[ptr++];  C  239 :
      }  C  240 :}524
  C  241 :
      if (c == '.') {  C  242 :
         c = s[ptr++];  C  243 :{527
         while((c >= '0' && c <= '9')) {  C  244 :
             pow10 = pow10*10;  C  245 :{533
             result += (c - '0')/pow10;  C  246 :V195V196
             c = s[ptr++];  C  247 :
         }  C  248 :}559
      }  C  249 :}560
  C  250 :
   }  C  251 :}561
  C  252 :
   if (sign == 1)  C  253 :
      result = -1*result;  C  254 :
   return(result);  C  255 :
}  C  256 :}564
  C  257 :
signed int atoi(char *s)  C  258 :f268V44
{  C  259 :{565
   signed int result;  C  260 :V197
   int sign, base, index;  C  261 :V198V199V200
   char c;  C  262 :V201
  C  263 :
   index = 0;  C  264 :
   sign = 0;  C  265 :
   base = 10;  C  266 :
   result = 0;  C  267 :
  C  268 :
   // Omit all preceeding alpha characters  C  269 :
   if(s)  C  270 :
      c = s[index++];  C  271 :
  C  272 :
   // increase index if either positive or negative sign is detected  C  273 :
   if (c == '-')  C  274 :
   {  C  275 :
      sign = 1;         // Set the sign to negative  C  276 :{571
      c = s[index++];  C  277 :
   }  C  278 :}572
   else if (c == '+')  C  279 :
   {  C  280 :
      c = s[index++];  C  281 :{576
   }  C  282 :}577
  C  283 :
   if (c >= '0' && c <= '9')  C  284 :
   {  C  285 :
  C  286 :
      // Check for hexa number  C  287 :
      if (c == '0' && (s[index] == 'x' || s[index] == 'X'))  C  288 :{582
      {  C  289 :
         base = 16;  C  290 :{589
         index++;  C  291 :
         c = s[index++];  C  292 :
      }  C  293 :}590
  C  294 :
      // The number is a decimal number  C  295 :
      if (base == 10)  C  296 :
      {  C  297 :
         while (c >= '0' && c <= '9')  C  298 :{593
         {  C  299 :
            result = 10*result + (c - '0');  C  300 :{599
            c = s[index++];  C  301 :
         }  C  302 :}600
      }  C  303 :}601
      else if (base == 16)    // The number is a hexa number  C  304 :
      {  C  305 :
         c = toupper(c);  C  306 :{605
         while ( (c >= '0' && c <= '9') || (c >= 'A' && c<='F'))  C  307 :
         {  C  308 :
            if (c >= '0' && c <= '9')  C  309 :{619
               result = (result << 4) + (c - '0');  C  310 :
            else  C  311 :
               result = (result << 4) + (c - 'A' + 10);  C  312 :
  C  313 :
            c = s[index++];  C  314 :
            c = toupper(c);  C  315 :
         }  C  316 :}629
      }  C  317 :}630
   }  C  318 :}631
  C  319 :
   if (sign == 1 && base == 10)  C  320 :
       result = -result;  C  321 :
  C  322 :
   return(result);  C  323 :
}  C  324 :}636
  C  325 :
signed long atol(char *s)  C  326 :f270V48
{  C  327 :{637
   signed long result;  C  328 :V202
   int sign, base, index;  C  329 :V203V204V205
   char c;  C  330 :V206
  C  331 :
   index = 0;  C  332 :
   sign = 0;  C  333 :
   base = 10;  C  334 :
   result = 0;  C  335 :
  C  336 :
   if(s)  C  337 :
      c = s[index++];  C  338 :
  C  339 :
   // increase index if either positive or negative sign is detected  C  340 :
   if (c == '-')  C  341 :
   {  C  342 :
      sign = 1;         // Set the sign to negative  C  343 :{643
      c = s[index++];  C  344 :
   }  C  345 :}644
   else if (c == '+')  C  346 :
   {  C  347 :
      c = s[index++];  C  348 :{648
   }  C  349 :}649
  C  350 :
   if (c >= '0' && c <= '9')  C  351 :
   {  C  352 :
      if (c == '0' && (s[index] == 'x' || s[index] == 'X'))  C  353 :{654
      {  C  354 :
         base = 16;  C  355 :{661
         index++;  C  356 :
         c = s[index++];  C  357 :
      }  C  358 :}662
  C  359 :
      // The number is a decimal number  C  360 :
      if (base == 10)  C  361 :
      {  C  362 :
         while (c >= '0' && c <= '9')  C  363 :{665
         {  C  364 :
            result = 10*result + (c - '0');  C  365 :{671
            c = s[index++];  C  366 :
         }  C  367 :}672
      }  C  368 :}673
      else if (base == 16)    // The number is a hexa number  C  369 :
      {  C  370 :
         c = toupper(c);  C  371 :{677
         while ( (c >= '0' && c <= '9') || (c >= 'A' && c <='F'))  C  372 :
         {  C  373 :
            if (c >= '0' && c <= '9')  C  374 :{691
               result = (result << 4) + (c - '0');  C  375 :
            else  C  376 :
               result = (result << 4) + (c - 'A' + 10);  C  377 :
  C  378 :
            c = s[index++];c = toupper(c);  C  379 :
         }  C  380 :}701
      }  C  381 :}702
   }  C  382 :}703
  C  383 :
   if (base == 10 && sign == 1)  C  384 :
      result = -result;  C  385 :
  C  386 :
   return(result);  C  387 :
}  C  388 :}708
  C  389 :
/* A fast routine to multiply by 10  C  390 :
 */  C  391 :
signed int32 mult_with10(int32 num)  C  392 :f353V208
{  C  393 :{710
   return ( (num << 1) + (num << 3) );  C  394 :
}  C  395 :}712
  C  396 :
signed int32 atoi32(char *s)  C  397 :f269V46
{  C  398 :{713
   signed int32 result;  C  399 :V209
   int sign, base, index;  C  400 :V210V211V212
   char c;  C  401 :V213
  C  402 :
   index = 0;  C  403 :
   sign = 0;  C  404 :
   base = 10;  C  405 :
   result = 0;  C  406 :
  C  407 :
   if(s)  C  408 :
      c = s[index++];  C  409 :
  C  410 :
   // increase index if either positive or negative sign is detected  C  411 :
   if (c == '-')  C  412 :
   {  C  413 :
      sign = 1;         // Set the sign to negative  C  414 :{719
      c = s[index++];  C  415 :
   }  C  416 :}720
   else if (c == '+')  C  417 :
   {  C  418 :
      c = s[index++];  C  419 :{724
   }  C  420 :}725
  C  421 :
   if (c >= '0' && c <= '9')  C  422 :
   {  C  423 :
      if (c == '0' && (s[index] == 'x' || s[index] == 'X'))  C  424 :{730
      {  C  425 :
         base = 16;  C  426 :{737
         index++;  C  427 :
         c = s[index++];  C  428 :
      }  C  429 :}738
  C  430 :
      // The number is a decimal number  C  431 :
      if (base == 10)  C  432 :
      {  C  433 :
         while (c >= '0' && c <= '9') {  C  434 :{741
            result = (result << 1) + (result << 3);  // result *= 10;  C  435 :{747
            result += (c - '0');  C  436 :
            c = s[index++];  C  437 :
         }  C  438 :}748
      }  C  439 :}749
      else if (base == 16)    // The number is a hexa number  C  440 :
      {  C  441 :
         c = toupper(c);  C  442 :{753
         while ((c >= '0' && c <= '9') || (c >= 'A' && c <='F'))  C  443 :
         {  C  444 :
            if (c >= '0' && c <= '9')  C  445 :{767
               result = (result << 4) + (c - '0');  C  446 :
            else  C  447 :
               result = (result << 4) + (c - 'A' + 10);  C  448 :
  C  449 :
            c = s[index++];c = toupper(c);  C  450 :
         }  C  451 :}777
      }  C  452 :}778
   }  C  453 :}779
  C  454 :
   if (base == 10 && sign == 1)  C  455 :
      result = -result;  C  456 :
  C  457 :
   return(result);  C  458 :
}  C  459 :}784
  C  460 :
float strtod(char *s,char *endptr) {  C  461 :f271V50V51{785
   float pow10 = 1.0;  C  462 :V214
   float result = 0.0;  C  463 :V215
   int sign = 0, point = 0;  C  464 :V216V217
   char c;  C  465 :V218
   int ptr = 0;  C  466 :V219
  C  467 :
   if(s)  C  468 :
   {  C  469 :
      c=s[ptr++];  C  470 :{789
   }  C  471 :}790
  C  472 :
   while((c>='0' && c<='9') || c=='+' || c=='-' || c=='.') {  C  473 :
      if(c == '-') {  C  474 :{802
         sign = 1;  C  475 :{805
         c = s[ptr++];  C  476 :
      }  C  477 :}806
  C  478 :
      while((c >= '0' && c <= '9') && point == 0) {  C  479 :
         result = 10*result + c - '0';  C  480 :{814
         c = s[ptr++];  C  481 :
      }  C  482 :}815
  C  483 :
      if (c == '.') {  C  484 :
         point = 1;  C  485 :{818
         c = s[ptr++];  C  486 :
      }  C  487 :}819
  C  488 :
      while((c >= '0' && c <= '9') && point == 1) {  C  489 :
         pow10 = pow10*10;  C  490 :{827
         result += (c - '0')/pow10;  C  491 :
         c = s[ptr++];  C  492 :
      }  C  493 :}828
  C  494 :
      if (c == '+') {  C  495 :
         c = s[ptr++];  C  496 :{831
      }  C  497 :}832
   }  C  498 :}833
  C  499 :
   if (sign == 1)  C  500 :
      result = -1*result;  C  501 :
   if(endptr)  C  502 :
   {  C  503 :
      if (ptr) {  C  504 :{838
         ptr--;  C  505 :{841
         *((char *)endptr)=s+ptr;  C  506 :
      }  C  507 :}842
      else  C  508 :
         *((char *)endptr)=s;  C  509 :
   }  C  510 :}844
  C  511 :
   return(result);  C  512 :
}  C  513 :}845
  C  514 :
long strtoul(char *s,char *endptr,signed int base)  C  515 :f275V57V58V59
{  C  516 :{846
   char *sc,*s1,*sd;  C  517 :V220V221V222
   unsigned long x=0;  C  518 :V223
   char sign;  C  519 :V224
   char digits[]="0123456789abcdefghijklmnopqstuvwxyz";  C  520 :V225
   for(sc=s;isspace(*sc);++sc);  C  521 :
   sign=*sc=='-'||*sc=='+'?sc++:'+';  C  522 :
   if(sign=='-')  C  523 :
   {  C  524 :
      if (endptr)  C  525 :{861
      {  C  526 :
        *((char *)endptr)=s;  C  527 :{864
      }  C  528 :}865
      return 0;  C  529 :
   }  C  530 :}866
  C  531 :
   if (base <0 || base ==1|| base >36) // invalid base  C  532 :
   {  C  533 :
      if (endptr)  C  534 :{873
      {  C  535 :
        *((char *)endptr)=s;  C  536 :{876
      }  C  537 :}877
      return 0;  C  538 :
   }  C  539 :}878
   else if (base)  C  540 :
   {  C  541 :
      if(base==16 && *sc =='0'&&(sc[1]=='x' || sc[1]=='X'))  C  542 :{882
         sc+=2;  C  543 :
      if(base==8 && *sc =='0')  C  544 :
         sc+=1;  C  545 :
      if(base==2 && *sc =='0'&&sc[1]=='b')  C  546 :
         sc+=2;  C  547 :
  C  548 :
   }  C  549 :}901
   else if(*sc!='0') // base is 0, find base  C  550 :
      base=10;  C  551 :
   else if (sc[1]=='x' || sc[1]=='X')  C  552 :
      base =16,sc+=2;  C  553 :
   else if(sc[1]=='b')  C  554 :
      base=2,sc+=2;  C  555 :
   else  C  556 :
      base=8;  C  557 :
   for (s1=sc;*sc=='0';++sc);// skip leading zeroes  C  558 :
   sd=memchr(digits,tolower(*sc),base);  C  559 :
   for(; sd!=0; )  C  560 :
   {  C  561 :
      x=x*base+(int16)(sd-digits);  C  562 :{930
      ++sc;  C  563 :
      sd=memchr(digits,tolower(*sc),base);  C  564 :
   }  C  565 :}935
   if(s1==sc)  C  566 :
   {  C  567 :
      if (endptr)  C  568 :{938
      {  C  569 :
        *((char *)endptr)=s;  C  570 :{941
      }  C  571 :}942
   return 0;  C  572 :
   }  C  573 :}943
   if (endptr)  C  574 :
        *((char *)endptr)=sc;  C  575 :
   return x;  C  576 :
}  C  577 :}946
  C  578 :
  C  579 :
signed long strtol(char *s,char *endptr,signed int base)  C  580 :f273V53V54V55
{  C  581 :{947
   char *sc,*s1,*sd;  C  582 :V226V227V228
   signed long x=0;  C  583 :V229
   char sign;  C  584 :V230
   char digits[]="0123456789abcdefghijklmnopqstuvwxyz";  C  585 :V231
   for(sc=s;isspace(*sc);++sc);  C  586 :
   sign=*sc=='-'||*sc=='+'?sc++:'+';  C  587 :
   if (base <0 || base ==1|| base >36) // invalid base  C  588 :
   {  C  589 :
      if (endptr)  C  590 :{966
      {  C  591 :
        *((char *)endptr)=s;  C  592 :{969
      }  C  593 :}970
      return 0;  C  594 :
   }  C  595 :}971
   else if (base)  C  596 :
   {  C  597 :
      if(base==16 && *sc =='0'&&(sc[1]=='x' || sc[1]=='X'))  C  598 :{975
         sc+=2;  C  599 :
      if(base==8 && *sc =='0')  C  600 :
         sc+=1;  C  601 :
      if(base==2 && *sc =='0'&&sc[1]=='b')  C  602 :
         sc+=2;  C  603 :
  C  604 :
   }  C  605 :}994
   else if(*sc!='0') // base is 0, find base  C  606 :
      base=10;  C  607 :
   else if (sc[1]=='x' || sc[1]=='X')  C  608 :
      base =16,sc+=2;  C  609 :
   else if(sc[1]=='b')  C  610 :
      base=2,sc+=2;  C  611 :
   else  C  612 :
      base=8;  C  613 :
   for (s1=sc;*sc=='0';++sc);// skip leading zeroes  C  614 :
  C  615 :
   sd=memchr(digits,tolower(*sc),base);  C  616 :
   for(;sd!=0;)  C  617 :
   {  C  618 :
      x=x*base+(int16)(sd-digits);  C  619 :{1023
      ++sc;  C  620 :
      sd=memchr(digits,tolower(*sc),base);  C  621 :
   }  C  622 :}1028
   if(s1==sc)  C  623 :
   {  C  624 :
      if (endptr)  C  625 :{1031
      {  C  626 :
        *((char *)endptr)=s;  C  627 :{1034
      }  C  628 :}1035
   return 0;  C  629 :
   }  C  630 :}1036
   if(sign=='-')  C  631 :
      x  =-x;  C  632 :
   if (endptr)  C  633 :
        *((char *)endptr)=sc;  C  634 :
   return x;  C  635 :
}  C  636 :}1041
  C  637 :
signed int system(char *string)  C  638 :f279V64
{  C  639 :{1042
   return 0;  C  640 :
}  C  641 :}1044
  C  642 :
int mblen(char *s,size_t n)  C  643 :f359V233V234
{  C  644 :{1046
   return strlen(s);  C  645 :
}  C  646 :}1048
  C  647 :
int mbtowc(wchar_t *pwc,char *s,size_t n)  C  648 :f360V236V237V238
{  C  649 :{1050
   *pwc=*s;  C  650 :
   return 1;  C  651 :
}  C  652 :}1052
  C  653 :
int wctomb(char *s,wchar_t wchar)  C  654 :f362V240V241
{  C  655 :{1054
   *s=wchar;  C  656 :
   return 1;  C  657 :
}  C  658 :}1056
  C  659 :
size_t mbstowcs(wchar_t *pwcs,char *s,size_t n)  C  660 :f364V243V244V245
{  C  661 :{1058
   strncpy(pwcs,s,n);  C  662 :
   return strlen(pwcs);  C  663 :
}  C  664 :}1060
  C  665 :
size_t wcstombs(char *s,wchar_t *pwcs,size_t n)  C  666 :f366V247V248V249
{  C  667 :{1062
   strncpy(s,pwcs,n);  C  668 :
   return strlen(s);  C  669 :
}  C  670 :}1064
  C  671 :
//---------------------------------------------------------------------------  C  672 :
// The random number implementation  C  673 :
//---------------------------------------------------------------------------  C  674 :
  C  675 :
unsigned int32 _Randseed = 1;  C  676 :V250
  C  677 :
long rand(void)  C  678 :f276
{  C  679 :{1065
   _Randseed = _Randseed * 1103515245 + 12345;  C  680 :V252V253
   return ((unsigned long)(_Randseed >> 16) % RAND_MAX);  C  681 :V255V256
}  C  682 :}1082
  C  683 :
void srand(unsigned int32 seed)  C  684 :f277V62
{  C  685 :{1083
   _Randseed = seed;  C  686 :
}  C  687 :}1085
  C  688 :
//---------------------------------------------------------------------------  C  689 :
// Searching and sorting utilities implementation  C  690 :
//---------------------------------------------------------------------------  C  691 :
  C  692 :
typedef signed int (*_Cmpfun)(char * p1,char * p2);  C  693 :f371V258V259P371
  C  694 :
void qsort(char * qdata, int qitems, int qsize, _Cmpfun cmp) {  C  695 :f374V262V263V264V265{1089
   int m,j,i,l;  C  696 :V266V267V268V269
   short done;  C  697 :V270
   BYTE t[16];  C  698 :V271
  C  699 :
   m = qitems/2;  C  700 :
   while( m > 0 ) {  C  701 :
     for(j=0; j<(qitems-m); ++j) {  C  702 :{1094
        i = j;  C  703 :{1101
        do  C  704 :
        {  C  705 :
           done=TRUE;  C  706 :{1105
           l = i+m;  C  707 :
           if( (*cmp)(qdata+i*qsize, qdata+l*qsize) > 0 ) {  C  708 :
              memcpy(t, qdata+i*qsize, qsize);  C  709 :{1109
              memcpy(qdata+i*qsize, qdata+l*qsize, qsize);  C  710 :
              memcpy(qdata+l*qsize, t, qsize);  C  711 :
              if(m <= i)  C  712 :
                i -= m;  C  713 :
                done = FALSE;  C  714 :
           }  C  715 :}1118
        } while(!done);  C  716 :}1119
     }  C  717 :}1122
     m = m/2;  C  718 :
   }  C  719 :}1123
}  C  720 :}1124
  C  721 :
  C  722 :
char *bsearch(char *key, char *base, size_t num, size_t width,_Cmpfun cmp)  C  723 :f385V274V275V276V277V278
{  C  724 :{1126
   char *p, *q;  C  725 :V279V280
   size_t n;  C  726 :V281
   size_t pivot;  C  727 :V282
   signed int val;  C  728 :V283
  C  729 :
   p = base;  C  730 :
   n = num;  C  731 :
  C  732 :
   while (n > 0)  C  733 :
   {  C  734 :
      pivot = n >> 1;  C  735 :{1131
      q = p + width * pivot;  C  736 :
  C  737 :
      val = (*cmp)(key, q);  C  738 :
  C  739 :
      if (val < 0)  C  740 :
         n = pivot;  C  741 :
      else if (val == 0)  C  742 :
         return ((char *)q);  C  743 :
      else {  C  744 :
         p = q + width;  C  745 :{1138
         n -= pivot + 1;  C  746 :
      }  C  747 :}1139
   }  C  748 :}1140
  C  749 :
   return NULL;      // There's no match  C  750 :
}  C  751 :}1141
  C  752 :
  C  753 :
#endif  C  754 :E
 C  754 :
#include <string.h> A   15 :
 ////////////////////////////////////////////////////////////////////////////  E    1 :I4
////        (C) Copyright 1996,2003 Custom Computer Services            ////  E    2 :
//// This source code may only be used by licensed users of the CCS C   ////  E    3 :
//// compiler.  This source code may only be distributed to other       ////  E    4 :
//// licensed users of the CCS C compiler.  No other use, reproduction  ////  E    5 :
//// or distribution is permitted without written permission.           ////  E    6 :
//// Derivative programs created using this software in object code     ////  E    7 :
//// form are not restricted in any way.                                ////  E    8 :
////////////////////////////////////////////////////////////////////////////  E    9 :
  E   10 :
#ifndef _STRING  E   11 :
#define _STRING  E   12 :
#include <stddef.h>  E   13 :
#include <ctype.h>  E   14 :
  E   15 :
  E   16 :
  E   17 :
//////////////////////////////////////////////  E   18 :
//// Uncomment the following define to    ////  E   19 :
//// allow some functions to use a        ////  E   20 :
//// quicker algorithm, but use more ROM  ////  E   21 :
////                                      ////  E   22 :
//// #define FASTER_BUT_MORE_ROM          ////  E   23 :
//////////////////////////////////////////////  E   24 :
  E   25 :
  E   26 :
  E   27 :
/*Copying functions*/  E   28 :
/* standard template:  E   29 :
   void *memmove(void *s1, void *s2, size_t n).  E   30 :
   Copies max of n characters safely (not following ending '\0')  E   31 :
   from s2 in s1; if s2 has less than n characters, appends 0 */  E   32 :
  E   33 :
char *memmove(void *s1,char *s2,size_t n)  E   34 :
{  E   35 :
   char *sc1;  E   36 :
   char *sc2;  E   37 :
   sc1=s1;  E   38 :
   sc2=s2;  E   39 :
   if(sc2<sc1 && sc1 <sc2 +n)  E   40 :
      for(sc1+=n,sc2+=n;0<n;--n)  E   41 :
         *--sc1=*--sc2;  E   42 :
   else  E   43 :
      for(;0<n;--n)  E   44 :
         *sc1++=*sc2++;  E   45 :
  return s1;  E   46 :
  }  E   47 :
  E   48 :
/* compiler ignored the name 'strcpy()'; perhaps, it's reserved? E   49 :
   Standard template: char *strcpy(char *s1, const char *s2)  E   50 :
   copies the string s2 including the null character to s1*/  E   51 :
  E   52 :
char *strcopy(char *s1, char *s2)  E   53 :
{  E   54 :
  char *s;  E   55 :
  E   56 :
  for (s = s1; *s2 != 0; s++, s2++) {  E   57 :
	  *s = *s2;  E   58 :
  }  E   59 :
  *s = *s2;  E   60 :
  return(s1);  E   61 :
}  E   62 :
  E   63 :
/* standard template:  E   64 :
   char *strncpy(char *s1, const char *s2, size_t n).  E   65 :
   Copies max of n characters (not following ending '\0')  E   66 :
   from s2 in s1; if s2 has less than n characters, appends 0 */  E   67 :
  E   68 :
char *strncpy(char *s1, char *s2, size_t n)  E   69 :
{  E   70 :
  char *s;  E   71 :
  E   72 :
  for (s = s1; n > 0 && *s2 != '\0'; n--)  E   73 :
     *s++ = *s2++;  E   74 :
  for (; n > 0; n--)  E   75 :
     *s++ = '\0';  E   76 :
  E   77 :
  return(s1);  E   78 :
}  E   79 :
/***********************************************************/  E   80 :
  E   81 :
/*concatenation functions*/  E   82 :
/* standard template: char *strcat(char *s1, const char *s2)  E   83 :
appends s2 to s1*/  E   84 :
  E   85 :
char *strcat(char *s1, char *s2)  E   86 :
{  E   87 :
   char *s;  E   88 :
  E   89 :
   for (s = s1; *s != '\0'; ++s);  E   90 :
   while(*s2 != '\0')  E   91 :
   {  E   92 :
      *s = *s2;  E   93 :
      ++s;  E   94 :
      ++s2;  E   95 :
   }  E   96 :
  E   97 :
   *s = '\0';  E   98 :
   return(s1);  E   99 :
}  E  100 :
/* standard template: char *strncat(char *s1, char *s2,size_t n)  E  101 :
appends not more than n characters from s2 to s1*/  E  102 :
  E  103 :
char *strncat(char *s1, char *s2, size_t n)  E  104 :
{  E  105 :
   char *s;  E  106 :
  E  107 :
   for (s = s1; *s != '\0'; ++s);  E  108 :
   while(*s2 != '\0' && 0<n)  E  109 :
   {  E  110 :
      *s = *s2;  E  111 :
      ++s;  E  112 :
      ++s2;  E  113 :
      --n;  E  114 :
   }  E  115 :
  E  116 :
   *s = '\0';  E  117 :
   return(s1);  E  118 :
}  E  119 :
  E  120 :
/***********************************************************/  E  121 :
  E  122 :
  E  123 :
/*comparison functions*/  E  124 :
/* standard template: signed int memcmp(void *s1, void *s2).  E  125 :
   Compares s1 & s2; returns -1 if s1<s2, 0 if s1=s2, 1 if s1>s2 */  E  126 :
  E  127 :
signed int memcmp(void * s1,char *s2,size_t n)  E  128 :
{  E  129 :
char *su1, *su2;  E  130 :
for(su1=s1, su2=s2; 0<n; ++su1, ++su2, --n)  E  131 :
{  E  132 :
   if(*su1!=*su2)  E  133 :
      return ((*su1<*su2)?1:+1);  E  134 :
}  E  135 :
return 0;  E  136 :
}  E  137 :
  E  138 :
/* standard template: int strcmp(const char *s1, const char *s2).  E  139 :
   Compares s1 & s2; returns -1 if s1<s2, 0 if s1=s2, 1 if s1>s2 */  E  140 :
  E  141 :
signed int strcmp(char *s1, char *s2)  E  142 :
{  E  143 :
   for (; *s1 == *s2; s1++, s2++)  E  144 :
      if (*s1 == '\0')  E  145 :
         return(0);  E  146 :
   return((*s1 < *s2) ?-1: 1);  E  147 :
}  E  148 :
/* standard template: int strcoll(const char *s1, const char *s2).  E  149 :
   Compares s1 & s2; returns -1 if s1<s2, 0 if s1=s2, 1 if s1>s2 */  E  150 :
  E  151 :
signed int strcoll(char *s1, char *s2)  E  152 :
{  E  153 :
   for (; *s1 == *s2; s1++, s2++)  E  154 :
      if (*s1 == '\0')  E  155 :
         return(0);  E  156 :
   return((*s1 < *s2) ?-1: 1);  E  157 :
}  E  158 :
  E  159 :
/* standard template:  E  160 :
   int strncmp(const char *s1, const char *s2, size_t n).  E  161 :
   Compares max of n characters (not following 0) from s1 to s2;  E  162 :
   returns same as strcmp */  E  163 :
  E  164 :
signed int strncmp(char *s1, char *s2, size_t n)  E  165 :
{  E  166 :
   for (; n > 0; s1++, s2++, n--)  E  167 :
      if (*s1 != *s2)  E  168 :
         return((*s1 <*s2) ?-1: 1);  E  169 :
      else if (*s1 == '\0')  E  170 :
         return(0);  E  171 :
   return(0);  E  172 :
}  E  173 :
/* standard template:  E  174 :
   int strxfrm(const char *s1, const char *s2, size_t n).  E  175 :
   transforms maximum of n characters from s2 and places them into s1*/  E  176 :
size_t strxfrm(char *s1, char *s2, size_t n)  E  177 :
{  E  178 :
  char *s;  E  179 :
  int n1;  E  180 :
  n1=n;  E  181 :
  for (s = s1; n > 0 && *s2 != '\0'; n--)  E  182 :
     *s++ = *s2++;  E  183 :
  for (; n > 0; n--)  E  184 :
     *s++ = '\0';  E  185 :
  E  186 :
  return(n1);  E  187 :
}  E  188 :
  E  189 :
  E  190 :
  E  191 :
  E  192 :
  E  193 :
/***********************************************************/  E  194 :
/*Search functions*/  E  195 :
/* standard template: void *memchr(const char *s, int c).  E  196 :
   Finds first occurrence of c in n characters of s */  E  197 :
  E  198 :
char *memchr(void *s,int c,size_t n)  E  199 :
{  E  200 :
   char uc;  E  201 :
   char *su;  E  202 :
   uc=c;  E  203 :
   for(su=s;0<n;++su,--n)  E  204 :
      if(*su==uc)  E  205 :
      return su;  E  206 :
   return NULL;  E  207 :
}  E  208 :
  E  209 :
/* standard template: char *strchr(const char *s, int c).  E  210 :
   Finds first occurrence of c in s */  E  211 :
  E  212 :
char *strchr(char *s, int c)  E  213 :
{  E  214 :
   for (; *s != c; s++)  E  215 :
      if (*s == '\0')  E  216 :
         return(0);  E  217 :
   return(s);  E  218 :
}  E  219 :
/* standard template:  E  220 :
   size_t strcspn(const char *s1, const char *s2).  E  221 :
   Computes length of max initial segment of s1 that  E  222 :
   consists entirely of characters NOT from s2*/  E  223 :
  E  224 :
int *strcspn(char *s1, char *s2)  E  225 :
{  E  226 :
   char *sc1, *sc2;  E  227 :
  E  228 :
   for (sc1 = s1; *sc1 != 0; sc1++)  E  229 :
      for (sc2 = s2; *sc2 != 0; sc2++)  E  230 :
         if (*sc1 == *sc2)  E  231 :
            return(sc1 - s1);  E  232 :
   return(sc1 - s1);  E  233 :
}  E  234 :
/* standard template:  E  235 :
   char *strpbrk(const char *s1, const char *s2).  E  236 :
   Locates first occurence of any character from s2 in s1;  E  237 :
   returns s1 if s2 is empty string */  E  238 :
  E  239 :
char *strpbrk(char *s1, char *s2)  E  240 :
{  E  241 :
   char *sc1, *sc2;  E  242 :
  E  243 :
   for (sc1 = s1; *sc1 != 0; sc1++)  E  244 :
      for (sc2 = s2; *sc2 != 0; sc2++)  E  245 :
         if (*sc1 == *sc2)  E  246 :
            return(sc1);  E  247 :
   return(0);  E  248 :
}  E  249 :
  E  250 :
  E  251 :
/* standard template: char *strrchr(const char *s, int c).  E  252 :
   Finds last occurrence of c in s */  E  253 :
  E  254 :
char *strrchr(char *s, int c)  E  255 :
{  E  256 :
   char *p;  E  257 :
  E  258 :
   for (p = 0; ; s++)  E  259 :
   {  E  260 :
      if (*s == c)  E  261 :
         p = s;  E  262 :
      if (*s == '\0')  E  263 :
         return(p);  E  264 :
   }  E  265 :
}  E  266 :
/* computes length of max initial segment of s1 consisting  E  267 :
   entirely of characters from s2 */  E  268 :
  E  269 :
int *strspn(char *s1, char *s2)  E  270 :
{  E  271 :
   char *sc1, *sc2;  E  272 :
  E  273 :
   for (sc1 = s1; *sc1 != 0; sc1++)  E  274 :
      for (sc2 = s2; ; sc2++)  E  275 :
	 if (*sc2 == '\0')  E  276 :
	    return(sc1 - s1);  E  277 :
         else if (*sc1 == *sc2)  E  278 :
            break;  E  279 :
   return(sc1 - s1);  E  280 :
}  E  281 :
/* standard template:  E  282 :
   char *strstr(const char *s1, const char *s2);  E  283 :
   Locates first occurence of character sequence s2 in s1;  E  284 :
   returns 0 if s2 is empty string  E  285 :
  E  286 :
   Uncomment #define FASTER_BUT_MORE_ROM at the top of the  E  287 :
   file to use the faster algorithm */  E  288 :
char *strstr(char *s1, char *s2)  E  289 :
{  E  290 :
	char *s, *t;  E  291 :
  E  292 :
   #ifdef FASTER_BUT_MORE_ROM  E  293 :
   if (*s2 == '\0')  E  294 :
         return(s1);  E  295 :
   #endif  E  296 :
  E  297 :
	while (*s1)  E  298 :
   {  E  299 :
      for(s = s1, t = s2; *t && *s == *t; ++s, ++t);  E  300 :
  E  301 :
		if (*t == '\0')  E  302 :
			return s1;  E  303 :
      ++s1;  E  304 :
      #ifdef FASTER_BUT_MORE_ROM  E  305 :
         while(*s1 != '\0' && *s1 != *s2)  E  306 :
            ++s1;  E  307 :
      #endif  E  308 :
	}  E  309 :
	return 0;  E  310 :
}  E  311 :
  E  312 :
/* standard template: char *strtok(char *s1, const char *s2).  E  313 :
  E  314 :
   Finds next token in s1 delimited by a character from separator  E  315 :
   string s2 (which can be different from call to call).  First call  E  316 :
   starts at beginning of s1 searching for first character NOT  E  317 :
   contained in s2; returns 0 if none is found.  E  318 :
   If one is found, it is the start of first token (return value).  E  319 :
   Function then searches from there for a character contained in s2.  E  320 :
   If none is found, current token extends to end of s1, and subsequent  E  321 :
   searches for a token will return 0.  If one is found, it is  E  322 :
   overwritten by '\0', which terminates current token.  Function saves  E  323 :
   pointer to following character from which next search will start.  E  324 :
   Each subsequent call, with 0 as first argument, starts searching  E  325 :
   from saved pointer */  E  326 :
  E  327 :
char *strtok(char *s1, char *s2)  E  328 :
{  E  329 :
   char *beg, *end;  E  330 :
   static char *save;  E  331 :
  E  332 :
   beg = (s1)?s1: save;  E  333 :
   beg += strspn(beg, s2);  E  334 :
   if (*beg == '\0')  E  335 :
   {  E  336 :
      *save = ' ';  E  337 :
      return(0);  E  338 :
   }  E  339 :
   end = strpbrk(beg, s2);  E  340 :
   if (*end != '\0')  E  341 :
   {  E  342 :
      *end = '\0';  E  343 :
      end++;  E  344 :
   }  E  345 :
   save = end;  E  346 :
   return(beg);  E  347 :
}  E  348 :
  E  349 :
/*****************************************************************/  E  350 :
/*Miscellaneous functions*/  E  351 :
/* standard template  E  352 :
maps error number in errnum to an error message string  E  353 :
Returns: Pointer to string  E  354 :
*/  E  355 :
#ifdef _ERRNO  E  356 :
char * strerror(int errnum)  E  357 :
{  E  358 :
char s[15];  E  359 :
switch( errnum)  E  360 :
{  E  361 :
case 0:  E  362 :
   strcpy(s,"no errors");  E  363 :
   return s;  E  364 :
case EDOM :  E  365 :
   strcpy(s,"domain error");  E  366 :
   return s;  E  367 :
case ERANGE:  E  368 :
   strcpy(s,"range error");  E  369 :
   return s;  E  370 :
}  E  371 :
}  E  372 :
#ENDIF  E  373 :
/* standard template: size_t strlen(const char *s).  E  374 :
   Computes length of s1 (preceding terminating 0) */  E  375 :
  E  376 :
int *strlen(char *s)  E  377 :
{  E  378 :
   char *sc;  E  379 :
  E  380 :
   for (sc = s; *sc != 0; sc++);  E  381 :
   return(sc - s);  E  382 :
}  E  383 :
  E  384 :
/* standard template: size_t stricmp(const char *s1, const char *s2).  E  385 :
   Compares s1 to s2 ignoring case (upper vs. lower) */  E  386 :
  E  387 :
signed int stricmp(char *s1, char *s2)  E  388 :
{  E  389 :
 for(; *s1==*s2||(isalpha(*s1)&&isalpha(*s2)&&(*s1==*s2+32||*s2==*s1+32));  E  390 :
    s1++, s2++)  E  391 :
    if (*s1 == '\0')  E  392 :
       return(0);  E  393 :
 return((*s1 < *s2) ?-1: 1);  E  394 :
}  E  395 :
  E  396 :
  E  397 :
/* standard template: char *strlwr(char *s).  E  398 :
   Replaces uppercase letters by lowercase;  E  399 :
   returns pointer to new string s */  E  400 :
  E  401 :
char *strlwr(char *s)  E  402 :
{  E  403 :
   char *p;  E  404 :
  E  405 :
   for (p = s; *p != '\0'; p++)  E  406 :
      if (*p >= 'A' && *p <='Z')  E  407 :
         *p += 'a' - 'A';  E  408 :
   return(s);  E  409 :
}  E  410 :
  E  411 :
  E  412 :
/************************************************************/  E  413 :
  E  414 :
  E  415 :
#endif  E  416 :E
 E  416 :
#include "freq_gen.h" A   16 :
 /****************************************************************************/  G    1 :I6
/* Copyright 2004 MBARI.                                                    */  G    2 :
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */  G    3 :
/* All rights reserved.                                                     */  G    4 :
/****************************************************************************/  G    5 :
#ifndef FREQ_GEN_H  G    6 :
#define FREQ_GEN_H  G    7 :
  G    8 :
#define FREQ_GEN_VERSION 0  G    9 :
#define FREQ_GEN_SUB_VERSION 40  G   10 :
  G   11 :
/*  G   12 :
    The ticks per second is based on an 8-bit counter over flowing that  G   13 :
    is driven by a 4 Mhz ocsillator prescaled by 4 and postscaled by 64.  G   14 :
  G   15 :
    TICKS_PER_SEC = (OSC_CLK) / (PRESCALE) / (POSTSCALE) / (8_BIT_OVERFLOW)  G   16 :
                  = 3580000   /     4      /      64     /      256  G   17 :
                  = 54.62646484375  G   18 :
    FREQ_REM_NUMERATOR  = 0.62646484375 * FREQ_REM_DENOMINATOR  G   19 :
*/  G   20 :
#define TICKS_PER_SEC           55  G   21 :
#define FREQ_DURATION           TICKS_PER_SEC  G   22 :
#define FREQ_REM_NUMERATOR      1283  G   23 :
#define FREQ_REM_DENOMINATOR    2048  G   24 :
  G   25 :
#define PORTC_CLEAR_ALL         0b00000000  G   26 :
  G   27 :
#endif  G   28 :E
 G   28 :
  A   17 :
/********************************** GLOBS ***********************************/  A   18 :
  A   19 :
long rtccTicks = 0;  A   20 :V284
int freqDuration = 0;  A   21 :V285
int freqEnabled = FALSE;  A   22 :V286
int magneticEnabled = TRUE;  A   23 :V287
int currentEnabled = TRUE;  A   24 :V288
char enabled[] = "enabled";  A   25 :V289
char disabled[] = "disabled";  A   26 :V290
  A   27 :
/********************************** GLOBS ***********************************/  A   28 :
  A   29 :
#INT_RTCC  A   30 :
rtccIsr()  A   31 :f405
{  A   32 :{1143
    if ( !freqEnabled )  A   33 :
    {  A   34 :
        rtccTicks = 0;  A   35 :{1147
        // stay in process commands   A   36 :
        freqDuration = 0;  A   37 :
        return;  A   38 :
    }  A   39 :}1148
  A   40 :
    rtccTicks++;  A   41 :
  A   42 :
    if ( FREQ_DURATION == rtccTicks )  A   43 :
    {  A   44 :
        rtccTicks = 0;  A   45 :{1151
        ++freqDuration;  A   46 :
    }  A   47 :}1152
}  A   48 :}1153
  A   49 :
void processCmds()  A   50 :f406
{  A   51 :{1155
    int b, c;  A   52 :V293V294
    static int old_b = 1;  A   53 :V295
  A   54 :
    b = input(PIN_B0);  A   55 :
    //b = 0x00;  A   56 :
  A   57 :
    if (b!=old_b && !b && !freqEnabled)  A   58 :
    {  A   59 :
		freqEnabled = TRUE;  A   60 :{1165
    }  A   61 :}1166
  A   62 :
    else if (b!=old_b && b && freqEnabled)  A   63 :
    {  A   64 :
		freqEnabled = FALSE;  A   65 :{1174
    }  A   66 :}1175
  A   67 :
    if (b!=old_b || kbhit() )  A   68 :
    {  A   69 :
        if(b==old_b)          A   70 :{1180
        {  A   71 :
            c = getc();  A   72 :{1183
	        switch (c)  A   73 :
	        {  A   74 :
	            case '1':     A   75 :
	                freqEnabled = TRUE;  A   76 :
	                break;  A   77 :
	            case '2':     A   78 :
	                freqEnabled = FALSE;  A   79 :
	                break;  A   80 :
	            case 'm':     A   81 :
	                magneticEnabled = TRUE;  A   82 :
	                break;  A   83 :
	            case 'M':     A   84 :
	                magneticEnabled = FALSE;  A   85 :
	                break;  A   86 :
	            case 'c':     A   87 :
	                currentEnabled = TRUE;  A   88 :
	                break;  A   89 :
	            case 'C':     A   90 :
	                currentEnabled = FALSE;  A   91 :
	                break;  A   92 :
	        }  A   93 :
	        printf("\r\nFreqGen %s\r\n",freqEnabled?nabled:disabled);  A   94 :
	        printf("Magnetic %s\r\n",magneticEnabled?nabled:disabled);  A   95 :
	        printf("Current %s\r\n",currentEnabled?nabled:disabled);  A   96 :
	        printf("1 - enable freq gen\r\n");  A   97 :
	        printf("2 - disable freq gen\r\n");  A   98 :
	        printf("m - enable magnetic\r\n");  A   99 :
	        printf("M - disable magnetic\r\n");  A  100 :
	        printf("c - enable current\r\n");  A  101 :
	        printf("C - disable current\r\n");  A  102 :
	        printf("?- help menu\r\n");  A  103 :
	    }  A  104 :}1266
    }  A  105 :}1267
    old_b = b;  A  106 :
    return;  A  107 :
}  A  108 :}1268
  A  109 :
#inline  A  110 :
void magnetic1000()  A  111 :f419
{  A  112 :{1270
    //50% duty cycle  A  113 :
    // off  A  114 :
    delay_us(124);  A  115 :
    // wave up   A  116 :
    output_high(PIN_C0);  A  117 :
    delay_us(247);  A  118 :
    // off  A  119 :
    output_low(PIN_C0);  A  120 :
    delay_us(247);  A  121 :
    // wave down   A  122 :
    output_high(PIN_C1);  A  123 :
    delay_us(247);  A  124 :
    // off  A  125 :
    output_low(PIN_C1);  A  126 :
    delay_us(119);  A  127 :
}  A  128 :}1277
  A  129 :
#inline  A  130 :
void magnetic316()  A  131 :f420
{  A  132 :{1279
    // 75% duty cycle  A  133 :
    // off  A  134 :
    delay_us(193);  A  135 :
    // wave up   A  136 :
    output_high(PIN_C0);   A  137 :
    delay_us(1160);  A  138 :
    // off  A  139 :
    output_low(PIN_C0);  A  140 :
    delay_us(387);  A  141 :
    // wave down   A  142 :
    output_high(PIN_C1);   A  143 :
    delay_us(1160);   A  144 :
    // off  A  145 :
    output_low(PIN_C1);   A  146 :
    delay_us(189);  A  147 :
}  A  148 :}1284
  A  149 :
#inline  A  150 :
void magnetic100()  A  151 :f421
{  A  152 :{1286
    // 75% duty cycle  A  153 :
    // off  A  154 :
    delay_us(607);  A  155 :
    // wave up   A  156 :
    output_high(PIN_C0);  A  157 :
    delay_us(3641);  A  158 :
    // off  A  159 :
    output_low(PIN_C0);   A  160 :
    delay_us(1214);  A  161 :
    // wave down   A  162 :
    output_high(PIN_C1);  A  163 :
    delay_us(3641);   A  164 :
    // off  A  165 :
    output_low(PIN_C1);   A  166 :
    delay_us(603);  A  167 :
}  A  168 :}1292
  A  169 :
#inline  A  170 :
void current1000()  A  171 :f422
{  A  172 :{1294
    // wave up  A  173 :
    output_high(PIN_C3);   A  174 :
    delay_us(490);  A  175 :
    // wave down   A  176 :
    output_low(PIN_C3);   A  177 :
    delay_us(480);  A  178 :
}  A  179 :}1298
  A  180 :
#inline  A  181 :
void current316()  A  182 :f423
{  A  183 :{1300
    // wave up   A  184 :
    output_high(PIN_C3);   A  185 :
    delay_us(1540);  A  186 :
    // wave down   A  187 :
    output_low(PIN_C3);   A  188 :
    delay_us(1530);   A  189 :
}  A  190 :}1304
  A  191 :
#inline  A  192 :
void current100()  A  193 :f424
{  A  194 :{1306
    // wave up   A  195 :
    output_high(PIN_C3);  A  196 :
    delay_us(4990);  A  197 :
    // wave down   A  198 :
    output_low(PIN_C3);   A  199 :
    delay_us(4980);  A  200 :
}  A  201 :}1310
  A  202 :
main()  A  203 :f425
{  A  204 :{1312
    // setup port b bit 0 as input, 1 as output   A  205 :
    set_tris_b(0x02);  A  206 :
  A  207 :
    // setup port c bits 0-6 as outputs, 7 as input   A  208 :
    set_tris_c(0x80);  A  209 :
  A  210 :
    // init freq pins   A  211 :
    output_c(PORTC_CLEAR_ALL);  A  212 :
  A  213 :
    // setup the system clock   A  214 :
    setup_counters(RTCC_INTERNAL, RTCC_DIV_64);  A  215 :
  A  216 :
    // make sure interrupts are enabled   A  217 :
    enable_interrupts(INT_RTCC);  A  218 :
    enable_interrupts(GLOBAL);  A  219 :
  A  220 :
    // welcome message   A  221 :
    printf("Freq Gen version %d.%d\r\n",  A  222 :
            FREQ_GEN_VERSION, FREQ_GEN_SUB_VERSION);  A  223 :V316V317V319V320
  A  224 :
    // main loop   A  225 :
    for(;;)  A  226 :
    {  A  227 :
        // wait for things to settle down   A  228 :
        delay_ms(50);  A  229 :{1348
  A  230 :
        // process any queued cmds   A  231 :
        while ( freqDuration == 0 )  A  232 :
        processCmds();  A  233 :
  A  234 :
        if(magneticEnabled)  A  235 :
        {  A  236 :
            // Magnetic source output  A  237 :
            printf("Magnetic signal...\r\n");  A  238 :{1354
  A  239 :
	        // Turn on Red LED  A  240 :
	        output_high(PIN_B1);   A  241 :
	  A  242 :
	        // 1000 Hz Magnetic  A  243 :
	        while ( freqDuration == 1 )  A  244 :
				magnetic1000();                     A  245 :
	       A  246 :
		    // enable 7.5 uF capacitor  A  247 :
	    	output_high(PIN_C5);   A  248 :
	  A  249 :
	        // 316 Hz Magnetic   A  250 :
	        while ( freqDuration == 2 )  A  251 :
	            magnetic316();  A  252 :
	  A  253 :
		    // enable 60 uF capacitor  A  254 :
	        output_high(PIN_C4);   A  255 :
	  A  256 :
	        // 100 Hz Magnetic        A  257 :
	        while ( freqDuration == 3 )  A  258 :
				magnetic100();                     A  259 :
	              A  260 :
	        // Turn off Red LED  A  261 :
	        output_low(PIN_B1);   A  262 :
	  A  263 :
	        // Wait for big capacitors to drain  A  264 :
	        delay_ms(50);  A  265 :
	  A  266 :
	        // Disable Capacitors  A  267 :
	        output_low(PIN_C5);   A  268 :
			delay_ms(25);  A  269 :
	        output_low(PIN_C4);   A  270 :
	  A  271 :
	        // wait for things to settle down   A  272 :
	        delay_ms(25);  A  273 :
	    }  A  274 :}1373
  A  275 :
        // process any queued cmds   A  276 :
        while ( freqDuration <= 4 )  A  277 :
        	processCmds();  A  278 :
        while ( freqDuration == 5 )  A  279 :
            processCmds();  A  280 :
  A  281 :
        // reset if disabled  A  282 :
        if(!freqEnabled)  A  283 :
            continue;  A  284 :
  A  285 :
        if(currentEnabled)  A  286 :
        {  A  287 :
			// Current source output  A  288 :
	        printf("Curent Source signal...\r\n");  A  289 :{1384
	  A  290 :
	        // Turn on Red LED  A  291 :
	        output_high(PIN_B1);   A  292 :
	        delay_ms(25);  A  293 :
	  A  294 :
	        // Turn on power to current loop  A  295 :
	        output_high(PIN_C2);  A  296 :
	  A  297 :
	        // 1000 Hz Current  A  298 :
	        while ( freqDuration == 6 )  A  299 :
	            current1000();  A  300 :
	  A  301 :
	        // 316 Hz Current  A  302 :
	        while ( freqDuration == 7 )  A  303 :
	            current316();  A  304 :
	  A  305 :
	        // 100 Hz Current  A  306 :
	        while ( freqDuration == 8 )  A  307 :
	            current100();  A  308 :
	  A  309 :
	        // Turn off power to current loop  A  310 :
	        output_low(PIN_C2);   A  311 :
	        delay_ms(25);  A  312 :
	  A  313 :
	        // Turn off Red LED  A  314 :
	        output_low(PIN_B1);   A  315 :
   A  316 :
            // Wait for things to settle down   A  317 :
            delay_ms(25);  A  318 :
	    }  A  319 :}1403
  A  320 :
        // process any queued cmds   A  321 :
        while ( freqDuration <= 9 )  A  322 :
        	processCmds();  A  323 :
  A  324 :
        // Do it again !   A  325 :
        freqDuration = 0;  A  326 :
    }  A  327 :}1407
}  A  328 :}1408E
 A  328 :
