#!/usr/local/bin/perl
#n1.plx

#use warnings;
#use strict;
############################################################################
# #
# Written by: Ben Schuchard and Chinmay Gupte 4.26.04 #
# For: EE571 VLSI Systems Design at Colorado State University #
# #
############################################################################

#User enters filename
print "Enter .cir file: ";
$file = <STDIN>;
chomp($file); #remove newline character from filename

#Get filename w/o extension
($name, $foo) = split /\x2e/,$file;

$output1 = $name."_pathhmill.spi";
$output2 = $name."_nanosim.spi";

open (READ_FILE, "<$file") or die "error: unable to open $file\n";
open (WRITE_FILE1, ">$output1") or die "Error, Unable to open $output1\n";
open (WRITE_FILE2, ">$output2") or die "Error, Unable to open $output2\n";

$newline = "Vadd VDD 0 1.8"; #new line to add for nanosim
print WRITE_FILE2 "$newline\n";

while(<READ_FILE>)
{
chomp($_);
$line = $_;

if(/^M_I/)
{
@fields = split /\s+/,$line;
$len = @fields;

#foreach $token (@fields)
for($i=0; $i<$len; $i++)
{
#if(count > 0)
if($i > 0)
{
#@array = split /_/,$token;
@array = split /_/,@fields[$i];
print "field at $i = @fields[$i]\n";
$length = @array;
if($length == 2)
{
$newstr = @array[0]."[".@array[1]."] ";
print WRITE_FILE1 "$newstr ";
print WRITE_FILE2 "$newstr ";
}
else
{
#print WRITE_FILE1 "$token ";
#print WRITE_FILE2 "$token ";
print WRITE_FILE1 "@fields[$i] ";
print WRITE_FILE2 "@fields[$i] ";
}
}
else
{
#print WRITE_FILE1 "$token ";
#print WRITE_FILE2 "$token ";
print WRITE_FILE1 "@fields[$i] ";
print WRITE_FILE2 "@fields[$i] ";
}
}
print WRITE_FILE1 "\n";
print WRITE_FILE2 "\n";
}
}

close(READ_FILE);
close(WRITE_FILE1);
close(WRITE_FILE2);