/* TT8PCLIB.C

Tattletale Mod. 8 Library Functions Modified for PC Compatibility */

#include  "tt8pclib.h"

long g_sim_tick_rate = 1000;

/************************************************************/

short AtoDReadWord(short chan){

  chan++;
  return 16380;

}

/***********************************************************
  "delay_lp()" places the Model 8 in a short loop
  that produces no bus cycles.  Power consumption can be
  reduced when running out of flash memory by calling this function
  in a time delay loop.
************************************************************/

void delay_lp(void){
  DelayMilliSecs(500);
}


/************************************************************/

void DelayMilliSecs(unsigned long mS){

  delay(mS);
  return;

}

/************************************************************/

long GetTickRate(void){

  return g_sim_tick_rate;
}

/************************************************************/

long MilliSecs(void){

  return (TensMilliSecs() * 10);
}

/************************************************************/

void PChange(char port, unsigned short pin){

  printf(" Pin %c%d Changed", port,pin);
  return;
}

/************************************************************/


void PClear(char port, unsigned short pin){
/*
  printf(" Pin %c%d Cleared", port,pin);
*/
  return;
}

/************************************************************/


int Pin(char port, unsigned short pin){

//  printf(" Pin %c%d Checked", port,pin);
  return 0;
}

/************************************************************/

void PSet(char port, unsigned short pin){
/*
  printf(" Pin %c%d Set", port,pin);
*/
  return;
}

/************************************************************/

void SerInFlush(void){

  while( kbhit() ) getch();
  return;
}

/************************************************************/

short SerGetByte(void)
{

  return (short)getch();
}

/************************************************************/

short SerGetByteLP(void){

  return (short)getch();
}

/************************************************************/

short SerTimedGetByte(register long msTimeout){

  return (short)getch();
}

/************************************************************/

void SerPutByte(ushort theByte){

  putch((int)theByte);
  return;
}

/************************************************************/

void SerPutStr(char *str){

  puts(str);
  return;
}

/************************************************************/

bool SerByteAvail(void){

  return (bool)kbhit();
}

/************************************************************/

bool SetTickRate(long tickRate){

  g_sim_tick_rate = tickRate;
  return 0;
}

/************************************************************/

void SetTimeSecs(time_t secs, vfptr sync){

  return;
}

/************************************************************/

long SerSetBaud(long baud, long freq){

  return baud;
}

/************************************************************/

long SimSetFSys(long freq){

  printf("\nSystem freq. now: %ld", freq);
  return freq;
}

/************************************************************/

bool Sleep(long ticks){
  int t;

  for(t=0; t<ticks; t++){
    DelayMilliSecs(1000/g_sim_tick_rate);
  }
  return 0;
}

/************************************************************/

ulong TensMilliSecs(void){
  struct time t;
  long tms;

  gettime(&t);
/*
  printf("\nHour: %d", t.ti_hour);
  printf("\nMin: %d", t.ti_min);
  printf("\nSec: %d", t.ti_sec);
  printf("\nMsec: %d", t.ti_hund);
*/
  tms = (long)(t.ti_hour)*360000 + (long)(t.ti_min)*6000
       + (long)(t.ti_sec)*100  + (long)(t.ti_hund);

return tms;

}

/************************************************************/

void TPUClearInterrupt(int chan){

  return;
}

/************************************************************/

int TPUGetPin(short chan){

  return 1;
}

/************************************************************/

int TPUGetPinES(short chan, short edge){

  return 1;
}

/************************************************************/

ulong TPUGetTCR1(void){

  return 79200;
}

/************************************************************/

void TPUSetPin(short chan, short pinval){

  return;
}

/************************************************************/

void TPUSetupPWM(short chan, short pwmhi, short pwmper, short priority){

  return;
}
/************************************************************/

void SetTimeTM(struct tm *tp, vfptr sync){

  return;
}

/************************************************************/

void PSMemFreeAll(void){

  return;
}

/*************************************************************/

void *PSMemAllocAll(long *psmemsize){

  *psmemsize = 1000;
  return malloc(1000);
}

/**************************************************************/

UeeErr UeeReadBlock(ushort ueeSrcAddr, uchar *buffer, short len){

  return 0;
}

/**************************************************************/

