/*****************************************************

	flashdem.c
	Z-World, Inc, 1999

	Example of using the WriteFlash function.

	This program demonstrates the use of the
	WriteFlash function by writing a block of data
	to a physical memory location in flash and then
	reading it with the xmem2root function.

	NOTE that this program writes to an arbitrary
	location in flash!  Doing this without careful
	thought may cause the running program to crash.
	 
******************************************************/

main()
{
	static char	buffer1[1024];				// buffer for data to write
	static char  buffer2[1024];				// buffer to read data back into
	static unsigned long physaddr;			// physical memory address to write to
		
	
	physaddr = 0x00010000ul;		// physical memory address (in flash)


	// clear buffer1	
	memset(buffer1, 0x00, 1024);

	// write buffer1 to memory
	printf("Writing buffer1 to flash\n");
	WriteFlash(physaddr, buffer1, 1024);

	// read data from memory into buffer2
	printf("Reading memory into buffer2\n");
	xmem2root(buffer2, physaddr, 1024);

	// compare the two buffers using string compare fcn strncmp
	//		strncmp returns 0 if strings are identical
	if (strncmp(buffer1, buffer2, 1024))
		printf("Error in write/read to XMEM\n");
	else
		printf("Buffers match\n\n");
			

	// set buffer1	to new value
	memset(buffer1, 0x55, 1024);

	// write buffer1 to memory
	printf("Writing buffer1 to flash\n");
	WriteFlash(physaddr, buffer1, 1024);
				
	// read data from memory into buffer2
	printf("Reading memory into buffer2\n");
	xmem2root(buffer2, physaddr, 1024);

	// compare the two buffers using string compare fcn strncmp
	//		strncmp returns 0 if strings are identical
	if (strncmp(buffer1, buffer2, 1024))
		printf("Error in write/read to XMEM\n");
	else
		printf("Buffers match\n\n");

}
