Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //kuy
- import java.io.*;
- public class Sim
- {
- public static int NUMMEMORY = 65536; /* maximum number of words in memory */
- public static int NUMREGS = 8; /* number of machine registers */
- public static int MAXLINELENGTH = 1000; // maximun number of instruction line
- /*File to be executed*/
- // public static String fileName = "input.txt";
- // public static String fileName = "inputMulti.txt";
- public static String fileName = "inputComb.txt";
- // public static String fileName = "inputSum.txt";
- /*Declaring the state class that contain pc, mem, reg and printState method*/
- public static class stateType
- {
- int pc;
- int[] mem = new int[NUMMEMORY];
- int[] reg = new int[NUMREGS];
- int numMemory;
- public stateType()
- {
- }
- public void printState(stateType state)
- {
- System.out.println("\n@@@\nstate:");
- System.out.println("\tpc "+state.pc);
- System.out.println("\tmemory:");
- for (int i = 0; i < state.numMemory ; i++ )
- {
- System.out.println("\t\tmem["+i+"] "+state.mem[i]);
- }
- System.out.println("\tregister:");
- for (int i = 0; i < NUMREGS ; i++ )
- {
- System.out.println("\t\treg["+i+"] "+state.reg[i]);
- }
- System.out.println("end state");
- }
- }
- public static void main(String[] args) throws IOException
- {
- stateType state = new stateType();
- state.numMemory = 0;
- /*Initializing all registers to zero by default*/
- for (int i = 0 ; i < NUMREGS ; i++ ) {
- state.reg[i] = 0;
- }
- /*Reading the executed file*/
- String line;
- FileReader inputFile = new FileReader(fileName);
- BufferedReader bufferReader = new BufferedReader(inputFile);
- while ((line = bufferReader.readLine()) != null)
- {
- state.mem[state.numMemory] = Integer.parseInt(line); //Passing the instructions to memory
- state.numMemory++; //Counting a memory number
- }
- /*Displaying instructions fetched memory*/
- for (int i = 0 ; i < state.numMemory ; i++ ) {
- System.out.println("memory["+i+"]="+state.mem[i]);
- }
- /*Declaring instruction , function , type and field 0 - 2 variables*/
- String[] ins = new String[state.numMemory]; //ins for containing the instruction line at any pc
- String[] func = new String[state.numMemory]; //func for containing the instruction function at any pc
- char[] type = new char[state.numMemory]; //type for containing the instruction type at any pc
- /*field0-2 for containing fields for any pc*/
- int[] field0 = new int[state.numMemory];
- int[] field1 = new int[state.numMemory];
- int[] field2 = new int[state.numMemory];
- /*Splitting instruction line to field0-2*/
- for (int i = 0; i < state.numMemory ; i++ )
- {
- ins[i] = Integer.toBinaryString(state.mem[i]); //Getting instructions from memory
- ins[i] = extendBitMac(ins[i]); //Extending rough binary to 32-bit binary string
- if(ins[i].length() > 16 && ins[i].substring(0,7).equals("0000000")) //Validating first seven binary bits
- {
- /*Getting function name and type from methods*/
- func[i] = insName(ins[i]);
- type[i] = insType(func[i]);
- /*If instruction type is not O then split instruct line for field0 and field1*/
- if(type[i] != 'O')
- {
- field0[i] = Integer.parseInt(ins[i].substring(10,13),2);
- field1[i] = Integer.parseInt(ins[i].substring(13,16),2);
- /*If get R-type instructions then get field2 from last 3 bits*/
- if(type[i] == 'R')
- {
- field2[i] = Integer.parseInt(ins[i].substring(29,32),2);
- }
- /*If get I-type instructions then get field2 from converting two's complement binary back to decimal*/
- else if(type[i] == 'I')
- {
- field2[i] = (short)Integer.parseInt(ins[i].substring(16,32),2);
- }
- }
- }
- }
- /*Initializing the executed counter*/
- int counter = 0;
- for(state.pc = 0 ; state.pc < state.numMemory ; state.pc++)
- {
- state.printState(state); //Displaying memory and registers state
- counter++; //Incresing the executed counter
- /*Instruction control*/
- switch(type[state.pc])
- {
- case 'R':
- switch(func[state.pc])
- {
- /*add : destReg = regA+regB*/
- case "add":
- state.reg[field2[state.pc]] = state.reg[field0[state.pc]]+state.reg[field1[state.pc]];
- break;
- /*nand : destReg = ~(regA & regB)*/
- case "nand":
- state.reg[field2[state.pc]] = ~ (state.reg[field0[state.pc]] & state.reg[field1[state.pc]]);
- break;
- }
- case 'I':
- switch(func[state.pc])
- {
- /*lw : regB = mem[regA+offset]*/
- case "lw":
- state.reg[field1[state.pc]] = state.mem[state.reg[field0[state.pc]] + field2[state.pc]];
- break;
- /*
- sw : mem[regA+offset] = regB
- If regA+offset is greater than memory used then increase memory by regA+offset
- */
- case "sw":
- if(state.numMemory < NUMMEMORY)
- {
- if(state.reg[field0[state.pc]] + field2[state.pc] > state.numMemory)
- {
- state.numMemory = (state.reg[field0[state.pc]] + field2[state.pc]);
- }
- state.mem[state.reg[field0[state.pc]] + field2[state.pc]] = state.reg[field1[state.pc]];
- }
- /*If memory is greater or equal than NUMMEMORY then display full memory error*/
- else
- {
- System.out.println("Memory is full.");
- }
- break;
- /*beq : if regA = regB then pc += offset*/
- case "beq":
- if(state.reg[field0[state.pc]] == state.reg[field1[state.pc]])
- {
- state.pc += field2[state.pc];
- }
- break;
- }
- case 'J':
- switch(func[state.pc])
- {
- /*jalr :
- regB = pc+1
- if regA = regB then pc = regB
- else pc = regA - 1
- */
- case "jalr":
- state.reg[field1[state.pc]] = state.pc + 1;
- if(field0[state.pc] == field1[state.pc])
- {
- state.pc = state.reg[field1[state.pc]];
- }
- else
- {
- state.pc = state.reg[field0[state.pc]] - 1;
- }
- break;
- }
- case 'O':
- switch(func[state.pc])
- {
- /* halt : display the last executed counter and shut the state down */
- case "halt":
- state.pc++;
- System.out.println("machine halted");
- System.out.println("total of "+counter+" instructions executed");
- System.out.println("final state of machine:");
- state.printState(state);
- state.pc = state.numMemory + 1;
- break;
- /* noop : do nothing*/
- case "noop":
- break;
- }
- }
- }
- bufferReader.close();
- }
- //Extending rough binary to 32-bit binary string
- public static String extendBitMac(String str)
- {
- while(str.length() < 32 && str.length() > 16)
- {
- str = 0+str;
- }
- return str;
- }
- /*To get instruction name from 3-bit binary*/
- public static String insName(String str)
- {
- String op = str.substring(7, 10);
- switch(op)
- {
- case "000":
- return "add";
- case "001":
- return "nand";
- case "010":
- return "lw";
- case "011":
- return "sw";
- case "100":
- return "beq";
- case "101":
- return "jalr";
- case "110":
- return "halt";
- case "111":
- return "noop";
- default:
- return "Wrong OP code";
- }
- }
- /*To get instruction type from instruction name*/
- public static char insType(String str)
- {
- switch(str)
- {
- case "add":
- case "nand":
- return 'R';
- case "lw":
- case "sw":
- case "beq":
- return 'I';
- case "jalr":
- return 'J';
- case "halt":
- case "noop":
- return 'O';
- default:
- return 'X';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement