-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvcddiff.cpp
More file actions
1492 lines (1329 loc) · 47.5 KB
/
vcddiff.cpp
File metadata and controls
1492 lines (1329 loc) · 47.5 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 1991-2004 Pragmatic C Software Corp.
// SPDX-FileCopyrightText: 2015-2026 Wilson Snyder
// SPDX-License-Identifier: GPL-3.0
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 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.
#define VERSION "2026-02-23-veripool"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include "vcddiff.h"
static int get_token(FILE*, char*);
static void read_to_end(FILE*);
static char timescale(FILE*, int*);
static void add_signal(char*, char*, unsigned int, int, int);
static int get_var_type(char*);
static void variable(FILE*, char*);
static void alloc_sig_mem();
static int get_vkeywrd(char*);
static long get_lines(FILE*, int*, int*, char*);
static void print_signal_info(signal_t*, signal_t*, vtime_t, vtime_t, bool);
static void print_scalar_state(signal_t*, signal_t*, vtime_t, vtime_t, char, char, bool);
static void print_vector_state(signal_t*, signal_t*, vtime_t, vtime_t, char*, char*, bool);
static void print_scalar_edge(signal_t*, signal_t*, vtime_t, vtime_t, char, char, bool);
static void print_edges(signal_t*, char*, int, int, char);
static void print_vector_edge(signal_t*, signal_t*, vtime_t, vtime_t, char*, char*, bool);
static int get_nxt_chg(FILE*, char*, int*, int*, char*, vtime_t*, bool);
static vtime_t get_time_diffs(FILE*, FILE*, char*, char*, long, long, vtime_t, vtime_t, bool);
static void print_map();
static void compare_types(char*, char*, signal_t*, signal_t*);
static bool map_var_names(char*, char*);
static void run_diffs(FILE*, FILE*, char*, char*, long, long);
static void print_header();
static void print_help();
static void edge_space(int, int, bool);
// globals
static int line_num1G; // cur position of file1
static int line_num2G; // cur position of file2
static char curmodG[1000]; // cur mod hier name
static FILE* file1G; // to check if it is the first file
static bool state_flagG; // print edges or states
static bool wrap_flagG; // print edges or states
static bool extended_flagG; // parse extended VCD format
static signal_t** sig_int1G; // int codes for file 1
static signal_t** sig_int2G; // int codes for file 2
static int* fd1_to_fd2_mapG; // mappings from one file to the other
static int* fd2_to_fd1_mapG;
static int max_codeG; // max code so sig_int? is large enough
static char scopesG[MAXSCOPES][MAXSIG]; // scope of mods
static int quit_flagG; // flag to quit
static bool next_listG; // to signal the next list of signals
signal_t* sig1_hdG; // the head of the first file of signals
signal_t* sig2_hdG; // the head of the second file of signals
signal_t* lastsigG; // mark the last signal of the file
// signal to code from dinotrace wave viewer www.veripool.org/dinotrace
// extended vcd converted, removes '<'
#define VERILOG_ID_TO_POS(_code_) \
strpbrk(_code_, "<") && extended_flagG \
? atoi(_code_ + 1) \
: (_code_[0] \
? ((_code_[0] - 32) \
+ 94 \
* (_code_[1] \
? ((_code_[1] - 32) \
+ 94 \
* (_code_[2] ? ((_code_[2] - 32) \
+ 94 * (_code_[3] ? ((_code_[3] - 32)) : 0)) \
: 0)) \
: 0)) \
: 0)
#define VERILOG_POS_TO_SIG1(_pos_) \
(((unsigned)(_pos_) < (unsigned)max_codeG) ? sig_int1G[(_pos_)] : 0)
#define VERILOG_POS_TO_SIG2(_pos_) \
(((unsigned)(_pos_) < (unsigned)max_codeG) ? sig_int2G[(_pos_)] : 0)
// **********************************************************************
// get a new token from the file fp, and place in token, return the length
static int get_token(FILE* fp, char* token) {
int i = 0;
int c;
while ((c = fgetc(fp)) != EOF) {
if (isspace(c)) {
if (c == '\n') {
if (fp == file1G)
line_num1G++;
else
line_num2G++;
}
continue;
}
break;
}
if (c == EOF) return EOF;
token[i++] = c;
while (!isspace(c = fgetc(fp)) && i < MAXTOKSIZE) {
if (c == EOF)
return EOF;
else
token[i++] = c;
}
if (i >= MAXTOKSIZE - 1) {
printf("Token too long.\n");
exit(1);
}
if (c == '\n') {
if (fp == file1G)
line_num1G++;
else
line_num2G++;
}
token[i] = '\0';
return i;
}
// read the file until an $end is reached
static void read_to_end(FILE* fp) {
static char token[MAXTOKSIZE];
while (get_token(fp, token) != EOF) {
if (!strncmp(token, "$end", 4)) return;
}
}
// get the timescale information for comparison return char of units, and
// the number 1, 10, 100 in tnum
static char timescale(FILE* fp, int* tnum) {
static char token[MAXTOKSIZE * 2]; // Needs to hold 2 tokens because of the concatenation.
static char tmp[MAXTOKSIZE];
char* tok;
int toklen = get_token(fp, token);
if (toklen == EOF) {
printf("*** ERROR Invalid timescale specification.\n");
exit(1);
}
assert(toklen < MAXTOKSIZE);
tok = token;
// AIV 02/04/03 there can be a space between 1 ns, not always 1ns
int i;
for (i = 0; i < toklen; i++) {
if (!isdigit(token[i])) break;
}
if (i == toklen) {
get_token(fp, tmp);
strcat(token, tmp);
}
*tnum = atoi(tok);
if (*tnum != 1 && *tnum != 10 && *tnum != 100) {
printf("*** ERROR-time number(%d) in timescale on line %d is illegal - assuming 1\n", *tnum,
(fp == file1G) ? line_num1G : line_num2G);
*tnum = 1;
}
while (isdigit(*tok)) tok++;
if (!*tok) get_token(fp, token);
switch (*tok) {
case 's':
case 'm':
case 'u':
case 'n':
case 'p':
case 'f': return *tok;
default:
printf("*** ERROR-illegal character(%c) in timescale on line %d\n", *tok,
(fp == file1G) ? line_num1G : line_num2G);
return 0;
}
}
// add a new signal to the list, init values signal name, identifies
// the code to index the array of signals, and the number of bits, if
// bits is 1 it is scalar
static void add_signal(char* signame, char* ident, unsigned int code, int bits, int type) {
char* cp;
// get rid of scapes before and after
while (isspace(*signame)) signame++;
for (cp = signame + strlen(signame) - 1; cp >= signame && isspace(*cp); cp--) *cp = '\0';
signal_t* newsig = (signal_t*)malloc(sizeof(signal_t));
newsig->sig_code = code;
newsig->ident = (char*)malloc(strlen(ident) + 1);
// init values to x's
newsig->state = '?';
newsig->type = type;
newsig->in_both = true;
newsig->size = bits;
newsig->next = nullptr;
if (type == REAL) {
// TODO what about large reals ???
newsig->vector = (char*)malloc(REALSIZE);
newsig->vector[0] = '0';
newsig->vector[1] = '\0';
} else if (bits > 1) {
newsig->vector = (char*)malloc(bits + 2);
newsig->vector[bits] = '\0';
bits--;
for (; bits >= 0; bits--) newsig->vector[bits] = '?';
} else {
newsig->vector = nullptr;
}
// signal not found so print first diff
newsig->found = false;
strcpy(newsig->ident, ident);
// link signals for hash setup and mapping
if (lastsigG) lastsigG->next = newsig;
if (!sig1_hdG) sig1_hdG = newsig;
if (next_listG && !sig2_hdG) sig2_hdG = newsig;
lastsigG = newsig;
newsig->signame = (char*)malloc(strlen(signame) + 1);
strcpy(newsig->signame, signame);
max_codeG = MAX((unsigned)max_codeG, code + 1);
}
// dump variable types must be kept in alphabetical order
// Values must match the var type #defines in vcddiff.h
static variable_types_t var_types[]
= {{"bit", BIT}, // GTKWave's fst2vcd
{"byte", BYTE}, // GTKWave's fst2vcd
{"event", EVENT},
{"int", INT}, // GTKWave's fst2vcd
{"integer", INTEGER},
{"logic", LOGIC}, // GTKWave's fst2vcd
{"longint", LONGINT}, // GTKWave's fst2vcd
{"parameter", PARAMETER},
{"port", PORT}, // GTKWave's fst2vcd
{"real", REAL},
{"real_parameter", REAL}, // GTKWave's fst2vcd, handle as if real
{"realtime", REALTIME}, // GTKWave's fst2vcd
{"reg", REG},
{"shortint", SHORTINT}, // GTKWave's fst2vcd
{"supply0", SUPPLY0},
{"supply1", SUPPLY1},
{"time", TIME},
{"tri", TRI},
{"tri0", TRI0},
{"tri1", TRI1},
{"triand", TRIAND},
{"trior", TRIOR},
{"trireg", TRIREG},
{"wand", WAND},
{"wire", WIRE},
{"wor", WOR}};
#define VARTYPE (sizeof(var_types) / sizeof(variable_types_t))
static int get_var_type(char* tstr) {
int l = 0;
int h = VARTYPE - 1;
for (;;) {
int m = (l + h) / 2;
int cv;
if ((cv = strcmp(var_types[m].vnam, tstr)) == 0) return var_types[m].vnum;
if (cv < 0)
l = m + 1;
else
h = m - 1;
if (h < l) break;
}
return -1;
}
// var line so add a new signal with its identifier
static void variable(FILE* fp, char* file_name) {
char signame[MAXSIG];
char ident[MAXIDLENGTH];
static char token[MAXTOKSIZE];
// type, size
{
int len = get_token(fp, token);
if (len == EOF) {
printf("ERROR - EOF during declaration of variable. Aborting.\n");
exit(1);
}
}
// most ofter a reg otherwise do a search
char type;
if (strncmp(token, "reg", 3) == 0)
type = REG;
else
type = get_var_type(token);
if (type < 0 || type > UNDEFINED) {
type = UNDEFINED;
printf("Error- Unknown variable type %s\n", token);
}
// according to the std ieee verilog
// if it is port is an evcd (port not allowed in vcd for signal declaration)
if (extended_flagG == false && strncmp(token, "port", 4) == 0) {
extended_flagG = true;
} else if (extended_flagG && strncmp(token, "port", 4) != 0) {
// parsing an evcd file but encountered in a non standard definition
printf("Error - Unknown variable type for EVCD standard %s\n", token);
}
// get number of bits
// AIV TODO error recovery should be used here for invalid tokens
// if a $var line is short vars (starts with a '$') rewind, print
// message and return
{
int len = get_token(fp, token);
if (len == EOF) {
printf("ERROR - EOF during declaration of variable. Aborting.\n");
exit(1);
}
}
int bits = atoi(token);
if (bits < 0) {
printf("Negative size illegal on line %d of %s\n", (fp == file1G) ? line_num1G : line_num2G,
file_name);
return;
}
// identifier
get_token(fp, token);
memset(
ident, 0,
sizeof(ident)); // ensure that ident is null-terminated, even it is too long to fit in ident
strncpy(ident, token, sizeof(ident) - 1);
for (char* s = token; *s; s++) {
if (!isprint(*s)) {
printf("*** ERROR-illegal character(%c) in signal name on line %d\n", *s,
(fp == file1G) ? line_num1G : line_num2G);
exit(1);
}
}
// variable name
get_token(fp, token);
memset(signame, 0, MAXSIG);
strncpy(signame, curmodG, MAXSIG - 1);
{
int available = MAXSIG - 1 - strlen(signame);
if (available > 0) { strncat(signame, token, available); }
}
get_token(fp, token);
// if there is a space between the var name and the range concat it
// to the name
if (token[0] != '$') {
int available = MAXSIG - 1 - strlen(signame);
if (available > 0) { strncat(signame, token, available); }
get_token(fp, token);
if (token[0] != '$') {
printf("ERROR - missing $end for %s\n", signame);
exit(1);
}
}
add_signal(signame, ident, VERILOG_ID_TO_POS(ident), bits, type);
}
// setup memory for the hash of signals, which is the max of all signals,
// then setup up arrays according to the signal's code index
static void alloc_sig_mem() {
sig_int1G = (signal_t**)calloc(sizeof(signal_t*), (max_codeG + 1));
for (signal_t* sig = sig1_hdG; sig; sig = sig->next) sig_int1G[sig->sig_code] = sig;
sig_int2G = (signal_t**)calloc(sizeof(signal_t*), (max_codeG + 1));
for (signal_t* sig = sig2_hdG; sig; sig = sig->next) sig_int2G[sig->sig_code] = sig;
}
// dump file keywords must be kept in alphabetical order
static variable_types_t vkeywds[]
= {{"attrbegin", V_ATTRBEGIN}, // GTKWave
{"comment", V_COMMENT}, {"date", V_DATE}, {"end", V_END},
{"enddefinitions", V_ENDDEF}, {"scope", V_SCOPE}, {"timescale", V_TIMESCALE},
{"upscope", V_UPSCOPE}, {"var", V_VAR}, {"version", V_VERSION}};
#define VKEYWDS (sizeof(vkeywds) / sizeof(variable_types_t))
// search for verilog keywords
static int get_vkeywrd(char* tstr) {
// start at $end
int l = -2;
int h = VKEYWDS - 1;
for (;;) {
int m = (l + h) / 2;
int cv;
if (m < 0) return EOF;
if ((cv = strcmp(vkeywds[m].vnam, tstr)) == 0) return vkeywds[m].vnum;
if (cv < 0)
l = m + 1;
else
h = m - 1;
if (h < l) break;
}
return EOF;
}
static void make_curmod(int level) {
char sep[2];
sep[0] = '.';
sep[1] = '\0';
strcpy(curmodG, scopesG[0]);
strcat(curmodG, sep);
for (int i = 1; i < level; i++) {
strcat(curmodG, scopesG[i]);
strcat(curmodG, sep);
}
}
// process all the lines until $enddef is reached, determines all variable
// names, seperated by a '.' between scopes, place the timescale number
// and units, and return the file location to start processing diffs
static long get_lines(FILE* fp, int* units, int* tnum, char* file_name) {
// 1+because of the line with "tok++", which would otherwise make the buffer
// smaller than expected by get_token
static char token[MAXTOKSIZE + 1];
int level = 0;
*units = 1;
*tnum = 1;
char* tok;
while (get_token(fp, token) != EOF) {
tok = token;
if (tok[0] != '$') {
printf("***ERROR Unknown token '%s' on line %d of %s\n", tok,
(fp == file1G) ? line_num1G : line_num2G, file_name);
continue;
}
tok++;
switch (get_vkeywrd(tok)) {
case V_END: break;
case V_VAR: variable(fp, file_name); break;
case V_UPSCOPE:
if (level > 0) level--;
make_curmod(level);
break;
case V_SCOPE:
get_token(fp, tok);
get_token(fp, tok);
if (level < MAXSCOPES) {
strcpy(scopesG[level], tok);
level++;
make_curmod(level);
} else {
printf("*** ERROR-exceeded max scope levels %d\n", level);
exit(0);
}
break;
case V_ATTRBEGIN:
case V_COMMENT:
case V_DATE:
case V_VERSION: read_to_end(fp); break;
case V_TIMESCALE: *units = timescale(fp, tnum); break;
case V_ENDDEF:
next_listG = true;
lastsigG = nullptr;
return ftell(fp);
default:
printf("Unknown command '%s' on line %d of %s\n", tok,
(fp == file1G) ? line_num1G : line_num2G, file_name);
break;
}
}
return -1;
}
// print the diff signal information
static void print_signal_info(signal_t* sig1, signal_t* sig2, vtime_t time1, vtime_t time2,
bool isone) {
if (time1 == time2) {
if (sig1->sig_code == sig2->sig_code)
printf("%s (%s) differs at time %lld\n", sig1->signame, sig1->ident, time1);
else
printf("%s (%s , %s) differs at time %lld\n", sig1->signame, sig1->ident, sig2->ident,
time1);
return;
} else {
if (sig1->sig_code == sig2->sig_code)
printf("%c %s (%s) at time %lld next occurence at time %lld\n", isone ? '>' : '<',
sig1->signame, sig1->ident, time1, time2);
else
printf("%c %s (%s, %s) at time %lld next occurence at time %lld\n", isone ? '>' : '<',
sig1->signame, sig1->ident, sig2->ident, time1, time2);
return;
}
}
// print the scalar value
static void print_scalar_state(signal_t* sig1, signal_t* sig2, vtime_t time1, vtime_t time2,
char state1, char state2, bool isone) {
if (time1 == time2) {
sig1->found = true;
sig2->found = true;
if (state1 != state2) {
print_signal_info(sig1, sig2, time1, time2, isone);
printf("\t%c\n\t%c\n", state1, state2);
return;
}
} else {
print_signal_info(sig1, sig2, time1, time2, isone);
printf("%c #%lld \t%c\n", isone ? '>' : '<', time1, state1);
printf("%c #%lld \t%c\n", isone ? '>' : '<', time2, state2);
return;
}
}
// print the vector value
static void print_vector_state(signal_t* sig1, signal_t* sig2, vtime_t time1, vtime_t time2,
char* sval1, char* sval2, bool isone) {
const int size1 = strlen(sval1);
const int size2 = strlen(sval2);
if (time1 == time2) {
sig1->found = true;
sig2->found = true;
if (strcmp(sval1, sval2) != 0) {
print_signal_info(sig1, sig2, time1, time2, isone);
if (size1 == size2)
printf("\t%s\n\t%s\n", sval1, sval2);
else {
if (size1 > size2) {
printf("\t%s\n\t", sval1);
for (int i = 0; i < size1 - size2; i++) putc(' ', stdout);
printf("%s\n", sval2);
} else {
printf("\t");
for (int i = 0; i < size2 - size1; i++) putc(' ', stdout);
printf("%s\n\t%s\n", sval1, sval2);
}
}
return;
}
} else {
print_signal_info(sig1, sig2, time1, time2, isone);
if (size1 == size2) {
printf("%c #%lld \t%s\n", isone ? '>' : '<', time1, sval1);
printf("%c #%lld \t%s\n", isone ? '>' : '<', time2, sval2);
} else {
if (size1 > size2) {
printf("%c #%lld \t%s\n", isone ? '>' : '<', time1, sval1);
printf("%c #%lld \t", isone ? '>' : '<', time2);
for (int i = 0; i < size1 - size2; i++) putc(' ', stdout);
printf("%s\n", sval2);
} else {
printf("%c #%lld \t", isone ? '>' : '<', time1);
for (int i = 0; i < size2 - size1; i++) putc(' ', stdout);
printf("%s\n", sval1);
printf("%c #%lld \t%s\n", isone ? '>' : '<', time2, sval2);
}
}
}
}
// print scalar edge value
static void print_scalar_edge(signal_t* sig1, signal_t* sig2, vtime_t time1, vtime_t time2,
char state1, char state2, bool isone) {
if (time1 == time2) {
sig1->found = true;
sig2->found = true;
if (state1 != state2) {
print_signal_info(sig1, sig2, time1, time2, isone);
if (sig1->state == state1)
printf("\t(%c-)\n", state1);
else
printf("\t(%c%c)\n", sig1->state, state1);
if (sig2->state == state2)
printf("\t(%c-)\n", state2);
else
printf("\t(%c%c)\n", sig2->state, state2);
return;
}
} else {
print_signal_info(sig1, sig2, time1, time2, isone);
if (sig1->state == state1)
printf("%c #%lld \t(%c-)\n", isone ? '>' : '<', time1, state1);
else
printf("%c #%lld \t(%c%c)\n", isone ? '>' : '<', time1, sig1->state, state1);
if (sig2->state == state2)
printf("%c #%lld \t(%c-)\n", isone ? '>' : '<', time2, state2);
else
printf("%c #%lld \t(%c%c)\n", isone ? '>' : '<', time2, sig2->state, state2);
return;
}
}
// print the edge values
// sig - signal pointer
// sval - sval string value
// str_size - size of sval
// search - '<' or '>' if a search was performed otherwise 'n' for none
// this is needed for wrapping of the line
static void print_edges(signal_t* sig, char* sval, int str_size, int vec_size, char searchc) {
// AIV 02/06/03 the print edges were wrong for different sizes
// for example, b10 is now b101 at the next time
// vector is the old value and sval is the new
int min;
if (str_size > vec_size)
min = vec_size;
else
min = str_size;
int tprint = 0;
for (int i = 0; i < min; i++, tprint++) {
if (sig->vector[i] == sval[i])
printf(" (%c-)", sig->vector[i]);
else
printf(" (%c%c)", sig->vector[i], sval[i]);
// only want to print 11 edges per line
if (wrap_flagG && tprint != 0 && !(tprint % EDGE_PER_LINE)) {
if (searchc != 'n')
printf(" \\\n%c\t", searchc);
else
printf(" \\\n\t");
}
}
if (str_size == vec_size) return;
if (min == vec_size) {
for (int i = min; i < str_size; i++, tprint++) {
printf(" (?%c)", sval[i]);
if (wrap_flagG && tprint != 0 && !(tprint % EDGE_PER_LINE)) {
if (searchc != 'n')
printf(" \\\n%c\t", searchc);
else
printf(" \\\n\t");
}
}
} else {
// if the first value is a '?', no need to print the rest
// since it can only be '?' on the first value change
if (sig->vector[0] != '?') {
for (int i = min; i < vec_size; i++, tprint++) {
printf(" (%c?)", sig->vector[i]);
if (wrap_flagG && tprint != 0 && !(tprint % EDGE_PER_LINE)) {
if (searchc != 'n')
printf(" \\\n%c\t", searchc);
else
printf(" \\\n\t");
}
}
}
}
}
// add space so the vector edge valued line up correctly
static void edge_space(int size1, int size2, bool is_sigone) {
int r;
if (size1 == size2) return;
if (is_sigone && size2 > size1) {
int diff = (size2 - size1) * CHAR_PER_EDGE;
if (wrap_flagG) {
r = diff / EDGE_PER_LINE;
if (r > 0) {
r = (r * EDGE_PER_LINE);
diff -= r;
if (diff <= CHAR_PER_EDGE) diff = 0;
}
}
for (int i = 0; i < diff; i++) putc(' ', stdout);
}
if (!is_sigone && size1 > size2) {
int diff = (size1 - size2) * CHAR_PER_EDGE;
if (wrap_flagG) {
r = diff / EDGE_PER_LINE;
if (r > 0) {
r = (r * EDGE_PER_LINE);
diff -= r;
if (diff <= CHAR_PER_EDGE) diff = 0;
}
}
for (int i = 0; i < diff; i++) putc(' ', stdout);
}
}
// print vector edge
static void print_vector_edge(signal_t* sig1, signal_t* sig2, vtime_t time1, vtime_t time2,
char* sval1, char* sval2, bool isone) {
int size1;
int size2;
int vsize1;
int vsize2;
int max1;
int max2;
if (time1 == time2) {
sig1->found = true;
sig2->found = true;
if (strcmp(sval1, sval2) != 0) {
if (sig1->type == REAL) {
print_vector_state(sig1, sig2, time1, time2, sval1, sval2, isone);
return;
}
print_signal_info(sig1, sig2, time1, time2, isone);
printf("\t");
size1 = strlen(sval1);
vsize1 = sig1->vector ? strlen(sig1->vector) : 0;
max1 = MAX(size1, vsize1);
size2 = strlen(sval2);
vsize2 = sig2->vector ? strlen(sig2->vector) : 0;
max2 = MAX(size2, vsize2);
edge_space(max1, max2, true);
print_edges(sig1, sval1, size1, vsize1, 'n');
if (wrap_flagG && sig1->size > EDGE_PER_LINE)
printf("\n\n\t");
else
printf("\n\t");
edge_space(max1, max2, false);
print_edges(sig2, sval2, size2, vsize2, 'n');
printf("\n");
return;
}
} else {
if (sig1->type == REAL) {
print_vector_state(sig1, sig2, time1, time2, sval1, sval2, isone);
return;
}
print_signal_info(sig1, sig2, time1, time2, isone);
char dirc = isone ? '>' : '<';
printf("%c #%lld \t", dirc, time1);
size1 = strlen(sval1);
vsize1 = sig1->vector ? strlen(sig1->vector) : 0;
max1 = MAX(size1, vsize1);
size2 = strlen(sval2);
vsize2 = sig2->vector ? strlen(sig2->vector) : 0;
max2 = MAX(size2, vsize2);
edge_space(max1, max2, true);
print_edges(sig1, sval1, size1, vsize1, dirc);
printf("\n%c #%lld \t", dirc, time2);
edge_space(max1, max2, false);
print_edges(sig2, sval2, size2, vsize2, dirc);
printf("\n");
return;
}
}
// get the next time change in the other file if there is one
static int get_nxt_chg(FILE* fp, char* fname, int* sigcode, int* bit, char* value, vtime_t* time,
bool isone) {
char* line;
char* separator;
int token_length;
static char token[MAXTOKSIZE];
while (get_token(fp, token) != EOF) {
line = token;
switch (*line++) {
// scalar cases
case '0':
case '1':
case 'z':
case 'Z':
case 'x':
case 'X':
if (extended_flagG) {
// check if it contains '<', if true parsing the eVCD
separator = strpbrk(token, "<"); // get the separator
if (!separator) {
printf("Unknown Identifier not found '%s' in file %d '%s' on line %d\n", line,
isone ? 1 : 2, fname, isone ? line_num1G : line_num2G);
continue;
} else if (separator - line > 1) {
// it is a vector
line--;
get_token(fp, token);
strncpy(value, line, separator - line);
*sigcode = VERILOG_ID_TO_POS(separator);
if (isone) {
if (!VERILOG_POS_TO_SIG1(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line,
isone ? 1 : 2, fname, isone ? line_num1G : line_num2G);
continue;
}
} else if (!VERILOG_POS_TO_SIG2(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line,
isone ? 1 : 2, fname, isone ? line_num1G : line_num2G);
continue;
}
return VECTOR;
}
}
// otherwise vcd
*bit = *(line - 1);
*sigcode = VERILOG_ID_TO_POS(line);
if (isone) {
if (!VERILOG_POS_TO_SIG1(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
continue;
}
} else if (!VERILOG_POS_TO_SIG2(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
continue;
}
return SCALAR;
break;
// vector or real cases
case 'b':
case 'r':
strncpy(value, line, MAXTOKSIZE);
get_token(fp, token);
*sigcode = VERILOG_ID_TO_POS(token);
if (isone) {
if (!VERILOG_POS_TO_SIG1(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
continue;
}
} else if (!VERILOG_POS_TO_SIG2(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
continue;
}
return VECTOR;
case 'p':
// pport_value 0_strength_component 1_strength_component identifier_code
// parsing "port_value"
strncpy(value, line, MAXTOKSIZE);
// parsing "0_strength_component"
token_length = get_token(fp, token);
strncat(value, " ", 2); // separate port value from strength
strncat(value, token, token_length);
// parsing "1_strength_component"
token_length = get_token(fp, token);
strncat(value, " ", 2); // separate port value from strength
strncat(value, token, token_length);
get_token(fp, token);
// parsing "identifier_code"
*sigcode = VERILOG_ID_TO_POS(token);
if (isone) {
if (!VERILOG_POS_TO_SIG1(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
continue;
}
} else if (!VERILOG_POS_TO_SIG2(*sigcode)) {
printf("Unknown Identifier '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
continue;
}
return VECTOR; // fake vector if single bit
// time change value
case '#': *time = (vtime_t)atoll(line); return TIME;
case '$':
// AIV 02/03/03 need to read until $end for $comment
if (!strcmp(line, "comment") || !strcmp(line, "dumpall")) read_to_end(fp);
if (!strcmp(line, "dumpports") && extended_flagG)
get_token(fp, token); // skip dumpports keyword
break;
default:
line--;
printf("***ERROR Unknown token '%s' in file %d '%s' on line %d\n", line, isone ? 1 : 2,
fname, isone ? line_num1G : line_num2G);
// TODO shouldn't die here if possible
exit(0);
continue;
break;
}
}
return EOF;
}
// used to place the string back on the fp to be used later
static void restore(FILE* fp, char* str, int size) {
{
int r = ungetc(' ', fp);
assert(r != EOF);
}
char* cp = &str[size - 1];
for (int i = 0; i < size; i++, cp--) {
int r = ungetc(*cp, fp);
if (r == EOF) {
// because ungetc implementations are not required to support
// more than one call of it in a row, this sort of usage is
// fundamentally flawed.
printf("*** ERROR Failed using ungetc. Aborting.\n");
exit(1);
}
}
}
// return true if the next signal isn't the same, otherwise false
static bool peek_nxt_sig(FILE* fp, int sigcode1, bool isone) {
static char tmp[MAXTOKSIZE], sig[MAXTOKSIZE];
const int size1 = get_token(fp, tmp);
if (size1 == EOF) return true;
char* cp = tmp;
if (tmp[0] == 'b') {
int size2;
size2 = get_token(fp, sig);
restore(fp, sig, size2);
restore(fp, tmp, size1);
cp = sig;
} else if (tmp[0] == '0' || tmp[0] == '1' || tmp[0] == 'x' || tmp[0] == 'z') {
restore(fp, tmp, size1);
cp++;
} else {
restore(fp, tmp, size1);
return true;
}
// map signal to the right sigcode
int sigcode2 = VERILOG_ID_TO_POS(cp);
// LOOKATME can this ever happen ??
if (sigcode2 < 0 || sigcode2 > max_codeG) return true;
if (isone) {
if (fd2_to_fd1_mapG[sigcode2] == -1) return true;
sigcode2 = fd2_to_fd1_mapG[sigcode2];
} else {
if (fd1_to_fd2_mapG[sigcode2] == -1) return true;
sigcode2 = fd1_to_fd2_mapG[sigcode2];
}
return sigcode1 != sigcode2;
}
// rewind to file and reset line count to the start of a #time
static void rewind_time(FILE* fp, long seek, vtime_t* time, vtime_t treset, int* linenum,
int lreset) {
*time = treset;
fseek(fp, seek, SEEK_SET);
// reset the line counts
*linenum = lreset;
}
// get the differences of a time block from #time to the next #time2 or EOF
static vtime_t get_time_diffs(FILE* mainfp, FILE* otherfp, char* mname, char* oname, long seek1,
long seek2, vtime_t ltime1, vtime_t ltime2, bool isone) {
static char svalue1[MAXTOKSIZE], svalue2[MAXTOKSIZE];
int retval = 0;
int state1 = '?';
int state2 = '?';
signal_t* sig1;
signal_t* sig2;