/***********************************************************************
	userblock_info.c
	Z-World, 2002

	This program reports on the size and capabilities of the ID/User
	Blocks.  In particular, it will report the version of the ID Block,
	the size of the ID and User Blocks, the size of the User Block's
	reserved for calibration constants etc. section, whether or not
	the ID/User blocks are mirrored, and the total amount of flash used
	by the ID/User Blocks.

	See userblock_clear.c for clearing the contents of the User Block
	(excepting the calibration constants reserved area and the ID Block).
***********************************************************************/

#class auto

// If it exists, use the Z-World reserved size that is explicitly set
//  for this controller in its hardware-specific library.
#ifndef ZWORLD_RESERVED_SIZE
// Otherwise, reserve 2K bytes of space below top of ID/User Blocks.
// This may be overprotective for boards with no calibration constants
//  or other Z-World installed information, but, better safe than sorry!
#define ZWORLD_RESERVED_SIZE 0x0800
#endif

void main(void)
{
	unsigned long available_size;
	unsigned long calib_size;
	unsigned long sysid_size;
	unsigned long total_size;

	printf("System ID Block version:  %d\n\n", SysIDBlock.tableVersion);

	if (SysIDBlock.tableVersion >= 3) {
		printf("Contains mirrored ID/User Blocks, which ensures that there is\n");
		printf("always a correct version of User Block information written.\n\n");
	} else {
		printf("Does not contain mirrored ID/User Blocks.  A power cycle while\n");
		printf("the User Block is being written can leave the User Block in an\n");
		printf("inconsistent state.\n\n");
	}

	if (SysIDBlock.tableVersion < 1) {
		available_size = GetIDBlockSize() * 4096ul;
		calib_size = sysid_size = total_size = 0ul;
   	printf("Simulated User Block requires 0x%08LX (%ld) bytes of flash.\n\n",
   	       16384ul, 16384ul);
	} else if (SysIDBlock.tableVersion == 1) {
		available_size = GetIDBlockSize() * 4096ul;
		calib_size = total_size = 0ul;
   	sysid_size = SysIDBlock.idBlockSize;
   	printf("Simulated User Block requires 0x%08LX (%ld) bytes of flash.\n\n",
   	       16384ul, 16384ul);
	} else if (SysIDBlock.tableVersion == 2) {
   	sysid_size = SysIDBlock.idBlockSize;
		calib_size = (unsigned long) (ZWORLD_RESERVED_SIZE) - sysid_size;
		total_size = GetIDBlockSize() * 4096ul - sysid_size;
		available_size = total_size - calib_size;
		printf("ID/User Blocks consume 0x%08LX (%ld) bytes of flash.\n\n",
   	       sysid_size + total_size, sysid_size + total_size);
	} else {	// SysIDBlock.tableVersion >= 3
   	sysid_size = SysIDBlock.idBlockSize;
		calib_size = (unsigned long) (ZWORLD_RESERVED_SIZE) - sysid_size;
		total_size = GetIDBlockSize() * 4096ul - sysid_size;
		available_size = total_size - calib_size;
		printf("ID/User Blocks consume 0x%08LX (%ld) bytes of flash.\n\n",
   	       (sysid_size + total_size) << 1ul, (sysid_size + total_size) << 1ul);
	}

	printf("System ID Block size:                            0x%08LX (%ld).\n",
	       sysid_size, sysid_size);
	printf("User Block size:                                 0x%08LX (%ld).\n",
	       total_size, total_size);
	printf("Sum of ID Block plus User Block sizes:           0x%08LX (%ld).\n\n",
	       sysid_size + total_size, sysid_size + total_size);
	printf("Available (possibly simulated) User Block size:  0x%08LX (%ld).\n",
	       available_size, available_size);
	printf("Reserved (for constants, etc.) User Block size:  0x%08LX (%ld).\n",
	       calib_size, calib_size);
}