OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [Utils.cpp] - Diff between revs 63 and 462

Show entire file | Details | Blame | View Log

Rev 63 Rev 462
Line 26... Line 26...
 
 
// $Id: Utils.cpp 324 2009-03-07 09:42:52Z jeremy $
// $Id: Utils.cpp 324 2009-03-07 09:42:52Z jeremy $
 
 
#include "Utils.h"
#include "Utils.h"
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//!Utility to give the value of a hex char
//!Utility to give the value of a hex char
 
 
//! @param[in] ch  A character representing a hexadecimal digit. Done as -1,
//! @param[in] ch  A character representing a hexadecimal digit. Done as -1,
//!                for consistency with other character routines, which can
//!                for consistency with other character routines, which can
//!                use -1 as EOF.
//!                use -1 as EOF.
 
 
//! @return  The value of the hex character, or -1 if the character is
//! @return  The value of the hex character, or -1 if the character is
//!          invalid.
//!          invalid.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
uint8_t
uint8_t Utils::char2Hex(int c)
Utils::char2Hex (int  c)
 
{
{
  return  ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
  return  ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
          ((c >= '0') && (c <= '9')) ? c - '0' :
          ((c >= '0') && (c <= '9')) ? c - '0' :
          ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
          ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
 
 
}       // char2Hex ()
}       // char2Hex ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Utility mapping a value to hex character
//! Utility mapping a value to hex character
 
 
//! @param[in] d  A hexadecimal digit. Any non-hex digit returns a NULL char
//! @param[in] d  A hexadecimal digit. Any non-hex digit returns a NULL char
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
const char
const char Utils::hex2Char(uint8_t d)
Utils::hex2Char (uint8_t  d)
 
{
{
  static const char map [] = "0123456789abcdef"
  static const char map [] = "0123456789abcdef"
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
Line 76... Line 72...
 
 
  return  map[d];
  return  map[d];
 
 
}       // hex2Char ()
}       // hex2Char ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Convert a register to a hex digit string
//! Convert a register to a hex digit string
 
 
//! The supplied 32-bit value is converted to an 8 digit hex string. The
//! The supplied 32-bit value is converted to an 8 digit hex string. The
//! string is null terminated for convenience. The hex string follows the
//! string is null terminated for convenience. The hex string follows the
//! universal printing convention: most significant digit on the left.
//! universal printing convention: most significant digit on the left.
 
 
//! @param[in]  val  The value to convert
//! @param[in]  val  The value to convert
//! @param[out] buf  The buffer for the text string
//! @param[out] buf  The buffer for the text string
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void
void Utils::reg2Hex(uint32_t val, char *buf)
Utils::reg2Hex (uint32_t  val,
 
                char     *buf)
 
{
 
  for (int  n = 7; n >= 0; n--)
 
    {
    {
 
        for (int n = 7; n >= 0; n--) {
      buf[n] = hex2Char (val & 0xf);
      buf[n] = hex2Char (val & 0xf);
      val /= 16;
      val /= 16;
    }
    }
 
 
  buf[8] = 0;                    // Useful to terminate as string
  buf[8] = 0;                    // Useful to terminate as string
 
 
}       // reg2hex ()
}       // reg2hex ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Convert a hex digit string to a register value
//! Convert a hex digit string to a register value
 
 
//! The supplied 8 digit hex string follows the universal printing convention:
//! The supplied 8 digit hex string follows the universal printing convention:
//! most significant digit on the left. It is converted to a 32-bit value.
//! most significant digit on the left. It is converted to a 32-bit value.
 
 
//! @param[in] buf  The buffer with the hex string
//! @param[in] buf  The buffer with the hex string
 
 
//! @return  The value to convert
//! @return  The value to convert
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
uint32_t
uint32_t Utils::hex2Reg(char *buf)
Utils::hex2Reg (char *buf)
 
{
{
  uint32_t  val = 0;     // The result
  uint32_t  val = 0;     // The result
 
 
  for (int  n = 0; n < 8; n++)
        for (int n = 0; n < 8; n++) {
    {
 
      val = val * 16 + char2Hex (buf[n]);
      val = val * 16 + char2Hex (buf[n]);
    }
    }
 
 
  return val;
  return val;
 
 
}       // hex2reg ()
}       // hex2reg ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Convert an ASCII character string to pairs of hex digits
//! Convert an ASCII character string to pairs of hex digits
 
 
//! Both source and destination are null terminated.
//! Both source and destination are null terminated.
 
 
//! @param[out] dest  Buffer to store the hex digit pairs (null terminated)
//! @param[out] dest  Buffer to store the hex digit pairs (null terminated)
//! @param[in]  src   The ASCII string (null terminated)                      */
//! @param[in]  src   The ASCII string (null terminated)                      */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void
void Utils::ascii2Hex(char *dest, char *src)
Utils::ascii2Hex (char *dest,
 
                  char *src)
 
