Advertisement
ptrawt

[CompArch] SimV2.0

Nov 28th, 2014
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. //kuy
  2. import java.io.*;
  3.  
  4. public class Sim
  5. {
  6.     public static int NUMMEMORY = 65536;    /* maximum number of words in memory */
  7.     public static int NUMREGS = 8;          /* number of machine registers */
  8.     public static int MAXLINELENGTH = 1000; // maximun number of instruction line
  9.    
  10.     /*File to be executed*/
  11.     // public static String fileName = "input.txt";
  12.     // public static String fileName = "inputMulti.txt";
  13.     public static String fileName = "inputComb.txt";
  14.     // public static String fileName = "inputSum.txt";
  15.    
  16.     /*Declaring the state class that contain pc, mem, reg and printState method*/
  17.     public static class stateType
  18.     {
  19.         int pc;
  20.         int[] mem = new int[NUMMEMORY];
  21.         int[] reg = new int[NUMREGS];
  22.         int numMemory;
  23.         public stateType()
  24.         {
  25.         }
  26.  
  27.         public void printState(stateType state)
  28.         {
  29.             System.out.println("\n@@@\nstate:");
  30.             System.out.println("\tpc "+state.pc);
  31.             System.out.println("\tmemory:");
  32.                 for (int i = 0; i < state.numMemory ; i++ )
  33.                 {
  34.                     System.out.println("\t\tmem["+i+"] "+state.mem[i]);
  35.                 }
  36.             System.out.println("\tregister:");
  37.                 for (int i = 0; i < NUMREGS ; i++ )
  38.                 {
  39.                     System.out.println("\t\treg["+i+"] "+state.reg[i]);
  40.                 }
  41.             System.out.println("end state");
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) throws IOException
  46.     {
  47.         stateType state = new stateType();
  48.         state.numMemory = 0;
  49.        
  50.         /*Initializing all registers to zero by default*/
  51.         for (int i = 0 ; i < NUMREGS ; i++ ) {
  52.             state.reg[i] = 0;
  53.         }
  54.        
  55.         /*Reading the executed file*/
  56.         String line;
  57.         FileReader inputFile = new FileReader(fileName);
  58.         BufferedReader bufferReader = new BufferedReader(inputFile);
  59.         while ((line = bufferReader.readLine()) != null)
  60.         {
  61.             state.mem[state.numMemory] = Integer.parseInt(line);            //Passing the instructions to memory
  62.             state.numMemory++;                                              //Counting a memory number
  63.         }
  64.         /*Displaying instructions fetched memory*/
  65.         for (int i = 0 ; i < state.numMemory ; i++ ) {
  66.             System.out.println("memory["+i+"]="+state.mem[i]);
  67.         }
  68.        
  69.         /*Declaring instruction , function , type and field 0 - 2 variables*/
  70.         String[] ins = new String[state.numMemory];                         //ins for containing the instruction line at any pc
  71.         String[] func = new String[state.numMemory];                        //func for containing the instruction function at any pc
  72.         char[] type = new char[state.numMemory];                            //type for containing the instruction type at any pc
  73.         /*field0-2 for containing fields for any pc*/
  74.         int[] field0 = new int[state.numMemory];
  75.         int[] field1 = new int[state.numMemory];
  76.         int[] field2 = new int[state.numMemory];
  77.        
  78.         /*Splitting instruction line to field0-2*/
  79.         for (int i = 0; i < state.numMemory ; i++ )
  80.         {
  81.             ins[i] = Integer.toBinaryString(state.mem[i]);                          //Getting instructions from memory
  82.             ins[i] = extendBitMac(ins[i]);                                          //Extending rough binary to 32-bit binary string
  83.             if(ins[i].length() > 16 && ins[i].substring(0,7).equals("0000000"))     //Validating first seven binary bits
  84.             {
  85.                 /*Getting function name and type from methods*/
  86.                 func[i] = insName(ins[i]);
  87.                 type[i] = insType(func[i]);
  88.                 /*If instruction type is not O then split instruct line for field0 and field1*/
  89.                 if(type[i] != 'O')
  90.                 {
  91.                     field0[i] = Integer.parseInt(ins[i].substring(10,13),2);
  92.                     field1[i] = Integer.parseInt(ins[i].substring(13,16),2);
  93.                     /*If get R-type instructions then get field2 from last 3 bits*/
  94.                     if(type[i] == 'R')
  95.                     {
  96.                         field2[i] = Integer.parseInt(ins[i].substring(29,32),2);
  97.                     }
  98.                     /*If get I-type instructions then get field2 from converting two's complement binary back to decimal*/
  99.                     else if(type[i] == 'I')
  100.                     {
  101.                         field2[i] = (short)Integer.parseInt(ins[i].substring(16,32),2);
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.  
  107.         /*Initializing the executed counter*/
  108.         int counter = 0;
  109.         for(state.pc = 0 ; state.pc < state.numMemory ; state.pc++)
  110.         {
  111.             state.printState(state);                //Displaying memory and registers state
  112.             counter++;                              //Incresing the executed counter
  113.            
  114.             /*Instruction control*/
  115.             switch(type[state.pc])
  116.             {
  117.                 case 'R':
  118.                     switch(func[state.pc])
  119.                     {
  120.                         /*add : destReg = regA+regB*/
  121.                         case "add":
  122.                             state.reg[field2[state.pc]] = state.reg[field0[state.pc]]+state.reg[field1[state.pc]];
  123.                             break;
  124.                         /*nand : destReg = ~(regA & regB)*/
  125.                         case "nand":
  126.                             state.reg[field2[state.pc]] = ~ (state.reg[field0[state.pc]] & state.reg[field1[state.pc]]);
  127.                             break;
  128.                     }
  129.                 case 'I':
  130.                     switch(func[state.pc])
  131.                     {
  132.                         /*lw : regB = mem[regA+offset]*/
  133.                         case "lw":
  134.                             state.reg[field1[state.pc]] = state.mem[state.reg[field0[state.pc]] + field2[state.pc]];
  135.                             break;
  136.                         /*
  137.                          sw : mem[regA+offset] = regB
  138.                             If regA+offset is greater than memory used then increase memory by regA+offset
  139.                         */
  140.                         case "sw":
  141.                             if(state.numMemory < NUMMEMORY)
  142.                             {
  143.                                 if(state.reg[field0[state.pc]] + field2[state.pc] > state.numMemory)
  144.                                 {
  145.                                     state.numMemory = (state.reg[field0[state.pc]] + field2[state.pc]);
  146.                                 }
  147.                                 state.mem[state.reg[field0[state.pc]] + field2[state.pc]] = state.reg[field1[state.pc]];
  148.                             }
  149.                             /*If memory is greater or equal than NUMMEMORY then display full memory error*/
  150.                             else
  151.                             {
  152.                                 System.out.println("Memory is full.");
  153.                             }
  154.                             break;
  155.                         /*beq : if regA = regB then pc += offset*/
  156.                         case "beq":
  157.                             if(state.reg[field0[state.pc]] == state.reg[field1[state.pc]])
  158.                             {
  159.                                 state.pc += field2[state.pc];
  160.                             }
  161.                             break;
  162.                     }
  163.                 case 'J':
  164.                     switch(func[state.pc])
  165.                     {
  166.                         /*jalr :
  167.                             regB = pc+1
  168.                             if regA = regB then pc = regB
  169.                             else pc = regA - 1
  170.                         */
  171.                         case "jalr":
  172.                             state.reg[field1[state.pc]] = state.pc + 1;
  173.                             if(field0[state.pc] == field1[state.pc])
  174.                             {
  175.                                 state.pc = state.reg[field1[state.pc]];
  176.                             }
  177.                             else
  178.                             {
  179.                                 state.pc = state.reg[field0[state.pc]] - 1;
  180.                             }
  181.                             break;
  182.                     }
  183.                 case 'O':
  184.                     switch(func[state.pc])
  185.                     {
  186.                         /* halt : display the last executed counter and shut the state down */
  187.                         case "halt":
  188.                             state.pc++;
  189.                             System.out.println("machine halted");
  190.                             System.out.println("total of "+counter+" instructions executed");
  191.                             System.out.println("final state of machine:");
  192.                             state.printState(state);
  193.                             state.pc = state.numMemory + 1;
  194.                             break;
  195.                         /* noop : do nothing*/
  196.                         case "noop":
  197.                             break;
  198.                     }
  199.             }
  200.         }
  201.         bufferReader.close();
  202.     }
  203.  
  204.     //Extending rough binary to 32-bit binary string
  205.     public static String extendBitMac(String str)
  206.     {
  207.         while(str.length() < 32 && str.length() > 16)
  208.         {
  209.             str = 0+str;
  210.         }
  211.         return str;
  212.     }
  213.  
  214.     /*To get instruction name from 3-bit binary*/
  215.     public static String insName(String str)
  216.     {
  217.         String op = str.substring(7, 10);
  218.         switch(op)
  219.         {
  220.             case "000":
  221.                 return "add";
  222.             case "001":
  223.                 return "nand";
  224.             case "010":
  225.                 return "lw";
  226.             case "011":
  227.                 return "sw";
  228.             case "100":
  229.                 return "beq";
  230.             case "101":
  231.                 return "jalr";
  232.             case "110":
  233.                 return "halt";
  234.             case "111":
  235.                 return "noop";
  236.             default:
  237.                 return "Wrong OP code";
  238.         }
  239.     }
  240.  
  241.     /*To get instruction type from instruction name*/
  242.     public static char insType(String str)
  243.     {
  244.         switch(str)
  245.         {
  246.             case "add":
  247.             case "nand":
  248.                 return 'R';
  249.             case "lw":
  250.             case "sw":
  251.             case "beq":
  252.                 return 'I';
  253.             case "jalr":
  254.                 return 'J';
  255.             case "halt":
  256.             case "noop":
  257.                 return 'O';
  258.             default:
  259.                 return 'X';
  260.         }
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement