Hi guys, Hope you guys are doing okay. I coded my first program in PERL last night and it's 'almost' ready. I am making a logic simulator which reads logic gates data from a text file and calculates the output. The text file looks something like this:
INV 1 2 BUF 5 8 AND 1 3 4 INPUT 1 5 OUTPUT 2 8
First I stored the gates (INV, BUF, AND) in an array. Then I calculated the maximum number of nets (no. of nets= 8 in this case) and assigned dummy values to the nets in the array. Then I created various routines for the gates. For some reason, the routines are just acting funnny. They work for a few cases and they wouldn't work for others. The logic routines are something like this:

#Logic Gates Routines $t=0; $a=0; LAB: { if($gate[$t] eq "INV") { $mystring= @raw_data[$a]; OUTER: while($mystring =~ m/(\d+)/g) { print "\r\n"; push (@new, $1); } print "\r\n"; print @new; print " rerwerwe \r\n"; $inp=$new[0]; $out=$new[1]; print "\r\n"; print $net[$out-1]; print "\r\n"; print "$out \r\n"; if ($net[$inp-1] eq "1") { @net[$out-1]=0; $gate[$t]=0; $t++; print "fef"; last LAB; } elsif ($net[$inp-1] eq "0") { $net[$out-1]=1; $gate[$t]=0; $t++; last LAB; } else { $t++; last LAB; } } if($gate[$t] eq "BUF") { $mystring= @raw_data[$a]; OUTER: while($mystring =~ m/(\d+)/g) { print "\r\n"; push (@new, $1); } print @new; print "\r\n"; $inp=$new[0]; $out=$new[1]; if ($net[$inp-1] eq "1") { $net[$out-1]=1; $gate[$t]=0; $t++; last LAB; } elsif ($net[$inp-1] eq "0") { $net[$out-1]=0; $gate[$t]=0; $t++; last LAB; } else { $t++; last LAB; } } if($gate[$t] eq "AND") { $mystring= @raw_data[$a]; OUTER: while($mystring =~ m/(\d+)/g) { print "\r\n"; push (@new, $1); } print @new; print "\r\n"; $inp1=$new[0]; $inp2=$new[1]; $out=$new[2]; print $net[$inp1-1]; $new=$net[$inp1-1]; $new1=$net[$inp2-1]; $new2=$net[$out-1]; print "\r\n"; print $net[$inp2-1]; print "\r\n"; if($new == 1) { if($new1 ==1) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; print "fesf"; last LAB; } elsif($new1 ==0) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; print "g"; last LAB; } } if(($new) or ($new1) == 0) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } else { print "fersf"; } } if($gate[$t] eq "NAND") { $mystring= @raw_data[$a]; OUTER: while($mystring =~ m/(\d+)/g) { print "\r\n"; push (@new, $1); } print @new; print "\r\n"; $inp1=$new[0]; $inp2=$new[1]; $out=$new[2]; print $net[$inp1-1]; $new=$net[$inp1-1]; $new1=$net[$inp2-1]; $new2=$net[$out-1]; print "\r\n"; print $net[$inp2-1]; print "\r\n"; if($new == 1) { if($new1 ==1) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; print "fesf"; last LAB; } elsif($new1 ==0) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; print "g"; last LAB; } } if(($new) or ($new1) == 0) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } else { $t++; last LAB; } } if($gate[$t] eq "OR") { $mystring= @raw_data[$a]; OUTER: while($mystring =~ m/(\d+)/g) { print "\r\n"; push (@new, $1); } print @new; print "\r\n"; $inp1=$new[0]; $inp2=$new[1]; $out=$new[2]; print $net[$inp1-1]; $new=$net[$inp1-1]; $new1=$net[$inp2-1]; $new2=$net[$out-1]; print "\r\n"; print $net[$inp2-1]; print "\r\n"; if(($new)== 1) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } elsif(($new1)== 1) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } elsif(($new) == 0) { if(($new1) == 0) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } elsif(($new1) == 1) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } } else { $t++; last LAB; } } if($gate[$t] eq "NOR") { $mystring= @raw_data[$a]; OUTER: while($mystring =~ m/(\d+)/g) { print "\r\n"; push (@new, $1); } print @new; print "\r\n"; $inp1=$new[0]; $inp2=$new[1]; $out=$new[2]; print $net[$inp1-1]; $new=$net[$inp1-1]; $new1=$net[$inp2-1]; $new2=$net[$out-1]; print "\r\n"; print $net[$inp2-1]; print "\r\n"; if(($new)== 1) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } elsif(($new1)== 1) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } elsif(($new) == 0) { if(($new1) == 0) { $net[$out-1]=1; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } elsif(($new1) == 1) { $net[$out-1]=0; print $net[$out-1]; $gate[$t]=0; $t++; last LAB; } } else { $t++; last LAB; } } } print "@net \r\n"; print "@gate \r\n"; exit 0;

And, there's more:

At some points, the loop would work but at some point, it would just execute it even when the conditions have not been satisfied.

This is the other part of my code which is working fine.

open(DAT, $data_file) || die("Could not open file!"); @raw_data=<DAT>; $int=0; #calculating the number of gates: $mystring = @raw_data; while($mystring =~ m/(\d+)/g) { print "\r\n"; $lines= $1; $lines= $lines-2; } print "The number of gates is " . $lines ; print "\r\n"; $linez=$lines+2; #calculating the number of nets: #max is the number of nets $a=0; while($a<=$linez) { $mystring= @raw_data[$a]; while($mystring =~ m/(\d+)/g) { print "$1 \n"; push(@array1, $1); } print "\r\n"; my $max = max(@array1); push(@netsize, $max); $a++; } my $max = max(@netsize); print "\r\n"; #initializing the nets to 2 $n=0; while ($n<=$max-1) { @net[$n]=2; $n++ } print "The net array is @net \r\n"; print "Please enter the input vectors"; #The number of inputs $z=0; $mystring = @raw_data[-2]; while($mystring =~ m/(\d+)/g) { print "\r\n"; $z++; } $z--; print "The number of inputs are $z \r\n"; print "The number of gates is $lines \r\n"; print "The number of nets are $max \r\n"; print "The number of gates are $lines \r\n"; print "Enter the inputs \r\n"; #Getting the value of the gates: $mystring= @raw_data[-2]; while($mystring =~ m/(\d+)/g) { print "Enter the vector for input $1 \r\n"; $ip = <STDIN>; @net[$1-1]=$ip; } print "\r\n"; print "@net[2] \r\n"; #pushing the gates in the array: $b=0; $a=0; while($a!=$lines) { $mystring = @raw_data[$a]; if($mystring =~ /\A(.*?) /) { print "The gate is $1"; print "\r\n"; @gate[$b]=$1; $b++; } $a++; } join(",",@gate); join(",",@net); print "The net array is @net \r\n"; print "The gate array is @gate \r\n";
Can you guys please help me out. Thanks already.

In reply to I am designing a logic simulator but stuck. by Steven_gerrard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.