/* BRAMEXMP.C

   SBC1190 Example program

   This program is an example of how data can be stored in
   battery-backed RAM at a fixed address.

   In this example, the BRAM is enabled from 8000:0 to A000:0.
   This creates a 128k window where it is visible.
   Other addresses may be used as well.
   */

#include <dos.h>
#include <stdio.h>

#ifdef __TURBOC__
#ifndef outpw
#define outpw   outport
#endif
#endif

#define GCS0ST          0xFF80
#define GCS0SP          0xFF82



/* This structure contains all of the elements that will go in the
   battery-backed RAM. */

struct BRAM
{
	char string_a[20];
	int num_a;
	int num_b;
	float a_float;
};

char str[] = "Hello, world";

/* This function copies one far string to another.  It is only necessary
   if the program is written in small or medium memory model.  In
   compact or large memory model, the normal strcpy() function may
   be used.  */
fstrcpy(char far* dest, char far* src)
{
	int n;

	/* copy each element of the string */
	for (n=0; src[n]; n++)
		dest[n] = src[n];

	dest[n] = 0;    /* null-terminate the string */
}

main()
{
	struct BRAM far* bram_ptr;

	outpw(GCS0ST, 0x8002);  /* start address 8000:0,  2-wait states */
	outpw(GCS0SP, 0xA00A);  /* end address A000:0, memory device */

	/* the BRAM begins at address 8000:0000 */
	bram_ptr = (struct BRAM far *) 0x80000000;

	/* initialize the BRAM elements */

	fstrcpy(bram_ptr->string_a, str);

	bram_ptr->num_a = 0x5555;

	bram_ptr->num_b = 0xAAAA;

	bram_ptr->a_float = 3.14;

	strcpy(str, "another string"); /* this just wipes out the near
					string for demonstration purposes */

	fstrcpy(str, bram_ptr->string_a);

	printf("The string is %s\n", str);
	printf("num_a = %x\n", bram_ptr->num_a);
	printf("num_b = %x\n", bram_ptr->num_b);
	printf("a_float = %f\n", bram_ptr->a_float);
}