UeeErr UeeWriteBlock(ushort ueeDestAddr, uchar *buffer, short len){

  return 0;
}

/**************************************************************/

XmdmErr XmodemSendMem(void *address, long length, ushort timeout){

  return 0;
}

/**************************************************************/

void MonCmd(char *cmdstr){

  return;
}


/*******************************************************************************
**	userio.c -- Tattletale Model 8 User I/O Interface Functions
**
**	Copyright (C) 1991-94 ONSET Computer Corp.  --jhg		All rights reserved.
**
**	Saturday, January 26, 1991
*******************************************************************************/


/*******************************************************************************
**	QueryDateTime		Query user for the Date and Time
**	
**	Return TRUE for all replies except ctrl-C.  Print prompt string (verbatim)
**	followed by the default reply (either the current system calander time if
**	defTime is TRUE or the calander time contained in the tm structure.
**	Returns result in struct tm, but does not set the system clock.
**	
**	NOTES:
**		1.	carriage return is not echoed.
**		2.	ctrl-C always return FALSE
**		3.	input format is compatable with CrossCut's Paste Time & Date (cmd-D)
**	
**	Typical Usage:
**		struct tm 	t;
**		if (! QueryDateTime("\nCurrent Date and Time", TRUE, &t))
**			... error processing for ctrl-C ...
**		SetTimeTM(&t, NULL);	\* set the system time *\
*******************************************************************************/
short QueryDateTime(ptr prompt, short defTime, struct tm *tm)
	{
	char		buf[40];
	char		def[40];
	time_t		secs;
	
	while (1)
		{
		printf(prompt);
		if (defTime)
			secs = time(NULL);
		else
			secs = mktime(tm);
			
		strftime(def, sizeof(def), "%m/%d/%y %H:%M:%S", localtime(&secs));
		
		printf(" [%s] ? ", def);

		if (! InputLine(buf, (short) sizeof(buf)))
			return (FALSE);

		if (! *buf)			/* just return keyed */
			strcpy(buf, def);	/* copy default and fall through */

		if (sscanf(buf, "%d%*c%d%*c%d%*c%d%*c%d%*c%d",
			&tm->tm_mon, &tm->tm_mday, &tm->tm_year,
			&tm->tm_hour, &tm->tm_min, &tm->tm_sec))
			{
			tm->tm_mon--;		/* adjust to c's 0 - 11 */
			return (TRUE);
			}
			
		if (*prompt != '\n')
			putchar('\n');	/* reprompt and try again */
		}
	
		return (TRUE);

	}	/* QueryDateTime() */


/*******************************************************************************
**	QueryYesNo		Query user for a yes or no reply.
**	
**	Return TRUE only for a Yes reply (which may be return only default if defYes
**	is TRUE).  Print prompt string (verbatim) followed by (Yes/No) followed by
**	default reply character inside square brackets, followed by a question mark.
**	
**	NOTES:
**		1.	carriage return is not echoed.
**		2.	ctrl-C always return FALSE
*******************************************************************************/
short QueryYesNo(ptr prompt, short defYes)
	{
	char	reply;
	char	buf[100];
	
	strncpy(buf, prompt, sizeof(buf) - 10);
	strcat(buf, " (Yes/No)");
	if (! QueryChar(buf, defYes ? 'Y' : 'N', "YN", &reply))
		return (FALSE);
	return ((short) (reply == 'Y'));
	
	}	/* QueryYesNo() */


