-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchronodraw.pl
executable file
·2142 lines (1822 loc) · 73.5 KB
/
chronodraw.pl
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
#!/bin/sh
#! -*-perl-*-
# line 4
#
# Make this script be run by /bin/sh, which in
# turn will run Perl on it... (see man perlrun(1))
# Fail otherwise!
#
eval 'exec perl -x $0 ${1+"$@"}; echo "ERROR, failed to launch PERL script $0" 1>&2; exit 2'
if 0;
#
# Copyright 2010-2018 Thibaud GAILLARD (thibaud dot gaillard at gmail dot com)
#
# 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, see <http://www.gnu.org/licenses/>.
#
# Use any script-attached or local SVG.pm, if any. Rely otherwise on the PERL installation...
# (SVG.pm not installed as a default)
#
BEGIN {
my ($spath, $sfile) = ($0 =~ /^(.*)\/([^\/]*)$/);
# printf ("#DEBUG# path = %s\n#DEBUG# script = %s\n", $spath, $sfile);
if ((-r "$spath/SVG.pm") && (-d "$spath/SVG")) {
push @INC , "$spath/";
push @INC , "$spath/SVG";
}
elsif ((-r "$spath/SVG/SVG.pm") && (-d "$spath/SVG/SVG")) {
push @INC , "$spath/SVG/";
push @INC , "$spath/SVG/SVG";
}
elsif ((-r "$spath/SVG/lib/SVG.pm") && (-d "$spath/SVG/lib/SVG")) {
push @INC , "$spath/SVG/lib";
push @INC , "$spath/SVG/lib/SVG";
}
elsif ((-r "./SVG.pm") && (-d "./SVG")) {
push @INC , "./";
push @INC , "./SVG";
}
elsif ((-r "./SVG/SVG.pm") && (-d "./SVG/SVG")) {
push @INC , "./SVG/";
push @INC , "./SVG/SVG";
}
elsif ((-r "./SVG/lib/SVG.pm") && (-d "./SVG/lib/SVG")) {
push @INC , "./SVG/lib";
push @INC , "./SVG/lib/SVG";
}
}
# Be strong about PERL syntax
use strict;
##use warnings;
# Let us parse options using perl library
use Getopt::Long;
# Get support from Struct Class
use Class::Struct;
# Get support from SVG
use SVG (indent => " ");
# Get support from "switch"-like directive...
use feature "switch";
no if ($] >= 5.018), 'warnings' => 'experimental';
# ...without the warnings that emerged in 5.18 onwards
##use experimental qw(smartmatch);
# Global variables
my $warning = 0;
my $error = 0;
my ($DEBUG, $HELP);
# Global settings
my $DSVG; # visually debug generated SVG
my $DPI = 72; # dot per inch (inkscape is 90 as per SVG standard!)
my $SCALE = 100; # overall scale factor (units)
my $MARGIN = 0; # overall margin (t/b/l/r, units) / 1cm = 28.35px
my $WIDTH = undef;# Overall width when defined
my $HEIGHT = undef;# Overall height when defined
my $RATIO = 1.00; # Overall h/v cycle ratio
my $CYCLE = undef;# Overall width of a cycle column
my $SIGROW = undef;# Overall height of a signal row
my $SEDGE = 0.10; # signal rise/fall width ratio over 1 cycle (1.0)
my $FSIZES = 0.30; # font size (signal)
my $FSIZEV = 0.20; # font size (value)
my $FSIZET = 0.20; # font size (timing)
my $SWIDTH = 0.03; # signal stroke width
my $VWIDTH = 0.01; # vertical bar stroke width
my $HWIDTH = $SWIDTH*3/4; # hiatus stroke width
my $SNAMEO = 0.10; # signal name offset
my $SNAMET = 0.05; # signal name tolerance
my $FNAME = 'Impact'; # font name used for texts
my $FHWRATIO = 2*(1 - 2.5/100); # font vertical/horizontal ratio / char (estimated)
my $FVORATIO = 1/3; # font vertical offset ratio to reach baseline (estimated)
my $DTL = 0.00; # cycle left offset
my $DTR = 0.00; # cycle right offset
my $SM = 0.20; # signal vertical margin (top & bottom, %)
my $TM = 0.10; # timing vertical margin (top & bottom, %)
my $HM = 0.10; # hiatus vertical margin (top & bottom, %)
my $TT = $SEDGE; # Text horizontal tolerance when left&right constrained
my $SIGNALH = 0.7; # overall height of a single wave row
my $TIMINGH = $SIGNALH/2; # overall height of a single timing row
my $HIATUSW = $SEDGE*1.50; # hiatus overall width (before blending)
my $HIATUSH = $SIGNALH*(1-2*$HM); # hiatus overall height
my $HIATUSO = $HIATUSW*0.75; # hiatus text offset from center (before blending)
my @HIATUSB = (1, 2); # hiatus length blending parameters (ratio, unratio)
my $re_ss_dash = '^[LZHW].';
my $re_color = '^#[0-Aa-fA-F]{6}$';
my $WHITE = "#FFFFFF"; # Full White
my $GRAY = "#D3D3D3"; # Light Gray
my $BLACK = "#000000"; # Full Black
my $RED = "#FF0000"; # Full Red
my $GREEN = "#00FF00"; # Full Green
my $GBLUE = "#00FFFF"; # Full Green/Blue (Turquoise?)
#
# Timing Diagram
#
struct (TIMING => [
scale => '$', # real
margin => '$', # real
wcratio => '$', # real (in %)
siglmax => '$', # integer
sx => '$', # real
sy => '$', # real
sigedge => '$', # real (in %)
cycles => '$', # integer
dtl => '$', # real
dtr => '$', # real
cyclesep => '@', # array of char
signals => '@', # array of strings
signal => '%', # -> %SIGNAL
]);
struct (SIGNAL => [
name => '$', # string
value => '$', # string
time => '$', # real
offset => '$', # real (in %)
pvalue => '$', # string
clock => '$', # integer (# phases per cycle if positive)
duty => '$', # real (in %)
noline => '$', # boolean
logic => '$', # logic (init)
wave => '@', # -> @WAVE
tagidx => '%', # hash index to @WAVE
maxlvl => '$', # integer (highest timref extent)
minlvl => '$', # integer (lowest timref extent)
hiatus => '@', # -> @HIATUS
yh => '$', # real (vertical elevation high)
y => '$', # real (vertical elevation)
yl => '$', # real (vertical elevation low)
]);
struct (WAVE => [
cycle => '$', # integer
time => '$', # real
jitter => '$', # string
value => '$', # char/string
logic => '$', # boolean
align => '$', # integer (-1/left, 0/center, +1/right)
timref => '@', # -> @TIMREF
timtag => '$', # boolean (show a timref tag mark)
]);
struct (TIMREF => [
sigref => '$', # string
evtref => '$', # integer
offref => '$', # integer
tagref => '$', # string
text => '$', # string
align => '$', # integer (-1/left, 0/center, +1/right)
arrows => '$', # integer (-1/out, 0/none, +1/in) wrt current signal
arrowr => '$', # integer (-1/out, 0/none, +1/in) wrt reference signal
level => '$', # integer (-n/below, 0/on, +n/above) at nth level
]);
struct (HIATUS => [
cycle => '$',
time => '$',
]);
struct (WAVEPT => [
s => '$', # char
v => '$', # string
j => '$', # string
a => '$', # (-1, 0, 1)
#
plpt => '@', # polyline/polygon point list (x,y)
xlpt => '@', # polyline/polygon point list (x,y)
ltpt => '@', # left text point (x,y)
rtpt => '@', # right text point (x,y)
]);
# Negated clock hash
my %CLOCKN = (
'0' => '1',
'1' => '0',
'L' => 'H',
'H' => 'L',
);
# Low clock boolean hash
my %CLOCKL = (
'0' => 1,
'1' => 0,
'L' => 1,
'H' => 0,
);
# Command-line/User-code accessible variables
my %VARIABLE = (
dpi => \$DPI, # Dot per inch
scale => \$SCALE, # Scale
margin => \$MARGIN, # Signal name left margin
ratio => \$RATIO, # Cycle / Interline ratio (h/v)
width => \$WIDTH, # Overall diagram width
height => \$HEIGHT, # Overall diagram height
sigrow => \$SIGROW, # Interline height
cycle => \$CYCLE, # Cycle width
fszsn => \$FSIZES, # Font size for signal names
fszsv => \$FSIZEV, # Font size for signal text value
fsztr => \$FSIZET, # Font size for timing reference text
dsvg => \$DSVG, # Debug SVG (within drawing)
);
my %VARLINESET;
#
# Impact font reverse-engineered (what a kl*dge!)
#
my $CHARFSIZE = 48.86; # Font-size
my $CHARWREF = 'e'; # Reference char
my %CHARWIDTH = ( # Origin to leftmost point of following 'e'
'a' => 26.0523, 'b' => 26.8157, 'c' => 25.5752, 'd' => 26.8157, 'e' => 26.4340, 'f' => 15.5550, 'g' => 26.8157, 'h' => 27.0066, 'i' => 14.7916, 'j' => 15.0779,
'k' => 24.8117, 'l' => 14.7916, 'm' => 39.1262, 'n' => 27.0066, 'o' => 26.4340, 'p' => 26.8157, 'q' => 26.7203, 'r' => 18.8951, 's' => 24.4300, 't' => 16.3185,
'u' => 27.0066, 'v' => 22.8077, 'w' => 34.1638, 'x' => 22.6168, 'y' => 23.3803, 'z' => 18.6088,
'A' => 26.2432, 'B' => 28.4380, 'C' => 28.5335, 'D' => 28.4380, 'E' => 21.7580, 'F' => 20.8991, 'G' => 28.3426, 'H' => 28.5335, 'I' => 15.5550, 'J' => 17.6545,
'K' => 27.6746, 'L' => 20.0402, 'M' => 36.4541, 'N' => 27.8655, 'O' => 28.1518, 'P' => 25.9569, 'Q' => 28.1518, 'R' => 27.7700, 'S' => 26.7203, 'T' => 21.9488,
'U' => 28.1518, 'V' => 27.0066, 'W' => 41.2256, 'X' => 25.0026, 'Y' => 22.3305, 'Z' => 20.8037,
'1' => 20.0402, '2' => 25.9569, '3' => 27.3883, '4' => 25.8614, '5' => 27.6746, '6' => 27.8655, '7' => 20.6128, '8' => 27.5792, '9' => 27.8655, '0' => 27.6746,
'!' => 14.6007, '@' => 39.3170, '#' => 32.0644, '$' => 28.1518, '^' => 25.0980, '&' => 29.5832, '%' => 35.3090, '*' => 15.1733, '(' => 16.7956, ')' => 16.7956,
'_' => 28.4380, '+' => 27.4838, '-' => 15.8413, '=' => 27.4838, ' ' => 10.0201, ',' => 9.6384, '.' => 10.4973, ';' => 11.3561, ':' => 11.3561, '<' => 27.4838,
'>' => 27.4838, '/' => 20.8037, '?' => 27.1020, '"' => 19.4677, '\''=> 10.4973, '`' => 17.7499, '[' => 15.2687, ']' => 15.2687, '{' => 19.4677, '}' => 19.4677,
'°' => 18.4179, '\\'=> 20.8037, '|' => 14.6962, '£' => 27.5792, '´' => 17.7499,
);
my %CHARLEFT = ( # Origin to leftmost point
'a' => 1.2883, 'b' => 1.7177, 'c' => 1.4314, 'd' => 1.4314, 'e' => 1.4314, 'f' => 0.0954, 'g' => 1.4314, 'h' => 1.7177, 'i' => 1.7177, 'j' => -0.1431,
'k' => 1.7177, 'l' => 1.7177, 'm' => 1.7177, 'n' => 1.7177, 'o' => 1.4314, 'p' => 1.7177, 'q' => 1.4314, 'r' => 1.7177, 's' => 1.0020, 't' => 0.1431,
'u' => 1.5746, 'v' => -0.2863, 'w' => -0.1431, 'x' => 0.0000, 'y' => -0.2863, 'z' => 0.2863,
'A' => -0.2863, 'B' => 2.0040, 'C' => 1.7177, 'D' => 2.0040, 'E' => 2.0040, 'F' => 2.0040, 'G' => 1.7177, 'H' => 2.0040, 'I' => 2.0040, 'J' => 0.4294,
'K' => 2.0040, 'L' => 2.0040, 'M' => 2.0040, 'N' => 2.0040, 'O' => 1.7177, 'P' => 2.0040, 'Q' => 1.7177, 'R' => 2.0040, 'S' => 1.1690, 'T' => 0.2863,
'U' => 1.8609, 'V' => -0.2863, 'W' => 0.0000, 'X' => 0.0000, 'Y' => -0.2863, 'Z' => 0.2863,
'1' => 0.2863, '2' => 1.2883, '3' => 1.4314, '4' => 0.2863, '5' => 1.5746, '6' => 1.7177, '7' => 0.2863, '8' => 1.5746, '9' => 1.7177, '0' => 1.7177,
'!' => 1.7177, '@' => 0.8589, '#' => 0.8589, '$' => 1.2883, '^' => 0.2863, '&' => 0.8589, '%' => 0.8589, '*' => 0.7157, '(' => 2.0040, ')' => 0.8589,
'_' => -0.2863, '+' => 1.5269, '-' => 0.8589, '=' => 1.5507, ' ' => 0.0000, ',' => 0.8589, '.' => 0.8589, ';' => 1.7177, ':' => 1.7177, '<' => 1.5507,
'>' => 1.5507, '/' => 0.2863, '?' => 2.0040, '"' => 0.8589, '\''=> 0.8589, '`' => 0.0000, '[' => 2.0040, ']' => 0.8589, '{' => 0.8589, '}' => 0.8589,
'°' => 0.8589, '\\'=> 0.2863, '|' => 4.0319, '£' => 1.7177, '´' => 3.6025,
);
my %CHARIGHT = ( # Origin to rightmost point
'a' => 23.0224, 'b' => 23.9290, 'c' => 23.0224, 'd' => 23.6427, 'e' => 23.5473, 'f' => 14.0043, 'g' => 23.6188, 'h' => 24.0006, 'i' => 11.6424, 'j' => 11.9287,
'k' => 23.6427, 'l' => 11.6424, 'm' => 36.0724, 'n' => 23.9529, 'o' => 23.5234, 'p' => 23.9051, 'q' => 23.5711, 'r' => 17.0581, 's' => 22.1397, 't' => 14.8870,
'u' => 23.7859, 'v' => 21.6387, 'w' => 32.8517, 'x' => 21.6148, 'y' => 21.9011, 'z' => 16.7241,
'A' => 25.0980, 'B' => 25.3843, 'C' => 25.4559, 'D' => 25.2650, 'E' => 19.4199, 'F' => 19.0144, 'G' => 25.0503, 'H' => 25.0980, 'I' => 12.0480, 'J' => 14.2906,
'K' => 26.4817, 'L' => 18.1555, 'M' => 32.9948, 'N' => 24.4300, 'O' => 24.9549, 'P' => 23.6427, 'Q' => 24.9787, 'R' => 24.4300, 'S' => 24.1199, 'T' => 22.2351,
'U' => 24.8356, 'V' => 25.8614, 'W' => 39.7942, 'X' => 24.1437, 'Y' => 23.4041, 'Z' => 18.7996,
'1' => 16.5809, '2' => 23.3564, '3' => 24.1676, '4' => 24.1199, '5' => 24.6447, '6' => 24.8594, '7' => 18.7042, '8' => 24.5254, '9' => 24.8594, '0' => 24.4539,
'!' => 12.3104, '@' => 37.0506, '#' => 29.7502, '$' => 25.4320, '^' => 23.3087, '&' => 28.1518, '%' => 32.9948, '*' => 13.0023, '(' => 14.4337, ')' => 13.2886,
'_' => 27.2690, '+' => 24.5254, '-' => 13.5272, '=' => 24.5016, ' ' => 0.0000, ',' => 7.3242, '.' => 8.1354, ';' => 8.9942, ':' => 8.9942, '<' => 24.5016,
'>' => 24.5016, '/' => 19.0382, '?' => 24.7640, '"' => 17.2012, '\''=> 8.1592, '`' => 12.6921, '[' => 12.9069, ']' => 11.7617, '{' => 17.1773, '}' => 17.1773,
'°' => 16.0799, '\\'=> 19.0382, '|' => 9.2090, '£' => 25.0026, '´' => 16.2946,
);
sub TextWidth ($@) {
my ($text, $fsize) = @_;
my $wref = $CHARWIDTH{$CHARWREF} - $CHARLEFT{$CHARWREF};
debug ("<TEXTWIDTH> \"%s\" @ %f ('%s' = %s @ %s)", $text, $fsize, $CHARWREF, $wref, $CHARFSIZE);
my @text = split (//, $text);
debug (" - text = [\"%s\"]", join ("\", \"", @text));
my @width;
my $left = $CHARLEFT{$text[0]};
my $right = $CHARWIDTH{$text[-1]} - $CHARIGHT{$text[-1]} - $CHARLEFT{$CHARWREF};
my $width = 0 - $left - $right;
foreach my $c (@text) {
my $w = $CHARWIDTH{$c} - $CHARLEFT{$CHARWREF};
push (@width, $w);
$width += $w;
}
debug (" - width = [\"%s\"]", join ("\", \"", @width));
if ($fsize <= 0) {
$fsize = 1.0;
}
my ($uleft, $uwidth, $uright) = ( $left/$CHARFSIZE, $width/$CHARFSIZE, $right/$CHARFSIZE);
my ($fleft, $fwidth, $fright) = ($uleft*$fsize, $uwidth*$fsize, $uright*$fsize );
debug (" => %s = %s / %s @ %f => %s", $uwidth, $width, $CHARFSIZE, $fsize, $fwidth);
return ($fleft, $fwidth, $fright);
}
#
# Useful REGEXPs
#
my $reSIGname = '(\w+)';
my $reSIGtext = '(?:\s*/"([^"]*)")?';
my $reSIGclocki = '(?:\s*~(\d+)(?:@(\d+)%)?)?'; # ($phases, $dutcyc)
my $reSIGclocke = '(?:\s*(~)(\d+)?(?:@(\d+)%)?)?'; # ($phases, $dutcyc)
my $reSIGvalue = '(?:([01XZLHW])|"(<?)([^>"]*)(>?)")'; # ($logicval, $ltvjust, $textval, $rtvjust)
my $reSIGoffset = '(?:\s*([-+]\d+(?:[.]\d+)?)%)?'; # ($offset)
my $reSIGjitter = '(?:\s*:(?:([-]\d+(?:[.]\d+)?)%[*](\d+))?' .
'(?:([+]\d+(?:[.]\d+)?)%[*](\d+))?)?'; # ($noffj, $ncntj, $poffj, $pcntj)
my $reSIGtimpos = '(/+|\\\\+)'; # ($vpos)
my $reSIGtimref = '([-<>])(?:(\w+)(?:@(\w+))?(?:([-+]\d+))?)([-<>])'; # ($leftend, $refname, $reftag, $refoffset, $rightend)
my $reSIGtimtxt = '"(<)?(?|([^"]*)(>)"|([^"]*[^>])"|")'; # ($leftalign, $text, $rightalign)
my $reSIGtiming = "(?:$reSIGtimpos$reSIGtimref$reSIGtimtxt)";
my $reSIGtag = '(?:/(\w+))?';
my $re_empty = '^\s*$';
my $re_comment = '^([^#]*)#\s*(.*)$'; # ($text, $comment)
my $re_variable = '^\s*\$(\w+)\s*=\s*(?:(\d+(?:\.\d+)?)\s*(px|pt|pc|in|mm|cm)?|"([^"]+)"|([-+]\d+(?:\.\d+)?)%)\s*$'; # ($varname, $varnum, $varunit, $vartext, $varprop)
my $re_boundary = '^\s*[[]([-+]?\d+(?:[.]\d+)?)%:([-+]?\d+(?:[.]\d+)?)%[]]\s*$'; # ($cycleol, $cycleor)
my $re_init = "^\\s*$reSIGname($reSIGtext)$reSIGclocki\\s*([=:])\\s*$reSIGvalue?$reSIGoffset\\s*\$";
my $re_cycle = '^\s*([.\|!]+)\s*$'; # (@cycle)
my $re_event = "^\\s*$reSIGname$reSIGtag$reSIGclocke\\s*=\\s*$reSIGvalue?$reSIGoffset$reSIGjitter\\s*(($reSIGtiming\\s*)*)\$";
my $re_timref = "^\\s*$reSIGname([-+]\\d+)?\\s*(($reSIGtiming\\s*)*)\$";
my $re_hiatus = "^\\s*~(?:$reSIGname\\s*)?$reSIGoffset\\s*\$";
my $re_subtime = '^\s*[{}](\s|[{})*\s*$';
#
# Command options:
# - DEBUG, output debugging information
# - HELP, give help about script
#
my %OPTIONS = (
"debug" => \$DEBUG,
"help" => \$HELP,
);
# Augment basic options with internal variables and parse them all
foreach my $vname (keys %VARIABLE) {
$OPTIONS{$vname . "=s"} = sub {ParseVARIABLE (@_)};
}
GetOptions (%OPTIONS);
# Print usage if arguments are inconsistent
if ($HELP) {
print STDERR "usage: $0 [-help] [-debug]\n";
exit (1);
}
#
# Here is the real stuff...
#
my $timing = TimingCreate ();
ParseTMG ($timing);
DumpTiming ($timing) if $DEBUG;
exit ($error) if ($error);
RenderTiming ($timing);
exit (0);
#
# End of main
#
#
# Support routines
#
sub warning ($@) {
my ($format, @varargs) = @_;
printf STDERR ("**WARNING:$.** " . $format . "\n", @varargs);
$warning++;
}
sub error ($@) {
my ($format, @varargs) = @_;
printf STDERR ("**ERROR:$.** " . $format . "\n", @varargs);
$error++;
}
sub debug ($@) {
my ($format, @varargs) = @_;
return if (!$DEBUG);
printf STDERR ("**DEBUG:$.** " . join ("\n**DEBUG:$.** ", (split ("\n", $format))) . "\n", @varargs);
}
#
# Timing Diagram
#
sub TimingCreate (@) { #$$$$$$) {
my ($scale, $margin, $ratio, $sx, $sy, $sedge) = @_;
my $timing = new TIMING (
scale => (defined $scale) ? $scale : 1.0,
margin => (defined $margin) ? $margin : 0.0,
wcratio => (defined $ratio) ? $ratio : 1.0,
siglmax => 0,
sx => (defined $sx) ? $sx : 0.0,
sy => (defined $sy) ? $sy : 0.0,
sigedge => (defined $sedge) ? $sedge : 0.0,
cycles => 0,
dtl => 0,
dtr => 0,
cyclesep => [],
signals => [],
signal => {},
);
return ($timing);
}
sub ParseTMG ($) {
my ($timing) = @_;
my $cycle = -1;
while (<>) {
# Clean up
chomp;
# debug ("<LINE> %s", $_);
# Catch & reduce comments
if (/$re_comment/o) {
my ($text, $comment) = ($1, $2);
debug ("<COMMENT> comment = \"%s\"", $comment);
$_ = $text;
}
# Skip empty lines
if (/$re_empty/o) {
next;
}
# Manage variable setting
if (/$re_variable/o) {
my ($vname, $vnum, $vunit, $vtext, $vprop) = (lc($1), $2, $3, $4, $5);
ParseVARIABLE ($vname, undef, $vnum, $vunit, $vtext, $vprop);
next;
}
# Manage cycle L/R boundaries
if (/$re_boundary/o) {
my ($cycleol, $cycleor) = ($1, $2);
debug ("<BOUNDARY> [%s%%:%s%%]", $cycleol, $cycleor);
# Left boundary must be negative or null, no more than a cycle...
if (($cycleol > 0.0) || ($cycleol < -100.0)) {
error ("cycle left boundary shall be negative or null, no more than a cycle (%s%%)", $cycleol);
} else {
$DTL = -$cycleol/100;
}
# Right boundary must be positive or null, no more than a cycle...
if (($cycleor < 0.0) || ($cycleol > 100.0)) {
error ("cycle right boundary shall be positive or null, no more than a cycle (%s%%)", $cycleor);
} else {
$DTR = $cycleor/100;
}
next;
}
# Manage signal init
if (($cycle < 0) && /$re_init/oi) {
my ($sname, $stextp, $stext, $scphases, $scduty, $sassign, $svlogic, $svtljust, $svtext, $svtrjust, $soffset) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);
debug ("<INIT> %s = %s (shown as \"%s\")", $sname, ($svlogic ? $svlogic : $svtext), ($stext ? $stext : $sname));
# Verify signal does not already exist
if (exists ${$timing->signal}{$sname}) {
error ("attempt to declare already existing signal (%s)", $sname);
next;
}
# Verify offset is kept within a cycle boundary
if ((defined $soffset) && !(($scduty > -100) && ($scduty < 100))) {
error ("attempt to declare signal with an invalid offset (%s / %d%)", $sname, $soffset);
next;
}
# Verify duty cycle is ... decent!
if ((defined $scduty) && !(($scduty > 0) && ($scduty < 100))) {
error ("attempt to declare clock signal with an invalid duty cycle (%s / %d%)", $sname, $scduty);
next;
}
# Then create it
{
my $signal = new SIGNAL (
name => $stextp ? $stext : $sname,
value => undef,
time => undef,
offset => (defined $soffset) ? ($soffset / 100.0) : 0.0,
clock => (defined $scphases) ? $scphases : 0,
duty => (defined $scduty) ? ($scduty / 100) : 0.5,
pvalue => (defined $svlogic) ? $svlogic : ((defined $svtext) ? $svtext : undef),
noline => ($sassign eq ':'),
logic => (defined $svlogic),
wave => [],
tagidx => {},
minlvl => 0,
maxlvl => 0,
hiatus => [],
);
$signal->value($signal->pvalue);
# Make the clock/signal exist before the largest left window
if (($signal->clock > 0) && ($signal->logic) && (exists $CLOCKN{$signal->pvalue})) {
CycleClockMaybe ($signal, -2);
CycleClockMaybe ($signal, -1);
} else {
my $wave = new WAVE (
cycle => -2,
time => -2.0,
value => $signal->pvalue,
logic => $signal->logic,
align => $svtljust ? ($svtrjust ? 0 : -1) : ($svtrjust ? 1 : 0),
);
push (@{$signal->wave}, $wave);
}
# Register signal in timing diagram's tables
${$timing->signal}{$sname} = $signal;
push (@{$timing->signals}, $sname);
# Update the max signal name length
my ($tl, $tw, $tr) = TextWidth ($signal->name);
if ($timing->siglmax < ($tl+$tw+$tr)) {
$timing->siglmax ($tl+$tw+$tr);
}
}
next;
}
# Manage cycles
if (/$re_cycle/oi) {
my ($cycles) = ($1);
foreach my $c (split (//, $cycles)) {
debug ("<CYCLE> %s", $c);
# Next cycle
$cycle++;
# Add vertical bar information if asked to
push (@{$timing->cyclesep}, $c);
# Handle clocks (as a special case out of init, do not run clocks on the very first cycle so we can force them if needed)
if ($cycle > 0) {
foreach my $signal (@{$timing->signals}) {
my $s = ${$timing->signal}{$signal};
if ($s->clock > 0) {
CycleClockMaybe ($s, $cycle-1);
}
}
}
}
next;
}
# Manage signal event
if (/$re_event/oi) {
my ($sname, $stag, $sclock, $scphase, $scduty, $svlogic, $svtljust, $svtext, $svtrjust, $soffset, $noffj, $ncntj, $poffj, $pcntj, $timrefs) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15);
my @timref = split (/\s+(?=[\/\\])/, $timrefs);
debug ("<EVENT>\n" .
" - signal = %s\n" .
" - tag = %s\n" .
" - clock = %s\n" .
" - phase = %s\n" .
" - duty = %s\n" .
" - value = %s%s%s%s\n" .
" - offset = %s\n" .
" - jitter = %s%%*%s / %s%%*%s\n" .
" - timref = %s | %s\n",
$sname, $stag, $sclock, $scphase, $scduty, $svlogic, $svtljust, $svtext, $svtrjust, $soffset, $noffj, $ncntj, $poffj, $pcntj, $timrefs, "[" . join ("] [", @timref) . "]");
# Catch attempts to provide full event info during init
if ($cycle < 0) {
error ("attempt to create signal event during init phase (\"%s\")", $sname);
next;
}
# Check signal has been declared...
if (! (exists ${$timing->signal}{$sname})) {
error ("unknown signal (\"%s\")", $sname);
next;
}
# Verify offset is kept within a cycle boundary
if ( (defined $soffset)
&& (($soffset <= -100) || (100 <= $soffset))) {
error ("attempt to assign signal an invalid offset (%s / %d%%)", $sname, $soffset);
next;
}
# Verify negative jitter is kept within a cycle boundary
if ( (defined $noffj)
&& (($noffj < -100) || (0 <= $noffj) || ($ncntj <= 0))) {
error ("attempt to assign signal an invalid negative jitter offset or count (%s / %d%% / %d)", $sname, $noffj, $ncntj);
next;
}
# Verify positive jitter is kept within a cycle boundary
if ( (defined $poffj)
&& (($poffj <= 0) || (100 < $poffj) || ($pcntj <= 0))) {
error ("attempt to assign signal an invalid positive jitter offset or count (%s / %d%% / %d)", $sname, $poffj, $pcntj);
next;
}
# Verify duty cycle is ... decent!
if ( (defined $scduty)
&& (($scduty <= 0) || (100 <= $scduty))) {
error ("attempt to assign clock signal an invalid duty cycle (%s / %d%%)", $sname, $scduty);
next;
}
# ... then add a new event
{
my $signal = ${$timing->signal}{$sname};
my $time = $cycle + ((defined $soffset) ? ($soffset / 100.0) : $signal->offset);
my $value = (defined $svlogic) ? $svlogic : ((defined $svtext) ? $svtext : undef);
my $align = (defined $svlogic) ? 0 : ($svtljust ? ($svtrjust ? 0 : -1) : ($svtrjust ? 1 : 0));
my $refwaveidx;
# Redefine a clock if asked to
my $ckoffset = 0;
if (defined $sclock) {
# Use the given phases or reuse previous one, if any
if (defined $scphase) {
$signal->clock ($scphase);
} else {
if ($signal->clock != 0) {
$signal->clock (abs($signal->clock));
} else {
error ("trying to implicitly reactivate clock for signal \"%s\"", $sname);
next;
}
}
# Use the given duty cycle or reuse previous one, if any
$signal->duty ((defined $scduty) ? ($scduty / 100) : 0.5);
# Finally verify clock value is cyclable!
if (exists $CLOCKN{$value}) {
$signal->pvalue ($CLOCKN{$value});
$signal->time (undef);
CycleClockMaybe ($signal, $cycle);
$ckoffset = $signal->clock - 1;
} else {
warning ("attempt to assign clock signal a non-clockable value (%s / %s), turning to signal", $sname, $value);
$sclock = undef;
}
}
# Deactivate clock (if it ever was) and register event
if (!(defined $sclock)) {
$signal->clock (-abs($signal->clock));
# Create jitter events before reference, if any
if (defined ($noffj)) {
for (my $i=0; $i<$ncntj; $i++) {
my $timej = $time + $noffj*($ncntj-$i)/$ncntj/100;
my $wave = new WAVE (
cycle => $cycle,
time => $timej,
jitter => (($i == 0) ? 'b' : 'm'),
value => $value,
logic => (defined $svlogic),
align => $align,
);
push (@{$signal->wave}, $wave);
$signal->time ($timej);
}
}
# Create reference event
my $wave = new WAVE (
cycle => $cycle,
time => $time,
jitter => defined ($noffj) ? (defined ($poffj) ? 'm' : 'e') : (defined ($poffj) ?'b' : undef),
value => $value,
logic => (defined $svlogic),
align => $align,
);
push (@{$signal->wave}, $wave);
$signal->time ($time);
$refwaveidx = $#{$signal->wave};
# Create jitter events after reference, if any
if (defined ($poffj)) {
for (my $i=0; $i<$pcntj; $i++) {
my $timej = $time + $poffj*($i+1)/$pcntj/100;
my $wavej = new WAVE (
cycle => $cycle,
time => $timej,
jitter => (($i == ($pcntj-1)) ? 'e' : 'm'),
value => $value,
logic => (defined $svlogic),
align => $align,
);
push (@{$signal->wave}, $wavej);
$signal->time ($timej);
}
}
# Finally adjust the signal for what it is
$signal->pvalue($signal->value);
$signal->value($value);
}
# Create a tag if asked too (allowed only once), appropriately adjusting for clocks
if (defined $stag) {
if (exists ${$signal->tagidx}{$stag}) {
error ("tag \"%s\" redefined for signal \"%s\"", $stag, $sname);
} else {
${$signal->tagidx}{$stag} = $refwaveidx - $ckoffset;
}
}
# Parse timing references, if any
my $wave = ${$signal->wave}[$refwaveidx - $ckoffset];
ParseTIMREF ($timing, $cycle, $signal, $wave, @timref);
}
next;
}
if (/$re_timref/oi) {
my ($sname, $eoffset, $timrefs) = ($1, $2, $3);
# Sanity check
if (!(defined $eoffset)) {
$eoffset = 0;
}
# Check signal has been declared...
my $signal = (exists ${$timing->signal}{$sname}) ? ${$timing->signal}{$sname} : undef;
if (!(defined $signal)) {
error ("unknown signal (\"%s\")", $sname);
next;
}
# Now we perform a few operations if referring to a clock
# - cycle the clock in this cycle (if not done already, allow it only once)
# - allow positive offset only is less than the number of phases
# - correct the offest by the number of phases, so we get a human-friendly reference
if ($signal->clock > 0) {
if ($eoffset >= $signal->clock) {
error ("bad timing ref, clock signal offset shall be less than clock phases (%d >= %d)", $sname, $eoffset, $signal->clock);
next;
}
CycleClockMaybe ($signal, $cycle);
$eoffset -= $signal->clock - 1;
} else {
if ($eoffset > 0) {
error ("bad timing ref, signal offset shall be negative or null (vs %d)", $sname, $eoffset);
next;
}
}
# Parse the timing references, if any
my @timref = split (/\s+(?=[\/\\])/, $timrefs);
debug ("<TIMREF>\n" .
" - signal = %s\n" .
" - offset = %s\n" .
" - timref = %s | %s\n",
$sname, $eoffset, $timrefs, "[" . join ("] [", @timref) . "]");
my $wave = ${$signal->wave}[-1+$eoffset];
if (defined $wave) {
ParseTIMREF ($timing, $cycle, $signal, $wave, @timref);
} else {
error ("attempt to reference a signal with no wave");
}
next;
}
# Manage signal hiatus
if (/$re_hiatus/oi) {
my ($sname, $hoffset) = ($1, $2);
debug ("<HIATUS>\n" .
" - signal = %s\n" .
" - offset = %s\n",
($sname ? $sname : "<all>"), $hoffset);
# Catch attempts to provide full event info during init
if ($cycle < 0) {
error ("attempt to create signal hiatus during init phase (\"%s\")", $sname);
next;
}
# Check signal has been declared...
if ($sname && (! (exists ${$timing->signal}{$sname}))) {
error ("unknown signal (\"%s\")", $sname);
next;
}
# ... then add a new hiatus
{
my $time = $cycle + ((defined $hoffset) ? ($hoffset / 100.0) : 0);
foreach my $s ($sname ? ($sname) : @{$timing->signals}) {
my $signal = ${$timing->signal}{$s};
if (! $signal->noline) {
my $hiatus = new HIATUS (
cycle => $cycle,
time => $time);
push (@{$signal->hiatus}, $hiatus);
}
}
}
next;
}
# Catch any unrecognized statement
error ("Unrecognized directive \"%s\"", $_);
}
# Create a (few) lasts event to reach end-of-cycle if needed
foreach my $s (values %{$timing->signal}) {
if (!(defined $s->time) || ($s->time < $cycle+2)) {
debug ("<LAST>\n" .
" - signal = %s\n" .
" - time = %s\n",
$s->name, $s->time);
if ($s->clock > 0) {
CycleClockMaybe ($s, $cycle+0);
CycleClockMaybe ($s, $cycle+1);
CycleClockMaybe ($s, $cycle+2);
} else {
my $lw = ${$s->wave}[-1];
push (@{$s->wave}, new WAVE (cycle => $cycle+2, time => $cycle+2, value => '=', logic => '=', align => undef));
$s->time ($cycle+2);
}
}
}
# Update & return parsed data
$timing->cycles($cycle);
return ($timing);
}
sub CycleClockMaybe ($$) {
my ($signal, $cycle) = @_;
# We can only cycle a clock with defined values
my $time = $signal->time;
my $value = $signal->value;
my $pvalue = $signal->pvalue;
my $phases = $signal->clock;
my $duty = $signal->duty;
my $offset = $signal->offset;
my $period = 2.0 / $phases;
my $widthh = $period * $duty;
my $widthl = $period - $widthh;
# If no event ever existed before, create the (pseudo) previous one
if (!(defined $time)) {
# At the very begining, create an event with the same value if we have an even number of phases, use opposite otherwise
if (($cycle < 0) && (exists $CLOCKN{$pvalue})) {
$value = (($cycle*$phases % 2) == 0) ? $pvalue : $CLOCKN{$pvalue};
} else {
$value = $pvalue;
}
# Now compute time of (pseudo) previous event
$time = $cycle + $offset - ($CLOCKL{$value} ? $widthl : $widthh);
# Do that only when on init, cuz we later unset/set $time later to manage "voids" (FIXME: what a kludge...)
if ($cycle < 0) {
debug (" - phase %d: %s @ %.3f", $phases, $value, $time);
push (@{$signal->wave}, new WAVE (cycle => $cycle-1, time => $time, value => $value, logic => 1, align => 0));
}
}
if (exists $CLOCKN{$value}) {
# Check this clock has not been cycled already
my $lwave = ${$signal->wave}[-1];
if (($lwave->cycle) == $cycle) {
debug ("<CLOCK> skipping %s clock cycling at cycle %d, done already", $signal->name, $cycle);
return;
} else {
debug ("<CLOCK> stepping %s by %d phases @ cycle %d", $signal->name, $signal->clock, $cycle);
}
# Step over each phase and add new events
for (my $phase=0; $phase < $phases; $phase++) {
$time = $time + ($CLOCKL{$value} ? $widthl : $widthh);
$pvalue = $value;
$value = $CLOCKN{$value};
debug (" - phase %d: %s @ %.3f", $phase+1, $value, $time);
push (@{$signal->wave}, new WAVE (cycle => $cycle, time => $time, value => $value, logic => 1, align => 0));
}
$signal->pvalue($pvalue);
$signal->time ($time);
$signal->value($value);
}
}
sub ParseVARIABLE ($$@) {
my ($varname, $optval, $varnum, $varunit, $vartext, $varprop) = @_;
if (defined $optval) {
my $vline = '$' . $varname . "=" . $optval;
($varname, $varnum, $varunit, $vartext, $varprop) = ($vline =~ /$re_variable/o);
}
$varname = lc ($varname);
debug ("<VARIABLE> %s = %s%s%s%s", $varname, $varnum, $varunit, '"' . $vartext . '"', $varprop . "%");
# Check variable exists
if (!(exists $VARIABLE{$varname})) {
error ("attempt to reference unknown variable \"%s\"", $varname);
}
# Variables cannot be set more than once...
elsif (exists $VARLINESET{$varname}) {
my $line = $VARLINESET{$varname};
if ($line) {
warning ("attempt to redefine variable (\"%s\", first set at line %d)", $varname, $line);
} else {
warning ("ignoring variable definition (\"%s\", first set on command line)", $varname);
}
}
else {
$VARLINESET{$varname} = $.;
# Text value directly get affected
if (defined $vartext) {
${$VARIABLE{$varname}} = $vartext;
debug (" - assign \"%s\"", $vartext);
}
# Text value directly get affected
elsif (defined $varprop) {
if (defined ${$VARIABLE{$varname}}) {
my $offset = $varprop / 100.0;
my $value = ${$VARIABLE{$varname}} * (1 + $offset);
debug (" - assign (%s %s %s%%) = %s", ${$VARIABLE{$varname}}, ($offset < 0) ? "-" : "+", $offset, $value);
${$VARIABLE{$varname}} = $value;
} else {
error ("attempt to apply an offset to a variable with an undefined value (%s%% to %s)", $varprop, $varname);
}
}
# Numerical value with no unit directly get affected too
elsif (!(defined $varunit)) {
${$VARIABLE{$varname}} = $varnum;
debug (" - assign %s ", $varnum);
}
# Otherwise handle & convert unit to pixels
else {
my $value;