2 * Copyright (c) 2002 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: vpip_hex.cc,v 1.4 2006/02/21 02:39:27 steve Exp $"
24 # include "vpi_priv.h"
27 # include <limits.h> /* for CHAR_BIT */
34 extern const char hex_digits
[256];
36 void vpip_hex_str_to_vec4(vvp_vector4_t
&val
, const char*str
)
38 unsigned str_len
= strlen(str
);
52 for (unsigned idx
= 0 ; idx
< val
.size() ; idx
+= 1) {
54 unsigned bit_off
= idx
%4;
55 unsigned str_off
= idx
/4;
58 if (str_off
>= str_len
)
61 ch
= str
[str_len
-str_off
-1];
75 val
.set_bit(idx
, ((tmp
>>bit_off
)&1)? BIT4_1
: BIT4_0
);
84 val
.set_bit(idx
, ((tmp
>>bit_off
)&1)? BIT4_1
: BIT4_0
);
93 val
.set_bit(idx
, ((tmp
>>bit_off
)&1)? BIT4_1
: BIT4_0
);
97 val
.set_bit(idx
, BIT4_X
);
101 val
.set_bit(idx
, BIT4_Z
);
104 fprintf(stderr
, "Unsupported digit %c(%d).\n", ch
, ch
);
111 void vpip_vec4_to_hex_str(const vvp_vector4_t
&bits
, char*buf
,
112 unsigned nbuf
, bool signed_flag
)
114 unsigned slen
= (bits
.size() + 3) / 4;
120 for (unsigned idx
= 0 ; idx
< bits
.size() ; idx
+= 1) {
121 unsigned vs
= (idx
%4) * 2;
123 switch (bits
.value(idx
)) {
138 buf
[slen
] = hex_digits
[val
];
147 buf
[slen
] = hex_digits
[val
];
149 case 'X': padd
= 2; break;
150 case 'Z': padd
= 3; break;
153 for (unsigned idx
= bits
.size() % 4; idx
< 4; idx
+= 1) {
154 val
= val
| padd
<< 2*idx
;
156 buf
[slen
] = hex_digits
[val
];