#!/usr/bin/perl

# A script to convert ISPYs output into Helios ressource map (.rm) format.
# 2009, by Axel Muhr
# v1.0 - initial release

use strict;
use warnings;


my $counter = 0;
my $net_name = "Network";
my @data;
my $ram_one;
my $ram_two;
my $checkram = 1;

# parse command line parameters
while ( my $arg = shift @ARGV ) {
	# change network name
	if ($arg eq '-n' ) {
		$net_name = shift @ARGV;
	}	
	# don't evaluate RAM columns 
	if ($arg eq '-r' ) {
		$checkram = 0;
	}	
}

# read ISPY input
while (my $line = <>) {

	++$counter;

	next unless $counter > 2; 				# skip the first 2 lines

	my @item = split(/\s+/, $line);

	if ($checkram) {
		push @data, { number => $item[1], part => $item[2], rate => $item[3], link => $item[4], link_0 => $item[6], link_1 => $item[7], link_2 => $item[8], link_3 => $item[9], int_ram => $item[11],  ram_1 => $item[12] , ram_2 => $item[13] };
	} else {
		push @data, { number => $item[1], part => $item[2], rate => $item[3], link => $item[4], link_0 => $item[6], link_1 => $item[7], link_2 => $item[8], link_3 => $item[9] };		
	} 	
}

# print Helios .rm netlist
print "network /" . $net_name . " {\nReset { driver; ~00; tram_ra.d}\n";

foreach my $d (@data) {

print "  processor " . sprintf("%02d", $d->{number}) . " { " . &fl($d->{link_0}) . ", " . &fl($d->{link_1}) . ", " . &fl($d->{link_2}) . ", " . &fl($d->{link_3}) . "; }";

	if ($checkram) {
		# Check available transputer RAM
		($ram_one) = ($d->{ram_1} =~ /^(\d+)K/);

		if (!$d->{ram_2}) {
			$ram_two = 0;
		} else {
			($ram_two) = ($d->{ram_2} =~ /^(\d+)K/);
		}
		# print warning comment if RAM < 1MB
		if(($ram_one < 1024) && ($ram_two < 1024)) {
				print "   # RAM size too small for Helios nucleus!";
		}
	}
print "\n";	
}

print "  processor IO { ~00; IO }\n}\n";
# END main


# FormatLink function
sub fl { 
	my ($link_str) = @_;
	my @linknum;
	
	if ($link_str eq "...") {
		return "";
	} elsif ($link_str eq "HOST") {
		return "~IO";
	} else {
		@linknum = split(/:/, $link_str);
		return "~" . sprintf("%02d", $linknum[0]);
	}
}