Subversion Repositories OpenARM Single-board Computer

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
118 jelle 1
#!/usr/bin/perl
2
# -*- perl -*-
3
 
4
# Copyright (C) 2006 DJ Delorie dj@delorie.com
5
# Released under the terms of the GNU General Public License, version 2
6
 
7
# Usage: djboxsym sample.symdef > sample.sym
8
 
9
$y{left} = 400;
10
$y{right} = 400;
131 agaran 11
$y{labels} = -200;
118 jelle 12
$labelpin = 0;
13
 
14
# Read in the file, storing information about each pin.
15
 
16
while (<>) {
17
    next if /^#/;
18
    s/^\s+//;
19
    s/\s+$//;
20
    s/[\s\t]+/ /g;
21
    s/[\r\n]+$//;
22
 
23
    # Note change of section.
24
    if (/^\[(.*)\]/) {
25
	$side = $1;
26
	$space = 0;
27
	next;
28
    }
29
 
30
    # Start a bus
31
    if (/^\.bus/) {
32
	$busmode = 1;
33
	next;
34
    }
35
 
36
    # blank lines - cancel bus, add gap.
37
    if (! /\S/) {
38
	if ($busmode) {
39
	    $y{$side} += 200;
40
	}
41
	$busmode = 0;
42
	if ($space) {
43
	    if ($side =~ /left|right/) {
131 agaran 44
		$y{$side} += 300;
118 jelle 45
	    }
46
	    if ($side =~ /top|bottom/) {
47
		$x{$side} += 400;
48
	    }
49
	    $space = 0;
50
	}
51
	next;
52
    }
53
 
54
    # Hidden labels are stored separately, because we don't care how
55
    # big they are.
56
    if (/! (\S.*)/ && $side eq "labels") {
57
	push(@attrs, $1);
58
	next;
59
    }
60
 
61
    # Visible labels are stored as pins because their size affects the
62
    # size of the symbols' box.
63
    if (/\S/ && $side eq "labels") {
64
	$labelpin --;
65
	$pinside{$labelpin} = $side;
66
	$piny{$labelpin} = $y{labels};
67
	$pinlabel{$labelpin} = $_;
131 agaran 68
	$y{labels} -= 200;
118 jelle 69
	$rlen{$labelpin} = &textlen($_);
70
	next;
71
    }
72
 
73
    # Regular pins are handled here.
74
    if (/^([0-9A-Za-z]+)\s*(.*)/) {
75
	$space = 1;
76
	($pin, $rest) = ($1,$2);
77
 
78
	if ($saw_pin{$pin}) {
79
	    print STDERR "DUPLICATE PIN $pin (was $pinlabel{$pin}, now $rest)\n";
80
	    $errors ++;
81
	}
82
	$saw_pin{$pin} = 1;
83
	$maxpin = $pin if $maxpin < $pin;
84
 
85
	$pinside{$pin} = $side;
86
	next if $side eq "nc";
87
	if ($rest =~ /^([!>]+) (.*)/) {
88
	    $flags = $1;
89
	    $pinlabel{$pin} = $2;
90
	    $bubble{$pin} = 1 if $flags =~ /!/;
91
	    $edge{$pin} = 1 if $flags =~ />/;
92
	} else {
93
	    $pinlabel{$pin} = $rest;
94
	}
95
	$rlen{$pin} = &textlen($pinlabel{$pin});
96
 
97
	if ($side =~ /left|right/) {
98
	    $y = $piny{$pin} = $y{$side};
132 agaran 99
	    $y{$side} += ($busmode ? 200 : 300);
118 jelle 100
	}
101
	if ($side =~ /top|bottom/) {
102
	    $tw = &alignpin((200 + $rlen{$pin}) / 2);
103
	    $pinx{$pin} = $w{$side} + $tw;
104
	    $w{$side} += $tw + $tw;
105
	}
106
 
107
    }
108
 
109
}
110
 
111
$minpin = $labelpin;
112
$boxwidth = 0;
113
%bw = ();
114
 
115
# for each horizontal slice of the symbol, keep track of how much
116
# width is used up by the left, middle, and right labels.
117
for $lp (keys %pinside) {
118
    next unless $pinside{$lp} =~ /left|right|label/;
119
    $yb = &alignpin($piny{$lp});
120
    for ($y=$yb-100; $y<=$yb+100; $y+=100) {
121
	if ($bw{$y}{$pinside{$lp}} < $rlen{$lp}) {
122
	    $bw{$y}{$pinside{$lp}} = $rlen{$lp};
123
	}
124
    }
125
}
126
 
