/******************************************************************************\
**	RTCSyncTest.c 	
**	
*****************************************************************************
**	Tests the new patchable RTCSyncStart for the CF2 Real-Time Clock
*****************************************************************************
**	2003/01/28
\******************************************************************************/
#include	<cfxpico.h>		// Persistor PicoDOS Definitions

#include	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<signal.h>
#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#include	<dirent.h>		// PicoDOS POSIX-like Directory Access Defines
#include	<dosdrive.h>	// PicoDOS DOS Drive and Directory Definitions
#include	<fcntl.h>		// PicoDOS POSIX-like File Access Definitions
#include	<stat.h>		// PicoDOS POSIX-like File Status Definitions
#include	<termios.h>		// PicoDOS POSIX-like Terminal I/O Definitions
#include	<unistd.h>		// PicoDOS POSIX-like UNIX Function Definitions


/******************************************************************************\
**	CustomRTCSyncStart	// Simple custom patch that syncs on IRQ5 going low
\******************************************************************************/
void CustomRTCSyncStart(void);
void CustomRTCSyncStart(void)
	{
							// cprintf("RTCSync %d: waiting for PBM\n", ok);
	while (PIORead(IRQ5))	// wait for PBM button to be pushed
		;
							// cprintf("RTCSync: exiting\n");
	}	//____ RTCSync() ____//


/******************************************************************************\
**	RTCSyncTestCmd
\******************************************************************************/
char *RTCSyncTestCmd(CmdInfoPtr cip);
char *RTCSyncTestCmd(CmdInfoPtr cip)
	{
	static ulong	secs = 0x20000000;
	static ushort	ticks = 1234;
	ulong			snow;
	ushort			tnow;
	vfptr			savedRTCSyncStart;
	DosSwitch	QMsw = { "/-", '?', 0, 0 };		// help

	char *RTCSyncTestCmdHelpText =
		{
		"\n"
		"RTSYNC\n"
		"Tests the new patchable RTCSyncStart for the CF2 Real-Time Clock\n"
		"\n"
		};

	CmdExtractCIDosSwitches(cip, "?TV", &QMsw);
	if (QMsw.pos)
		{
		cprintf("%s\n", RTCSyncTestCmdHelpText);
		return 0;
		}

	cprintf("\nPush PBM TO sync to %lX:%X \n", secs, ticks);
	cdrain();

	savedRTCSyncStart = BIOSPatchInsert(RTCSyncStart, CustomRTCSyncStart);
	RTCSetTime(secs, ticks);	
	BIOSPatchInsert(RTCSyncStart, savedRTCSyncStart);

	RTCGetTime(&snow, &tnow);
	cprintf("Time reads %lX:%X \n", snow, tnow);
	execstr("DATE");

	// setup for next time through
	secs += 0x08000000;
	ticks += 1111;
	if (ticks >= 40000)
		ticks = 1234;
	
	return 0;
	
	}	//____ RTCSyncTestCmd() ____//





            
            