{
{
  int  i;
  int  i;
 
 
  // Step through converting the source string
  // Step through converting the source string
  for (i = 0; src[i] != '\0'; i++)
        for (i = 0; src[i] != '\0'; i++) {
    {
 
      char  ch = src[i];
      char  ch = src[i];
 
 
      dest[i * 2]     = hex2Char(ch >> 4 & 0xf);
      dest[i * 2]     = hex2Char(ch >> 4 & 0xf);
      dest[i * 2 + 1] = hex2Char(ch      & 0xf);
      dest[i * 2 + 1] = hex2Char(ch      & 0xf);
    }
    }
 
 
  dest[i * 2] = '\0';
  dest[i * 2] = '\0';
 
 
}       // ascii2hex ()
}       // ascii2hex ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Convert pairs of hex digits to an ASCII character string
//! Convert pairs of hex digits to an ASCII character string
 
 
//! Both source and destination are null terminated.
//! Both source and destination are null terminated.
 
 
//! @param[out] dest  The ASCII string (null terminated)
//! @param[out] dest  The ASCII string (null terminated)
//! @param[in]  src   Buffer holding the hex digit pairs (null terminated)
//! @param[in]  src   Buffer holding the hex digit pairs (null terminated)
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void
void Utils::hex2Ascii(char *dest, char *src)
Utils::hex2Ascii (char *dest,
 
                  char *src)
 
{
{
  int  i;
  int  i;
 
 
  // Step through convering the source hex digit pairs
  // Step through convering the source hex digit pairs
  for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++)
        for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++) {
    {
 
      dest[i] = ((char2Hex (src[i * 2]) & 0xf) << 4) |
      dest[i] = ((char2Hex (src[i * 2]) & 0xf) << 4) |
                 (char2Hex (src[i * 2 + 1]) & 0xf);
                 (char2Hex (src[i * 2 + 1]) & 0xf);
    }
    }
 
 
  dest[i] = '\0';
  dest[i] = '\0';
 
 
}       // hex2ascii ()
}       // hex2ascii ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! "Unescape" RSP binary data
//! "Unescape" RSP binary data
 
 
//! '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
//! '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
 
 
Line 193... Line 173...
//! @param[in] buf  The array of bytes to convert
//! @param[in] buf  The array of bytes to convert
//! @para[in]  len   The number of bytes to be converted
//! @para[in]  len   The number of bytes to be converted
 
 
//! @return  The number of bytes AFTER conversion
//! @return  The number of bytes AFTER conversion
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int
int Utils::rspUnescape(char *buf, int len)
Utils::rspUnescape (char *buf,
 
                    int   len)
 
{
{
  int  fromOffset = 0;           // Offset to source char
  int  fromOffset = 0;           // Offset to source char
  int  toOffset   = 0;           // Offset to dest char
  int  toOffset   = 0;           // Offset to dest char
 
 
  while (fromOffset < len)
        while (fromOffset < len) {
    {
 
      // Is it escaped
      // Is it escaped
      if ( '}' == buf[fromOffset])
                if ('}' == buf[fromOffset]) {
        {
 
          fromOffset++;
          fromOffset++;
          buf[toOffset] = buf[fromOffset] ^ 0x20;
          buf[toOffset] = buf[fromOffset] ^ 0x20;
        }
                } else {
      else
 
        {
 
          buf[toOffset] = buf[fromOffset];
          buf[toOffset] = buf[fromOffset];
        }
        }
 
 
      fromOffset++;
      fromOffset++;
      toOffset++;
      toOffset++;
Line 221... Line 195...
 
 
  return  toOffset;
  return  toOffset;
 
 
}       // rspUnescape () */
}       // rspUnescape () */
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Convert 32-bit value from host to target endianness
//! Convert 32-bit value from host to target endianness
 
 
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
 
 
//! @param[in] hostVal  The value in host endianness
//! @param[in] hostVal  The value in host endianness
 
 
//! @return  The value in target endianness
//! @return  The value in target endianness
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
uint32_t
uint32_t Utils::htotl(uint32_t hostVal)
Utils::htotl (uint32_t hostVal)
 
{
{
  uint8_t  targetBytes[4];
  uint8_t  targetBytes[4];
 
 
#ifdef TARGET_BIG_ENDIAN
#ifdef TARGET_BIG_ENDIAN
  targetBytes[0] = hostVal / 256 / 256 / 256;
  targetBytes[0] = hostVal / 256 / 256 / 256;
Line 255... Line 227...
 
 
  return *((uint32_t *)targetBytes);
  return *((uint32_t *)targetBytes);
 
 
}       // htotl ()
}       // htotl ()
 
 
 
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//! Convert 32-bit value from target to host endianness
//! Convert 32-bit value from target to host endianness
 
 
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
 
 
//! @param[in] targetVal  The value in target endianness
//! @param[in] targetVal  The value in target endianness
 
 
//! @return  The value in target endianness
//! @return  The value in target endianness
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
uint32_t
uint32_t Utils::ttohl(uint32_t targetVal)
Utils::ttohl (uint32_t targetVal)
 
{
{
  uint8_t *targetBytes = (uint8_t *)(&targetVal);
  uint8_t *targetBytes = (uint8_t *)(&targetVal);
  uint32_t hostVal;
  uint32_t hostVal;
 
 
#ifdef TARGET_BIG_ENDIAN
#ifdef TARGET_BIG_ENDIAN

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.