127
# Compute the height of the box. 
128
for $p (keys %pinside) {
129
    next unless $pinside{$p} =~ /left|right/;
130
    if ($maxy < $piny{$p}) {
131
	$maxy = $piny{$p};
132
    }
133
}
134
$maxy += 400;
135
 
136
# Now, use the slice widths to compute the minimum width of the box.
137
for ($i=0; $i<$maxy; $i+=100) {
138
    $w = $bw{$i}{left} + $bw{$i}{labels} + $bw{$i}{right};
139
    if ($bw{$i}{labels}) {
140
	$wl = ($bw{$i}{left} + $bw{$i}{labels}/2) * 2;
141
	$w = $wl if $w < $wl;
142
	$wl = ($bw{$i}{right} + $bw{$i}{labels}/2) * 2;
143
	$w = $wl if $w < $wl;
144
    }
145
    if ($bw{$i}{left} && $bw{$i}{labels}) {
146
	$w += 100;
147
    } elsif ($bw{$i}{left} && $bw{$i}{right}) {
148
	$w += 200;
149
    }
150
    if ($bw{$i}{right} && $bw{$i}{labels}) {
151
	$w += 100;
152
    }
153
    if ($boxwidth < $w) {
154
	$boxwidth = $w;
155
    }
156
}
157
 
121 jelle 158
 
159
for ($i = $y{labels}; $i <= 0; $i+=100) {
160
	my $w = $bw{$i}{labels};
161
	$w += 100 - ($w%100);
162
	#$w = (int($w/100) * 100) + (($w/100) > int($w/100))?100:0;
163
	if ($boxwidth < $w) {
164
		$boxwidth = $w;
165
	}
166
}
167
 
168
#printf STDERR "test: %d\n", $y{labels};
169
 
118 jelle 170
$boxwidth = $w{top} if $boxwidth < $w{top};
171
$boxwidth = $w{bottom} if $boxwidth < $w{bottom};
172
 
173
# Flip Y coordinates (we count from the top, but symbols coordinates
174
# are from the bottom).
175
for $p (keys %pinside) {
176
    next unless $pinside{$p} =~ /left|right|labels/;
177
    $piny{$p} = $maxy - $piny{$p} + 300;
178
}
179
 
180
$boxwidth = &alignpin($boxwidth);
181
$boxwidth += 200;
182
 
183
# Adjust the position of the top/bottom pins so that, as a group,
184
# they're centered.
185
for $p (keys %pinside) {
186
    next unless $pinside{$p} =~ /top|bottom/;
187
    $pinx{$p} += &alignpin(($boxwidth - $w{$pinside{$p}})/2) + 300;
188
}
189
 
190
# Labels are centered in the box.
121 jelle 191
# labels have to be top, lef
118 jelle 192
for $lp ($minpin..-1) {
121 jelle 193
    #$pinx{$lp} = &alignpin($boxwidth/2) + 300;
131 agaran 194
    $pinx{$lp} = 300; # + &textlen($pinlabel{$lp})/2;
118 jelle 195
}
196
 
197
# Version.
198
print "v 20060123 1\n";
199
 
200
# Symbol box.
201
printf("B %d %d %d %d 3 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",
202
       300, 300, $boxwidth, $maxy);
203
 
204
# These are the hidden labels.
205
$ax = 300 + $boxwidth;
206
$ay = 400 + $maxy;
207
for $a (reverse @attrs) {
208
    printf("T %d %d 9 10 0 0 0 0 1\n%s\n",
209
	   $ax, $ay, $a);
210
    $ay += 200;
211
}
212
 
213
# Now print all the pins.
214
for $p (sort {$a<=>$b} keys %pinside) {
215
    next unless $pinside{$p};
216
    if ($pinside{$p} eq "left") {
217
	$pinx{$p} = 300;
218
    }
219
    if ($pinside{$p} eq "right") {
220
	$pinx{$p} = 300 + $boxwidth;
221
    }
222
    if ($p > 0 && !$saw_pin{$p}) {
223
	print STDERR "MISSING PIN $p\n";
224
	$errors++;
225
    } else {
226
	printf STDERR ("%3d  %-6s  %4d %4d  %s\n",
227
		       $p, $pinside{$p}, $pinx{$p}, $piny{$p}, $pinlabel{$p});
228
    }
229
 
230
    eval "&drawpin_$pinside{\"$p\"} (\"$p\")";
231
}
232
 
233
# what remains are helper functions; for drawing each type of pin,
234
# each type of label, etc.
235
 
236
sub drawpin_nc {
237
}
238
 
