#include <stdio.h>
#include <flcd.h>

#define LCD_IO_BASE			0x90600000
#define GPIO_BASE			0x98700000
#define GPIO_PIN_DIR (*(volatile unsigned int *) (GPIO_BASE + 0x008))
#define GPIO_DATA_IN (*(volatile unsigned int *) (GPIO_BASE + 0x004))

void delay(unsigned int count);

int osd_main()
{
	int i,j;
	unsigned char *pFrameBuffer;
	unsigned int panel_width = 320, panel_height = 240;
	char buffer [33];
	int T_hh=0;
	int	T_mm=0;
	int T_ss=0;

	pFrameBuffer = malloc(panel_width * panel_height * 3 + 64);
	pFrameBuffer = (unsigned char *) (~0x3f & (0x3f + (unsigned) pFrameBuffer));

	// Set Frame Base
	*(unsigned int *) (LCD_IO_BASE + 0x10) = (unsigned int) pFrameBuffer;

	// Init LCD
	*(unsigned int *)(LCD_IO_BASE + 0x1C) &= ~1;
	*(unsigned int *)(LCD_IO_BASE + 0x1C) = 0;
	*(unsigned int *)(LCD_IO_BASE + 0x00) = 0x0605004c;
	*(unsigned int *)(LCD_IO_BASE + 0x04) = 0x010100ef;
	*(unsigned int *)(LCD_IO_BASE + 0x08) = 0x00003fc6;
	*(unsigned int *)(LCD_IO_BASE + 0x18) = 0;
	*(unsigned int *)(LCD_IO_BASE + 0x20) = 0x0f;

	// Enable LCD
	*(unsigned int *)(LCD_IO_BASE + 0x1c) = 0x0001b929;

	// Color test
	unsigned int * p = (unsigned int *) pFrameBuffer;
#if 0
	for(i=0; i< 320 * 240 ; i++)
		*(p + i )  = 0xF800;
	for(i=0; i< 320 * 240 ; i++)
		*(p + i )  = 0x07E0;
	for(i=0; i< 320 * 240 ; i++)
		*(p + i )  = 0x001F;
#endif
	// Clear Screen
	for(i=0; i< 320 * 240 ; i++)
		*(p + i )  = 0x0000;

	// Load OSD font
	for(i=0; i< 16*10 + 16* 26 + 3* 16 + 16  ; i++)
		*(unsigned int *) (LCD_IO_BASE + 0x8000 +i*4 )  = OSD_Font[i];

	OSD_fg_color(LCD_IO_BASE, 0xFF, 0x03, 0x03, 0x03);
 	OSD_bg_color(LCD_IO_BASE, 0x00, 0x00, 0xe0);
	OSD_On(LCD_IO_BASE);
	i=8;
	j=1;
	OSD_Pos(LCD_IO_BASE, 0,80);
	OSD_Scal(LCD_IO_BASE, 2,2 );
	OSD_Dim( LCD_IO_BASE, i, j);


	// clock emulation
	while(1)
	{
		if (T_ss >= 60)
		{
			T_mm++;
			T_ss = 0;
		}
		if (T_mm >= 60)
		{
			T_hh++;
		    T_mm = 0;
		}
		if (T_hh >= 24)
			T_hh = 0;

		sprintf(buffer, "%02d:%02d:%02d", T_hh, T_mm, T_ss);
		OSD_puts(buffer, 0);
		delay (400000);
		T_ss++;
	}
	return 0;
}

void delay(unsigned int count)
{
	for(;count>0;count--);
}
