fork download
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Switch;
  5.  
  6. sub dec2bin {
  7. my $str = unpack("B32", pack("N", shift));
  8. $str =~ s/^0+(?=\d)//; # otherwise we get leading zeros
  9. switch($str)
  10. {
  11. case("0") {$str = '00'.$str;}
  12. case("1") {$str = '00'.$str;}
  13. case("10") {$str = '0'.$str;}
  14. case("11") {$str = '0'.$str;}
  15. }
  16. return $str;
  17. }
  18.  
  19. my @matrix_banyan;
  20. my $out_port;
  21.  
  22.  
  23. # Step 1 -
  24. # Pre Routing Matrix - To be used after Batcher sorted is implemented
  25. $matrix_banyan[9][0] = '0';
  26. $matrix_banyan[9][1] = '2';
  27. $matrix_banyan[9][2] = '4';
  28. $matrix_banyan[9][3] = '6';
  29. $matrix_banyan[9][4] = '1';
  30. $matrix_banyan[9][5] = '3';
  31. $matrix_banyan[9][6] = '5';
  32. $matrix_banyan[9][7] = '7';
  33.  
  34. $matrix_banyan[0][0] = '0';
  35. $matrix_banyan[0][1] = '4';
  36. $matrix_banyan[0][2] = '2';
  37. $matrix_banyan[0][3] = '6';
  38. $matrix_banyan[0][4] = '1';
  39. $matrix_banyan[0][5] = '5';
  40. $matrix_banyan[0][6] = '3';
  41. $matrix_banyan[0][7] = '7';
  42.  
  43. $matrix_banyan[1][0] = '0';
  44. $matrix_banyan[1][1] = '2';
  45. $matrix_banyan[1][2] = '1';
  46. $matrix_banyan[1][3] = '3';
  47. $matrix_banyan[1][4] = '4';
  48. $matrix_banyan[1][5] = '6';
  49. $matrix_banyan[1][6] = '5';
  50. $matrix_banyan[1][7] = '7';
  51.  
  52. $matrix_banyan[2][0] = '0';
  53. $matrix_banyan[2][1] = '1';
  54. $matrix_banyan[2][2] = '2';
  55. $matrix_banyan[2][3] = '3';
  56. $matrix_banyan[2][4] = '4';
  57. $matrix_banyan[2][5] = '5';
  58. $matrix_banyan[2][6] = '6';
  59. $matrix_banyan[2][7] = '7';
  60.  
  61.  
  62. my @random_set;
  63. my %seen;
  64.  
  65.  
  66. # S1 *
  67. # Generate Random Numbers
  68. for (1..8) {
  69. my $candidate = int rand(8); # rand returns random numbers
  70. redo if $seen{$candidate}++;
  71. if( $candidate eq 0 or $candidate eq 2 or $candidate eq 1) {
  72. next;
  73. }
  74. push @random_set, $candidate;
  75. }
  76.  
  77. print "\n => STEP 1 : Generate random output port options : ";
  78. print "\n\n \t => Output Ports are :\t";
  79. my @output_port_list = @random_set;
  80. print join(', ', @output_port_list), "\n";
  81.  
  82. # S2 *
  83. print "\n\n\n => STEP 2 : Apply Batcher Sorter to the Output Ports...";
  84. print "\n\n \t => Sorted Output Port Order :\t";
  85. # Use Merge Sort to sort the output ports in ascending order
  86. my @sorted_output_ports = sort @output_port_list; # Sort output ports
  87. print join(', ', @sorted_output_ports), "\n";
  88.  
  89. my @input; # Contains sorted output ports for each input port after batcher sorter
  90. my @final_input_ports= ();
  91.  
  92.  
  93. # Pre Banyan Switch Routing
  94. # Sorted Output Ports are routed to input ports before the banyan switch
  95. # Now, Make routing decision based on matrix_banyan[9][$i]
  96. # Now each input port has a packet with an output port address attached to it
  97. # Feed this array into Main Banyan Switcher
  98.  
  99. # S 3 * Pre-banyan - Trap Network( re route packets for input into banyan)
  100. # Trap network is for shuffling
  101. for ( my $i = 0,my $j = 0; $i < 8; $i++,$j++)
  102. {
  103. # Assign output ports to input ports
  104. $input[$i] = $sorted_output_ports[$j]; # ip[0] = op
  105. }
  106. print "\n\n\n => STEP 3 : Mapping Output Ports after Batcher Sorter & Trap Network:\n";
  107. # Assign NEW input ports to output ports based on [9][]
  108. # Each input port attaches output port number as header
  109. for(my $i = 0 ; $i < scalar(@sorted_output_ports); $i++)
  110. {
  111. # Packet at [9][6] input port should be assigned to [0][5] input port
  112. $final_input_ports[$i] = $matrix_banyan[9][$i];
  113. print "\n\t => Packet for Output Port $input[$i] will be routed through Input Port $final_input_ports[$i]\n\n\n";
  114. }
  115.  
  116. # Move packets from input to output
  117. print "\n\nSTEP 4 : Routing using Banyan : ";
  118.  
  119. # S 5 .. *
  120. for ( my $s = 0, my $p = 1; $s < @sorted_output_ports ; $s++,$p++)
  121. {
  122. # Process input ports one by one
  123. my $input_port = $final_input_ports[$s];
  124.  
  125. # Get the output port for the current input port
  126. my $output_port = $input[$s];
  127.  
  128. # Convert output port to binary for bit processing
  129. my $bin_output_port = dec2bin($output_port);
  130.  
  131. # Extract each bit of output port
  132. my @digits = split('',$bin_output_port);
  133.  
  134. my $current_port = $input_port;
  135. if ($current_port eq '')
  136. {next;}
  137.  
  138. print "\n\n\tInput Port $s => $bin_output_port\n";
  139.  
  140. # Main Banyan Switch
  141. for( my $i = 0, my $step = 1; $i<scalar(@digits);$i++, $step++)
  142. {
  143. my $ip = $current_port;
  144.  
  145. switch($digits[$i])
  146. {
  147. case 0 {
  148. # If current bit is 0 and current_port is a 1 in the 2*2 microswitch.
  149. # The packet has to go through 0 in the microswitch. Hence, the currentport value
  150. # is decremented.
  151. if ($current_port eq 1 or $current_port eq 3 or $current_port eq 5 or $current_port eq 7 )
  152. {
  153. $current_port = $current_port - 1;
  154. }
  155. $out_port = $matrix_banyan[$i][$current_port]; # Go Straight ahead through 0
  156. $current_port = $out_port; # Assign the output port as current port
  157.  
  158. }
  159. case 1 {
  160. # If current bit is 1 and current_port is a 0 in the 2*2 microswitch.
  161. # The packet has to go through 1 in the microswitch. Hence, the currentport value
  162. # is incremented.
  163. if ($current_port eq 0 or $current_port eq 2 or $current_port eq 4 or $current_port eq 6 )
  164. {
  165. $current_port = $current_port + 1;
  166. }
  167. $out_port = $matrix_banyan[$i][$current_port]; # Go through 1
  168. $current_port = $out_port; # Assign the output port as current port
  169. }
  170.  
  171. }
  172. print "\t\t\t\tStage $step :\n \t\t\t\tCurrent Port : $ip \n \t\t\t\tBit Being Processed : $digits[$i]\n \t\t\t\tOutput Port : $out_port \n\n";
  173. }
  174. }
Runtime error #stdin #stdout #stderr 1.72s 188928KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
===SORRY!===
Could not find strict in any of: /usr/lib/parrot/6.6.0/languages/perl6/lib, /usr/lib/parrot/6.6.0/languages/perl6