239
sub drawpin_top {
240
    my($pin) = @_;
241
    $y = $maxy + 300;
242
    printf("P %d %d %d %d 1 0 0\n",
243
	   $pinx{$pin}, $y+300, $pinx{$pin}, $y);
244
    print "{\n";
245
    &pltext($pinx{$pin}, $y-50, 5, $pinlabel{$pin});
246
    &ntext($pinx{$pin}+50, $y+50, 0, $pin);
247
    print "}\n";
248
}
249
 
250
sub drawpin_bottom {
251
    my($pin) = @_;
252
    printf("P %d %d %d %d 1 0 0\n",
253
	   $pinx{$pin}, 0, $pinx{$pin}, 300);
254
    print "{\n";
255
    &pltext($pinx{$pin}, 350, 3, $pinlabel{$pin});
256
    &ntext($pinx{$pin}+50, 250, 2, $pin);
257
    print "}\n";
258
}
259
 
260
sub drawpin_labels {
261
    my($pin) = @_;
131 agaran 262
    &ltext($pinx{$pin}, $piny{$pin}, 0, $pinlabel{$pin});
118 jelle 263
}
264
 
265
sub circle {
266
    my ($x, $y) = @_;
267
    print "V $x $y 50 3 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n";
268
}
269
 
270
sub drawpin_left {
271
    my($pin) = @_;
272
    $x = $pinx{$pin};
273
    $px = 50;
274
    if ($bubble{$pin}) {
275
	$x -= 100;
276
	&circle($x+50, $piny{$pin});
277
    }
278
    if ($edge{$pin}) {
279
	$px += 100;
280
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
281
	       $pinx{$pin}, $piny{$pin}-50,
282
	       $pinx{$pin}+100, $piny{$pin});
283
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
284
	       $pinx{$pin}+100, $piny{$pin},
285
	       $pinx{$pin}, $piny{$pin}+50);
286
 
287
    }
288
    printf("P %d %d %d %d 1 0 0\n",
289
	   $pinx{$pin} - 300, $piny{$pin}, $x, $piny{$pin});
290
    print "{\n";
291
    &pltext($pinx{$pin} + $px, $piny{$pin}, 1, $pinlabel{$pin});
292
    &ntext($pinx{$pin} -100, $piny{$pin} + 50, 6, $pin);
293
    print "}\n";
294
}
295
 
296
sub drawpin_right {
297
    my($pin) = @_;
298
    $x = $pinx{$pin};
299
    $px = 50;
300
    if ($bubble{$pin}) {
301
	$x += 100;
302
	&circle($x-50, $piny{$pin});
303
    }
304
    if ($edge{$pin}) {
305
	$px += 100;
306
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
307
	       $pinx{$pin}, $piny{$pin}-50,
308
	       $pinx{$pin}-100, $piny{$pin});
309
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
310
	       $pinx{$pin}-100, $piny{$pin},
311
	       $pinx{$pin}, $piny{$pin}+50);
312
 
313
    }
314
    printf("P %d %d %d %d 1 0 0\n",
315
	   $pinx{$pin} + 300, $piny{$pin}, $x, $piny{$pin});
316
    print "{\n";
317
    &pltext($pinx{$pin} - $px, $piny{$pin}, 7, $pinlabel{$pin});
318
    &ntext($pinx{$pin} +100, $piny{$pin} + 50, 0, $pin);
319
    print "}\n";
320
}
321
 
322
sub ntext {
323
    my ($x, $y, $a, $s) = @_;
324
    printf("T %d %d 5 8 1 1 0 %s 1\npinnumber=%s\n", $x, $y, $a, $s);
325
    printf("T %d %d 5 8 0 1 0 %s 1\npinseq=%s\n", $x, $y, $a, $s);
326
}
327
 
328
sub pltext {
329
    my ($x, $y, $a, $s) = @_;
330
    $s = "pinlabel=$s" unless $s =~ /=/;
331
    printf("T %d %d 9 10 1 1 0 %s 1\n%s\n", $x, $y, $a, $s);
332
}
333
 
334
sub ltext {
335
    my ($x, $y, $a, $s) = @_;
336
    printf("T %d %d 9 10 1 1 0 %s 1\n%s\n", $x, $y, $a, $s);
337
}
338
 
339
sub textlen {
340
    my($t) = @_;
341
    $t =~ s/^[^=]*=//;
342
    $t =~ s@\\_@@g;
343
    return length($t) * 120;
344
}
345
 
346
sub alignpin {
347
    my($v) = @_;
348
    return int(($v + 99) / 100) * 100;
349
}
350
 
351
exit $errors;