#include <reg52.h>

#include "Page1.h"
#include "PalmChip.h"

volatile BOOLEAN Timer_bIndicated;
volatile BOOLEAN Timer_bIndicated2;
BOOLEAN Sys_bHighSpeed = TRUE;		// we will call SysNormalSpeed() first

#define _iTH0	0x6f	
#define _iTL0	0x78	

// Call this function immediately after startup
void SysNormalSpeed()
{
	if (!Sys_bHighSpeed)	return;

	EA = 0;
	Run_22Mhz();		// 22.2Mhz
	Timer_bIndicated = FALSE;
	Timer_bIndicated2 = FALSE;
	Sys_bHighSpeed = FALSE;

	TH0 = _iTH0;
    TL0 = _iTL0;
	Sys_iWholeSecond = 0;
	ET0 = 1;
	EA = 1;
}

void SysHighSpeed()
{
	if (Sys_bHighSpeed)	return;

	EA = 0;
	Run_46Mhz();		// 45.6Mhz
	Timer_bIndicated = FALSE;
	Timer_bIndicated2 = FALSE;
	Sys_bHighSpeed = TRUE;
	ET0 = 0;
	EA = 1;
}

void Timer0Interrupt() interrupt 1 //	using 1
{
	Sys_iWholeSecond ++;
	Timer_bIndicated2 = TRUE;
	if (Sys_iWholeSecond >= 50)
	{
		Sys_iWholeSecond = 0;
		Timer_bIndicated = TRUE;
	}
#ifdef CALL_IAX2
	Sys_lTicker += 20;
#endif

    // set the initial number again
	TH0 = _iTH0;
    TL0 = _iTL0;
}

// Call this function just before entering main loop
#ifdef _USE_RS232
void ControlInitialize()
{
	TMOD = 0x21;		// T/C0 function mode 1 timer; T/C1 function mode 2 timer for serial communication
	CKCON &= 0xe7;		// timer0, timer 1 count by clk/12
	TH0 = _iTH0;    // set timer 0 initial number, make it interrupt every 20ms
	TL0 = _iTL0;
	SCON0 = 0x50;		// set serial port in mode 1, and enable receive
	PCON |= 0x80;		// set SMOD = 1, so the serial port baudrate will be calculated by double
	TCON |= 0x50;		// start timer 0 and timer 1; TR0 = 1, TR1 = 1;
//	TR1 = 1;
	IP = 0;				// no interrupt has higher piority
//	PS0 = 1;			// Serial port interrpt is setting in higher piority
//	IE = 0x96;		// enable timer0, serial and external 1 interrupts; EA = 1, ES = 1, EX1 = 1, ET0 = 1
	ES0 = 1;
	ET0 = 1;
	EX1 = 1;
	EA = 1;
	// This function is called just before the main loop
}
#else
void ControlInitialize()
{
	TMOD = 0x21;		// T/C0 function mode 1 timer; T/C1 function mode 2 timer for serial communication
    CKCON &= 0xe7;		// timer0, timer 1 count by clk/12
	TH0 = _iTH0;		// set timer 0 initial number, make it interrupt every 20ms
	TL0 = _iTL0;
	TR0 = 1;
	IP = 0;				// no interrupt has higher piority
//    IE = 0x96;		// enable timer0, serial and external 1 interrupts; EA = 1, ES = 1, EX1 = 1, ET0 = 1
	ET0 = 1;
	EX1 = 1;
	EA = 1;
}
#endif
