#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "unistd.h"
#include "io.h"
#include "system.h"




#define tft_base (SDRAM_FB_BASE + SDRAM_FB_BASE / 2) // Where we want our frame buffers
#define  WIDTH 800
#define  HEIGHT 480

volatile uint8_t *fb = tft_base | 0x80000000;


void fb_init(uint8_t *framebuffer, int width, int height, int color_depth) {
	IOWR(MASTER_RD_CTRL_BASE, 0, 0);	//رʾ
	IOWR(MASTER_RD_CTRL_BASE, 1, framebuffer);	//ַָ
	IOWR(MASTER_RD_CTRL_BASE, 2, width*height*color_depth/8);	//ָС
	IOWR(MASTER_RD_CTRL_BASE, 0, 0x2);	//ʾ
}

void display_device_init() {
	fb_init(fb, WIDTH, HEIGHT, 8);
	memset(fb, 0, 800*480);
	memset(fb, 0, 800*480);
	memset(fb, 3, 800*480);
	memset(fb, 3, 800*480);
}

alt_u8 LCD_DrawPoint(alt_u16 x,alt_u16 y,alt_u16 color)
{
	*(fb + (HEIGHT - 1 - y)*WIDTH + x) = color;
	return 0;
}

alt_u8 LCD_Fill(alt_u16 sx,alt_u16 sy,alt_u16 ex,alt_u16 ey,alt_u16 color)
{
	alt_u16 i=0,j=0;
	//ͼ
	for(i=sx;i<=ex;i++)
	  for(j=sy;j<=ey;j++)
	      LCD_DrawPoint(i,j,color);

	return 0;
}



void create_wave_window(int x, int y, int width, int hight)
{
	//һõڵϢ
	int WXL = x;
	int WXR = x + width;
	int WYD = y;
	int WYU = y + hight;

	//ڶ
	LCD_Fill(WXL, WYD, WXR, WYU,0);//䲨ʾɫ
}

int main()
{
	display_device_init();

	create_wave_window(20,30,600,400);




  return 0;
}
