1 /*
2  *     *********************************************************************
3  *     * Copyright (C) 1988, 1990 Stanford University.                     *
4  *     * Permission to use, copy, modify, and distribute this              *
5  *     * software and its documentation for any purpose and without        *
6  *     * fee is hereby granted, provided that the above copyright          *
7  *     * notice appear in all copies.  Stanford University                 *
8  *     * makes no representations about the suitability of this            *
9  *     * software for any purpose.  It is provided "as is" without         *
10  *     * express or implied warranty.  Export of this software outside     *
11  *     * of the United States of America may require an export license.    *
12  *     *********************************************************************
13  */
14 
15 #include <stdio.h>
16 #include "ana.h"
17 
18 private	char    result[ 200 ];
19 private	char    HexMap[] = "0123456789abcdefX";
20 
21 
22 /*
23  * Convert a trace entry (vector) to an ascii string
24  */
HistToStr(hist,nbits,b_digit,offset)25 public char *HistToStr( hist, nbits, b_digit, offset )
26   hptr  *hist;
27   int   nbits, b_digit, offset;
28   {
29     register char  *p;
30     register int   i, j;
31     Ulong digit;
32 
33     /* Handle decimal notation (special cases)		*/
34     /* Since decimal does not map directly to bits,	*/
35     /* we only print the result if no bits are unknown.	*/
36 
37     if (b_digit == 5) {
38       hptr *htmp = hist;
39       for (i = nbits; i > 0; i--) {
40 	 if ((*htmp)->val == X) {
41 	    sprintf(result, "???");
42 	    return (result);
43 	 }
44 	 htmp += offset;
45       }
46       digit = (Ulong)0;
47       for (i = nbits; i > 0; i--) {
48 	 digit <<= 1;
49 	 if ((*hist)->val == HIGH) digit |= 1;
50 	 hist += offset;
51       }
52       if (digit < 0)
53 	 sprintf(result, "(overflow)");
54       else
55          sprintf(result, PRINTF_LLONG "u", digit);
56       return (result);
57     }
58 
59     /* Handle signed decimal notation */
60 
61     else if (b_digit == 6) {
62       hptr *htmp = hist;
63       char negative;
64 
65       negative = ((*htmp)->val == HIGH) ? 1 : 0;
66       for (i = nbits; i > 0; i--) {
67 	 if ((*htmp)->val == X) {
68 	    sprintf(result, "???");
69 	    return (result);
70 	 }
71 	 htmp += offset;
72       }
73       digit = (Ulong)0;
74       for (i = nbits; i > 0; i--) {
75 	 digit <<= 1;
76 	 if (((negative == 1) && ((*hist)->val == LOW)) ||
77 		((negative == 0) && ((*hist)->val == HIGH)))
78 	    digit |= 1;
79 	 hist += offset;
80       }
81       if (negative) digit = -(digit + 1);
82       sprintf(result, PRINTF_LLONG "d", digit);
83       return (result);
84     }
85 
86     p = result;
87     j = nbits % b_digit;
88     if( j == 0 )
89 	j = b_digit;
90     for( i = nbits; i > 0; i -= j )
91       {
92 	digit = 0;
93 	do
94 	  {
95 	    switch( (*hist)->val )
96 	      {
97 		case LOW :
98 		    digit = (digit << 1);
99 		    break;
100 		case HIGH :
101 		    digit = (digit << 1) | 1;
102 		    break;
103 		case X :
104 		    digit = 16;
105 		    while( j != 1 )
106 		      {
107 			j--;
108 			hist += offset;
109 		      }
110 		    break;
111 	      }
112 	    j--;
113 	    hist += offset;
114 	  }
115 	while( j > 0 );
116 	*p++ = HexMap[ digit ];
117 	j = b_digit;
118       }
119     *p = '\0';
120     return( result );
121   }
122