/*******************************************************************************
**	QueryChar		Query user for a character reply.
**	
**	Return TRUE for all replies except ctrl-C.  Print prompt string (verbatim)
**	followed by optional default character inside square brackets, followed by
**	a question mark.  Return default character if just return pressed, otherwise
**	scan reply accepting only characters from scanSet.
**	
**	NOTES:
**		1.	carriage return is not echoed.
**		2. 	If the scanSet contains no lower case characters, the input will be
**			converted to upper case.
**		3.	Same as 2 but opposite case sense.
**	
*******************************************************************************/
short QueryChar(ptr prompt, short defChar, ptr scanSet, char *reply)
	{
	char	c, buf[2];
	short	forceUpper = FALSE, forceLower = FALSE;
	ptr		pss = scanSet;
	
	if (scanSet)
		{
		forceUpper = forceLower = TRUE;
		while (c = *pss++)
			if (isupper(c))
				forceLower = FALSE;
			else if (islower(c))
				forceUpper = FALSE;
		}

	while (1)
		{
		printf(prompt);
		if (defChar)
			printf(" [%c]", *reply = defChar);
		printf(" ? ");

		if (! InputLine(buf, (short) sizeof(buf)))
			return (FALSE);

		if ((! *buf) && defChar)
			return (TRUE);

		if (forceUpper)
			*reply = toupper(*buf);
		else if (forceLower)
			*reply = tolower(*buf);
		else
			*reply = *buf;

		if (scanSet)
			if (strchr(scanSet, *reply))
				return (TRUE);

		if (*prompt != '\n')
			putchar('\n');	/* reprompt and try again */
		}

	}	/* QueryChar() */



/*******************************************************************************
**	QueryNum		Query user for a numeric value.
**	
**	Return TRUE for all replies except ctrl-C.  Print prompt string (verbatim)
**	followed by optional default value (if defFmt in non-NULL and not a NULL
**	string) inside square brackets, followed by a question mark.  Return default
**	value if just return pressed, otherwise scan reply using scanFmt string.
**	
**	NOTES:
**		1. defFmt and scanFmt are scanf specifiers (e.g. "%ld", "lu", "%lx")
**		2. carriage return is not echoed.
*******************************************************************************/
short QueryNum(ptr prompt, ptr defFmt, ptr scanFmt, ulong *value)
	{
	char	buf[20];
	
	while (1)
		{
		printf(prompt);
		if (defFmt)
			if (*defFmt)
				{
				printf(" [");
				printf(defFmt, *value);
				printf("]");
				}
		printf(" ? ");

		if (! InputLine(buf, (short) sizeof(buf)))
			return (FALSE);

		if (! *buf)			/* just return keyed */
			return (TRUE);

		if (sscanf(buf, scanFmt, value))
			return (TRUE);

		if (*prompt != '\n')
			putchar('\n');	/* reprompt and try again */
		}

	}	/* QueryNum() */


/*******************************************************************************
**	InputLine		Input line from keyboard with minimal editing features
**	
**	Returns TRUE for all input except ctrl-C and returs NULL terminated string
**	in buffer.  Does not echo or include the terminating return character.
**	
**	Typical Usage:
**		char	myline[40];
**		if (! InputLine(myline, sizeof(myline)))
**			... error processing for ctrl-C ...
**	
**	
**	Edit Commands:
**		ctrl-C 0x03		cancel operation, return FALSE
**		ctrl-H 0x08		backspace and erase
**		ctrl-R 0x12		re-display current line
**		ctrl-U 0x15		clear line and start again
**	
*******************************************************************************/
short InputLine(ptr linebuf, short linelen)
	{
	short			i;
	char			ch;
	char			*bufptr = linebuf;
	
	while (1)
		{
		switch (ch = getchar())
			{
			case '\r' :
			case '\n' :
				*bufptr = '\0';
				return (TRUE);
				
			case BKSP :
				if (bufptr > linebuf)
					{
					bufptr--;
					putchar(BKSP);
					}
				break;

			case '\0' :
			case ABORT :
				*linebuf = '\0';
				return (FALSE);

			case REDISP :
				putchar('\n');
				for (i = 0; &linebuf[i] < bufptr; i++)
					putchar(linebuf[i]);
				break;

			case RESTRT :
				while (bufptr > linebuf)
					{
					bufptr--;
					putchar(BKSP);
					}
				break;

				*linebuf = '\0';
				return (FALSE);

			default :
				if (bufptr < linebuf + linelen-1)
					{
					*bufptr++ = ch;
					putchar(ch);
					}
				else
					{
					bufptr[-1] = ch;
					putchar(BKSP);
					putchar(ch);
					}
				break;
			}
		}
	
	}	/* InputLine() */


/*******************************************************************************
**	kbflush		Flush any keyboard characters pending and return TRUE if any
**				were available.
*******************************************************************************/
short kbflush(void)
	{
	short	avail = SerByteAvail();
	
	while (SerByteAvail())
		getchar();
		
	return (avail);
	
	}	/* kbflush() */

