Fix for assertion error when expanding macro.
[iverilog.git] / vvp / vpip_hex.cc
blob3ef3056cd16b4d75cce0dc6e31ae1c94206ada57
1 /*
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)
8 * any later version.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: vpip_hex.cc,v 1.4 2006/02/21 02:39:27 steve Exp $"
21 #endif
23 # include "config.h"
24 # include "vpi_priv.h"
25 # include <stdio.h>
26 # include <string.h>
27 # include <limits.h> /* for CHAR_BIT */
28 # include <stdlib.h>
29 #ifdef HAVE_MALLOC_H
30 # include <malloc.h>
31 #endif
32 # include <assert.h>
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);
40 char pad = '0';
41 switch (str[0]) {
42 case 'x':
43 case 'X':
44 pad = 'x';
45 break;
46 case 'z':
47 case 'Z':
48 pad = 'z';
49 break;
52 for (unsigned idx = 0 ; idx < val.size() ; idx += 1) {
53 unsigned tmp;
54 unsigned bit_off = idx%4;
55 unsigned str_off = idx/4;
57 char ch;
58 if (str_off >= str_len)
59 ch = pad;
60 else
61 ch = str[str_len-str_off-1];
63 switch (ch) {
64 case '0':
65 case '1':
66 case '2':
67 case '3':
68 case '4':
69 case '5':
70 case '6':
71 case '7':
72 case '8':
73 case '9':
74 tmp = ch - '0';
75 val.set_bit(idx, ((tmp>>bit_off)&1)? BIT4_1 : BIT4_0);
76 break;
77 case 'a':
78 case 'b':
79 case 'c':
80 case 'd':
81 case 'e':
82 case 'f':
83 tmp = ch - 'a' + 10;
84 val.set_bit(idx, ((tmp>>bit_off)&1)? BIT4_1 : BIT4_0);
85 break;
86 case 'A':
87 case 'B':
88 case 'C':
89 case 'D':
90 case 'E':
91 case 'F':
92 tmp = ch - 'A' + 10;
93 val.set_bit(idx, ((tmp>>bit_off)&1)? BIT4_1 : BIT4_0);
94 break;
95 case 'x':
96 case 'X':
97 val.set_bit(idx, BIT4_X);
98 break;
99 case 'z':
100 case 'Z':
101 val.set_bit(idx, BIT4_Z);
102 break;
103 default:
104 fprintf(stderr, "Unsupported digit %c(%d).\n", ch, ch);
105 assert(0);
106 break;
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;
115 assert(slen < nbuf);
117 buf[slen] = 0;
119 unsigned val = 0;
120 for (unsigned idx = 0 ; idx < bits.size() ; idx += 1) {
121 unsigned vs = (idx%4) * 2;
123 switch (bits.value(idx)) {
124 case BIT4_0:
125 break;
126 case BIT4_1:
127 val |= 1 << vs;
128 break;
129 case BIT4_X:
130 val |= 2 << vs;
131 break;
132 case BIT4_Z:
133 val |= 3 << vs;
136 if (vs == 6) {
137 slen -= 1;
138 buf[slen] = hex_digits[val];
139 val = 0;
143 if (slen > 0) {
144 unsigned padd = 0;
146 slen -= 1;
147 buf[slen] = hex_digits[val];
148 switch(buf[slen]) {
149 case 'X': padd = 2; break;
150 case 'Z': padd = 3; break;
152 if (padd) {
153 for (unsigned idx = bits.size() % 4; idx < 4; idx += 1) {
154 val = val | padd << 2*idx;
156 buf[slen] = hex_digits[val];