-
Notifications
You must be signed in to change notification settings - Fork 17
/
tclqrouter.c
3359 lines (2990 loc) · 90.7 KB
/
tclqrouter.c
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
/*--------------------------------------------------------------*/
/* tclqrouter.c: */
/* Tcl routines for qrouter command-line functions */
/* Copyright (c) 2013 Tim Edwards, Open Circuit Design, Inc. */
/*--------------------------------------------------------------*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <math.h> /* for round() */
#include <tk.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "qrouter.h"
#include "mask.h"
#include "maze.h"
#include "qconfig.h"
#include "lef.h"
#include "def.h"
#include "graphics.h"
#include "node.h"
#include "output.h"
#include "tkSimple.h"
/* Global variables */
Tcl_HashTable QrouterTagTable;
Tcl_Interp *qrouterinterp;
Tcl_Interp *consoleinterp;
int stepnet = -1;
int batchmode = 0;
/* Command structure */
typedef struct {
const char *cmdstr;
int (*func)(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
} cmdstruct;
/* Forward declarations of commands */
static int qrouter_map(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_start(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_stage1(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_stage2(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_stage3(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_cleanup(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_writedef(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_writefailed(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_writedelays(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_antenna(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_readdef(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_readlef(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_readconfig(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_failing(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_cost(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_tag(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_remove(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_obs(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_layerinfo(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_priority(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_ignore(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_via(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_resolution(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_congested(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_layers(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_drc(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_query(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_passes(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_vdd(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_gnd(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_verbose(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_print(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_quit(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_pitchx(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_pitchy(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static int qrouter_unblock(
ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
static cmdstruct qrouter_commands[] =
{
{"tag", qrouter_tag},
{"start", qrouter_start},
{"stage1", qrouter_stage1},
{"stage2", qrouter_stage2},
{"stage3", qrouter_stage3},
{"cleanup", qrouter_cleanup},
{"write_def", qrouter_writedef},
{"read_def", qrouter_readdef},
{"read_lef", qrouter_readlef},
{"read_config", qrouter_readconfig},
{"write_delays", qrouter_writedelays},
{"antenna", qrouter_antenna},
{"write_failed", qrouter_writefailed},
{"layer_info", qrouter_layerinfo},
{"obstruction", qrouter_obs},
{"ignore", qrouter_ignore},
{"priority", qrouter_priority},
{"pitchx", qrouter_pitchx},
{"pitchy", qrouter_pitchy},
{"unblock", qrouter_unblock},
{"via", qrouter_via},
{"resolution", qrouter_resolution},
{"congested", qrouter_congested},
{"layers", qrouter_layers},
{"drc", qrouter_drc},
{"passes", qrouter_passes},
{"query", qrouter_query},
{"vdd", qrouter_vdd},
{"gnd", qrouter_gnd},
{"failing", qrouter_failing},
{"remove", qrouter_remove},
{"cost", qrouter_cost},
{"map", qrouter_map},
{"verbose", qrouter_verbose},
{"redraw", redraw},
{"print", qrouter_print},
{"quit", qrouter_quit},
{"", NULL} /* sentinel */
};
/*-----------------------*/
/* Tcl 8.4 compatibility */
/*-----------------------*/
#ifndef CONST84
#define CONST84
#endif
/*----------------------------------------------------------------------*/
/* Deal with systems which don't define va_copy(). */
/*----------------------------------------------------------------------*/
#ifndef HAVE_VA_COPY
#ifdef HAVE___VA_COPY
#define va_copy(a, b) __va_copy(a, b)
#else
#define va_copy(a, b) a = b
#endif
#endif
#ifdef ASG
extern int SetDebugLevel(int *level);
#endif
/*----------------------------------------------------------------------*/
/* Reimplement strdup() to use Tcl_Alloc(). */
/*----------------------------------------------------------------------*/
char *Tcl_Strdup(const char *s)
{
char *snew;
int slen;
slen = 1 + strlen(s);
snew = Tcl_Alloc(slen);
if (snew != NULL)
memcpy(snew, s, slen);
return snew;
}
/*----------------------------------------------------------------------*/
/* Reimplement vfprintf() as a call to Tcl_Eval(). */
/* */
/* Since the string goes through the interpreter, we need to escape */
/* various characters like brackets, braces, dollar signs, etc., that */
/* will otherwise be modified by the interpreter. */
/*----------------------------------------------------------------------*/
void tcl_vprintf(FILE *f, const char *fmt, va_list args_in)
{
va_list args;
static char outstr[128] = "puts -nonewline std";
char *outptr, *bigstr = NULL, *finalstr = NULL;
int i, nchars, escapes = 0;
/* If we are printing an error message, we want to bring attention */
/* to it by mapping the console window and raising it, as necessary. */
/* I'd rather do this internally than by Tcl_Eval(), but I can't */
/* find the right window ID to map! */
if ((f == stderr) && (consoleinterp != qrouterinterp)) {
Tk_Window tkwind;
tkwind = Tk_MainWindow(consoleinterp);
if ((tkwind != NULL) && (!Tk_IsMapped(tkwind)))
Tcl_Eval(consoleinterp, "wm deiconify .\n");
Tcl_Eval(consoleinterp, "raise .\n");
}
strcpy (outstr + 19, (f == stderr) ? "err \"" : "out \"");
outptr = outstr;
/* This mess circumvents problems with systems which do not have */
/* va_copy() defined. Some define __va_copy(); otherwise we must */
/* assume that args = args_in is valid. */
va_copy(args, args_in);
nchars = vsnprintf(outptr + 24, 102, fmt, args);
va_end(args);
if (nchars >= 102) {
va_copy(args, args_in);
bigstr = Tcl_Alloc(nchars + 26);
strncpy(bigstr, outptr, 24);
outptr = bigstr;
vsnprintf(outptr + 24, nchars + 2, fmt, args);
va_end(args);
}
else if (nchars == -1) nchars = 126;
for (i = 24; *(outptr + i) != '\0'; i++) {
if (*(outptr + i) == '\"' || *(outptr + i) == '[' ||
*(outptr + i) == ']' || *(outptr + i) == '\\' ||
*(outptr + i) == '$')
escapes++;
}
if (escapes > 0) {
finalstr = Tcl_Alloc(nchars + escapes + 26);
strncpy(finalstr, outptr, 24);
escapes = 0;
for (i = 24; *(outptr + i) != '\0'; i++) {
if (*(outptr + i) == '\"' || *(outptr + i) == '[' ||
*(outptr + i) == ']' || *(outptr + i) == '\\' ||
*(outptr + i) == '$') {
*(finalstr + i + escapes) = '\\';
escapes++;
}
*(finalstr + i + escapes) = *(outptr + i);
}
outptr = finalstr;
}
*(outptr + 24 + nchars + escapes) = '\"';
*(outptr + 25 + nchars + escapes) = '\0';
Tcl_Eval(consoleinterp, outptr);
if (bigstr != NULL) Tcl_Free(bigstr);
if (finalstr != NULL) Tcl_Free(finalstr);
}
/*------------------------------------------------------*/
/* Console output flushing which goes along with the */
/* routine tcl_vprintf() above. */
/*------------------------------------------------------*/
void tcl_stdflush(FILE *f)
{
Tcl_SavedResult state;
static char stdstr[] = "::flush stdxxx";
char *stdptr = stdstr + 11;
Tcl_SaveResult(qrouterinterp, &state);
strncpy(stdptr, (f == stderr) ? "err" : "out", 3);
Tcl_Eval(qrouterinterp, stdstr);
Tcl_RestoreResult(qrouterinterp, &state);
}
/*----------------------------------------------------------------------*/
/* Reimplement fprintf() as a call to Tcl_Eval(). */
/*----------------------------------------------------------------------*/
void tcl_printf(FILE *f, const char *format, ...)
{
va_list ap;
va_start(ap, format);
tcl_vprintf(f, format, ap);
va_end(ap);
}
/*----------------------------------------------------------------------*/
/* Implement tag callbacks on functions */
/* Find any tags associated with a command and execute them. */
/*----------------------------------------------------------------------*/
int QrouterTagCallback(Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
int objidx, result = TCL_OK;
char *postcmd, *substcmd, *newcmd, *sptr, *sres;
char *croot = Tcl_GetString(objv[0]);
Tcl_HashEntry *entry;
Tcl_SavedResult state;
int reset = FALSE;
int i, llen;
entry = Tcl_FindHashEntry(&QrouterTagTable, croot);
postcmd = (entry) ? (char *)Tcl_GetHashValue(entry) : NULL;
if (postcmd)
{
substcmd = (char *)Tcl_Alloc(strlen(postcmd) + 1);
strcpy(substcmd, postcmd);
sptr = substcmd;
/*--------------------------------------------------------------*/
/* Parse "postcmd" for Tk-substitution escapes */
/* Allowed escapes are: */
/* %W substitute the tk path of the calling window */
/* %r substitute the previous Tcl result string */
/* %R substitute the previous Tcl result string and */
/* reset the Tcl result. */
/* %[0-5] substitute the argument to the original command */
/* %N substitute all arguments as a list */
/* %% substitute a single percent character */
/* %* (all others) no action: print as-is. */
/*--------------------------------------------------------------*/
while ((sptr = strchr(sptr, '%')) != NULL)
{
switch (*(sptr + 1))
{
case 'W': {
char *tkpath = NULL;
Tk_Window tkwind = Tk_MainWindow(interp);
if (tkwind != NULL) tkpath = Tk_PathName(tkwind);
if (tkpath == NULL)
newcmd = (char *)Tcl_Alloc(strlen(substcmd));
else
newcmd = (char *)Tcl_Alloc(strlen(substcmd) + strlen(tkpath));
strcpy(newcmd, substcmd);
if (tkpath == NULL)
strcpy(newcmd + (int)(sptr - substcmd), sptr + 2);
else
{
strcpy(newcmd + (int)(sptr - substcmd), tkpath);
strcat(newcmd, sptr + 2);
}
Tcl_Free(substcmd);
substcmd = newcmd;
sptr = substcmd;
} break;
case 'R':
reset = TRUE;
case 'r':
sres = (char *)Tcl_GetStringResult(interp);
newcmd = (char *)Tcl_Alloc(strlen(substcmd)
+ strlen(sres) + 1);
strcpy(newcmd, substcmd);
sprintf(newcmd + (int)(sptr - substcmd), "\"%s\"", sres);
strcat(newcmd, sptr + 2);
Tcl_Free(substcmd);
substcmd = newcmd;
sptr = substcmd;
break;
case '0': case '1': case '2': case '3': case '4': case '5':
objidx = (int)(*(sptr + 1) - '0');
if ((objidx >= 0) && (objidx < objc))
{
newcmd = (char *)Tcl_Alloc(strlen(substcmd)
+ strlen(Tcl_GetString(objv[objidx])));
strcpy(newcmd, substcmd);
strcpy(newcmd + (int)(sptr - substcmd),
Tcl_GetString(objv[objidx]));
strcat(newcmd, sptr + 2);
Tcl_Free(substcmd);
substcmd = newcmd;
sptr = substcmd;
}
else if (objidx >= objc)
{
newcmd = (char *)Tcl_Alloc(strlen(substcmd) + 1);
strcpy(newcmd, substcmd);
strcpy(newcmd + (int)(sptr - substcmd), sptr + 2);
Tcl_Free(substcmd);
substcmd = newcmd;
sptr = substcmd;
}
else sptr++;
break;
case 'N':
llen = 1;
for (i = 1; i < objc; i++)
llen += (1 + strlen(Tcl_GetString(objv[i])));
newcmd = (char *)Tcl_Alloc(strlen(substcmd) + llen);
strcpy(newcmd, substcmd);
strcpy(newcmd + (int)(sptr - substcmd), "{");
for (i = 1; i < objc; i++) {
strcat(newcmd, Tcl_GetString(objv[i]));
if (i < (objc - 1))
strcat(newcmd, " ");
}
strcat(newcmd, "}");
strcat(newcmd, sptr + 2);
Tcl_Free(substcmd);
substcmd = newcmd;
sptr = substcmd;
break;
case '%':
newcmd = (char *)Tcl_Alloc(strlen(substcmd) + 1);
strcpy(newcmd, substcmd);
strcpy(newcmd + (int)(sptr - substcmd), sptr + 1);
Tcl_Free(substcmd);
substcmd = newcmd;
sptr = substcmd;
break;
default:
break;
}
}
/* Fprintf(stderr, "Substituted tag callback is \"%s\"\n", substcmd); */
/* Flush(stderr); */
Tcl_SaveResult(interp, &state);
result = Tcl_Eval(interp, substcmd);
if ((result == TCL_OK) && (reset == FALSE))
Tcl_RestoreResult(interp, &state);
else
Tcl_DiscardResult(&state);
Tcl_Free(substcmd);
}
return result;
}
/*--------------------------------------------------------------*/
/* Add a command tag callback */
/*--------------------------------------------------------------*/
static int
qrouter_tag(ClientData clientData,
Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
Tcl_HashEntry *entry;
char *hstring;
int new;
if (objc != 2 && objc != 3)
return TCL_ERROR;
entry = Tcl_CreateHashEntry(&QrouterTagTable, Tcl_GetString(objv[1]), &new);
if (entry == NULL) return TCL_ERROR;
hstring = (char *)Tcl_GetHashValue(entry);
if (objc == 2)
{
Tcl_SetResult(interp, hstring, NULL);
return TCL_OK;
}
if (strlen(Tcl_GetString(objv[2])) == 0)
{
Tcl_DeleteHashEntry(entry);
}
else
{
hstring = Tcl_Strdup(Tcl_GetString(objv[2]));
Tcl_SetHashValue(entry, hstring);
}
return TCL_OK;
}
/*--------------------------------------------------------------*/
/* Initialization procedure for Tcl/Tk */
/*--------------------------------------------------------------*/
int
Qrouter_Init(Tcl_Interp *interp)
{
int cmdidx;
char command[256];
char version_string[20];
Tk_Window tktop;
char *nullgvar;
/* Interpreter sanity checks */
if (interp == NULL) return TCL_ERROR;
/* Remember the interpreter */
qrouterinterp = interp;
if (Tcl_InitStubs(interp, "8.5", 0) == NULL) return TCL_ERROR;
strcpy(command, "qrouter::");
/* NOTE: Qrouter makes calls to Tk routines that may or may not */
/* exist, depending on whether qrouter was called with or without */
/* graphics. We depend on the Tcl/Tk stubs methods to allow */
/* qrouter to run without linking to Tk libraries. */
nullgvar = (char *)Tcl_GetVar(interp, "no_graphics_mode", TCL_GLOBAL_ONLY);
if ((nullgvar == NULL) || !strcasecmp(nullgvar, "false")) {
if (Tk_InitStubs(interp, "8.5", 0) == NULL) return TCL_ERROR;
tktop = Tk_MainWindow(interp);
batchmode = 0;
}
else {
tktop = NULL;
batchmode = 1;
}
/* Create all of the commands (except "simple") */
for (cmdidx = 0; qrouter_commands[cmdidx].func != NULL; cmdidx++) {
sprintf(command + 9, "%s", qrouter_commands[cmdidx].cmdstr);
Tcl_CreateObjCommand(interp, command,
(Tcl_ObjCmdProc *)qrouter_commands[cmdidx].func,
(ClientData)tktop, (Tcl_CmdDeleteProc *) NULL);
}
if (tktop != NULL) {
Tcl_CreateObjCommand(interp, "simple",
(Tcl_ObjCmdProc *)Tk_SimpleObjCmd,
(ClientData)tktop, (Tcl_CmdDeleteProc *) NULL);
}
Tcl_Eval(interp, "lappend auto_path .");
sprintf(version_string, "%s", VERSION);
Tcl_SetVar(interp, "QROUTER_VERSION", version_string, TCL_GLOBAL_ONLY);
Tcl_Eval(interp, "namespace eval qrouter namespace export *");
Tcl_PkgProvide(interp, "Qrouter", version_string);
/* Initialize the console interpreter, if there is one. */
if ((consoleinterp = Tcl_GetMaster(interp)) == NULL)
consoleinterp = interp;
/* Initialize the command tag table */
Tcl_InitHashTable(&QrouterTagTable, TCL_STRING_KEYS);
return TCL_OK;
}
/*------------------------------------------------------*/
/* Command "start" */
/*------------------------------------------------------*/
static int
qrouter_start(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
int i, result, argc;
char *scriptfile = NULL;
char **argv;
/* For compatibility with the original C code, convert Tcl */
/* object arguments to strings. Handle "-s <name>", */
/* which is not handled by runqrouter(), and source the */
/* script <name> between runqrouter() and read_def(). */
argv = (char **)malloc((objc - 1) * sizeof(char *));
argc = 0;
for (i = 1; i < objc; i++) {
if (!strcmp(Tcl_GetString(objv[i]), "-s"))
scriptfile = strdup(Tcl_GetString(objv[i + 1]));
argv[argc++] = strdup(Tcl_GetString(objv[i]));
}
init_config();
result = runqrouter(argc, argv);
if ((result == 0) && (batchmode == 0)) GUI_init(interp);
for (i = 0; i < argc; i++)
free(argv[i]);
free(argv);
if (scriptfile != NULL) {
/* First check that the script file exists. If not, */
/* then generate an error here. */
FILE *scriptf = fopen(scriptfile, "r");
if (scriptf == NULL) {
Fprintf(stderr, "Script file \"%s\" unavaliable or unreadable.\n",
scriptfile);
Tcl_SetResult(interp, "Script file unavailable or unreadable.", NULL);
result = TCL_ERROR;
}
else {
fclose(scriptf);
result = Tcl_EvalFile(interp, scriptfile);
}
/* The script file should determine whether or not to */
/* exit by including the "quit" command. But if there */
/* is an error in the script, then always quit. */
/* If tkcon console is in use and there is an error in */
/* the script, then print the error message to the */
/* terminal, not the console, or else it vanishes. */
if (result != TCL_OK) {
if (consoleinterp == interp)
Fprintf(stderr, "Script file \"%s\" failed with result \'%s\'\n",
scriptfile, Tcl_GetStringResult(interp));
else
fprintf(stderr, "Script file \"%s\" failed with result \'%s\'\n",
scriptfile, Tcl_GetStringResult(interp));
free(scriptfile);
/* Make sure Tcl has generated all output */
while (Tcl_DoOneEvent(TCL_DONT_WAIT) != 0);
/* And exit gracefully */
qrouter_quit(clientData, interp, 1, objv);
}
else
free(scriptfile);
}
if ((DEFfilename != NULL) && (Nlgates == NULL)) {
read_def(NULL);
draw_layout();
}
return QrouterTagCallback(interp, objc, objv);
}
/*------------------------------------------------------*/
/* Command: qrouter_quit */
/* */
/* Call tkcon's exit routine, which will make sure */
/* the history file is updated before final exit. */
/*------------------------------------------------------*/
int
qrouter_quit(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, "(no arguments)");
return TCL_ERROR;
}
/* Free up failed net list */
remove_failed();
/* Should be doing other cleanup tasks here. . . */
if (consoleinterp == interp)
Tcl_Exit(TCL_OK);
else
Tcl_Eval(interp, "catch {tkcon eval exit}\n");
return TCL_OK; /* Not reached */
}
/*------------------------------------------------------*/
/* Command "map" */
/* */
/* Specify what to draw in the graphics window */
/* */
/* map obstructions draw routes (normal) */
/* map congestion draw actual congestion */
/* map estimate draw estimated congestion */
/* map none route background is plain */
/* map routes draw routes over map */
/* map noroutes don't draw routes over map */
/* map unrouted draw unrouted nets over map */
/* map nounrouted don't draw unrouted nets */
/*------------------------------------------------------*/
static int
qrouter_map(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
int idx, result;
static char *subCmds[] = {
"obstructions", "congestion", "estimate", "none",
"routes", "noroutes", "unrouted", "nounrouted", NULL
};
enum SubIdx {
ObsIdx, CongIdx, EstIdx, NoneIdx, RouteIdx, NoRouteIdx, UnroutedIdx, NoUnroutedIdx
};
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg?");
return TCL_ERROR;
}
else if ((result = Tcl_GetIndexFromObj(interp, objv[1],
(CONST84 char **)subCmds, "option", 0, &idx)) != TCL_OK)
return result;
switch (idx) {
case ObsIdx:
if ((mapType & MAP_MASK) != MAP_OBSTRUCT) {
mapType &= ~MAP_MASK;
mapType |= MAP_OBSTRUCT;
draw_layout();
}
break;
case CongIdx:
if ((mapType & MAP_MASK) != MAP_CONGEST) {
mapType &= ~MAP_MASK;
mapType |= MAP_CONGEST;
draw_layout();
}
break;
case EstIdx:
if ((mapType & MAP_MASK) != MAP_ESTIMATE) {
mapType &= ~MAP_MASK;
mapType |= MAP_ESTIMATE;
draw_layout();
}
break;
case NoneIdx:
if ((mapType & MAP_MASK) != MAP_NONE) {
mapType &= ~MAP_MASK;
mapType |= MAP_NONE;
draw_layout();
}
break;
case RouteIdx:
mapType |= DRAW_ROUTES;
draw_layout();
break;
case NoRouteIdx:
mapType &= ~DRAW_ROUTES;
draw_layout();
break;
case UnroutedIdx:
mapType |= DRAW_UNROUTED;
draw_layout();
break;
case NoUnroutedIdx:
mapType &= ~DRAW_UNROUTED;
draw_layout();
}
return QrouterTagCallback(interp, objc, objv);
}
/*------------------------------------------------------*/
/* Find the net with number "number" in the list of */
/* nets and return a pointer to it. */
/* */
/* NOTE: This could be hashed like net names, but is */
/* only used in one place, and router performance does */
/* not depend on it. */
/*------------------------------------------------------*/
NET LookupNetNr(int number)
{
NET net;
int i;
for (i = 0; i < Numnets; i++) {
net = Nlnets[i];
if (net->netnum == number)
return net;
}
return NULL;
}
/*------------------------------------------------------*/
/* Command "stage1" */
/* */
/* Execute stage1 routing. This works through the */
/* entire netlist, routing as much as possible but not */
/* doing any rip-up and re-route. Nets that fail to */
/* route are put in the "FailedNets" list. */
/* */
/* The interpreter result is set to the number of */
/* failed routes at the end of the first stage. */
/* */
/* Options: */
/* */
/* stage1 debug Draw the area being searched in */
/* real-time. This slows down the */
/* algorithm and is intended only */
/* for diagnostic use. */
/* stage1 step Single-step stage one. */
/* stage1 mask none Don't limit the search area */
/* stage1 mask auto Select the mask automatically */
/* stage1 mask bbox Use the net bbox as a mask */
/* stage1 mask <value> Set the mask size to <value>, */
/* an integer typ. 0 and up. */
/* stage1 route <net> Route net named <net> only. */
/* */
/* stage1 force Force a terminal to be routable */
/*------------------------------------------------------*/
static int
qrouter_stage1(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
u_char dodebug;
u_char dostep;
u_char saveForce, saveOverhead;
int i, idx, idx2, val, result, failcount = 0;
NET net = NULL;
static char *subCmds[] = {
"debug", "mask", "route", "force", "step", NULL
};
enum SubIdx {
DebugIdx, MaskIdx, RouteIdx, ForceIdx, StepIdx
};
static char *maskSubCmds[] = {
"none", "auto", "bbox", NULL
};
enum maskSubIdx {
NoneIdx, AutoIdx, BboxIdx
};
// Command defaults
dodebug = FALSE;
dostep = FALSE;
maskMode = MASK_AUTO; // Mask mode is auto unless specified
// Save these global defaults in case they are locally changed
saveForce = forceRoutable;
if (objc >= 2) {
for (i = 1; i < objc; i++) {
if ((result = Tcl_GetIndexFromObj(interp, objv[i],
(CONST84 char **)subCmds, "option", 0, &idx))
!= TCL_OK)
return result;
switch (idx) {
case DebugIdx:
dodebug = TRUE;
break;
case StepIdx:
dostep = TRUE;
break;
case ForceIdx:
forceRoutable = TRUE;
break;
case RouteIdx:
if (i >= objc - 1) {
Tcl_WrongNumArgs(interp, 0, objv, "route ?net?");
return TCL_ERROR;
}
i++;
net = DefFindNet(Tcl_GetString(objv[i]));
if (net == NULL) {
Tcl_SetResult(interp, "No such net", NULL);
return TCL_ERROR;
}
break;
case MaskIdx:
if (i >= objc - 1) {
Tcl_WrongNumArgs(interp, 0, objv, "mask ?type?");
return TCL_ERROR;
}
i++;
if ((result = Tcl_GetIndexFromObj(interp, objv[i],
(CONST84 char **)maskSubCmds, "type", 0,
&idx2)) != TCL_OK) {
Tcl_ResetResult(interp);
result = Tcl_GetIntFromObj(interp, objv[i], &val);
if (result != TCL_OK) return result;
else if (val < 0 || val > 200) {
Tcl_SetResult(interp, "Bad mask value", NULL);
return TCL_ERROR;
}
maskMode = (u_char)val;
}
else {
switch(idx2) {
case NoneIdx:
maskMode = MASK_NONE;
break;
case AutoIdx:
maskMode = MASK_AUTO;
break;
case BboxIdx:
maskMode = MASK_BBOX;
break;
}
}
break;
}
}
}
if (dostep == FALSE) stepnet = -1;
else stepnet++;
if (net == NULL)
failcount = dofirststage(dodebug, stepnet);
else {
if ((net != NULL) && (net->netnodes != NULL)) {
result = doroute(net, (u_char)0, dodebug);
failcount = (result == 0) ? 0 : 1;
/* Remove from FailedNets list if routing */
/* was successful */
if (result == 0 && FailedNets != NULL) {
NETLIST fnet, lnet = NULL;
for (fnet = FailedNets; fnet != NULL; fnet = fnet->next) {
if (fnet->net == net) {
if (lnet == NULL)
FailedNets = fnet->next;
else
lnet->next = fnet->next;
free(fnet);
break;
}
lnet = fnet;
}
}
}
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(failcount));
if (stepnet >= (Numnets - 1)) stepnet = -1;
// Restore global defaults in case they were locally changed