Fix the debugger to finish correctly.
[iverilog.git] / vvp / vpip_oct.cc
blob9e0a77bcfa4b36b70ff39e0bd8b92325b7da9256
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_oct.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>
28 # include <stdlib.h>
29 #ifdef HAVE_MALLOC_H
30 # include <malloc.h>
31 #endif
32 # include <assert.h>
34 extern const char oct_digits[64];
36 void vpip_oct_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%3;
55 unsigned str_off = idx/3;
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 tmp = ch - '0';
73 val.set_bit(idx, ((tmp>>bit_off)&1)? BIT4_1 : BIT4_0);
74 break;
75 case 'x':
76 case 'X':
77 val.set_bit(idx, BIT4_X);
78 break;
79 case 'z':
80 case 'Z':
81 val.set_bit(idx, BIT4_Z);
82 break;
83 default:
84 fprintf(stderr, "Unsupported digit %c(%d).\n", ch, ch);
85 assert(0);
86 break;
92 void vpip_bits_to_oct_str(const unsigned char*bits, unsigned nbits,
93 char*buf, unsigned nbuf, bool signed_flag)
95 unsigned slen = (nbits + 2) / 3;
96 unsigned val = 0;
97 assert(slen < nbuf);
99 buf[slen] = 0;
101 for (unsigned idx = 0 ; idx < nbits ; idx += 1) {
102 unsigned bi = idx/4;
103 unsigned bs = (idx%4) * 2;
104 unsigned bit = (bits[bi] >> bs) & 3;
106 unsigned vs = (idx%3) * 2;
107 val |= bit << vs;
109 if (vs == 4) {
110 slen -= 1;
111 buf[slen] = oct_digits[val];
112 val = 0;
116 if (slen > 0) {
117 slen -= 1;
118 buf[slen] = oct_digits[val];
122 void vpip_vec4_to_oct_str(const vvp_vector4_t&bits, char*buf, unsigned nbuf,
123 bool signed_flag)
125 unsigned slen = (bits.size() + 2) / 3;
126 assert(slen < nbuf);
128 buf[slen] = 0;
130 unsigned val = 0;
131 for (unsigned idx = 0 ; idx < bits.size() ; idx += 1) {
132 unsigned vs = (idx%3) * 2;
134 switch (bits.value(idx)) {
135 case BIT4_0:
136 break;
137 case BIT4_1:
138 val |= 1 << vs;
139 break;
140 case BIT4_X:
141 val |= 2 << vs;
142 break;
143 case BIT4_Z:
144 val |= 3 << vs;
147 if (vs == 4) {
148 slen -= 1;
149 buf[slen] = oct_digits[val];
150 val = 0;
154 if (slen > 0) {
155 slen -= 1;
156 buf[slen] = oct_digits[val];