home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / utils / amd-udi / montip / endian.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-23  |  5.1 KB  |  185 lines

  1. static char _[] = "@(#)endian.c    5.18 93/07/30 16:40:17, Srini, AMD.";
  2. /******************************************************************************
  3.  * Copyright 1991 Advanced Micro Devices, Inc.
  4.  *
  5.  * This software is the property of Advanced Micro Devices, Inc  (AMD)  which
  6.  * specifically  grants the user the right to modify, use and distribute this
  7.  * software provided this notice is not removed or altered.  All other rights
  8.  * are reserved by AMD.
  9.  *
  10.  * AMD MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS
  11.  * SOFTWARE.  IN NO EVENT SHALL AMD BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL
  12.  * DAMAGES IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, OR
  13.  * USE OF THIS SOFTWARE.
  14.  *
  15.  * So that all may benefit from your experience, please report  any  problems
  16.  * or  suggestions about this software to the 29K Technical Support Center at
  17.  * 800-29-29-AMD (800-292-9263) in the USA, or 0800-89-1131  in  the  UK,  or
  18.  * 0031-11-1129 in Japan, toll free.  The direct dial number is 512-462-4118.
  19.  *
  20.  * Advanced Micro Devices, Inc.
  21.  * 29K Support Products
  22.  * Mail Stop 573
  23.  * 5900 E. Ben White Blvd.
  24.  * Austin, TX 78741
  25.  * 800-292-9263
  26.  *****************************************************************************
  27.  *      Engineer: Srini Subramanian.
  28.  *****************************************************************************
  29.  ** 
  30.  **       This module implements the endian conversion routines used by MONTIP.
  31.  **
  32.  *****************************************************************************
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include  "messages.h"
  38.  
  39. #ifdef MSDOS
  40. #include <string.h>
  41. #else
  42. #include <string.h>
  43. #endif  /* MSDOS */
  44.  
  45. /* Functions */
  46. void  tip_convert32 PARAMS((BYTE *));
  47. void  tip_convert16 PARAMS((BYTE *));
  48.  
  49. /*
  50. ** This function is used to convert the endian of messages.
  51. ** Both host to target and target to host messages can be
  52. ** converted using this function.
  53. **
  54. ** Note that all monitor messages have a header consisting of
  55. ** a 32 bit message number and a 32 bit size.  Following this
  56. ** may be one or more 32 bit parameters.  And folowing these
  57. ** parameters may be an array of bytes.
  58. **
  59. ** This function converts the endian of the header and any
  60. ** parameters.  It is not necessary to convert the array of
  61. ** bytes.
  62. **
  63. ** Note that the use of 32 bit parameters makes this conversion
  64. ** routine fairly simple.
  65. */
  66.  
  67. void
  68. endian_cvt(msg_buf, direction)
  69.    union  msg_t  *msg_buf;
  70.    int    direction;
  71.    {
  72.    INT32  i;
  73.    BYTE  *byte;
  74.    INT32  code;
  75.    INT32  length;
  76.  
  77.  
  78.    /*
  79.    ** If incoming message, convert endian, then get message
  80.    ** type and message length.  If outgoing message, get
  81.    ** message type and message length, then convert endian.
  82.    */
  83.  
  84.    if ((direction != OUTGOING_MSG) &&
  85.        (direction != INCOMING_MSG))
  86.       return;
  87.  
  88.    if (direction == OUTGOING_MSG) {
  89.       code = (msg_buf->generic_msg).code;
  90.       length = (msg_buf->generic_msg).length;
  91.       }
  92.  
  93.    /* Change endian of "code" field */
  94.    tip_convert32((BYTE *) &(msg_buf->generic_msg).code);
  95.  
  96.    /* Change endian of "length" field */
  97.    tip_convert32((BYTE *) &(msg_buf->generic_msg).length);
  98.  
  99.    if (direction == INCOMING_MSG) {
  100.       code = (msg_buf->generic_msg).code;
  101.       length = (msg_buf->generic_msg).length;
  102.       }
  103.  
  104.    /*
  105.    ** Some messages, notably WRITE_REQ, FILL, READ
  106.    ** and TRACE have data following the message
  107.    ** parameters.  Since we don't want to swap bytes
  108.    ** in the data array, we need to get the number of
  109.    ** of bytes taken up by the parameters.  This is
  110.    ** still better than having to find ALL of the
  111.    ** message lengths statically.
  112.    */
  113.  
  114.    if (code == WRITE_REQ) 
  115.       length = msg_length(WRITE_REQ);
  116.    else
  117.    if (code == FILL)
  118.       length = MSG_LENGTH(struct fill_msg_t);
  119.    else
  120.    if (code == READ_ACK)
  121.       length = MSG_LENGTH(struct read_ack_msg_t);
  122.    else
  123.    if (code == CHANNEL1)
  124.       length = MSG_LENGTH(struct channel1_msg_t);
  125.    else
  126.    if (code == CHANNEL2)
  127.       length = MSG_LENGTH(struct channel2_msg_t);
  128.    else
  129.    if (code == CHANNEL0)
  130.       length = MSG_LENGTH(struct channel0_msg_t);
  131.    else
  132.    if (code == STDIN_NEEDED_ACK)
  133.       length = MSG_LENGTH(struct stdin_needed_ack_msg_t);
  134.  
  135.    /* Convert message parameters */
  136.  
  137.    byte = (BYTE *) &(msg_buf->generic_msg).byte;
  138.    for (i=0; i<(length/sizeof(INT32)); i=i+1) {
  139.       tip_convert32(byte);
  140.       byte = byte + sizeof(INT32);
  141.       }
  142.  
  143.    }   /* end endian_cvt */
  144.  
  145.  
  146. /*
  147. ** This function is used to swap the bytes in a 32 bit
  148. ** word.  This will convert "little endian" (IBM-PC / Intel)
  149. ** words to "big endian" (Sun / Motorola) words.
  150. */
  151.  
  152.  
  153. void
  154. tip_convert32(byte)
  155.    BYTE *byte;
  156.    {
  157.    BYTE temp;
  158.  
  159.    temp = byte[0];  /* Swap bytes 0 and 3 */
  160.    byte[0] = byte[3];
  161.    byte[3] = temp;
  162.    temp = byte[1];  /* Swap bytes 1 and 2 */
  163.    byte[1] = byte[2];
  164.    byte[2] = temp;
  165.    }   /* end tip_convert32() */
  166.  
  167.  
  168. /*
  169. ** This function is used to swap the bytes in a 16 bit
  170. ** word.  This will convert "little endian" (IBM-PC / Intel)
  171. ** half words to "big endian" (Sun / Motorola) half words.
  172. */
  173.  
  174. void
  175. tip_convert16(byte)
  176.    BYTE *byte;
  177.    {
  178.    BYTE temp;
  179.  
  180.    temp = byte[0];  /* Swap bytes 0 and 1 */
  181.    byte[0] = byte[1];
  182.    byte[1] = temp;
  183.  
  184.    }   /* end tip_convert16() */
  185.