home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 September / PCWK996.iso / demo / wgelectr / pk51demo / files.2 / EXAMPLES / SAMPL517 / SAMPL517.C < prev    next >
C/C++ Source or Header  |  1995-09-07  |  6KB  |  252 lines

  1. /********************************************************************/
  2. /*                                                                  */
  3. /*    SAMPL517.C:  C51 COMPILER 80C517 Demo                         */
  4. /*                                                                  */
  5. /*    Copyright 1995 KEIL Software, Inc.                            */
  6. /*                                                                  */
  7. /********************************************************************/
  8.     
  9. /* The following program is a simple UPN calculator.                */
  10.  
  11. #include <80C517.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <reg517.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17.  
  18. union {
  19.   unsigned char c [4];
  20.   unsigned long l;
  21. } time = {0, 0, 0, 0};
  22.  
  23. code char menu[] =
  24. "\n"
  25. "+****************** C51 COMPILER UPN DEMO *********************+\n"
  26. "! Usage is like any HP calculator: a number goes to TOS        !\n"
  27. "! An operation or function forces the calculation              !\n"
  28. "+- operations --------------+- functions ----------------------+\n"
  29. "! add:      +   subtract: - ! SIN   COS    TAN    SQRT   LOG   !\n"
  30. "! multiply: *   divide:   / ! ASIN  ACOS   ATAN   EXP    LOG10 !\n"
  31. "+---------------------------+----------------------------------+\n";
  32.  
  33. /**************************************/
  34. /* Timer 0 interrupt service function */
  35. /**************************************/
  36.  
  37. /* executes once timer 0 overflow */
  38. /* maintains high part of usec counter */
  39.  
  40. #pragma NOMOD517          /* Interrupt Routine without 80C517 support */
  41. timer0() interrupt 1      /* no Register Bank using on simple routines */
  42. {
  43.   time.c[1]++;            /* increment on timer overflow */
  44. }
  45.  
  46. float val [4] = {0.0, 0.0, 0.0, 0.0};
  47. code char fchar [] = {'X', 'Y', 'Z', 'T'};
  48. code char error [] = "\nERROR *** Illegal Command";
  49.  
  50. #pragma MOD517            /* All 80C517 instructions */
  51.  
  52. dispvals ()  {
  53.   unsigned char i;
  54.  
  55.   printf517 ("Stack:\n");
  56.   for (i = 0; i != 4; i++)  {
  57.     printf517 ("%c: %G\n", fchar[i], val[i]);
  58.   }
  59. }
  60.  
  61. pushval (float f)  {
  62.   unsigned char i;
  63.  
  64.   for (i = 3; i > 0; i--) val [i] = val [i-1];
  65.   val[0] = f;
  66. }
  67.  
  68. float popval ()  {
  69.   unsigned char i;
  70.   float f;
  71.  
  72.   f = val[0];
  73.   for (i = 0; i < 3; i++) val [i] = val [i+1];
  74.   return(f);
  75. }
  76.  
  77. code struct  { unsigned char len;
  78.           char         *cmd;  }  fcttab []  =  {
  79.   { 1, "+" },
  80.   { 1, "-" },
  81.   { 1, "*" },
  82.   { 1, "/" },
  83.   { 3, "SIN" },
  84.   { 3, "COS" },
  85.   { 3, "TAN" },
  86.   { 4, "ASIN" },
  87.   { 4, "ACOS" },
  88.   { 4, "ATAN" },
  89.   { 3, "EXP" },
  90.   { 5, "LOG10" },
  91.   { 3, "LOG" },
  92.   { 4, "SQRT" },
  93.   { 0, "" } };
  94.  
  95. #define ADD   0
  96. #define SUB   1
  97. #define MUL   2
  98. #define DIV   3
  99. #define SIN   4
  100. #define COS   5
  101. #define TAN   6
  102. #define ASIN  7
  103. #define ACOS  8
  104. #define ATAN  9
  105. #define EXP   10
  106. #define LOG10 11
  107. #define LOG   12
  108. #define SQRT  13
  109.  
  110.  
  111. unsigned char findfct (char *p)  {
  112.   unsigned char i = 0;
  113.  
  114.   while (fcttab[i].len != 0)  {
  115.     if (memcmp (fcttab[i].cmd, p, fcttab[i].len) == 0) return (i);
  116.     i++;
  117.   }
  118.   return (0xFF);
  119. }
  120.  
  121.  
  122. /****************/
  123. /* main program */
  124. /****************/
  125.  
  126. main ()  {
  127.   char cmdbuf [15];
  128.   unsigned char i;
  129.  
  130.   /* initialize serial interface */
  131. #if 0
  132.   S0CON = 0x5A;   /* S0CON */
  133.   BD = 1;         /* internal Baudrate Generator */
  134.   PCON |= 0x80;   /* 9600 Baud @ 12 MHz */
  135. #endif
  136.   S1REL = 0xD9;      /* Reload Value */
  137.   S1CON = 0xB2;    /* Init Serial Interface */
  138.  
  139.   /* setup timer 0 interrupt */
  140.   TMOD = TMOD | 0x01;      /* select mode 1 */
  141.   TR0 = 1;                 /* start timer 0 */
  142.   ET0 = 1;                 /* enable timer 0 interrupt */
  143.  
  144.   EAL = 1;                 /* global interrupt enable */
  145.  
  146.   printf517 (menu);
  147.   while (1)  {
  148.     dispvals ();
  149.     printf517 ("\nCommand: ");
  150.     gets (cmdbuf, sizeof (cmdbuf));
  151.  
  152.     for (i = 0; i < sizeof (cmdbuf); i++)  {   /* Upper Chars */
  153.       cmdbuf[i] = toupper(cmdbuf[i]);
  154.     }
  155.  
  156.     for (i = 0; cmdbuf[i] == ' '; i++);        /* Skip Blanks */
  157.  
  158.     if (isdigit (cmdbuf[i]) ||
  159.        (cmdbuf[i] == '-' && isdigit (cmdbuf[i+1])) )  {
  160.       pushval (atof517 (&cmdbuf[i]));
  161.     }
  162.  
  163.     else  {
  164.       i = findfct (&cmdbuf[i]);                /* Find Command */
  165.  
  166.       EAL = 0;                                 /* Disable Int and timer */
  167.       TR0 = 0;                                 /* to avoid error        */
  168.       time.c[1] = 0;                           /* Set Time to Zero      */
  169.       TH0 = 0;
  170.       TL0 = 0;
  171.       EAL = 1;
  172.       TR0 = 1;
  173.  
  174.       switch (i)  {
  175.         case ADD:
  176.           pushval (popval () + popval ());
  177.           break;
  178.  
  179.         case SUB:
  180.           pushval (popval () - popval ());
  181.           break;
  182.  
  183.         case MUL:
  184.           pushval (popval () * popval ());
  185.           break;
  186.  
  187.         case DIV:
  188.           pushval (popval () / popval ());
  189.           break;
  190.  
  191.         case SIN:
  192.           pushval (sin517 (popval ()));
  193.           break;
  194.  
  195.         case COS:
  196.           pushval (cos517 (popval ()));
  197.           break;
  198.  
  199.         case TAN:
  200.           pushval (tan517 (popval ()));
  201.           break;
  202.  
  203.         case ASIN:
  204.           pushval (asin517 (popval ()));
  205.           break;
  206.  
  207.         case ACOS:
  208.           pushval (acos517 (popval ()));
  209.           break;
  210.  
  211.         case ATAN:
  212.           pushval (atan517 (popval ()));
  213.           break;
  214.             
  215.         case EXP:
  216.           pushval (exp517 (popval ()));
  217.           break;
  218.  
  219.         case LOG:
  220.           pushval (log517 (popval ()));
  221.           break;
  222.  
  223.         case LOG10:
  224.           pushval (log10517 (popval ()));
  225.           break;
  226.  
  227.         case SQRT:
  228.           pushval (sqrt517 (popval ()));
  229.           break;
  230.  
  231.         default:
  232.           printf517 (error);
  233.           printf517 (menu);
  234.           break;
  235.  
  236.       }
  237.  
  238.                                                /* Display execution time */
  239.       EAL = 0;                                 /* Disable Int and timer  */
  240.       TR0 = 0;                                 /* to avoid error         */
  241.       time.c[2] = TH0;                         /* Set Time to Zero       */
  242.       time.c[3] = TL0;
  243.  
  244.       printf517 ("Execution Time: %lu usec.\n", time.l);  
  245.  
  246.       EAL = 1;
  247.       TR0 = 1;
  248.     }
  249.   }
  250. }
  251.  
  252.