#include<cdefBF527.h>
#include<ccblkfn.h>
#include<sys/exception.h>

#define SystemCLK	 100000		//Value in KHz
#define VCO_CoreCLK  1			//Value of VCO/CCLK - value can be 1, 2, 4, 8
#define VCO			 500000		//Value in KHz
#define CLKin		 25000

void ConfigurePLL(void);
void SetUpPortMuxing(void);
void SetUpInterrupts(void);
void SetupandEnableHostPort(void);


//write data in location 0xFF900060 for the Host to read data.
//For Host write operations, look for data in location 0xFF900060
EX_INTERRUPT_HANDLER (HostDMA);

void main()
{
	ConfigurePLL();
	SetUpPortMuxing();
	SetUpInterrupts();
	SetupandEnableHostPort();
	
	while(1);
}

void ConfigurePLL()
{
	*pSIC_IWR0 = 0x1;
	*pPLL_CTL = SET_MSEL(VCO / CLKin);
	ssync();
	idle();
	*pPLL_DIV = SET_SSEL(VCO / SystemCLK) | ((VCO_CoreCLK)&0x30);
	asm("ssync;");
}

void SetUpPortMuxing()
{
	// Enable HD0-HD15
	
	*pPORTH_FER = 0xFFFF;
	ssync();
	*pPORTH_MUX = 0x2A;	
	ssync();
	
	// Enable HCE, HWR, HRD,HADDR, HACK,  
	
	*pPORTG_FER = PG15 | PG14 | PG13 | PG12 | PG11;
	ssync();
	*pPORTG_MUX = 0x2800;
	ssync();
}

void SetUpInterrupts()
{
	
	register_handler(ik_ivg12,HostDMA);
	*pSIC_IMASK0 |= 0x10000000;
}

void SetupandEnableHostPort()
{
	*pHOST_CONTROL = EHR | EHW | 0x4 | 0x1;	//Enable Host Reads, Enable Host Writes, Data Size = 16, Host Port Enable
}

EX_INTERRUPT_HANDLER (HostDMA)
{
	*pDMA1_IRQ_STATUS |= 1;
	ssync();
	*pHOST_STATUS = 0x8;
}
