/*
 * This file is part of the Raisonance RC51 compiler demonstration
 * package, and may be freely distributed, as long as the entire demo package
 * is kept intact.
 */

/* 
 *FileName:	calc.c, the Raisonance RC51 Compiler Calculator Demo
 * Purpose:     to illustrate the effectiveness of the Raisonance C Compiler
 */
#include <stdio.h>

/* setup serial port control hardware (1200 BAUD @6MHz) */

/* 
 * Definitions and global values
 */
#define LENGTH 10
#define XOFF (13)

char mode,string[LENGTH];


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	FUNCtion atoi( char *s )
 * Purpose:	to convert an ASCII decimal string to an integer
 *   Input:	a pointer to the character string to be converted
 *Function:	ignore white space, get the sign and convert the ASCII string
 *  Output:	the signed integer value
 *
 *   Scope:	none
 */
int atoi( char *s ) {
	char sign;
	int n;

/* skip white space */
	for( ; *s == ' ' || *s == '\n' || *s == '\t'; s++);

/* establish sign */
        sign = 1;
	if(*s == '+' || *s == '-')
		sign = (*s++ == '+') ? 1 : -1;

/* compute decimal value */
	for(n=0; *s >= '0' && *s <= '9'; s++)
		   n = (n * 10) + (*s - '0');

	return( sign * n );

}	/* end of FUNCtion atoi() */


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	FUNCtion htoi()
 * Purpose:	convert an ASCII hexadecimal string to an integer value
 *   Input:	a pointer to the ASCII string
 *Function:	to skip white space and compute a HEX integer value
 *  Output:	an integer value base HEX
 *
 * History:	07/10/86 - ??? - version 1.0 (author unknown
 *
 *   Scope:	none
 */
int htoi( char *s ) {
	int n;

/* skip white space */
	for( ; *s == ' ' || *s == '\n' || *s == '\t'; s++);

/* compute hexadecimal value */
	for( n = 0;  ; s++ ) {
		if((*s >= '0') && (*s <= '9'))
			n = (n * 0x10) + (*s - '0');

		else if((*s >= 'a') && (*s <= 'f'))
                	n = (n * 0x10) + (*s - 'a' + 0xa);

		else if((*s >= 'A') && (*s <= 'F'))
                	n = (n * 0x10) + (*s - 'A' + 0xA);

		else return( n );
	}	/* of for ... */
}	/* end of htoi() */


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	FUNCtion getchar()
 * Purpose:	to get a character from the input stream
 *   Input:	none
 *Function:	wait for a keystroke, print it to the screen, and return it
 *  Output:	a single character
 *
 *   Scope:	putchar() - to print the current character
 */
int MyGetChar(void) {	/* was char */
	return getchar();	//return default getchar
}	/* end of FUNCtion MyGetChar() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	FUNCtion getline()
 * Purpose:	to read characters from the serial port
 *   Input:	a character string, and an integer of length
 *Function:	gets input from the serial port
 *  Output:	the number of characters actually read
 *
 *   Scope:	MyGetChar(), putchar() 
 */
int getline( char * s , int lim ) {
	int c, i = 0;

	while (--lim > 0 && (c=MyGetChar()) != XOFF && c != '\n')
		s[i++] = c;

	if (c == '\n')  s[i++] = c;

	s[i] = '\0';
	putchar ( 10 );
	return ( i );

}	/* end of FUNCtion getline() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	FUNCtion getop()
 * Purpose:	to define, capture and return an operand
 *   Input:	none
 *Function:	prompt the user and process for a valid operand
 *  Output:	an integer value
 *
 *   Scope:	printf(), getline(), atoi(), htoi();
 */
int getop(char operand) {
	printf("\nEnter operand #%c: ",operand);
	getline( string , LENGTH );
	return((mode == 'D') ? atoi(string) : htoi(string));
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	FUNCtion upper()
 * Purpose:	up shift a character
 *   Input:	an ASCII character value
 *Function:	range test and UPSHIFT if lower case
 *  Output:	and unsigned character in the range of 'A' to 'Z'
 *
 *   Scope:	none
 */
unsigned char upper(unsigned char c) {
   if (c >= 'a' && c <= 'z') return(c - 0x20);
   return(c);
}	/* end of FUNCtion upper() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    Name:	PROCedure main()
 * Purpose:	serves as the main procedure for this program
 *   Input:	none
 *Function:	execute and control program specific operat+ions
 *  Output:	none
 *
 *   Scope:	none
 */
void main(void) {
	int a,b,result;
	char op;

	printf("\nRC51 C Compilers Calculator Demo\n");
	while (1) {
		while (1) {	/* establish calculator mode */
			printf("\nEnter Decimal or Hexadecimal (D/H): ");
			mode = upper(MyGetChar());

			if (mode == 'D'|| mode == 'H')
				break; /* start calculator */
		}	/* of while ... */

		while (1) {	/* calculator procedure */
			putchar('\n');
			a = getop('1'); /* get operand-1 */

			printf("Enter operator: ");
			op = MyGetChar();
			if (op!='+' && op!='-' && op!='*' && op!='/') {
				printf("\nIllegal Operator\n");
				break;
			}

			b = getop('2'); /* get operand-2 */

			switch (op) {
				case '+': result=a+b;
					break;

				case '-': result=a-b;
					break;

				case '*': result=a*b;
					break;

				case '/': result=a/b;
					break;
			}	/* of switch on op */

			printf("    Result -> ");
			printf((mode == 'D') ? "%6d" : "%6x",result);

		}	/* end of calculator procedure */
	}	/* end of calculator demo */
}	/* end of main() routine */

/*
 * end of calc.c
 */
