-
Notifications
You must be signed in to change notification settings - Fork 512
/
vpi_const.cc
620 lines (520 loc) · 14.4 KB
/
vpi_const.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* Copyright (c) 2001 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vpi_const.cc,v 1.20 2002/11/03 02:07:24 steve Exp $"
#endif
# include "vpi_priv.h"
# include <stdio.h>
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif
# include <stdlib.h>
# include <string.h>
# include <assert.h>
static char buf[4096];
static int string_get(int code, vpiHandle ref)
{
struct __vpiStringConst*rfp;
switch (code) {
case vpiSize:
rfp = (struct __vpiStringConst*)ref;
assert(ref->vpi_type->type_code == vpiConstant);
//fprintf(stderr, "String:|%s|, Length: %d\n", rfp->value, strlen(rfp->value));
return strlen(rfp->value)*8;
case vpiSigned:
return 0;
case vpiConstType:
return vpiStringConst;
default:
fprintf(stderr, "vvp error: get %d not supported "
"by vpiStringConst\n", code);
assert(0);
return 0;
}
}
static void string_value(vpiHandle ref, p_vpi_value vp)
{
int size;
unsigned uint_value;
char *cp;
struct __vpiStringConst*rfp = (struct __vpiStringConst*)ref;
assert(ref->vpi_type->type_code == vpiConstant);
switch (vp->format) {
case vpiObjTypeVal:
case vpiStringVal:
vp->value.str = (char*)rfp->value;
vp->format = vpiStringVal;
break;
case vpiDecStrVal:
size = strlen(rfp->value);
if (size > 4){
// We only support standard integers. Ignore other bytes...
size = 4;
fprintf(stderr, "Warning (vpi_const.cc): %%d on constant strings only looks "
"at first 4 bytes!\n");
}
uint_value = 0;
for(int i=0; i<size;i ++){
uint_value <<=8;
uint_value += (unsigned char)(rfp->value[i]);
}
sprintf(buf, "%u", uint_value);
vp->format = vpiDecStrVal;
vp->value.str = buf;
break;
case vpiBinStrVal:
size = strlen(rfp->value);
if (size*8 > (int)(sizeof(buf)/sizeof(char))-1 ){
// Avoid overflow of 'buf'
// 4096 should be sufficient for most cases though. ;-)
size = (sizeof(buf)/sizeof(char)-2)/8;
}
cp = buf;
for(int i=0; i<size;i ++){
for(int bit=7;bit>=0; bit--){
*cp++ = "01"[ (rfp->value[i]>>bit)&1 ];
}
}
*cp = 0;
vp->format = vpiBinStrVal;
vp->value.str = buf;
break;
case vpiHexStrVal:
size = strlen(rfp->value);
if (size*2 > (int)(sizeof(buf)/sizeof(char))-1 ){
// Avoid overflow of 'buf'
// 4096 should be sufficient for most cases though. ;-)
size = (sizeof(buf)/sizeof(char)-2)/2;
}
cp = buf;
for(int i=0; i<size;i++){
for(int nibble=1;nibble>=0; nibble--){
*cp++ = "0123456789abcdef"[ (rfp->value[i]>>(nibble*4))&15 ];
}
}
*cp = 0;
vp->format = vpiHexStrVal;
vp->value.str = buf;
break;
case vpiOctStrVal:
fprintf(stderr, "ERROR (vpi_const.cc): %%o display of constant strings not yet implemented\n");
assert(0);
break;
default:
fprintf(stderr, "ERROR (vpi_const.cc): vp->format: %d\n", vp->format);
assert(0);
vp->format = vpiSuppressVal;
break;
}
}
static const struct __vpirt vpip_string_rt = {
vpiConstant,
string_get,
0,
string_value,
0,
0,
0
};
vpiHandle vpip_make_string_const(char*text)
{
struct __vpiStringConst*obj;
obj = (struct __vpiStringConst*)
malloc(sizeof (struct __vpiStringConst));
obj->base.vpi_type = &vpip_string_rt;
obj->value = text;
return &obj->base;
}
static int binary_get(int code, vpiHandle ref)
{
struct __vpiBinaryConst*rfp = (struct __vpiBinaryConst*)ref;
assert(ref->vpi_type->type_code == vpiConstant);
switch (code) {
case vpiConstType:
return vpiBinaryConst;
case vpiSigned:
return rfp->signed_flag? 1 : 0;
case vpiSize:
return rfp->nbits;
default:
fprintf(stderr, "vvp error: get %d not supported "
"by vpiBinaryConst\n", code);
assert(0);
return 0;
}
}
static void binary_vpiStringVal(struct __vpiBinaryConst*rfp, p_vpi_value vp)
{
unsigned nchar = rfp->nbits / 8;
unsigned tail = rfp->nbits%8;
char*cp = buf;
if (tail > 0) {
char char_val = 0;
for (unsigned idx = rfp->nbits-tail; idx < rfp->nbits; idx += 1) {
unsigned nibble = idx/4;
unsigned shift = 2 * (idx%4);
unsigned val = (rfp->bits[nibble] >> shift) & 3;
if (val & 1)
char_val |= 1 << idx;
}
if (char_val != 0)
*cp++ = char_val;
}
for (unsigned idx = 0 ; idx < nchar ; idx += 1) {
unsigned bit = (nchar - idx - 1) * 8;
unsigned nibble = bit/4;
unsigned vall = rfp->bits[nibble+0];
unsigned valh = rfp->bits[nibble+1];
char char_val = 0;
if (vall&0x01) char_val |= 0x01;
if (vall&0x04) char_val |= 0x02;
if (vall&0x10) char_val |= 0x04;
if (vall&0x40) char_val |= 0x08;
if (valh&0x01) char_val |= 0x10;
if (valh&0x04) char_val |= 0x20;
if (valh&0x10) char_val |= 0x40;
if (valh&0x40) char_val |= 0x80;
if (char_val != 0)
*cp++ = char_val;
}
*cp = 0;
vp->format = vpiStringVal;
vp->value.str = buf;
}
static void binary_value(vpiHandle ref, p_vpi_value vp)
{
struct __vpiBinaryConst*rfp = (struct __vpiBinaryConst*)ref;
assert(ref->vpi_type->type_code == vpiConstant);
switch (vp->format) {
case vpiObjTypeVal:
case vpiBinStrVal:
assert(rfp->nbits < sizeof buf);
for (unsigned idx = 0 ; idx < rfp->nbits ; idx += 1) {
unsigned nibble = idx/4;
unsigned shift = 2 * (idx%4);
unsigned val = (rfp->bits[nibble] >> shift) & 3;
buf[rfp->nbits-idx-1] = "01xz"[val];
}
buf[rfp->nbits] = 0;
vp->value.str = buf;
vp->format = vpiBinStrVal;
break;
case vpiDecStrVal: {
unsigned wid = rfp->nbits;
unsigned char*tmp = new unsigned char[wid];
for (unsigned idx = 0 ; idx < wid ; idx += 1)
tmp[idx] = (rfp->bits[idx/4] >> 2*(idx%4)) & 3;
vpip_bits_to_dec_str(tmp, wid, buf, sizeof buf,
rfp->signed_flag);
delete[]tmp;
vp->value.str = buf;
vp->format = vpiDecStrVal;
break;
}
case vpiHexStrVal: {
unsigned nchar = (rfp->nbits+3)/4;
assert(nchar < sizeof buf);
for (unsigned idx = 0 ; idx < rfp->nbits ; idx += 4) {
unsigned nibble = idx/4;
unsigned vals = rfp->bits[nibble];
if (vals == 0xff) {
buf[nchar-idx/4-1] = 'z';
} else if (vals == 0xaa) {
buf[nchar-idx/4-1] = 'x';
} else if (vals & 0xaa) {
buf[nchar-idx/4-1] = 'X';
} else {
unsigned val = vals&1;
if (vals&0x04) val |= 2;
if (vals&0x10) val |= 4;
if (vals&0x40) val |= 8;
buf[nchar-idx/4-1] = "0123456789abcdef"[val];
}
}
buf[nchar] = 0;
vp->value.str = buf;
vp->format = vpiHexStrVal;
break;
}
case vpiIntVal: {
unsigned val = 0;
unsigned bit_val = 0;
for (unsigned idx = 0 ; idx < rfp->nbits ; idx += 1) {
unsigned nibble = idx/4;
unsigned shift = 2 * (idx%4);
bit_val = (rfp->bits[nibble] >> shift) & 3;
if (bit_val > 1) {
vp->value.integer = 0;
return;
} else {
val |= bit_val << idx;
}
}
/* sign extend */
if (rfp->signed_flag && bit_val) {
for (unsigned idx = rfp->nbits; idx <sizeof(val)*8; idx++)
{
val |= bit_val << idx;
}
}
vp->value.integer = val;
break;
}
case vpiVectorVal: {
unsigned int obit = 0;
unsigned hwid = (rfp->nbits - 1)/32 + 1;
char*rbuf = need_result_buf(hwid*sizeof(s_vpi_vecval), RBUF_VAL);
s_vpi_vecval *op = (p_vpi_vecval)rbuf;
vp->value.vector = op;
op->aval = op->bval = 0;
for (unsigned idx = 0 ; idx < rfp->nbits ; idx += 1) {
unsigned nibble = idx/4;
unsigned shift = 2 * (idx%4);
unsigned bit_val = (rfp->bits[nibble] >> shift) & 3;
switch (bit_val) {
case 0:
op->aval &= ~(1 << obit);
op->bval &= ~(1 << obit);
break;
case 1:
op->aval |= (1 << obit);
op->bval &= ~(1 << obit);
break;
case 2:
op->aval |= (1 << obit);
op->bval |= (1 << obit);
break;
case 3:
op->aval &= ~(1 << obit);
op->bval |= (1 << obit);
break;
}
obit++;
if (!(obit % 32)) {
op += 1;
if ((op - vp->value.vector) < (ptrdiff_t)hwid)
op->aval = op->bval = 0;
obit = 0;
}
}
break;
}
case vpiStringVal:
binary_vpiStringVal(rfp, vp);
break;
default:
fprintf(stderr, "vvp error: format %d not supported "
"by vpiBinaryConst\n", vp->format);
vp->format = vpiSuppressVal;
break;
}
}
static const struct __vpirt vpip_binary_rt = {
vpiConstant,
binary_get,
0,
binary_value,
0,
0,
0
};
/*
* Make a VPI constant from a vector string. The string is normally a
* ASCII string, with each letter a 4-value bit. The first character
* may be an 's' if the vector is signed.
*/
vpiHandle vpip_make_binary_const(unsigned wid, char*bits)
{
struct __vpiBinaryConst*obj;
obj = (struct __vpiBinaryConst*)
malloc(sizeof (struct __vpiBinaryConst));
obj->base.vpi_type = &vpip_binary_rt;
obj->signed_flag = 0;
obj->nbits = wid;
obj->bits = (unsigned char*)malloc((obj->nbits + 3) / 4);
memset(obj->bits, 0, (obj->nbits + 3) / 4);
const char*bp = bits;
if (*bp == 's') {
bp += 1;
obj->signed_flag = 1;
}
for (unsigned idx = 0 ; idx < obj->nbits ; idx += 1) {
unsigned nibble = idx / 4;
unsigned val = 0;
switch (bp[wid-idx-1]) {
case '0':
val = 0;
break;
case '1':
val = 1;
break;
case 'x':
val = 2;
break;
case 'z':
val = 3;
break;
}
obj->bits[nibble] |= val << (2 * (idx%4));
}
free(bits);
return &(obj->base);
}
static int dec_get(int code, vpiHandle ref)
{
switch (code) {
case vpiConstType:
return vpiDecConst;
case vpiSigned:
return 1;
case vpiSize:
return 32;
default:
fprintf(stderr, "vvp error: get %d not supported "
"by vpiDecConst\n", code);
assert(0);
return 0;
}
}
static void dec_value(vpiHandle ref, p_vpi_value vp)
{
struct __vpiDecConst*rfp = (struct __vpiDecConst*)ref;
char* cp;
assert(ref->vpi_type->type_code == vpiConstant);
switch (vp->format) {
case vpiObjTypeVal:
case vpiIntVal: {
vp->value.integer = rfp->value;
break;
}
case vpiDecStrVal:
sprintf(buf, "%d", rfp->value);
vp->format = vpiDecStrVal;
vp->value.str = buf;
break;
case vpiBinStrVal:
cp = buf;
for(int bit=31; bit<=0;bit--){
*cp++ = "01"[ (rfp->value>>bit)&1 ];
}
*cp = 0;
vp->format = vpiBinStrVal;
vp->value.str = buf;
break;
case vpiHexStrVal:
sprintf(buf, "%08x", rfp->value);
vp->format = vpiHexStrVal;
vp->value.str = buf;
break;
case vpiOctStrVal:
sprintf(buf, "%011x", rfp->value);
vp->format = vpiOctStrVal;
vp->value.str = buf;
break;
default:
fprintf(stderr, "vvp error (vpi_const.cc): format %d not supported "
"by vpiDecConst\n", vp->format);
vp->format = vpiSuppressVal;
break;
}
}
static const struct __vpirt vpip_dec_rt = {
vpiConstant,
dec_get,
0,
dec_value,
0,
0,
0
};
vpiHandle vpip_make_dec_const(struct __vpiDecConst*obj, int value)
{
obj->base.vpi_type = &vpip_dec_rt;
obj->value = value;
return &(obj->base);
}
vpiHandle vpip_make_dec_const(int value)
{
struct __vpiDecConst*obj;
obj = (struct __vpiDecConst*)
malloc(sizeof (struct __vpiDecConst));
return vpip_make_dec_const(obj, value);
}
/*
* $Log: vpi_const.cc,v $
* Revision 1.20 2002/11/03 02:07:24 steve
* Get VectorVals from constant values.
*
* Revision 1.19 2002/08/12 01:35:08 steve
* conditional ident string using autoconfig.
*
* Revision 1.18 2002/06/23 18:23:09 steve
* trivial performance boost.
*
* Revision 1.17 2002/06/14 22:05:28 steve
* sign extend signed vectors vpiIntVal.
*
* Revision 1.16 2002/05/17 04:12:19 steve
* Rewire vpiMemory and vpiMemoryWord handles to
* support proper iteration of words, and the
* vpiIndex value.
*
* Revision 1.15 2002/04/27 23:26:24 steve
* Trim leading nulls from string forms.
*
* Revision 1.14 2002/04/27 22:36:39 steve
* Support drawing vpiBinaryConst in hex.
*
* Revision 1.13 2002/04/14 03:53:20 steve
* Allow signed constant vectors for call_vpi parameters.
*
* Revision 1.12 2002/03/18 05:33:24 steve
* vpip_bits_to_dec_str takes a bit array in a specific format.
*
* Revision 1.11 2002/02/03 01:01:51 steve
* Use Larrys bits-to-decimal-string code.
*
* Revision 1.10 2002/01/31 04:28:17 steve
* Full support for $readmem ranges (Tom Verbeure)
*
* Revision 1.8 2002/01/15 03:21:18 steve
* Support DesSTrVal for binary constants.
*
* Revision 1.7 2001/09/15 18:27:05 steve
* Make configure detect malloc.h
*
* Revision 1.6 2001/08/08 00:57:20 steve
* Unused variable warnings.
*
* Revision 1.5 2001/07/11 04:40:52 steve
* Get endian of vpiIntVal from constants.
*
* Revision 1.4 2001/04/04 05:07:19 steve
* Get intval from a binary constant.
*
* Revision 1.3 2001/04/04 04:33:08 steve
* Take vector form as parameters to vpi_call.
*
* Revision 1.2 2001/04/02 00:24:31 steve
* Take numbers as system task parameters.
*
* Revision 1.1 2001/03/18 04:35:18 steve
* Add support for string constants to VPI.
*
*/