/*  @(#) $Header: /usr/local/scalawags_cvs/Scalawags/Frc2006/ifibyte.c,v 1.3 2006/02/08 04:42:47 abrown Exp $
 * Description: 
 *	ifibyte's are 8 bit numbers where the zero is set to 127.
 *	The max number is 255.  The min is 0.  This makes for very
 *	awkward math.  These routines convert ifibytes to int and
 *	back.
 *	If "reverse" is true, the max is 0 and min is 255.
 * Limitations:
 *	int2ifibyte() may lose info since ints are bigger than ifibytes.
 * Author:
 *	Allen Brown  http://brown.armoredpenguin.com/~abrown/contact.html
 */

#include <stdio.h>
#include "ifibyte.h"
#include "supportfuncs.h"
#define ZERO 128
#define MAXVAL 255
#define MINVAL 0
#if defined(FRC_NOCOPROCESSOR) || defined(FRC_COPROCESSOR)
  #define INLINE
#else
  #define INLINE inline
#endif

/* ---------------------------------------------------------------- */
INLINE int ifibyte2int(unsigned char ifibyte, unsigned char reverse)
{
  int value;

  if( reverse )
    {
      value = ZERO - (int)ifibyte;
    }
  else
    {
      value = (int)ifibyte - ZERO;
    }
  return( value );
} // ifibyte2int()


INLINE unsigned char int2ifibyte(int value, unsigned char reverse)
{
  unsigned char ifibyte;
  int tempval;

  if( reverse )
    {
      tempval = ZERO-value;
    }
  else
    {
      tempval = ZERO+value;
    }
  if( (tempval > MAXVAL) || (tempval < MINVAL) )
    {
      reportstart(__FILE__,__LINE__,ERROR,"int2ifibyte out of range.  value=");
      reportint(value);
      reporttext(", reverse=");
      reportint((int)reverse);
      reporttext(".  Forced to ");
      if( tempval > MAXVAL )
	{
	  tempval = MAXVAL;
	}
      else if ( tempval < MINVAL )
	{
	  tempval = MINVAL;
	}
      reportint(tempval);
      reportend(".");
    }
  ifibyte = (unsigned char)(0xFF & tempval);
  return( ifibyte );
} // int2ifibyte()

/* ---------------------------------------------------------------- */
/*
 * $Log: ifibyte.c,v $
 * Revision 1.3  2006/02/08 04:42:47  abrown
 * MPLab doesn't support inline keyword.
 *
 * Revision 1.2  2006/02/05 18:48:59  abrown
 * Make functions inline.
 *
 * Revision 1.1  2006/01/28 23:21:32  abrown
 * Create ifibyte to replace byte2int.
 *
 */


syntax highlighted by Code2HTML, v. 0.9.1