Etch/GDSII.pm

filter: Statementstmt 95.7% 273 / 285 Branchbran 71.6% 116 / 162 Conditioncond 61.6% 37 / 60 MC/DCmcdc 28.1% 9 / 32 Subroutinesub 94.1% 32 / 34 Podpod 0.0% 0 / 15 totaltotal 79.4% 467 / 588 CC 118 SCAR 29.9
CC
118
Cov
84%
parse
52.7
_serialize_element
19.8
_collect
17.3
CRAP 19.8
↑ Summary Etch/Process.pm »
1 package Physics::Etch::GDSII;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.02';
8  
9 # ===========================================================================
10 # Minimal, dependency-free GDSII stream reader / writer.
11 #
12 # Supports the records needed to describe an IC mask layer and flatten a cell
13 # hierarchy into absolute polygons:
14 # HEADER BGNLIB LIBNAME UNITS ENDLIB BGNSTR STRNAME ENDSTR
15 # BOUNDARY PATH SREF AREF LAYER DATATYPE WIDTH XY ENDEL SNAME COLROW
16 # STRANS MAG ANGLE PATHTYPE
17 #
18 # GDSII stores coordinates as signed 32-bit integers in "database units".
19 # UNITS gives (user_units_per_db_unit, meters_per_db_unit) as 8-byte reals.
20 # ===========================================================================
21  
22 # record types
23 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 R_HEADER => 0x00, R_BGNLIB => 0x01, R_LIBNAME => 0x02, R_UNITS => 0x03,
25 R_ENDLIB => 0x04, R_BGNSTR => 0x05, R_STRNAME => 0x06, R_ENDSTR => 0x07,
26 R_BOUNDARY => 0x08, R_PATH => 0x09, R_SREF => 0x0A, R_AREF => 0x0B,
27 R_TEXT => 0x0C, R_LAYER => 0x0D, R_DATATYPE => 0x0E, R_WIDTH => 0x0F,
28 R_XY => 0x10, R_ENDEL => 0x11, R_SNAME => 0x12, R_COLROW => 0x13,
29 R_STRANS => 0x1A, R_MAG => 0x1B, R_ANGLE => 0x1C, R_PATHTYPE => 0x21,
30 5 };
31  
32 # data types
33 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 D_NODATA => 0, D_BITARR => 1, D_INT2 => 2, D_INT4 => 3,
35 D_REAL8 => 5, D_ASCII => 6,
36 5 };
37  
38 # ---------------------------------------------------------------------------
39 # Constructor (used by both writer and reader)
40 # ---------------------------------------------------------------------------
41 2 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
42 2 my ( $class, %args ) = @_;
43 my $self = {
44 libname => $args{libname} // 'ETCH',
45 user_unit => $args{user_unit} // 1e-6, # 1 user unit = 1 micron
46 2 ▶ db_unit => $args{db_unit} // 1e-9, # 1 db unit = 1 nm
Condition100%6/6
$args{"libname"} // "ETCH"
l!l
11
$args{"user_unit"} // "1e-06"
l!l
11
$args{"db_unit"} // "1e-09"
l!l
11
MC/DC100%1/1
$args{"libname"} // "ETCH"
$args{"libname"}
MC/DC100%1/1
$args{"user_unit"} // "1e-06"
$args{"user_unit"}
MC/DC100%1/1
$args{"db_unit"} // "1e-09"
$args{"db_unit"}
Truth table100%2/2
A
A= $args{"libname"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"user_unit"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"db_unit"}
Aresult
11
00
47 structures => {}, # name => { name, elements }
48 order => [], # structure insertion order
49 };
50 2 return bless $self, $class;
51 }
52  
53 1 ▶sub libname { $_[0]->{libname} }
Subroutine libname: called CC=1 SCAR=0.0
Pod undocumented
54 1 ▶sub user_unit { $_[0]->{user_unit} } # meters per user unit
Subroutine user_unit: called CC=1 SCAR=0.0
Pod undocumented
55 1 ▶sub db_unit { $_[0]->{db_unit} } # meters per database unit
Subroutine db_unit: called CC=1 SCAR=0.0
Pod undocumented
56  
57 # db units per user unit (integer scale for coordinates)
58 4 ▶sub _scale { $_[0]->{user_unit} / $_[0]->{db_unit} }
Subroutine _scale: called CC=1 SCAR=0.0
59  
60 0 ▶sub structures { keys %{ $_[0]->{structures} } }
Subroutine structures: not called CC=1 SCAR=6.9
Pod undocumented
61 0 ▶sub structure { $_[0]->{structures}{ $_[1] } }
Subroutine structure: not called CC=1 SCAR=6.9
Pod undocumented
62  
63 # ===========================================================================
64 # Writer API
65 # ===========================================================================
66 6 ▶sub add_structure {
Subroutine add_structure: called CC=2 SCAR=6.9
Pod undocumented
67 6 my ( $self, $name ) = @_;
68 6 ▶ unless ( $self->{structures}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"structures"}{$name}) 4 2 6
69 4 $self->{structures}{$name} = { name => $name, elements => [] };
70 4 push @{ $self->{order} }, $name;
71 }
72 6 return $self;
73 }
74  
75 # points given in USER units (e.g. microns); stored as db-unit integers
76 1 ▶sub add_boundary {
Subroutine add_boundary: called CC=3 SCAR=11.1
Pod undocumented
77 1 my ( $self, $struct, %a ) = @_;
78 1 $self->add_structure($struct);
79 1 my $scale = $self->_scale;
80 5 my @xy = map { [ _round( $_->[0] * $scale ), _round( $_->[1] * $scale ) ] }
81 1 @{ $a{points} };
82 1 ▶ push @{ $self->{structures}{$struct}{elements} },
Condition50%2/4
$a{"layer"} // 0
l!l
10
$a{"datatype"} // 0
l!l
01
MC/DC0%0/1
$a{"layer"} // 0
$a{"layer"}
MC/DC0%0/1
$a{"datatype"} // 0
$a{"datatype"}
Truth table50%1/2
A
A= $a{"layer"}
Aresult
11
00
Truth table50%1/2
A
A= $a{"datatype"}
Aresult
11
00
83 {
84 type => 'boundary',
85 layer => $a{layer} // 0,
86 1 datatype => $a{datatype} // 0,
87 xy => \@xy,
88 };
89 1 return $self;
90 }
91  
92 # convenience: axis-aligned rectangle from (x,y) width x height, user units
93 1 ▶sub add_rectangle {
Subroutine add_rectangle: called CC=1 SCAR=0.0
Pod undocumented
94 1 my ( $self, $struct, %a ) = @_;
95 1 my ( $x, $y, $w, $h ) = @a{qw(x y width height)};
96 return $self->add_boundary(
97 $struct,
98 layer => $a{layer},
99 datatype => $a{datatype},
100 1 points => [ [ $x, $y ], [ $x + $w, $y ], [ $x + $w, $y + $h ],
101 [ $x, $y + $h ], [ $x, $y ] ],
102 );
103 }
104  
105 2 ▶sub add_sref {
Subroutine add_sref: called CC=4 SCAR=14.0
Pod undocumented
106 2 my ( $self, $struct, %a ) = @_;
107 2 $self->add_structure($struct);
108 2 my $scale = $self->_scale;
109 2 ▶ push @{ $self->{structures}{$struct}{elements} },
Condition66%4/6
$a{"reflect"} // 0
l!l
02
$a{"mag"} // 1
l!l
02
$a{"angle"} // 0
l!l
11
MC/DC0%0/1
$a{"reflect"} // 0
$a{"reflect"}
MC/DC0%0/1
$a{"mag"} // 1
$a{"mag"}
MC/DC100%1/1
$a{"angle"} // 0
$a{"angle"}
Truth table50%1/2
A
A= $a{"reflect"}
Aresult
11
00
Truth table50%1/2
A
A= $a{"mag"}
Aresult
11
00
Truth table100%2/2
A
A= $a{"angle"}
Aresult
11
00
110 {
111 type => 'sref',
112 sname => $a{sname},
113 xy => [ [ _round( $a{x} * $scale ), _round( $a{y} * $scale ) ] ],
114 reflect => $a{reflect} // 0,
115 mag => $a{mag} // 1,
116 2 angle => $a{angle} // 0,
117 };
118 2 return $self;
119 }
120  
121 1 ▶sub add_aref {
Subroutine add_aref: called CC=4 SCAR=14.2
Pod undocumented
122 1 my ( $self, $struct, %a ) = @_;
123 1 $self->add_structure($struct);
124 1 my $scale = $self->_scale;
125 1 my ( $cols, $rows ) = ( $a{cols}, $a{rows} );
126 # origin, col-axis point, row-axis point (user units)
127 3 my @pts = map { [ _round( $_->[0] * $scale ), _round( $_->[1] * $scale ) ] }
128 ( [ $a{x}, $a{y} ],
129 [ $a{x} + $cols * $a{col_pitch}, $a{y} ],
130 1 [ $a{x}, $a{y} + $rows * $a{row_pitch} ] );
131 1 ▶ push @{ $self->{structures}{$struct}{elements} },
Condition50%3/6
$a{"reflect"} // 0
l!l
01
$a{"mag"} // 1
l!l
01
$a{"angle"} // 0
l!l
01
MC/DC0%0/1
$a{"reflect"} // 0
$a{"reflect"}
MC/DC0%0/1
$a{"mag"} // 1
$a{"mag"}
MC/DC0%0/1
$a{"angle"} // 0
$a{"angle"}
Truth table50%1/2
A
A= $a{"reflect"}
Aresult
11
00
Truth table50%1/2
A
A= $a{"mag"}
Aresult
11
00
Truth table50%1/2
A
A= $a{"angle"}
Aresult
11
00
132 {
133 type => 'aref',
134 sname => $a{sname},
135 colrow => [ $cols, $rows ],
136 xy => \@pts,
137 reflect => $a{reflect} // 0,
138 mag => $a{mag} // 1,
139 1 angle => $a{angle} // 0,
140 };
141 1 return $self;
142 }
143  
144 1 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod undocumented
145 1 my ( $self, $file ) = @_;
146 1 ▶ open my $fh, '>:raw', $file or croak "GDSII: cannot write '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $file or croak("GDSII: cannot write '${file}': $!") 1 0 1
147 1 print {$fh} $self->_serialize;
148 1 close $fh;
149 1 return $self;
150 }
151  
152 1 ▶sub _serialize {
Subroutine _serialize: called CC=3 SCAR=11.0
153 1 my ($self) = @_;
154 1 my $out = '';
155 1 $out .= _rec( R_HEADER, D_INT2, 600 ); # version 6
156 1 $out .= _rec( R_BGNLIB, D_INT2, (0) x 12 ); # timestamps
157 1 $out .= _rec( R_LIBNAME, D_ASCII, $self->{libname} );
158 $out .= _rec( R_UNITS, D_REAL8,
159 1 $self->{db_unit} / $self->{user_unit}, $self->{db_unit} );
160  
161 1 for my $name ( @{ $self->{order} } ) {
162 2 my $s = $self->{structures}{$name};
163 2 $out .= _rec( R_BGNSTR, D_INT2, (0) x 12 );
164 2 $out .= _rec( R_STRNAME, D_ASCII, $name );
165 2 for my $el ( @{ $s->{elements} } ) {
166 4 $out .= $self->_serialize_element($el);
167 }
168 2 $out .= _rec( R_ENDSTR, D_NODATA );
169 }
170 1 $out .= _rec( R_ENDLIB, D_NODATA );
171 1 return $out;
172 }
173  
174 4 ▶sub _serialize_element {
Subroutine _serialize_element: called CC=17 SCAR=29.9
175 4 my ( $self, $el ) = @_;
176 4 my $out = '';
177 4 my $t = $el->{type};
178  
179 4 ▶ if ( $t eq 'boundary' || $t eq 'path' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'boundary' or $t eq 'path') { } 1 3 4
elsif ($t eq 'sref' or $t eq 'aref') { } 3 0 3
Condition66%2/3
$t eq "boundary" or $t eq "path"
l!l&&r!l&&!r
103
MC/DC50%1/2
$t eq "boundary" or $t eq "path"
$t eq "boundary" $t eq "path"
Truth table66%2/3
A or B
A= $t eq "boundary"B= $t eq "path"
ABresult
1X1
011
000
180 1 ▶ $out .= _rec( $t eq 'boundary' ? R_BOUNDARY : R_PATH, D_NODATA );
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'boundary' ? : 1 0 1
181 1 ▶ $out .= _rec( R_LAYER, D_INT2, $el->{layer} // 0 );
Condition50%1/2
$el->{"layer"} // 0
l!l
10
MC/DC0%0/1
$el->{"layer"} // 0
$el->{"layer"}
Truth table50%1/2
A
A= $el->{"layer"}
Aresult
11
00
182 1 ▶ $out .= _rec( R_DATATYPE, D_INT2, $el->{datatype} // 0 );
Condition50%1/2
$el->{"datatype"} // 0
l!l
10
MC/DC0%0/1
$el->{"datatype"} // 0
$el->{"datatype"}
Truth table50%1/2
A
A= $el->{"datatype"}
Aresult
11
00
183 1 ▶ $out .= _rec( R_WIDTH, D_INT4, $el->{width} ) if $el->{width};
Branch: 1 branch
BranchTrueFalseTotal
if $el->{"width"} 0 1 1
184 1 $out .= _rec( R_XY, D_INT4, map { @$_ } @{ $el->{xy} } );
185 1 $out .= _rec( R_ENDEL, D_NODATA );
186 }
187 ▶ elsif ( $t eq 'sref' || $t eq 'aref' ) {
Condition66%2/3
$t eq "sref" or $t eq "aref"
l!l&&r!l&&!r
210
MC/DC0%0/2
$t eq "sref" or $t eq "aref"
$t eq "sref" $t eq "aref"
Truth table66%2/3
A or B
A= $t eq "sref"B= $t eq "aref"
ABresult
1X1
011
000
188 3 ▶ $out .= _rec( $t eq 'sref' ? R_SREF : R_AREF, D_NODATA );
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'sref' ? : 2 1 3
189 3 $out .= _rec( R_SNAME, D_ASCII, $el->{sname} );
190 3 ▶ if ( $el->{reflect} || $el->{mag} != 1 || $el->{angle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->{"reflect"} or $el->{"mag"} != 1 or $el->{"angle"}) 1 2 3
Condition50%3/6
$el->{"reflect"} or $el->{"mag"} != 1 or $el->{"angle"}
l!l&&r!l&&!r
012
$el->{"reflect"} or $el->{"mag"} != 1
l!l&&r!l&&!r
003
MC/DC33%1/3
$el->{"reflect"} or $el->{"mag"} != 1 or $el->{"angle"}
$el->{"reflect"} $el->{"mag"} != 1 $el->{"angle"}
Truth table50%2/4
A or B or C
A= $el->{"reflect"}B= $el->{"mag"} != 1C= $el->{"angle"}
ABCresult
1XX1
01X1
0011
0000
191 1 ▶ $out .= _rec( R_STRANS, D_BITARR, $el->{reflect} ? 0x8000 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$el->{'reflect'} ? : 0 1 1
192 1 ▶ $out .= _rec( R_MAG, D_REAL8, $el->{mag} ) if $el->{mag} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $el->{"mag"} != 1 0 1 1
193 1 ▶ $out .= _rec( R_ANGLE, D_REAL8, $el->{angle} ) if $el->{angle};
Branch: 1 branch
BranchTrueFalseTotal
if $el->{"angle"} 1 0 1
194 }
195 3 ▶ $out .= _rec( R_COLROW, D_INT2, @{ $el->{colrow} } ) if $t eq 'aref';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "aref" 1 2 3
196 3 $out .= _rec( R_XY, D_INT4, map { @$_ } @{ $el->{xy} } );
197 3 $out .= _rec( R_ENDEL, D_NODATA );
198 }
199 4 return $out;
200 }
201  
202 # ===========================================================================
203 # Reader API
204 # ===========================================================================
205 1 ▶sub read {
Subroutine read: called CC=2 SCAR=7.0
Pod undocumented
206 1 my ( $class, $file ) = @_;
207 1 ▶ open my $fh, '<:raw', $file or croak "GDSII: cannot read '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or croak("GDSII: cannot read '${file}': $!") 1 0 1
208 1 local $/;
209 1 my $bytes = <$fh>;
210 1 close $fh;
211 1 return $class->parse($bytes);
212 }
213  
214 1 ▶sub parse {
Subroutine parse: called CC=38 SCAR=39.6
Pod undocumented
215 1 my ( $class, $bytes ) = @_;
216 1 my $self = $class->new;
217 1 $self->{structures} = {};
218 1 $self->{order} = [];
219  
220 1 my $pos = 0;
221 1 my $len = length $bytes;
222 1 my $cur_s; # current structure
223 my $cur_e; # current element
224  
225 1 ▶ while ( $pos + 4 <= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 4 <= $len) 31 0 31
226 31 my $rlen = unpack( 'n', substr( $bytes, $pos, 2 ) );
227 31 ▶ last if $rlen < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $rlen < 4 0 31 31
228 31 my $rtype = unpack( 'C', substr( $bytes, $pos + 2, 1 ) );
229 31 my $dtype = unpack( 'C', substr( $bytes, $pos + 3, 1 ) );
230 31 my $data = substr( $bytes, $pos + 4, $rlen - 4 );
231 31 $pos += $rlen;
232  
233 31 ▶ if ( $rtype == R_LIBNAME ) { $self->{libname} = _unstr($data); }
Branch: 21 branches
BranchTrueFalseTotal
if ($rtype == 2) { } 1 30 31
elsif ($rtype == 3) { } 1 29 30
elsif ($rtype == 5) { } 2 27 29
elsif ($rtype == 6) { } 2 25 27
elsif ($rtype == 7) { } 2 23 25
elsif ($rtype == 8) { } 1 22 23
elsif ($rtype == 9) { } 0 22 22
elsif ($rtype == 10) { } 2 20 22
elsif ($rtype == 11) { } 1 19 20
elsif ($rtype == 12) { } 0 19 19
elsif ($rtype == 13) { } 1 18 19
elsif ($rtype == 14) { } 1 17 18
elsif ($rtype == 15) { } 0 17 17
elsif ($rtype == 18) { } 3 14 17
elsif ($rtype == 19) { } 1 13 14
elsif ($rtype == 26) { } 1 12 13
elsif ($rtype == 27) { } 0 12 12
elsif ($rtype == 28) { } 1 11 12
elsif ($rtype == 16) { } 4 7 11
elsif ($rtype == 17) { } 4 3 7
elsif ($rtype == 4) { } 1 2 3
234 elsif ( $rtype == R_UNITS ) {
235 1 my @r = _real8_decode_all($data);
236 # r[0] = user units per db unit, r[1] = meters per db unit
237 1 $self->{db_unit} = $r[1];
238 1 ▶ $self->{user_unit} = $r[1] / $r[0] if $r[0];
Branch: 1 branch
BranchTrueFalseTotal
if $r[0] 1 0 1
239 }
240 2 elsif ( $rtype == R_BGNSTR ) { $cur_s = undef; }
241 elsif ( $rtype == R_STRNAME ) {
242 2 my $name = _unstr($data);
243 2 $self->add_structure($name);
244 2 $cur_s = $self->{structures}{$name};
245 }
246 2 elsif ( $rtype == R_ENDSTR ) { $cur_s = undef; }
247 1 elsif ( $rtype == R_BOUNDARY ) { $cur_e = { type => 'boundary', xy => [] }; }
248 0 elsif ( $rtype == R_PATH ) { $cur_e = { type => 'path', xy => [] }; }
249 2 elsif ( $rtype == R_SREF ) { $cur_e = { type => 'sref', reflect => 0, mag => 1, angle => 0 }; }
250 1 elsif ( $rtype == R_AREF ) { $cur_e = { type => 'aref', reflect => 0, mag => 1, angle => 0 }; }
251 0 elsif ( $rtype == R_TEXT ) { $cur_e = { type => 'text' }; }
252 1 ▶ elsif ( $rtype == R_LAYER ) { $cur_e->{layer} = unpack( 's>', $data ) if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 1 0 1
253 1 ▶ elsif ( $rtype == R_DATATYPE ) { $cur_e->{datatype} = unpack( 's>', $data ) if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 1 0 1
254 0 ▶ elsif ( $rtype == R_WIDTH ) { $cur_e->{width} = unpack( 'l>', $data ) if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 0 0 0
255 3 ▶ elsif ( $rtype == R_SNAME ) { $cur_e->{sname} = _unstr($data) if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 3 0 3
256 1 ▶ elsif ( $rtype == R_COLROW ) { $cur_e->{colrow} = [ unpack( 's>*', $data ) ] if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 1 0 1
257 1 ▶ elsif ( $rtype == R_STRANS ) { $cur_e->{reflect} = ( unpack( 'n', $data ) & 0x8000 ) ? 1 : 0 if $cur_e; }
Branch: 2 branches
BranchTrueFalseTotal
if $cur_e 1 0 1
unpack('n', $data) & 32768 ? : 0 1 1
258 0 ▶ elsif ( $rtype == R_MAG ) { $cur_e->{mag} = ( _real8_decode_all($data) )[0] if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 0 0 0
259 1 ▶ elsif ( $rtype == R_ANGLE ) { $cur_e->{angle} = ( _real8_decode_all($data) )[0] if $cur_e; }
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 1 0 1
260 elsif ( $rtype == R_XY ) {
261 4 my @v = unpack( 'l>*', $data );
262 4 my @pts;
263 4 push @pts, [ $v[$_], $v[ $_ + 1 ] ] for grep { $_ % 2 == 0 } 0 .. $#v;
264 4 ▶ $cur_e->{xy} = \@pts if $cur_e;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_e 4 0 4
265 }
266 elsif ( $rtype == R_ENDEL ) {
267 4 ▶ push @{ $cur_s->{elements} }, $cur_e if $cur_s && $cur_e;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_s and $cur_e 4 0 4
Condition33%1/3
$cur_s and $cur_e
!ll&&!rl&&r
004
MC/DC0%0/2
$cur_s and $cur_e
$cur_s $cur_e
Truth table33%1/3
A and B
A= $cur_sB= $cur_e
ABresult
0X0
100
111
268 4 $cur_e = undef;
269 }
270 1 elsif ( $rtype == R_ENDLIB ) { last; }
271 }
272 1 return $self;
273 }
274  
275 # ===========================================================================
276 # Flatten hierarchy -> absolute polygons on a layer
277 # returns list of arrayrefs of [x, y] points.
278 # unit: 'db' (integers), 'm' (meters, default), 'um', 'nm'
279 # ===========================================================================
280 2 ▶sub polygons {
Subroutine polygons: called CC=8 SCAR=22.4
Pod undocumented
281 2 my ( $self, %a ) = @_;
282 2 ▶ my $top = $a{structure} // $self->_top_structure;
Condition33%1/3
$a{"structure"} // $self->_top_structure
l!l&&r!l&&!r
020
MC/DC0%0/2
$a{"structure"} // $self->_top_structure
$a{"structure"} $self->_top_structure
Truth table33%1/3
A // B
A= $a{"structure"}B= $self->_top_structure
ABresult
1X1
011
000
283 2 my $layer = $a{layer};
284 2 ▶ my $unit = $a{unit} // 'm';
Condition100%2/2
$a{"unit"} // "m"
l!l
11
MC/DC100%1/1
$a{"unit"} // "m"
$a{"unit"}
Truth table100%2/2
A
A= $a{"unit"}
Aresult
11
00
285  
286 2 ▶ croak "GDSII: no such structure '$top'" unless $self->{structures}{$top};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"structures"}{$top} 0 2 2
287  
288 2 my @db = $self->_collect( $top, [ 1, 0, 0, 1, 0, 0 ], {}, $layer );
289  
290 my $f =
291 $unit eq 'db' ? 1
292 : $unit eq 'm' ? $self->{db_unit}
293 : $unit eq 'um' ? $self->{db_unit} / 1e-6
294 2 ▶ : $unit eq 'nm' ? $self->{db_unit} / 1e-9
Branch: 4 branches
BranchTrueFalseTotal
$unit eq 'db' ? : 0 2 2
$unit eq 'm' ? : 1 1 2
$unit eq 'um' ? : 1 0 1
$unit eq 'nm' ? : 0 0 0
295 : croak "GDSII: unknown unit '$unit'";
296  
297 2 return map { [ map { [ $_->[0] * $f, $_->[1] * $f ] } @$_ ] } @db;
298 }
299  
300 # recursive collection; transform t = [a,b,c,d,e,f] maps (x,y)->(a*x+c*y+e, b*x+d*y+f)
301 18 ▶sub _collect {
Subroutine _collect: called CC=16 SCAR=28.5
302 18 my ( $self, $sname, $t, $seen, $layer ) = @_;
303 18 ▶ return () if $seen->{$sname}++; # cycle guard
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$sname}++ 0 18 18
304 18 ▶ my $s = $self->{structures}{$sname} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"structures"}{$sname} or return () 18 0 18
305 18 my @out;
306  
307 18 for my $el ( @{ $s->{elements} } ) {
308 22 ▶ if ( $el->{type} eq 'boundary' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($el->{'type'} eq 'boundary') { } 16 6 22
elsif ($el->{'type'} eq 'sref') { } 4 2 6
elsif ($el->{'type'} eq 'aref') { } 2 0 2
309 16 ▶ next if defined $layer && ( $el->{layer} // 0 ) != $layer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $layer and ($el->{"layer"} // 0) != $layer 8 8 16
Condition60%3/5
defined $layer and ($el->{"layer"} // 0) != $layer
!ll&&!rl&&r
088
$el->{"layer"} // 0
l!l
160
MC/DC0%0/2
defined $layer and ($el->{"layer"} // 0) != $layer
defined $layer $el->{"layer"}
Truth table33%1/3
A and B
A= defined $layerB= $el->{"layer"}
ABresult
0X0
100
111
310 8 push @out, [ map { [ _apply( $t, @$_ ) ] } @{ $el->{xy} } ];
311 }
312 elsif ( $el->{type} eq 'sref' ) {
313 my $ct = _compose( $t,
314 4 _placement( $el, $el->{xy}[0][0], $el->{xy}[0][1] ) );
315 4 push @out, $self->_collect( $el->{sname}, $ct, { %$seen }, $layer );
316 }
317 elsif ( $el->{type} eq 'aref' ) {
318 2 my ( $cols, $rows ) = @{ $el->{colrow} };
319 2 my ( $p0, $p1, $p2 ) = @{ $el->{xy} };
320 2 ▶ my $cvx = $cols ? ( $p1->[0] - $p0->[0] ) / $cols : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cols ? : 2 0 2
321 2 ▶ my $cvy = $cols ? ( $p1->[1] - $p0->[1] ) / $cols : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cols ? : 2 0 2
322 2 ▶ my $rvx = $rows ? ( $p2->[0] - $p0->[0] ) / $rows : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rows ? : 2 0 2
323 2 ▶ my $rvy = $rows ? ( $p2->[1] - $p0->[1] ) / $rows : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rows ? : 2 0 2
324 2 for my $i ( 0 .. $cols - 1 ) {
325 6 for my $j ( 0 .. $rows - 1 ) {
326 12 my $ox = $p0->[0] + $i * $cvx + $j * $rvx;
327 12 my $oy = $p0->[1] + $i * $cvy + $j * $rvy;
328 12 my $ct = _compose( $t, _placement( $el, $ox, $oy ) );
329 push @out,
330 12 $self->_collect( $el->{sname}, $ct, { %$seen }, $layer );
331 }
332 }
333 }
334 }
335 18 return @out;
336 }
337  
338 # build a placement transform from strans/mag/angle + translation
339 16 ▶sub _placement {
Subroutine _placement: called CC=4 SCAR=14.1
340 16 my ( $el, $dx, $dy ) = @_;
341 16 ▶ my $m = $el->{mag} // 1;
Condition50%1/2
$el->{"mag"} // 1
l!l
160
MC/DC0%0/1
$el->{"mag"} // 1
$el->{"mag"}
Truth table50%1/2
A
A= $el->{"mag"}
Aresult
11
00
342 16 ▶ my $ang = ( $el->{angle} // 0 ) * 3.14159265358979 / 180;
Condition50%1/2
$el->{"angle"} // 0
l!l
160
MC/DC0%0/1
$el->{"angle"} // 0
$el->{"angle"}
Truth table50%1/2
A
A= $el->{"angle"}
Aresult
11
00
343 16 my $c = cos $ang;
344 16 my $s = sin $ang;
345 16 ▶ my $ry = $el->{reflect} ? -1 : 1; # reflect about x-axis first
Branch: 1 branch
BranchTrueFalseTotal
$el->{'reflect'} ? : 0 16 16
346 # combined: reflect(y) -> scale(m) -> rotate(ang) -> translate
347 # matrix [a,b,c,d,e,f]: x' = a*x + c*y + e ; y' = b*x + d*y + f
348 16 my $a = $m * $c;
349 16 my $b = $m * $s;
350 16 my $cc = -$m * $s * $ry;
351 16 my $d = $m * $c * $ry;
352 16 return [ $a, $b, $cc, $d, $dx, $dy ];
353 }
354  
355 16 ▶sub _compose {
Subroutine _compose: called CC=1 SCAR=0.0
356 16 my ( $t, $u ) = @_; # apply u then t: result = t o u
357 16 my ( $a1, $b1, $c1, $d1, $e1, $f1 ) = @$t;
358 16 my ( $a2, $b2, $c2, $d2, $e2, $f2 ) = @$u;
359 return [
360 16 $a1 * $a2 + $c1 * $b2,
361 $b1 * $a2 + $d1 * $b2,
362 $a1 * $c2 + $c1 * $d2,
363 $b1 * $c2 + $d1 * $d2,
364 $a1 * $e2 + $c1 * $f2 + $e1,
365 $b1 * $e2 + $d1 * $f2 + $f1,
366 ];
367 }
368  
369 40 ▶sub _apply {
Subroutine _apply: called CC=1 SCAR=0.0
370 40 my ( $t, $x, $y ) = @_;
371 40 return ( $t->[0] * $x + $t->[2] * $y + $t->[4],
372 $t->[1] * $x + $t->[3] * $y + $t->[5] );
373 }
374  
375 3 ▶sub _top_structure {
Subroutine _top_structure: called CC=5 SCAR=16.5
376 3 my ($self) = @_;
377 # a top cell is one never referenced by another; if several, pick the one
378 # with the most elements (the richest layout cell).
379 3 my %ref;
380 3 for my $s ( values %{ $self->{structures} } ) {
381 6 for my $el ( @{ $s->{elements} } ) {
382 12 ▶ $ref{ $el->{sname} } = 1 if $el->{sname};
Branch: 1 branch
BranchTrueFalseTotal
if $el->{"sname"} 9 3 12
383 }
384 }
385 3 my @tops = grep { !$ref{$_} } @{ $self->{order} };
386 3 ▶ return $self->{order}[-1] unless @tops;
Branch: 1 branch
BranchTrueFalseTotal
unless @tops 0 3 3
387 my ($best) = sort {
388 0 scalar( @{ $self->{structures}{$b}{elements} } )
389 0 <=> scalar( @{ $self->{structures}{$a}{elements} } )
390 } @tops;
391 3 return $best;
392 }
393  
394 # ===========================================================================
395 # Low-level helpers
396 # ===========================================================================
397 20 ▶sub _round { my $x = shift; return int( $x + ( $x >= 0 ? 0.5 : -0.5 ) ); }
Subroutine _round: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$x >= 0 ? : 20 0 20
398  
399 6 ▶sub _unstr { my $s = shift; $s =~ s/\x00+$//; return $s; }
Subroutine _unstr: called CC=1 SCAR=0.0
400  
401 # build one record (auto-pads to even length)
402 31 ▶sub _rec {
Subroutine _rec: called CC=8 SCAR=20.8
403 31 my ( $rtype, $dtype, @data ) = @_;
404 31 my $payload = '';
405 31 ▶ if ( $dtype == D_INT2 || $dtype == D_BITARR ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($dtype == 2 or $dtype == 1) { } 8 23 31
elsif ($dtype == 3) { } 4 19 23
elsif ($dtype == 5) { } 2 17 19
elsif ($dtype == 6) { } 6 11 17
Condition100%3/3
$dtype == 2 or $dtype == 1
l!l&&r!l&&!r
7123
MC/DC100%2/2
$dtype == 2 or $dtype == 1
$dtype == 2 $dtype == 1
Truth table100%3/3
A or B
A= $dtype == 2B= $dtype == 1
ABresult
1X1
011
000
406 8 $payload = pack( 's>*', @data );
407 }
408 elsif ( $dtype == D_INT4 ) {
409 4 $payload = pack( 'l>*', @data );
410 }
411 elsif ( $dtype == D_REAL8 ) {
412 2 $payload = join '', map { _real8_encode($_) } @data;
413 }
414 elsif ( $dtype == D_ASCII ) {
415 6 ▶ $payload = $data[0] // '';
Condition50%1/2
$data[0] // ""
l!l
60
MC/DC0%0/1
$data[0] // ""
$data[0]
Truth table50%1/2
A
A= $data[0]
Aresult
11
00
416 6 ▶ $payload .= "\x00" if length($payload) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if length($payload) % 2 2 4 6
417 }
418 31 my $rlen = 4 + length $payload;
419 31 return pack( 'n', $rlen ) . pack( 'C', $rtype ) . pack( 'C', $dtype ) . $payload;
420 }
421  
422 # --- GDSII 8-byte real (sign + 7-bit excess-64 base-16 exp + 56-bit mantissa)
423 2 ▶sub _real8_decode_all {
Subroutine _real8_decode_all: called CC=2 SCAR=6.9
424 2 my ($data) = @_;
425 2 my @out;
426 2 ▶ for ( my $i = 0; $i + 8 <= length $data; $i += 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 8 <= length $data) 3 2 5
427 3 push @out, _real8_decode( substr( $data, $i, 8 ) );
428 }
429 2 return @out;
430 }
431  
432 11 ▶sub _real8_decode {
Subroutine _real8_decode: called CC=3 SCAR=11.0
433 11 my ($b) = @_;
434 11 my @c = unpack( 'C8', $b );
435 11 ▶ my $sign = ( $c[0] & 0x80 ) ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$c[0] & 128 ? : 1 10 11
436 11 my $exp = ( $c[0] & 0x7f ) - 64;
437 11 my $mant = 0;
438 11 $mant = $mant * 256 + $c[$_] for 1 .. 7;
439 11 return $sign * $mant / ( 2**56 ) * ( 16**$exp );
440 }
441  
442 11 ▶sub _real8_encode {
Subroutine _real8_encode: called CC=9 SCAR=22.2
443 11 my ($v) = @_;
444 11 ▶ return "\x00" x 8 if $v == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v == 0 1 10 11
445 10 my $sign = 0;
446 10 ▶ if ( $v < 0 ) { $sign = 0x80; $v = -$v; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v < 0) 1 9 10
447 10 my $exp = 64;
448 10 ▶ while ( $v >= 1 ) { $v /= 16; $exp++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v >= 1) 11 10 21
449 10 ▶ while ( $v < 1 / 16 ) { $v *= 16; $exp--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v < "0.0625") 18 10 28
450 10 my $mant = int( $v * ( 2**56 ) + 0.5 );
451 0 ▶ if ( $mant >= 2**56 ) { $mant /= 16; $exp++; } # rounding carry
Branch: 1 branch
BranchTrueFalseTotal
if ($mant >= "7.20575940379279e+16") 0 10 10
452 10 ▶ $exp = 0 if $exp < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exp < 0 0 10 10
453 10 ▶ $exp = 127 if $exp > 127;
Branch: 1 branch
BranchTrueFalseTotal
if $exp > 127 0 10 10
454 10 my @bytes = ( $sign | ( $exp & 0x7f ) );
455 10 for my $i ( reverse 0 .. 6 ) {
456 70 my $shift = 2**( 8 * $i );
457 70 my $byte = int( $mant / $shift ) % 256;
458 70 push @bytes, $byte;
459 }
460 10 return pack( 'C8', @bytes );
461 }
462  
463 5 1;
464  
465 __END__
↑ Summary Etch/Process.pm »