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;
11
$y{labels} = 700;
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/) {
44
		$y{$side} += 400;
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} = $_;
68
	$y{labels} += 400;
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};
99
	    $y{$side} += ($busmode ? 200 : 400);
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
 
158
$boxwidth = $w{top} if $boxwidth < $w{top};
159
$boxwidth = $w{bottom} if $boxwidth < $w{bottom};
160
 
161
# Flip Y coordinates (we count from the top, but symbols coordinates
162
# are from the bottom).
163
for $p (keys %pinside) {
164
    next unless $pinside{$p} =~ /left|right|labels/;
165
    $piny{$p} = $maxy - $piny{$p} + 300;
166
}
167
 
168
$boxwidth = &alignpin($boxwidth);
169
$boxwidth += 200;
170
 
171
# Adjust the position of the top/bottom pins so that, as a group,
172
# they're centered.
173
for $p (keys %pinside) {
174
    next unless $pinside{$p} =~ /top|bottom/;
175
    $pinx{$p} += &alignpin(($boxwidth - $w{$pinside{$p}})/2) + 300;
176
}
177
 
178
# Labels are centered in the box.
179
for $lp ($minpin..-1) {
180
    $pinx{$lp} = &alignpin($boxwidth/2) + 300;
181
}
182
 
183
# Version.
184
print "v 20060123 1\n";
185
 
186
# Symbol box.
187
printf("B %d %d %d %d 3 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",
188
       300, 300, $boxwidth, $maxy);
189
 
190
# These are the hidden labels.
191
$ax = 300 + $boxwidth;
192
$ay = 400 + $maxy;
193
for $a (reverse @attrs) {
194
    printf("T %d %d 9 10 0 0 0 0 1\n%s\n",
195
	   $ax, $ay, $a);
196
    $ay += 200;
197
}
198
 
199
# Now print all the pins.
200
for $p (sort {$a<=>$b} keys %pinside) {
201
    next unless $pinside{$p};
202
    if ($pinside{$p} eq "left") {
203
	$pinx{$p} = 300;
204
    }
205
    if ($pinside{$p} eq "right") {
206
	$pinx{$p} = 300 + $boxwidth;
207
    }
208
    if ($p > 0 && !$saw_pin{$p}) {
209
	print STDERR "MISSING PIN $p\n";
210
	$errors++;
211
    } else {
212
	printf STDERR ("%3d  %-6s  %4d %4d  %s\n",
213
		       $p, $pinside{$p}, $pinx{$p}, $piny{$p}, $pinlabel{$p});
214
    }
215
 
216
    eval "&drawpin_$pinside{\"$p\"} (\"$p\")";
217
}
218
 
219
# what remains are helper functions; for drawing each type of pin,
220
# each type of label, etc.
221
 
222
sub drawpin_nc {
223
}
224
 
225
sub drawpin_top {
226
    my($pin) = @_;
227
    $y = $maxy + 300;
228
    printf("P %d %d %d %d 1 0 0\n",
229
	   $pinx{$pin}, $y+300, $pinx{$pin}, $y);
230
    print "{\n";
231
    &pltext($pinx{$pin}, $y-50, 5, $pinlabel{$pin});
232
    &ntext($pinx{$pin}+50, $y+50, 0, $pin);
233
    print "}\n";
234
}
235
 
236
sub drawpin_bottom {
237
    my($pin) = @_;
238
    printf("P %d %d %d %d 1 0 0\n",
239
	   $pinx{$pin}, 0, $pinx{$pin}, 300);
240
    print "{\n";
241
    &pltext($pinx{$pin}, 350, 3, $pinlabel{$pin});
242
    &ntext($pinx{$pin}+50, 250, 2, $pin);
243
    print "}\n";
244
}
245
 
246
sub drawpin_labels {
247
    my($pin) = @_;
248
    &ltext($pinx{$pin}, $piny{$pin}, 3, $pinlabel{$pin});
249
}
250
 
251
sub circle {
252
    my ($x, $y) = @_;
253
    print "V $x $y 50 3 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n";
254
}
255
 
256
sub drawpin_left {
257
    my($pin) = @_;
258
    $x = $pinx{$pin};
259
    $px = 50;
260
    if ($bubble{$pin}) {
261
	$x -= 100;
262
	&circle($x+50, $piny{$pin});
263
    }
264
    if ($edge{$pin}) {
265
	$px += 100;
266
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
267
	       $pinx{$pin}, $piny{$pin}-50,
268
	       $pinx{$pin}+100, $piny{$pin});
269
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
270
	       $pinx{$pin}+100, $piny{$pin},
271
	       $pinx{$pin}, $piny{$pin}+50);
272
 
273
    }
274
    printf("P %d %d %d %d 1 0 0\n",
275
	   $pinx{$pin} - 300, $piny{$pin}, $x, $piny{$pin});
276
    print "{\n";
277
    &pltext($pinx{$pin} + $px, $piny{$pin}, 1, $pinlabel{$pin});
278
    &ntext($pinx{$pin} -100, $piny{$pin} + 50, 6, $pin);
279
    print "}\n";
280
}
281
 
282
sub drawpin_right {
283
    my($pin) = @_;
284
    $x = $pinx{$pin};
285
    $px = 50;
286
    if ($bubble{$pin}) {
287
	$x += 100;
288
	&circle($x-50, $piny{$pin});
289
    }
290
    if ($edge{$pin}) {
291
	$px += 100;
292
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
293
	       $pinx{$pin}, $piny{$pin}-50,
294
	       $pinx{$pin}-100, $piny{$pin});
295
	printf("L %d %d %d %d 3 0 0 0 0 0\n",
296
	       $pinx{$pin}-100, $piny{$pin},
297
	       $pinx{$pin}, $piny{$pin}+50);
298
 
299
    }
300
    printf("P %d %d %d %d 1 0 0\n",
301
	   $pinx{$pin} + 300, $piny{$pin}, $x, $piny{$pin});
302
    print "{\n";
303
    &pltext($pinx{$pin} - $px, $piny{$pin}, 7, $pinlabel{$pin});
304
    &ntext($pinx{$pin} +100, $piny{$pin} + 50, 0, $pin);
305
    print "}\n";
306
}
307
 
308
sub ntext {
309
    my ($x, $y, $a, $s) = @_;
310
    printf("T %d %d 5 8 1 1 0 %s 1\npinnumber=%s\n", $x, $y, $a, $s);
311
    printf("T %d %d 5 8 0 1 0 %s 1\npinseq=%s\n", $x, $y, $a, $s);
312
}
313
 
314
sub pltext {
315
    my ($x, $y, $a, $s) = @_;
316
    $s = "pinlabel=$s" unless $s =~ /=/;
317
    printf("T %d %d 9 10 1 1 0 %s 1\n%s\n", $x, $y, $a, $s);
318
}
319
 
320
sub ltext {
321
    my ($x, $y, $a, $s) = @_;
322
    printf("T %d %d 9 10 1 1 0 %s 1\n%s\n", $x, $y, $a, $s);
323
}
324
 
325
sub textlen {
326
    my($t) = @_;
327
    $t =~ s/^[^=]*=//;
328
    $t =~ s@\\_@@g;
329
    return length($t) * 120;
330
}
331
 
332
sub alignpin {
333
    my($v) = @_;
334
    return int(($v + 99) / 100) * 100;
335
}
336
 
337
exit $errors;