forked from wrcad/xictools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvl_sim.cc
4178 lines (3817 loc) · 113 KB
/
vl_sim.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
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
/*========================================================================*
* *
* Distributed by Whiteley Research Inc., Sunnyvale, California, USA *
* http://wrcad.com *
* Copyright (C) 2017 Whiteley Research Inc., all rights reserved. *
* Author: Stephen R. Whiteley, except as indicated. *
* *
* As fully as possible recognizing licensing terms and conditions *
* imposed by earlier work from which this work was derived, if any, *
* this work is released under the Apache License, Version 2.0 (the *
* "License"). You may not use this file except in compliance with *
* the License, and compliance with inherited licenses which are *
* specified in a sub-header below this one if applicable. A copy *
* of the License is provided with this distribution, or you may *
* obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* See the License for the specific language governing permissions *
* and limitations under the License. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON- *
* INFRINGEMENT. IN NO EVENT SHALL WHITELEY RESEARCH INCORPORATED *
* OR STEPHEN R. WHITELEY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
*========================================================================*
* XicTools Integrated Circuit Design System *
* *
* vl -- Verilog Simulator and Verilog support library. *
* *
*========================================================================*
$Id:$
*========================================================================*/
#include <stdio.h>
#include <math.h>
#include "vl_list.h"
#include "vl_st.h"
#include "vl_defs.h"
#include "vl_types.h"
// needed to find top modules in resolve routines
vl_simulator *vl_context::simulator;
extern bool mmon_start(int);
extern bool mmon_stop();
extern bool mmon_dump(char*);
//---------------------------------------------------------------------------
// Local
//---------------------------------------------------------------------------
template<class T> void
vl_setup_list(vl_simulator *sim, lsList<T> *stmts)
{
if (!stmts)
return;
lsGen<T> gen(stmts);
T stmt;
while (gen.next(&stmt))
stmt->setup(sim);
}
template<class T> void
vl_disable_list(lsList<T> *stmts, vl_stmt *s)
{
if (!stmts)
return;
lsGen<T> gen(stmts);
T stmt;
while (gen.next(&stmt))
stmt->disable(s);
}
// Return the vl_port corresponding to name, or num if name is nil
//
static vl_port *
find_port(lsList<vl_port*> *ports, const char *name, int num = 0)
{
lsGen<vl_port*> pgen(ports);
vl_port *port;
int cnt = 0;
while (pgen.next(&port)) {
if (name) {
if (port->port_exp) {
if (port->name && !strcmp(port->name, name))
return (port);
lsGen<vl_var*> vgen(port->port_exp);
vl_var *v;
if (vgen.next(&v)) {
if (v->name && !strcmp(v->name, name))
return (port);
}
}
}
else if (num == cnt)
return (port);
cnt++;
}
return (0);
}
// Comparison function for combinational primitive tokens
//
static bool
dcmp(unsigned char a, unsigned char b)
{
if (a == b)
return (true);
if (a == PrimQ || b == PrimQ)
return (true);
if ((a == PrimB && b <= Prim1) || (b == PrimB && a <= Prim1))
return (true);
return (false);
}
// Comparison function for sequential primitive tokens
//
static bool
dcmpseq(unsigned char a, unsigned char l, unsigned char c)
{
if (c == Prim0)
return (a == Prim0);
if (c == Prim1)
return (a == Prim1);
if (c == PrimX)
return (a == PrimX);
if (c == PrimB)
return (a == Prim0 || a == Prim1);
if (c == PrimQ)
return (a == Prim0 || a == Prim1 || a == PrimX);
if (c == PrimR)
return (l == Prim0 && a == Prim1);
if (c == PrimF)
return (l == Prim1 && a == Prim0);
if (c == PrimP)
return (((l == Prim0 && (a == Prim1 || a == PrimX)) ||
(l == PrimX && a == Prim1)));
if (c == PrimN)
return (((l == Prim1 && (a == Prim0 || a == PrimX)) ||
(l == PrimX && a == Prim0)));
if (c == PrimS)
return (l != a);
if (c == PrimM)
return (l == a);
if (c == Prim0X)
return (l == Prim0 && a == PrimX);
if (c == Prim1X)
return (l == Prim1 && a == PrimX);
if (c == PrimX0)
return (l == PrimX && a == Prim0);
if (c == PrimX1)
return (l == PrimX && a == Prim1);
if (c == PrimXB)
return (l == PrimX && (a == Prim0 || a == Prim1));
if (c == PrimBX)
return ((l == Prim0 || l == Prim1) && a == PrimX);
if (c == PrimBB)
return ((l == Prim0 && a == Prim1) || (l == Prim1 && a == Prim0));
if (c == PrimQ0)
return ((l == Prim1 || l == PrimX) && a == Prim0);
if (c == PrimQ1)
return ((l == Prim0 || l == PrimX) && a == Prim1);
if (c == PrimQB)
return ((l == Prim0 || l == Prim1 || l == PrimX) &&
(a == Prim0 || a == Prim1) && l != a);
if (c == Prim0Q)
return (l == Prim0 && (a == Prim1 || a == PrimX));
if (c == Prim1Q)
return (l == Prim1 && (a == Prim0 || a == PrimX));
if (c == PrimBQ)
return ((l == Prim0 || l == Prim1) &&
(a == Prim0 || a == Prim1 || a == PrimX) && l != a);
return (false);
}
//---------------------------------------------------------------------------
// Simulator objects
//---------------------------------------------------------------------------
vl_simulator::vl_simulator()
{
description = 0;
dmode = DLYtyp;
stop = VLrun;
first_point = true;
monitor_state = fmonitor_state = false;
monitors = 0;
fmonitors = 0;
time = 0;
context = 0;
timewheel = 0;
next_actions = 0;
fj_end = 0;
top_modules = 0;
dbg_flags = 0;
channels[0] = 0;
channels[1] = &cout;
channels[2] = &cerr;
for (int i = 3; i < 32; i++) channels[i] = 0;
dmpfile = 0;
dmpcx = 0;
dmpdepth = 0;
dmpstatus = 0;
dmpdata = 0;
dmplast = 0;
dmpsize = 0;
dmpindx = 0;
tfunit = 0;
tfprec = 0;
tfsuffix = 0;
tfwidth = 20;
}
vl_simulator::~vl_simulator()
{
vl_context::simulator = 0;
vl_var::simulator = 0;
if (description && description->simulator == this)
description->simulator = 0;
while (monitors) {
vl_monitor *m = monitors;
monitors = monitors->next;
delete m;
}
while (fmonitors) {
vl_monitor *m = fmonitors;
monitors = fmonitors->next;
delete m;
}
vl_context::destroy(context);
delete timewheel;
vl_action_item::destroy(next_actions);
vl_action_item::destroy(fj_end);
if (top_modules) {
for (int i = 0; i < top_modules->num; i++)
delete top_modules->mods[i];
}
delete top_modules;
// close any open files
close_files();
delete dmpcx;
delete [] dmplast;
delete dmpdata;
}
// Initialize the simulator, call before simulation, call with desc = 0
// to clear/free everything
//
bool
vl_simulator::initialize(vl_desc *desc, VLdelayType dly, int dbg)
{
const vl_simulator *vlsim = this;
if (!vlsim)
return (false);
description = 0;
dmode = DLYtyp;
stop = VLrun;
monitor_state = fmonitor_state = false;
while (monitors) {
vl_monitor *m = monitors;
monitors = monitors->next;
delete m;
}
while (fmonitors) {
vl_monitor *m = fmonitors;
monitors = fmonitors->next;
delete m;
}
time = 0;
vl_context::destroy(context);
context = 0;
delete timewheel;
timewheel = 0;
vl_action_item::destroy(next_actions);
next_actions = 0;
vl_action_item::destroy(fj_end);
fj_end = 0;
if (top_modules) {
for (int i = 0; i < top_modules->num; i++)
delete top_modules->mods[i];
}
delete top_modules;
top_modules = 0;
dbg_flags = 0;
time_data.u.t = 0;
close_files();
delete dmpcx;
dmpcx = 0;
dmpdepth = 0;
dmpstatus = 0;
delete dmpdata;
dmpdata = 0;
delete [] dmplast;
dmplast = 0;
dmpsize = 0;
dmpindx = 0;
tfunit = 0;
tfprec = 0;
delete [] tfsuffix;
tfsuffix = 0;
tfwidth = 20;
vl_new_var(CXclear);
if (!desc)
return (true);
dmode = dly;
dbg_flags = dbg;
description = desc;
vl_context::simulator = this;
vl_var::simulator = this;
timewheel = new vl_timeslot(0);
vl_module *mod;
lsGen<vl_module*> gen(desc->modules);
int count = 0;
while (gen.next(&mod))
if (!mod->inst_count && !mod->ports)
count++;
if (count <= 0) {
vl_error("no top module!");
return (false);
}
top_modules = new vl_top_mod_list;
top_modules->num = count;
top_modules->mods = new vl_module*[count];
gen.reset(desc->modules);
count = 0;
while (gen.next(&mod)) {
if (!mod->inst_count && !mod->ports) {
// copy the module, fills in blk_st and task_st
top_modules->mods[count] = mod->copy();
count++;
}
}
for (int i = 0; i < top_modules->num; i++) {
context = context->push(top_modules->mods[i]);
top_modules->mods[i]->init();
context = context->pop();
}
for (int i = 0; i < top_modules->num; i++) {
context = context->push(top_modules->mods[i]);
top_modules->mods[i]->setup(this);
context = context->pop();
}
if (dbg_flags & DBG_mod_dmp) {
for (int i = 0; i < top_modules->num; i++)
top_modules->mods[i]->dump(cout);
}
monitor_state = true;
fmonitor_state = true;
context = 0;
vl_new_var(CXclear);
time_data.data_type = Dtime;
time_data.u.t = 0;
tfunit = (int)(log10(description->tstep) - 0.5);
return (true);
}
// Perform the simulation
//
bool
vl_simulator::simulate()
{
vl_context::simulator = this;
vl_var::simulator = this;
if (dbg_flags & DBG_desc)
description->dump(cout);
while (timewheel && stop == VLrun) {
vl_timeslot *t = timewheel;
if (dbg_flags & DBG_tslot) {
for ( ; t; t = t->next)
t->print(cout);
cout << "\n\n";
}
vl_new_var(CXclear);
time_data.u.t = timewheel->time;
timewheel->eval_slot(this);
if (monitor_state && monitors) {
for (vl_monitor *m = monitors; m; m = m->next) {
vl_context *tcx = context;
context = m->cx;
if (monitor_change(m->args))
display_print(m->args, cout, m->dtype, 0);
context = tcx;
}
}
if (fmonitor_state && fmonitors) {
for (vl_monitor *m = fmonitors; m; m = m->next) {
vl_context *tcx = context;
context = m->cx;
if (monitor_change(m->args))
fdisplay_print(m->args, m->dtype, 0);
context = tcx;
}
}
if (dmpstatus & DMP_ACTIVE)
do_dump();
t = timewheel->next;
delete timewheel;
timewheel = t;
first_point = false;
}
// close any open files
close_files();
return (true);
}
// Step one time point
//
VLstopType
vl_simulator::step(vl_time_t vtime)
{
vl_context::simulator = this;
vl_var::simulator = this;
while (timewheel && stop == VLrun && timewheel->time <= vtime) {
time_data.u.t = timewheel->time;
timewheel->eval_slot(this);
if (monitor_state && monitors) {
for (vl_monitor *m = monitors; m; m = m->next) {
vl_context *tcx = context;
context = m->cx;
if (monitor_change(m->args))
display_print(m->args, cout, m->dtype, 0);
context = tcx;
}
}
if (fmonitor_state && fmonitors) {
for (vl_monitor *m = fmonitors; m; m = m->next) {
vl_context *tcx = context;
context = m->cx;
if (monitor_change(m->args))
fdisplay_print(m->args, m->dtype, 0);
context = tcx;
}
}
if (dmpstatus & DMP_ACTIVE)
do_dump();
vl_timeslot *t = timewheel->next;
delete timewheel;
timewheel = t;
first_point = false;
vl_new_var(CXclear);
}
// Always keep a non-nil timewheel when stepping.
if (!timewheel) {
vl_timeslot *ts = new vl_timeslot(0);
ts->next = timewheel;
timewheel = ts;
}
if (stop != VLrun)
close_files();
return (stop);
}
// Close all open files.
//
void
vl_simulator::close_files()
{
delete dmpfile;
dmpfile = 0;
for (int i = 3; i < 32; i++) {
delete channels[i];
channels[i] = 0;
}
}
// Flush all open files.
//
void
vl_simulator::flush_files()
{
if (dmpfile)
dmpfile->flush();
for (int i = 3; i < 32; i++) {
if (channels[i])
channels[i]->flush();
}
}
// End vl_simulator functions
// Copy context
//
vl_context *
vl_context::copy()
{
vl_context *cx0 = 0, *cx = 0;
for (vl_context *c = this; c; c = c->parent) {
if (!cx0)
cx = cx0 = new vl_context(*c);
else {
cx->parent = new vl_context(*c);
cx = cx->parent;
}
cx->parent = 0;
}
return (cx0);
}
// Push to new context, return the new context
//
vl_context *
vl_context::push()
{
vl_context *cx = new vl_context();
cx->parent = this;
return (cx);
}
vl_context *
vl_context::push(vl_mp *m)
{
vl_context *cx = push();
if (m->type == ModDecl)
cx->module = (vl_module*)m;
else if (m->type == CombPrimDecl || m->type == SeqPrimDecl)
cx->primitive = (vl_primitive*)m;
else {
vl_error("internal, bad object type %d (not mod/prim)", m->type);
simulator->abort();
}
return (cx);
}
vl_context *
vl_context::push(vl_module *m)
{
vl_context *cx = push();
if (m->type == ModDecl)
cx->module = m;
else {
vl_error("internal, bad object type %d (not module)", m->type);
simulator->abort();
}
return (cx);
}
vl_context *
vl_context::push(vl_primitive *p)
{
vl_context *cx = push();
if (p->type == CombPrimDecl || p->type == SeqPrimDecl)
cx->primitive = p;
else {
vl_error("internal, bad object type %d (not primitive)", p->type);
simulator->abort();
}
return (cx);
}
vl_context *
vl_context::push(vl_task *t)
{
vl_context *cx = push();
if (t->type == TaskDecl)
cx->task = t;
else {
vl_error("internal, bad object type %d (not task)", t->type);
simulator->abort();
}
return (cx);
}
vl_context *
vl_context::push(vl_function *f)
{
vl_context *cx = push();
if (f->type >= IntFuncDecl && f->type <= RangeFuncDecl)
cx->function = f;
else {
vl_error("internal, bad object type %d (not function)", f->type);
simulator->abort();
}
return (cx);
}
vl_context *
vl_context::push(vl_begin_end_stmt *b)
{
vl_context *cx = push();
if (b->type == BeginEndStmt)
cx->block = b;
else {
vl_error("internal, bad object type %d (not begin/end)", b->type);
simulator->abort();
}
return (cx);
}
vl_context *
vl_context::push(vl_fork_join_stmt *f)
{
vl_context *cx = push();
if (f->type == ForkJoinStmt)
cx->fjblk = f;
else {
vl_error("internal, bad object type %d (not fork/join)", f->type);
simulator->abort();
}
return (cx);
}
// Pop to previous context, return the previous context
//
vl_context *
vl_context::pop()
{
const vl_context *vcx = this;
if (vcx) {
vl_context *cx = parent;
delete this;
return (cx);
}
return (0);
}
// Return true if the passed object is in the context hierarchy
//
bool
vl_context::in_context(vl_stmt *blk)
{
if (!blk)
return (false);
if (blk->type == BeginEndStmt) {
for (vl_context *cx = this; cx; cx = cx->parent) {
if (cx->block == blk)
return (true);
}
}
else if (blk->type == ForkJoinStmt) {
for (vl_context *cx = this; cx; cx = cx->parent) {
if (cx->fjblk == blk)
return (true);
}
}
else if (blk->type == TaskDecl) {
for (vl_context *cx = this; cx; cx = cx->parent) {
if (cx->task == blk)
return (true);
}
}
return (false);
}
// Retrieve a value for the named variable, search present context
// exclusively if thisonly is true
//
vl_var *
vl_context::lookup_var(const char *name, bool thisonly)
{
const char *bname = name;
vl_context *cx = this;
while (cx) {
table<vl_var*> *st = cx->resolve_st(&bname, false);
if (st) {
vl_var *data;
if (st->lookup(bname, &data) && data)
return (data);
}
if (thisonly || bname != name)
break;
if (cx->module || cx->primitive)
break;
cx = cx->parent;
}
return (0);
}
// Return the named vl_begin_end_stmt or vl_fork_join_stmt
//
vl_stmt *
vl_context::lookup_block(const char *name)
{
const char *bname = name;
vl_context cvar;
if (!resolve_cx(&bname, cvar, false)) {
if (bname != name)
// unresolved path prefix
return (0);
else
// no path prefix
cvar = *this;
}
vl_context *cx = &cvar;
while (cx) {
vl_stmt *stmt;
if (cx->block) {
if (cx->block->name && !strcmp(cx->block->name, bname))
return (cx->block);
if (cx->block->blk_st &&
cx->block->blk_st->lookup(bname, &stmt) && stmt)
return (stmt);
}
if (cx->fjblk) {
if (cx->fjblk->name && !strcmp(cx->fjblk->name, bname))
return (cx->fjblk);
if (cx->fjblk->blk_st &&
cx->fjblk->blk_st->lookup(bname, &stmt) && stmt)
return (stmt);
}
if (cx->function) {
if (cx->function->blk_st &&
cx->function->blk_st->lookup(bname, &stmt) && stmt)
return (stmt);
}
if (cx->task) {
if (cx->task->blk_st &&
cx->task->blk_st->lookup(bname, &stmt) && stmt)
return (stmt);
}
if (cx->module) {
if (cx->module->blk_st &&
cx->module->blk_st->lookup(bname, &stmt) && stmt)
return (stmt);
}
if (bname != name)
break;
if (cx->module)
break;
cx = cx->parent;
}
return (0);
}
// Return the named vl_task
//
vl_task *
vl_context::lookup_task(const char *name)
{
const char *bname = name;
table<vl_task*> *st = resolve_task(&bname);
if (st) {
vl_task *vtask;
if (st->lookup(bname, &vtask) && vtask)
return (vtask);
}
return (0);
}
// Return the named vl_function
//
vl_function *
vl_context::lookup_func(const char *name)
{
const char *bname = name;
table<vl_function*> *st = resolve_func(&bname);
if (st) {
vl_function *func;
if (st->lookup(bname, &func) && func)
return (func);
}
return (0);
}
vl_inst *
vl_context::lookup_mp(const char *name)
{
const char *bname = name;
table<vl_inst*> *st = resolve_mp(&bname);
if (st) {
vl_inst *mp;
if (st->lookup(bname, &mp) && mp)
return (mp);
}
return (0);
}
// Return the symbol table for name, resolving the '.' notation if any,
// if crt is true, create new symbol table if empty. The base name is
// returned in 'name'
//
table<vl_var*> *
vl_context::resolve_st(const char **name, bool crt)
{
vl_context cvar;
const char *bname = *name;
if (!resolve_cx(name, cvar, false)) {
if (bname != *name)
// unresolved path prefix
return (0);
else
// no path prefix
cvar = *this;
}
table<vl_var*> *st = 0;
if (cvar.module) {
if (crt && !cvar.module->sig_st)
cvar.module->sig_st = new table<vl_var*>;
st = cvar.module->sig_st;
}
else if (cvar.primitive) {
if (crt && !cvar.primitive->sig_st)
cvar.primitive->sig_st = new table<vl_var*>;
st = cvar.primitive->sig_st;
}
else if (cvar.task) {
if (crt && !cvar.task->sig_st)
cvar.task->sig_st = new table<vl_var*>;
st = cvar.task->sig_st;
}
else if (cvar.function) {
if (crt && !cvar.function->sig_st)
cvar.function->sig_st = new table<vl_var*>;
st = cvar.function->sig_st;
}
else if (cvar.block) {
if (crt && !cvar.block->sig_st)
cvar.block->sig_st = new table<vl_var*>;
st = cvar.block->sig_st;
}
else if (cvar.fjblk) {
if (crt && !cvar.fjblk->sig_st)
cvar.fjblk->sig_st = new table<vl_var*>;
st = cvar.fjblk->sig_st;
}
return (st);
}
table<vl_task*> *
vl_context::resolve_task(const char **name)
{
vl_context cvar;
const char *bname = *name;
if (!resolve_cx(name, cvar, false)) {
if (bname != *name)
// unresolved path prefix
return (0);
else
// no path prefix
cvar = *this;
}
table<vl_task*> *st = 0;
vl_context *cx = &cvar;
while (cx) {
if (cx->module) {
st = cx->module->task_st;
break;
}
cx = cx->parent;
}
return (st);
}
table<vl_function*> *
vl_context::resolve_func(const char **name)
{
vl_context cvar;
const char *bname = *name;
if (!resolve_cx(name, cvar, false)) {
if (bname != *name)
// unresolved path prefix
return (0);
else
// no path prefix
cvar = *this;
}
table<vl_function*> *st = 0;
vl_context *cx = &cvar;
while (cx) {
if (cx->module) {
st = cx->module->func_st;
break;
}
cx = cx->parent;
}
return (st);
}
table<vl_inst*> *
vl_context::resolve_mp(const char **name)
{
vl_context cvar;
const char *bname = *name;
if (!resolve_cx(name, cvar, false)) {
if (bname != *name)
// unresolved path prefix
return (0);
else
// no path prefix
cvar = *this;
}
table<vl_inst*> *st = 0;
vl_context *cx = &cvar;
while (cx) {
if (cx->module) {
st = cx->module->inst_st;
break;
}
cx = cx->parent;
}
return (st);
}
// Strip the prefix from name, and return the object which should contain
// the base symbol name in cvar. If error or no prefix, return false.
// If modonly is true, return the module containing item.
//
bool
vl_context::resolve_cx(const char **name, vl_context &cvar, bool modonly)
{
const char *r = *name;
const char *rp = 0;
while (*r) {
if (*r == '\\') {
while (*r && !isspace(*r))
r++;
while (*r && isspace(*r))
r++;
}
if (*r == '.')
rp = r;
r++;
}
r = rp;
if (!r)
return (false);
int os = r - *name;
char buf[256];
strcpy(buf, *name);
*name = r + 1;
*(buf + os) = 0;
r = buf;
char *s = (char*)r;
while (*s) {
if (*s == '\\') {
while (*s && !isspace(*s))
s++;
while (*s && isspace(*r))
r++;
}
if (*s == '.')
break;
s++;
}
bool fdot = false;
if (*s == '.') {
*s++ = 0;
fdot = true;
}
// look for top module
cvar.module = 0;
cvar.parent = 0;
for (int i = 0; i < simulator->top_modules->num; i++) {
if (!strcmp(r, simulator->top_modules->mods[i]->name)) {
cvar.module = simulator->top_modules->mods[i];
r = s;
break;
}
}
if (!cvar.module) {
vl_context *cx = this;