#include <io.h>
#include <system.h>
#include <stdio.h>
//write vga data
#define IOWR_DATA(base, offset, data) \
		IOWR_16DIRECT(base, (offset) * 2, data)
//read vga data
#define IORD_DATA(base, offset) \
		IORD_16DIRECT(base, (offset) * 2)

#define RD_SYNC 0
#define RD_start 1
#define WR_start 2
#define WR_readselects 3
#define RD_dx 2
#define RD_dy 3


int main()
{
	unsigned int blank = 0;

	//bottom right corner of pixel dump
	unsigned int xcoordinate = 0;
	unsigned int ycoordinate = 0;

	int deltaX;
	int deltaY;

	IOWR_DATA(VGA_BASE, WR_readselects, 1);

	for(;;){
		//while(!blank){
			//read from address 0 for vsync/blank status
			blank = IORD_DATA(VGA_BASE, RD_SYNC);
			//read from address 1 for x position of sample
			deltaX = IORD_DATA(GPIO_BASE, RD_dx);
			//read from address 2 for y position of sample
			deltaY = IORD_DATA(GPIO_BASE, RD_dy);
		//}
		//reset
		if(IORD_GPIO(GPIO_BASE, 0)==1){
			xcoordinate = 0;
			ycoordinate = 0;
			continue;
		}


		xcoordinate+=deltaX;
		ycoordinate+=deltaY;

		//Boundary conditions
		if(xcoordinate<0 || ycoordinate<0 || xcoordinate>16368 || ycoordinate>16368){
			continue;
		}

		//write back new position
		IOWR_DATA(GPIO_BASE, WR_start, xcoordinate*256+ycoordinate);



	}
}
