blib/lib/Perlilog/sysclasses/PLverilog.pl untested

filter: Statementstmt 0.0% 0 / 252 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 423 CC 49 SCAR 40.1
CC
49
Cov
0%
headers
182.0
instantiate
72.0
addvar
30.0
CRAP 55.3
1 #
2 # This file is part of the Perlilog project.
3 #
4 # Copyright (C) 2003, Eli Billauer
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #
20 # A copy of the license can be found in a file named "licence.txt", at the
21 # root directory of this project.
22 #
23  
24 ${__PACKAGE__.'::errorcrawl'}='system';
25 sub new {
26 my $this = shift;
27 my $self = $this->SUPER::new(@_);
28 $self -> const('inshash', {});
29  
30 return $self;
31 }
32  
33 sub who {
34 my $self = shift;
35 return "Verilog Obj. \'".$self->get('name')."\'";
36 }
37  
38 sub complete {
39 my $self = shift;
40 $self->SUPER::complete(@_);
41 my $fname= $self->get('vfile');
42 my $g = $self->globalobj;
43 unless (defined $fname) {
44 my $dir=$self->globalobj->get('filesdir');
45 $fname=$dir.'/'.$self->get('name').'.v';
46 $self->const('vfile',$fname);
47 }
48 $g ->ppush('vfiles', $fname);
49 }
50  
51 sub epilogue {
52 my $self = shift;
53 $self->SUPER::epilogue(@_);
54  
55 my $v = $self->get('verilog');
56 $self->ontop('`timescale 1ns / 10ps',"\n")
57 unless ((defined $v) && ($v =~ /[\t\s]*\`timescale/));
58 }
59  
60 sub files {
61 my $self = shift;
62 $self->SUPER::files(@_);
63 return if ($self->get('perlilog-no-file'));
64  
65 my $twin = $self->get('perlilog-equivalent');
66 if ((defined $twin) && ($self->isobject($twin))) {
67 # All is fine if the 'verilog's stringwise EQUAL, no less
68 return if ($twin->get('verilog') eq $self->get('verilog'));
69  
70 # Or yell... We don't stop execution, because we want to generate files
71 # for comparison.
72  
73 fishy("The Verilog produced by ".$self->who." is not equal to ".
74 $twin->who.", as should be due to the \'equivalent\' declaration of ".
75 "the former. Compare files ".$self->get('vfile')." and ".
76 $twin->get('vfile')."!\nNOTE: There Verilog code hereby produced should be ".
77 "considered unreliable\n");
78 }
79  
80 my $comment = $self->get('header-comment') || "";
81  
82 my $fname= $self->get('vfile');
83 my $verilog = $comment."\n".$self->get('verilog');
84  
85 # Now remove double line breaks (with possible associated white spaces)
86 $verilog =~ s/([\s\t]*\n){3,}/\n\n/g;
87  
88 open (VFILE, ">$fname")
89 || blow("Failed to open Verilog output file $fname\n");
90 print VFILE $verilog;
91 close VFILE;
92  
93 my $g = $self->globalobj();
94 $g->ppush('verilogfiles', $fname);
95 $g->ppush('verilogfilesobjects', $self);
96 }
97  
98 sub addvar {
99 my ($self, $var, $type, $drive, $dim) = @_;
100 my $lvar = lc($var);
101 my @vars = $self->get('varslist');
102 foreach (@vars) {
103 blow("Variable \'$var\' assigned to object ".$self->who." when a variable \'$_\' is already defined\n")
104 if (lc($_) eq $lvar);
105 }
106 my $ID = undef;
107 # If $type isn't defined, this is only a name reservation
108 if (defined $type) {
109 $ID = $self->makeID($var);
110 $self->set(['vars', $var, 'type'],$type);
111 $self->const(['vars', $var, 'dim'],$dim) if (defined $dim);
112 $self->set(['vars', $var, 'drive'],$drive) if (defined $drive);
113 $self->const(['vars', $var, 'ID'], $ID);
114 }
115 $self->ppush('varslist', $var);
116 return $ID;
117 }
118  
119 sub suggestvar {
120 my ($self, $name) = @_;
121 my $sug = $name;
122 my ($bulk, $num) = ($name =~ /^(.*)_(\d+)$/);
123 my @vars = $self->get('varslist');
124 my %v;
125  
126 foreach (@vars) { $v{lc($_)}=1; } # Store lowercased names
127 unless (defined $bulk) {
128 $bulk = $name;
129 $num = 0;
130 }
131
132 while ($v{lc($sug)}) {
133 $num++;
134 $sug = $bulk.'_'.$num;
135 }
136 return $sug;
137 }
138  
139 sub namevar {
140 my ($self, $var, $type, $drive, $dim) = @_;
141 my $name = $self->suggestvar($var);
142 my $ID = $self->addvar($name, $type, $drive, $dim);
143 return $name unless wantarray;
144 return ($name, $ID);
145 }
146  
147 sub copyvar {
148 my ($self, $target) = @_;
149 my @cluster = @{$Perlilog::EQVARS[$target]};
150 my $i;
151 my ($obj, $ID, $var);
152  
153 # TODO: When all works fine, add this shortcut (Look if
154 # the variable happens to be under our nose first).
155  
156 # ($obj, $var) = @{$Perlilog::VARS[$target]};
157 # if ($obj == $self) {
158 # return $var unless wantarray;
159 # return ($var, $target);
160 # }
161  
162  
163 # Now we search all the members is the cluster to see
164 # is one of them happen to belong to our object.
165  
166 foreach $i (@cluster) {
167 ($obj, $var) = @{$Perlilog::VARS[$i]};
168 if ($obj == $self) {
169 $ID = $i;
170 last;
171 }
172 }
173  
174 # If the variable doesn't appear, we make one, and attach
175 # it. The variable name we try is the last variable name
176 # we saw while searching. This will give a name that may
177 # make sense.
178  
179 unless (defined $ID) {
180 ($var, $ID) = $self->namevar($var, 'wire', 'in');
181 $self->attach($ID, $target);
182 }
183  
184 return $var unless wantarray;
185 return ($var, $ID);
186 }
187  
188 sub makeID {
189 my ($self, $var)=@_;
190 push @Perlilog::VARS, [$self, $var];
191 my $index = $#Perlilog::VARS;
192 $Perlilog::EQVARS[$index]=[$index];
193 return $index;
194 }
195  
196  
197 sub addins {
198 my ($self, $ins, $detached) = @_;
199  
200 $self->addvar($ins); # Reserve the name
201  
202 return $ins;
203 }
204  
205 sub suggestins {
206 my ($self, $name) = @_;
207 $name=$self->get('name').'_ins'
208 unless (defined $name);
209  
210 return $self->suggestvar($name);
211 }
212  
213 sub equivalent {
214 my ($self, $twin) = @_;
215  
216 puke("Target is not an object\n")
217 unless ($self->isobject($twin));
218  
219 puke($twin->who." can't be used as equivalent because it's declared as ".
220 "equivalent to another object\n")
221 if (defined $twin->get('perlilog-equivalent'));
222  
223 puke($self->who." can't be declared as equivalent to another object, because ".
224 $self->get('perlilog-equivalent-lock')->who." depends on it\n")
225 if (defined $self->get('perlilog-equivalent-lock'));
226  
227 $self->const('perlilog-equivalent', $twin);
228 $twin->set('perlilog-equivalent-lock', $self);
229  
230 return 1;
231 }
232  
233 sub bitrange {
234 my $self = shift;
235 my $ID = shift;
236 my ($obj, $var) = $self->IDvar($ID);
237  
238 puke("Faulty ID ".$self->prettyval($ID)." given\n")
239 unless ($self->isobject($obj));
240
241 my $dim = $obj->get(['vars', $var, 'dim']);
242
243 wrong("The dimension was not set for variable \'$var\' in ".
244 $obj->who()."\n") unless (defined $dim);
245  
246 return (0,0) if (length($dim)==0);
247
248 my ($x,$y) = $dim =~ /^\[(\d+):(\d+)\]$/;
249  
250 wrong("Faulty dimension ".$self->prettyval($dim)." for variable \'$var\' in ".
251 $obj->who()."\n") unless (defined $y);
252  
253 return ($x, $y);
254 }
255  
256 sub ontop {
257 my $self = shift;
258 return 0 if ($self->get('static'));
259 my @code=@_;
260 chomp @code;
261 my $code = join("\n", @code)."\n";
262 my $verilog = $self->get('verilog');
263 $verilog = '' unless (defined $verilog);
264 $self->set('verilog', $code.$verilog);
265 return 1; # Succeeded.
266 }
267  
268 sub append {
269 my $self = shift;
270 return 0 if ($self->get('static'));
271 my @code=@_;
272 chomp @code;
273 my $code = join("\n", @code)."\n";
274 my $verilog = $self->get('verilog');
275 $verilog = '' unless (defined $verilog);
276 $self->set('verilog',$verilog.$code);
277 return 1; # Succeeded.
278 }
279  
280 sub clocked {
281 my ($self, $code, $clk, @vars) = @_;
282 my ($ID, $type) = $self->getreset();
283 my $reset = $self->copyvar($ID);
284 my $neg = $type =~ /^neg/i;
285 $type =~ s/^neg//i; # Chop of negation if it is there
286 my $async = ($type eq 'async');
287 my $ifreset = $neg ? "!$reset" : $reset;
288 my $edge = $neg ? 'negedge' : 'posedge';
289 my $edges = $async ? "posedge $clk or $edge $reset" : "posedge $clk";
290 my $zeros='';
291 foreach (@vars) {
292 $zeros.=" $_ <= #1 0;\n";
293 }
294 chomp $zeros;
295 chomp $code;
296 my $always = <<END;
297 always \@($edges)
298 if ($ifreset)
299 begin
300 $zeros
301 end
302 else
303 begin
304 $code
305 end
306 END
307 return $always;
308 }
309  
310 sub headers {
311 my $self = shift;
312 $self->SUPER::headers(@_);
313 return 0 if ($self->get('static'));
314  
315 # If we have an equivalent object, we steal its name. This is necessary,
316 # so that the Verilog will be perfectly equivalent, and thus no warning
317 # is generated.
318  
319 my $twin = $self->get('perlilog-equivalent');
320 my $name;
321 if ((defined $twin) && ($self->isobject($twin))) {
322 $name = $twin->get('name');
323 } else {
324 $name = $self->get('name');
325 }
326  
327 my @vars=$self->get('varslist');
328  
329 # We now check up whether a Verilog module should be created
330 # at all: If it doesn't have any variables and it isn't static
331 # then the headers will be empty anyhow, which means that the
332 # module does nothing relevant. That means no Verilog code,
333 # and no Verilog file. (The Verilog code generated may be
334 # hosted by other modules).
335  
336 return if ($self->get('perlilog-no-file'));
337  
338 my @inputs = ();
339 my @outputs = ();
340 my @inouts = ();
341 my @headvars = ();
342 my @wires = ();
343 my @regs = ();
344 my ($v, $type, $i);
345  
346 # We now scan through the variable list and distribute
347 # them by their type
348 foreach $v (@vars) {
349 $type=$self->get(['vars', $v, 'type']);
350 next unless (defined $type);
351 # This block works like a "case" or "switch"
352 if ($type eq 'input') {
353 push @inputs, $v;
354 push @headvars, $v;
355 }
356 elsif ($type eq 'output') {
357 push @outputs, $v;
358 push @headvars, $v;
359 }
360 elsif ($type eq 'inout') {
361 push @inouts, $v;
362 push @headvars, $v;
363 }
364 elsif ($type eq 'wire') {
365 push @wires, $v;
366 }
367 elsif ($type eq 'reg') {
368 push @regs, $v;
369 }
370 elsif ($type eq 'outreg') {
371 push @outputs, $v;
372 push @regs, $v;
373 push @headvars, $v;
374 } else {
375 wrong("Unknown variable type ".$self->prettyval($type).
376 " of variable \'$v\' in Verilog module object \'$name\'\n");
377 }
378 }
379  
380 # Now we generate the module's header
381 my $decl = "module $name";
382 my $hvars=join(', ',@headvars);
383 $decl.="($hvars)" if $hvars;
384 $decl = $self->linebreak($decl.';', ' ');
385  
386 $decl.="\n\n";
387
388 # And on to variable declarations. We define a local subroutine that does the
389 # dirty job. This is good because it will have access to this scope's variables
390
391 my $d = sub {
392 my $type = shift;
393 my ($var, $dim);
394 foreach $var (@_) {
395 $dim = $self->get(['vars', $var, 'dim']);
396 wrong("No dimension set for variable \'$var\' in Verilog module object \'$name\'\n")
397 unless (defined $dim);
398 # Note: Right now we don't support arrays.
399 $decl.=" $type $dim $var;\n";
400 }
401 };
402
403 # We now use the subroutine to generate the relevant Verilog code
404 &$d('input', @inputs);
405 &$d('output', @outputs);
406 &$d('inout', @inouts);
407 &$d('reg', @regs);
408 &$d('wire', @wires);
409
410 $self->ontop($decl."\n");
411 $self->append("\nendmodule");
412 }
413  
414 sub instantiate {
415 my $self = shift;
416 $self->SUPER::instantiate(@_);
417  
418 my @vars = $self->get('varslist');
419  
420 # If the object has no variables (and thus no inputs or outputs)
421 # and is not going to instantiate anything, no need to make a
422 # Verilog file of it, nor instantiate it.
423  
424 if (($#vars==-1) && (not $self->get('static')) &&
425 (not ($self->get('children') ) )) {
426 $self->const('perlilog-no-file',1);
427 return;
428 }
429  
430 my $papa = $self->get('parent');
431 return unless (ref $papa);
432  
433 # Here we check for the 'equivalent' property. If it's an object, we copy the
434 # name of our twin sybling. If not, we const-assign "0" to this property, so
435 # noone else tries to change it later.
436  
437 my $twin = $self->get('perlilog-equivalent');
438 my $name;
439 if ((defined $twin) && ($self->isobject($twin))) {
440 $name = $twin->get('name');
441 } else {
442 $self->const('perlilog-equivalent',0) unless (defined $twin); # Block the property
443 $name = $self->get('name');
444 }
445  
446 my $h = $self->get('inshash');
447 my $extra = $self->get('insparams');
448 $extra = '' unless (defined $extra);
449 my $insname = $papa->suggestins($name.'_ins');
450 $papa->addins($insname);
451 my ($v, $pv);
452 my @i = ();
453  
454 my $ins = " $name $extra $insname(";
455
456 foreach $v (@vars) { # Scan variables for those who reach the outer world
457 $pv = ${$h}{$v};
458 next unless (defined $pv);
459 push @i, ".$v($pv)";
460 }
461  
462 $ins .= join(', ', @i).");";
463 $ins = $self->linebreak($ins, ' ');
464  
465 wrong("Failed to instantiate ".$self->who()." since parent is static Verilog\n")
466 unless ($papa->append("\n".$ins."\n\n"));
467 }