/******************** * Andrew Fowler * * Project 3 * * UFID 9499-6684 * ********************/ /* Welcome to the SPIM simulator that I have made, What it does is takes a binary file and converts it to MIPS assembally language as well as copies The registers (which will be stored in an array numerically) and then reprinted each time an operation finishes. */ // Here we have to import some I/O stuff and things // of that nature. import java.io.*; import java.util.*; import java.lang.*; public class P2 { public static void main (String[] args) throws java.io.IOException { // Some intializations int i =0; int break1 = 0; int arrayLength = 0; int instructionLength = 0; int Cycle = 0; int dataStart, RDin = 0, RSin = 0, RTin = 0; int[] regs; int memLocation = 0; int memOp = 0; String[] Sample; String wholeRow = ""; String first6 = ""; String last6 = ""; String first = ""; String operation = ""; String new123 = ""; String RS = "", RD = "", RT = "", veryLargeString = ""; // See where the break is and how many lines the array needs to be BufferedReader br0 = new BufferedReader (new FileReader ("Sample.txt")); while((wholeRow = br0.readLine()) != null) { wholeRow = wholeRow.substring(0, 32); if(break1==1) arrayLength++; else instructionLength++; if(wholeRow.equals("00000000000000000000000000001101")) break1 = 1; } dataStart = 64+instructionLength*4; regs = new int[32+arrayLength]; Sample = new String[arrayLength+instructionLength]; // Done with that part // Now to load the array with data arrayLength = 0; break1 = 0; br0 = new BufferedReader (new FileReader ("Sample.txt")); i=0; while((wholeRow = br0.readLine()) != null) { wholeRow = wholeRow.substring(0, 32); Sample[i++] = wholeRow; if(break1==1){ regs[32+arrayLength] = convert2s(wholeRow); arrayLength++; } else instructionLength++; if(wholeRow.equals("00000000000000000000000000001101")) break1 = 1; } // All done with the array BufferedReader br = new BufferedReader (new FileReader ("Sample.txt")); int numberIns = 0; int forever=1; int minus = 0; while(minus++ < 75){ first = "" + Sample[numberIns].charAt(0); first6 = Sample[numberIns].substring(0, 6); last6 = Sample[numberIns].substring(26, 32); int decValue = Integer.parseInt(last6, 2); if (first6.equals("000001")) { // BLTZ System.out.println(numberIns); String offsetS = ""; int offset = 0; RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); offsetS = Sample[numberIns].substring(16, 32); offsetS += "00"; offsetS = offsetS.substring(2, 18); offset = convert216s(offsetS); RSin = Integer.parseInt(RS, 2); RTin = 0; memOp = numberIns*4+64; operation = "BGTZ\tR" + RSin + ", #" + offset; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); offset += -4+regs[RSin]; if(regs[RSin]regs[RTin]){ numberIns += offset/4 +1; System.out.println("sucess2"); } else numberIns++; } if (first6.equals("001011")) { // SW String offsetS = ""; int offset = 0; RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); offsetS = Sample[numberIns].substring(16, 32); offset = convert216s(offsetS); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); int tableoff = (offset+regs[RSin]-20)/4; regs[tableoff] = regs[RTin]; memOp = numberIns*4+64; operation = "SW\tR" + RTin + ", " + offset + "(R" + RSin + ")" ; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; System.out.println(regs[tableoff]); } if (first6.equals("000011")) { // LW System.out.println("LW"); String offsetS = ""; int offset = 0; RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); offsetS = Sample[numberIns].substring(16, 32); offset = convert216s(offsetS); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); int tableoff = (offset+regs[RSin]-20)/4; regs[RTin] = regs[tableoff]; memOp = numberIns*4+64; operation = "LW\tR" + RTin + ", " + offset + "(R" + RSin + ")" ; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; } if (first6.equals("011100")) { // MUL System.out.println("MUL"); RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); RD = Sample[numberIns].substring(16, 21); RDin = Integer.parseInt(RD, 2); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); regs[RDin] = regs[RSin]*regs[RTin]; memOp = numberIns*4+64; operation = "MUL\tR" + RDin + ", R" + RSin + ", R" + RTin; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; } if (first6.equals("000000")) { // choices choices choices if (last6.equals("001101")){ // BREAK memOp = numberIns*4+64; operation = "BREAK"; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; break; } if (last6.equals("000010")){ // SRL System.out.println("SRL"); RS = Sample[numberIns].substring(21, 26); RT = Sample[numberIns].substring(11, 16); RD = Sample[numberIns].substring(16, 21); RDin = Integer.parseInt(RD, 2); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); regs[RDin] = regs[RTin]/(int)Math.pow(2, RSin); memOp = numberIns*4+64; operation = "SRL\tR" + RDin + ", R" + RTin + ", #" + RSin; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; } if (last6.equals("100000")){ // ADD RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); RD = Sample[numberIns].substring(16, 21); RDin = Integer.parseInt(RD, 2); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); regs[RDin] = regs[RSin] + regs[RTin]; memOp = numberIns*4+64; operation = "ADD\tR" + RDin + ", R" + RSin + ", R" + RTin; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; } if (last6.equals("100010")){ // SUB RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); RD = Sample[numberIns].substring(16, 21); RDin = Integer.parseInt(RD, 2); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); regs[RDin] = regs[RSin] - regs[RTin]; memOp = numberIns*4+64; operation = "SUB\tR" + RDin + ", R" + RSin + ", R" + RTin; Cycle++; veryLargeString += "--------------------" + "\n" + "Cycle:"+Cycle+"\t"+memOp+"\t"+operation +"\n\n"; veryLargeString += sGen(regs, dataStart); numberIns++; } if (last6.equals("100100")){ // AND RS = Sample[numberIns].substring(6, 11); RT = Sample[numberIns].substring(11, 16); RD = Sample[numberIns].substring(16, 21); RDin = Integer.parseInt(RD, 2); RSin = Integer.parseInt(RS, 2); RTin = Integer.parseInt(RT, 2); regs[RDin] = regs[RSin] + regs[RTin]; RS = Integer.toBinaryString(regs[RSin]); RT = Integer.toBinaryString(regs[RTin]); RD = ""; for(int k=0; k