From 55a7c112903044a61a872bf3bc7cd86ee2fabbe8 Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Fri, 7 Oct 2022 19:34:32 +0530 Subject: [PATCH] added statistics Co-authored-by: SriRam Mudragada --- .vscode/launch.json | 2 +- assignment-2/.vscode/launch.json | 22 ++ assignment-2/200010024_200010033-1/src/1.asm | 9 + assignment-2/200010024_200010033-1/src/1.out | Bin 0 -> 20 bytes .../src/configuration/Configuration.java | 116 ++++++++ .../src/configuration/config.xml | 43 +++ ...java_74fc5dae77a3b9a48197eb148ed1e37a.prob | 1 + .../src/generic/Instruction.java | 96 +++++++ .../src/generic/Misc.java | 10 + .../src/generic/Operand.java | 41 +++ .../src/generic/ParsedProgram.java | 268 ++++++++++++++++++ .../src/generic/Simulator.java | 153 ++++++++++ .../src/generic/Statistics.java | 13 + .../200010024_200010033-1/src/main/Main.java | 19 ++ assignment-2/bin/1.asm | 9 + assignment-2/bin/1.out | Bin 0 -> 20 bytes assignment-2/bin/configuration/config.xml | 43 +++ ...java_74fc5dae77a3b9a48197eb148ed1e37a.prob | 1 + assignment-2/build.xml | 44 +++ assignment-2/descending.out | Bin 0 -> 92 bytes assignment-2/supporting_files/bin/1.asm | 9 + assignment-2/supporting_files/bin/1.out | Bin 0 -> 20 bytes assignment-2/supporting_files/descending.out | 0 assignment-2/supporting_files/src/1.asm | 9 + assignment-2/supporting_files/src/1.out | Bin 0 -> 20 bytes .../src/configuration/Configuration.class | Bin 3909 -> 3909 bytes .../supporting_files/src/descending.out | Bin 0 -> 120 bytes .../supporting_files/src/even-odd.out | Bin 0 -> 36 bytes .../supporting_files/src/fibonacci.out | Bin 0 -> 100 bytes .../generic/Instruction$OperationType.class | Bin 2412 -> 2412 bytes .../src/generic/Instruction.class | Bin 2716 -> 2716 bytes .../supporting_files/src/generic/Misc.class | Bin 423 -> 423 bytes .../src/generic/Operand$OperandType.class | Bin 1076 -> 1076 bytes .../src/generic/Operand.class | Bin 1622 -> 1622 bytes .../src/generic/ParsedProgram$1.class | Bin 2007 -> 2007 bytes .../src/generic/ParsedProgram.class | Bin 5297 -> 5297 bytes .../src/generic/Simulator$1.class | Bin 1465 -> 1465 bytes .../src/generic/Simulator.class | Bin 4358 -> 4358 bytes .../src/generic/Simulator.java | 251 +++++++--------- .../src/generic/Statistics.class | Bin 283 -> 283 bytes .../supporting_files/src/main/Main.class | Bin 557 -> 557 bytes .../supporting_files/src/palindrome.out | Bin 0 -> 92 bytes assignment-2/supporting_files/src/prime.out | Bin 0 -> 60 bytes assignment-4/bin/hello.txt | 6 +- assignment-4/src/generic/Simulator.java | 6 +- assignment-4/src/generic/Statistics.java | 22 +- assignment-4/src/hello.txt | 6 +- .../src/processor/pipeline/Execute.java | 4 + .../src/processor/pipeline/OperandFetch.java | 17 +- assignment-4/supporting_files/descending.out | Bin 0 -> 120 bytes assignment-4/supporting_files/even-odd.out | Bin 0 -> 36 bytes assignment-4/supporting_files/fibonacci.out | Bin 0 -> 100 bytes assignment-4/supporting_files/palindrome.out | Bin 0 -> 92 bytes assignment-4/supporting_files/prime.out | Bin 0 -> 60 bytes 54 files changed, 1048 insertions(+), 172 deletions(-) create mode 100644 assignment-2/.vscode/launch.json create mode 100644 assignment-2/200010024_200010033-1/src/1.asm create mode 100644 assignment-2/200010024_200010033-1/src/1.out create mode 100644 assignment-2/200010024_200010033-1/src/configuration/Configuration.java create mode 100644 assignment-2/200010024_200010033-1/src/configuration/config.xml create mode 100644 assignment-2/200010024_200010033-1/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob create mode 100644 assignment-2/200010024_200010033-1/src/generic/Instruction.java create mode 100644 assignment-2/200010024_200010033-1/src/generic/Misc.java create mode 100644 assignment-2/200010024_200010033-1/src/generic/Operand.java create mode 100644 assignment-2/200010024_200010033-1/src/generic/ParsedProgram.java create mode 100644 assignment-2/200010024_200010033-1/src/generic/Simulator.java create mode 100644 assignment-2/200010024_200010033-1/src/generic/Statistics.java create mode 100644 assignment-2/200010024_200010033-1/src/main/Main.java create mode 100644 assignment-2/bin/1.asm create mode 100644 assignment-2/bin/1.out create mode 100644 assignment-2/bin/configuration/config.xml create mode 100644 assignment-2/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob create mode 100644 assignment-2/build.xml create mode 100644 assignment-2/descending.out create mode 100644 assignment-2/supporting_files/bin/1.asm create mode 100644 assignment-2/supporting_files/bin/1.out create mode 100644 assignment-2/supporting_files/descending.out create mode 100644 assignment-2/supporting_files/src/1.asm create mode 100644 assignment-2/supporting_files/src/1.out create mode 100644 assignment-2/supporting_files/src/descending.out create mode 100644 assignment-2/supporting_files/src/even-odd.out create mode 100644 assignment-2/supporting_files/src/fibonacci.out create mode 100644 assignment-2/supporting_files/src/palindrome.out create mode 100644 assignment-2/supporting_files/src/prime.out create mode 100644 assignment-4/supporting_files/descending.out create mode 100644 assignment-4/supporting_files/even-odd.out create mode 100644 assignment-4/supporting_files/fibonacci.out create mode 100644 assignment-4/supporting_files/palindrome.out create mode 100644 assignment-4/supporting_files/prime.out diff --git a/.vscode/launch.json b/.vscode/launch.json index c3cac34..f82083f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "name": "Launch Current File", "request": "launch", "mainClass": "${file}", - "args": ["assignment-4/src/configuration/config.xml", "assignment-4/src/hello.txt", "assignment-4/supporting_files/test_cases/descending.out"] + "args": ["assignment-1/src/descending.asm", "descending.out"] }, { "type": "java", diff --git a/assignment-2/.vscode/launch.json b/assignment-2/.vscode/launch.json new file mode 100644 index 0000000..db2aeb5 --- /dev/null +++ b/assignment-2/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Launch Current File", + "request": "launch", + "mainClass": "${file}", + "args": ["../assignment-1/src/descending.asm", "descending.out"] + }, + { + "type": "java", + "name": "Launch Main", + "request": "launch", + "mainClass": "main.Main", + "projectName": "assignment-2_d93382a2" + } + ] +} \ No newline at end of file diff --git a/assignment-2/200010024_200010033-1/src/1.asm b/assignment-2/200010024_200010033-1/src/1.asm new file mode 100644 index 0000000..613ab49 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/1.asm @@ -0,0 +1,9 @@ + .data +a: + 10 +b: + 20 + .text +main: + load %x0, $a, %x4 + end diff --git a/assignment-2/200010024_200010033-1/src/1.out b/assignment-2/200010024_200010033-1/src/1.out new file mode 100644 index 0000000000000000000000000000000000000000..57736dd0bdcc8b87d9e2925568fb6536c128c5b0 GIT binary patch literal 20 YcmZQzU|?ckVBi8`kqsOS3@?BT01Ctb!2kdN literal 0 HcmV?d00001 diff --git a/assignment-2/200010024_200010033-1/src/configuration/Configuration.java b/assignment-2/200010024_200010033-1/src/configuration/Configuration.java new file mode 100644 index 0000000..d7b43e1 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/configuration/Configuration.java @@ -0,0 +1,116 @@ +package configuration; + +import java.io.File; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import generic.Misc; + +public class Configuration { + public static int ALU_count; + public static int ALU_latency; + public static int ALU_reciprocal_of_throughput; + public static int multiplier_count; + public static int multiplier_latency; + public static int multiplier_reciprocal_of_throughput; + public static int divider_count; + public static int divider_latency; + public static int divider_reciprocal_of_throughput; + + public static int L1i_numberOfLines; + public static int L1i_latency; + public static int L1i_associativity; + public static String L1i_replacementPolicy; + + public static int L1d_numberOfLines; + public static int L1d_latency; + public static int L1d_associativity; + public static String L1d_replacementPolicy; + + public static int L2_numberOfLines; + public static int L2_latency; + public static int L2_associativity; + public static String L2_replacementPolicy; + + public static int mainMemoryLatency; + + public static void parseConfiguratioFile(String configFileName) + { + Document doc = null; + + try + { + File file = new File(configFileName); + DocumentBuilderFactory DBFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder DBuilder = DBFactory.newDocumentBuilder(); + doc = DBuilder.parse(file); + doc.getDocumentElement().normalize(); + } + catch(Exception e) + { + e.printStackTrace(); + Misc.printErrorAndExit("Error in reading config file : " + e); + } + + NodeList nodeLst = doc.getElementsByTagName("ALU"); + Element elmnt = (Element) nodeLst.item(0); + ALU_count = Integer.parseInt(getImmediateString("Count", elmnt)); + ALU_latency = Integer.parseInt(getImmediateString("Latency", elmnt)); + ALU_reciprocal_of_throughput = Integer.parseInt(getImmediateString("ReciprocalOfThroughput", elmnt)); + + nodeLst = doc.getElementsByTagName("Multiplier"); + elmnt = (Element) nodeLst.item(0); + multiplier_count = Integer.parseInt(getImmediateString("Count", elmnt)); + multiplier_latency = Integer.parseInt(getImmediateString("Latency", elmnt)); + multiplier_reciprocal_of_throughput = Integer.parseInt(getImmediateString("ReciprocalOfThroughput", elmnt)); + + nodeLst = doc.getElementsByTagName("Divider"); + elmnt = (Element) nodeLst.item(0); + divider_count = Integer.parseInt(getImmediateString("Count", elmnt)); + divider_latency = Integer.parseInt(getImmediateString("Latency", elmnt)); + divider_reciprocal_of_throughput = Integer.parseInt(getImmediateString("ReciprocalOfThroughput", elmnt)); + + nodeLst = doc.getElementsByTagName("L1iCache"); + elmnt = (Element) nodeLst.item(0); + L1i_numberOfLines = Integer.parseInt(getImmediateString("NumberOfLines", elmnt)); + L1i_latency = Integer.parseInt(getImmediateString("Latency", elmnt)); + L1i_associativity = Integer.parseInt(getImmediateString("Associativity", elmnt)); + L1i_replacementPolicy = getImmediateString("ReplacementPolicy", elmnt); + + nodeLst = doc.getElementsByTagName("L1dCache"); + elmnt = (Element) nodeLst.item(0); + L1d_numberOfLines = Integer.parseInt(getImmediateString("NumberOfLines", elmnt)); + L1d_latency = Integer.parseInt(getImmediateString("Latency", elmnt)); + L1d_associativity = Integer.parseInt(getImmediateString("Associativity", elmnt)); + L1d_replacementPolicy = getImmediateString("ReplacementPolicy", elmnt); + + nodeLst = doc.getElementsByTagName("L2Cache"); + elmnt = (Element) nodeLst.item(0); + L2_numberOfLines = Integer.parseInt(getImmediateString("NumberOfLines", elmnt)); + L2_latency = Integer.parseInt(getImmediateString("Latency", elmnt)); + L2_associativity = Integer.parseInt(getImmediateString("Associativity", elmnt)); + L2_replacementPolicy = getImmediateString("ReplacementPolicy", elmnt); + + nodeLst = doc.getElementsByTagName("Configuration"); + elmnt = (Element) nodeLst.item(0); + mainMemoryLatency = Integer.parseInt(getImmediateString("MainMemoryLatency", elmnt)); + } + + private static String getImmediateString(String tagName, Element parent) // Get the immediate string value of a particular tag name under a particular parent tag + { + NodeList nodeLst = parent.getElementsByTagName(tagName); + if (nodeLst.item(0) == null) + { + Misc.printErrorAndExit("XML Configuration error : Item \"" + tagName + "\" not found inside the \"" + parent.getTagName() + "\" tag in the configuration file!!"); + } + Element NodeElmnt = (Element) nodeLst.item(0); + NodeList resultNode = NodeElmnt.getChildNodes(); + return ((Node) resultNode.item(0)).getNodeValue(); + } +} diff --git a/assignment-2/200010024_200010033-1/src/configuration/config.xml b/assignment-2/200010024_200010033-1/src/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/configuration/config.xml @@ -0,0 +1,43 @@ + + + + + 2 + 1 + 1 + + + 1 + 4 + 1 + + + 1 + 10 + 1 + + + + + 256 + 2 + 4 + LRU + + + + 256 + 2 + 4 + LRU + + + + 2048 + 10 + 4 + LRU + + + 40 + \ No newline at end of file diff --git a/assignment-2/200010024_200010033-1/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob b/assignment-2/200010024_200010033-1/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob new file mode 100644 index 0000000..2fec9c8 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob @@ -0,0 +1 @@ +{"name":"Local: Simulator","url":"c:\\Users\\karth\\OneDrive\\Desktop\\new\\src\\generic\\Simulator.java","tests":[{"id":1662603879409,"input":"","output":""},{"id":1662603899098,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\karth\\OneDrive\\Desktop\\new\\src\\generic\\Simulator.java","group":"local","local":true} \ No newline at end of file diff --git a/assignment-2/200010024_200010033-1/src/generic/Instruction.java b/assignment-2/200010024_200010033-1/src/generic/Instruction.java new file mode 100644 index 0000000..ec3ef99 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/Instruction.java @@ -0,0 +1,96 @@ +package generic; + +public class Instruction { + + public enum OperationType {add, addi, sub, subi, mul, muli, div, divi, and, andi, or, ori, xor, xori, slt, slti, sll, slli, srl, srli, sra, srai, load, store, jmp, beq, bne, blt, bgt, end}; + + int programCounter; + OperationType operationType; + Operand sourceOperand1; + Operand sourceOperand2; + Operand destinationOperand; + + public int getProgramCounter() { + return programCounter; + } + public void setProgramCounter(int programCounter) { + this.programCounter = programCounter; + } + public OperationType getOperationType() { + return operationType; + } + public void setOperationType(OperationType operationType) { + this.operationType = operationType; + } + public Operand getSourceOperand1() { + return sourceOperand1; + } + public void setSourceOperand1(Operand sourceOperand1) { + this.sourceOperand1 = sourceOperand1; + } + public Operand getSourceOperand2() { + return sourceOperand2; + } + public void setSourceOperand2(Operand sourceOperand2) { + this.sourceOperand2 = sourceOperand2; + } + public Operand getDestinationOperand() { + return destinationOperand; + } + public void setDestinationOperand(Operand destinationOperand) { + this.destinationOperand = destinationOperand; + } + public String toString() + { + if(sourceOperand1 != null) + { + if(sourceOperand2 != null) + { + if(destinationOperand != null) + { + return "PC="+ programCounter + "\t" + operationType + "\t" + sourceOperand1 + "\t" + sourceOperand2 + "\t" + destinationOperand + "\n"; + } + else + { + return "PC="+ programCounter + "\t" + operationType + "\t" + sourceOperand1 + "\t" + sourceOperand2 + "\tnull" + "\n"; + } + } + else + { + if(destinationOperand != null) + { + return "PC="+ programCounter + "\t" + operationType + "\t" + sourceOperand1 + "\tnull" + "\t" + destinationOperand + "\n"; + } + else + { + return "PC="+ programCounter + "\t" + operationType + "\t" + sourceOperand1 + "\tnull" + "\tnull" + "\n"; + } + } + } + else + { + if(sourceOperand2 != null) + { + if(destinationOperand != null) + { + return "PC="+ programCounter + "\t" + operationType + "\tnull" + "\t" + sourceOperand2 + "\t" + destinationOperand + "\n"; + } + else + { + return "PC="+ programCounter + "\t" + operationType + "\tnull" + "\t" + sourceOperand2 + "\tnull" + "\n"; + } + } + else + { + if(destinationOperand != null) + { + return "PC="+ programCounter + "\t" + operationType + "\tnull" + "\tnull" + "\t" + destinationOperand + "\n"; + } + else + { + return "PC="+ programCounter + "\t" + operationType + "\tnull" + "\tnull" + "\tnull" + "\n"; + } + } + } + } +} diff --git a/assignment-2/200010024_200010033-1/src/generic/Misc.java b/assignment-2/200010024_200010033-1/src/generic/Misc.java new file mode 100644 index 0000000..0be690a --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/Misc.java @@ -0,0 +1,10 @@ +package generic; + +public class Misc { + + public static void printErrorAndExit(String message) + { + System.err.println(message); + System.exit(1); + } +} diff --git a/assignment-2/200010024_200010033-1/src/generic/Operand.java b/assignment-2/200010024_200010033-1/src/generic/Operand.java new file mode 100644 index 0000000..3224099 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/Operand.java @@ -0,0 +1,41 @@ +package generic; + +public class Operand { + + public enum OperandType {Register, Immediate, Label}; + + OperandType operandType; + int value; + String labelValue; //only applicable for Label type; + //Note that Label type is only applicable for functional emulation of assembly file + + public OperandType getOperandType() { + return operandType; + } + public void setOperandType(OperandType operandType) { + this.operandType = operandType; + } + public int getValue() { + return value; + } + public void setValue(int value) { + this.value = value; + } + public String getLabelValue() { + return labelValue; + } + public void setLabelValue(String labelValue) { + this.labelValue = labelValue; + } + public String toString() + { + if(operandType == OperandType.Register || operandType == OperandType.Immediate) + { + return "[" + operandType.toString() + ":" + value + "]"; + } + else + { + return "[" + operandType.toString() + ":" + labelValue + "]"; + } + } +} diff --git a/assignment-2/200010024_200010033-1/src/generic/ParsedProgram.java b/assignment-2/200010024_200010033-1/src/generic/ParsedProgram.java new file mode 100644 index 0000000..7cc69f0 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/ParsedProgram.java @@ -0,0 +1,268 @@ +package generic; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Scanner; +import java.util.regex.Pattern; +import generic.Instruction.OperationType; +import generic.Operand.OperandType; + +public class ParsedProgram { + static ArrayList code = new ArrayList(); + static int mainFunctionAddress; + static int firstCodeAddress; + static ArrayList data = new ArrayList(); + + public static void setMainFunctionAddress(int addr) + { + mainFunctionAddress = addr; + } + + public static void setFirstCodeAddress(int addr) + { + firstCodeAddress = addr; + } + + public static Instruction getInstructionAt(int programCounter) + { + return code.get(programCounter - firstCodeAddress); + } + + + static HashMap symtab = new HashMap(); + + + public static int parseDataSection(String assemblyProgramFile) + { + FileInputStream inputStream = null; + try + { + inputStream = new FileInputStream(assemblyProgramFile); + } + catch(FileNotFoundException e) { + Misc.printErrorAndExit(e.toString()); + } + + Scanner sc=new Scanner(inputStream); + int address = 0; //Store data staring from 1st memory location. At 0th location we denote the start of the code section. + + + while(sc.hasNextLine()) //TODO 3 loops? + { + String line=sc.nextLine(); + + if(line.contains(".data")) //Processing the .data section + { + line=sc.next(); + + do + { + if(Pattern.matches("[a-zA-Z]+([0-9]*)(:)", line)) + { + ParsedProgram.symtab.put(line.replaceAll("[^a-zA-Z]",""), address);//TODO removed statements that were adding data to the code arraylist + + while(Pattern.matches("-?\\d+", line=sc.next())) + { + data.add(Integer.parseInt(line)); + address++; + + } + } + }while(!line.contains(".text")); + + break; + } + } + + sc.close(); + + setFirstCodeAddress(address); + + return address; + } + + public static void parseCodeSection(String assemblyProgramFile, int firstCodeAddress) + { + FileInputStream inputStream = null; + try + { + inputStream = new FileInputStream(assemblyProgramFile); + } + catch(FileNotFoundException e) { + Misc.printErrorAndExit(e.toString()); + } + + Scanner sc=new Scanner(inputStream); + int address = firstCodeAddress; + + while(sc.hasNextLine()) + { + String line=sc.nextLine(); + if(line.contains(".text")) + { + break; + } + } + + while(sc.hasNextLine()) + { + String line; + if(Pattern.matches("[a-zA-Z]+([0-9]*)(:)", line=sc.nextLine())) + { + ParsedProgram.symtab.put(line.replaceAll(":",""), address); + if(line.replaceAll(":","").compareTo("main") == 0) + { + ParsedProgram.setMainFunctionAddress(address); + } + } + else + { + ParsedProgram.code.add(address-firstCodeAddress, getInstructionFromString(line, address)); + address++; + } + } + sc.close(); + } + + private static Instruction getInstructionFromString(String line, int address) + { + Instruction newInstruction = new Instruction(); + newInstruction.setProgramCounter(address); + + Scanner sc = new Scanner(line); + newInstruction.setOperationType(OperationType.valueOf(sc.next())); + + switch(newInstruction.getOperationType()) + { + //R3I type + case add : + case sub : + case mul : + case div : + case and : + case or : + case xor : + case slt : + case sll : + case srl : + case sra : { + newInstruction.setSourceOperand1(getRegisterOperandFromString(sc.next())); + newInstruction.setSourceOperand2(getRegisterOperandFromString(sc.next())); + newInstruction.setDestinationOperand(getRegisterOperandFromString(sc.next())); + break; + } + + //R2I type + case addi : + case subi : + case muli : + case divi : + case andi : + case ori : + case xori : + case slti : + case slli : + case srli : + case srai : + case load : + case store : { + newInstruction.setSourceOperand1(getRegisterOperandFromString(sc.next())); + String str = sc.next(); + if(Pattern.matches("-?\\d+(,)",str)) + { + //absolute immediate + newInstruction.setSourceOperand2(getImmediateOperandFromString(str)); + } + else + { + //label / symbol + newInstruction.setSourceOperand2(getLabelOperandFromString(str)); + } + newInstruction.setDestinationOperand(getRegisterOperandFromString(sc.next())); + break; + } + + case beq : + case bne : + case blt : + case bgt : { + newInstruction.setSourceOperand1(getRegisterOperandFromString(sc.next())); + newInstruction.setSourceOperand2(getRegisterOperandFromString(sc.next())); + String str = sc.next(); + if(Pattern.matches("[0-9]+(,)",str)) + { + //absolute immediate + newInstruction.setDestinationOperand(getImmediateOperandFromString(str)); + } + else + { + //label / symbol + newInstruction.setDestinationOperand(getLabelOperandFromString(str)); + } + break; + } + + //RI type : + case jmp : { + String str = sc.next(); + if(Pattern.matches("[0-9]+(,)",str)) + { + //absolute immediate + newInstruction.setDestinationOperand(getImmediateOperandFromString(str)); + } + else if(Pattern.matches("%x([0-9]{1,2})",str)) { + newInstruction.setDestinationOperand(getRegisterOperandFromString(str)); + } + else + { + //label / symbol + newInstruction.setDestinationOperand(getLabelOperandFromString(str)); + } + break; + } + + case end : break; + + default: Misc.printErrorAndExit("unknown instruction!!"); + } + + sc.close(); + + return newInstruction; + } + + private static Operand getRegisterOperandFromString(String str) + { + Operand operand = new Operand(); + operand.setOperandType(OperandType.Register); + operand.setValue(Integer.parseInt(str.replaceAll("[^0-9]", ""))); + return operand; + } + + private static Operand getImmediateOperandFromString(String str) + { + Operand operand = new Operand(); + operand.setOperandType(OperandType.Immediate); + operand.setValue(Integer.parseInt(str.replaceAll("[^-?\\d+]",""))); + return operand; + } + + private static Operand getLabelOperandFromString(String str) + { + Operand operand = new Operand(); + operand.setOperandType(OperandType.Label); + operand.setLabelValue(str.replaceAll("[$,]", "")); + return operand; + } + + static void printState() + { + System.out.println("Symbol Table :"); + System.out.println(Arrays.asList(symtab)); + System.out.println("\nParsed instructions :"); + System.out.println(Arrays.asList(code)); + } +} diff --git a/assignment-2/200010024_200010033-1/src/generic/Simulator.java b/assignment-2/200010024_200010033-1/src/generic/Simulator.java new file mode 100644 index 0000000..7384637 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/Simulator.java @@ -0,0 +1,153 @@ +package generic; + +import java.io.FileInputStream; +import generic.Operand.OperandType; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.io.ByteArrayInputStream; +import java.nio.ByteBuffer; + +public class Simulator { + + static FileInputStream inputcodeStream = null; + + public static Map map = new HashMap<>() { + { + put("add", "00000"); + put("sub", "00010"); + put("mul", "00100"); + put("div", "00110"); + put("and", "01000"); + put("or", "01010"); + put("xor", "01100"); + put("slt", "01110"); + put("sll", "10000"); + put("srl", "10010"); + put("sra", "10100"); + put("beq", "11001"); + put("bne", "11010"); + put("blt", "11011"); + put("bgt", "11100"); + put("load", "10110"); + put("store", "10111"); + put("jmp", "11000"); + put("addi", "00001"); + put("subi", "00011"); + put("muli", "00101"); + put("divi", "00111"); + put("andi", "01001"); + put("ori", "01011"); + put("xori", "01101"); + put("slti", "01111"); + put("slli", "10001"); + put("srli", "10011"); + put("srai", "10101"); + put("end", "11101"); + + } + }; + private static String toBinaryOfSpecificPrecision(int num, int lenOfTargetString) { + String binary = String.format("%" + lenOfTargetString + "s", Integer.toBinaryString(num)).replace(' ', '0'); + return binary; + } + private static String convert_1(Operand inst, int precision) { + + return toBinaryOfSpecificPrecision(inst.getValue(), precision); + + } + private static String convert(Operand inst, int precision) { + if (inst == null) + return toBinaryOfSpecificPrecision(0, precision); + + if (inst.getOperandType() == Operand.OperandType.Label) + return toBinaryOfSpecificPrecision(ParsedProgram.symtab.get(inst.getLabelValue()), precision); + + return toBinaryOfSpecificPrecision(inst.getValue(), precision); + + } + public static void setupSimulation(String assemblyProgramFile) + { + int firstCodeAddress = ParsedProgram.parseDataSection(assemblyProgramFile); + ParsedProgram.parseCodeSection(assemblyProgramFile, firstCodeAddress); + ParsedProgram.printState(); + } + + public static void assemble(String objectProgramFile) + { + + FileOutputStream file; + try { + file = new FileOutputStream(objectProgramFile); + //write the firstCodeAddress to the file + file.write(ByteBuffer.allocate(4).putInt(ParsedProgram.firstCodeAddress).array()); + //write the data to the file + for (var value : ParsedProgram.data) { + byte[] dataValue = ByteBuffer.allocate(4).putInt(value).array(); + file.write(dataValue); + } + //assemble one instruction at a time, and write to the file + for (var instruction : ParsedProgram.code) { + String line = ""; + String optype=(instruction.operationType).toString(); + line += map.get(optype); + // int op = Integer.parseInt(line, 2); + int op=Integer.parseInt(map.get(optype),2); + int pc = instruction.getProgramCounter(); + + if (op <= 20 && op % 2 == 0) { + // R3 Type + line += convert_1(instruction.getSourceOperand1(), 5); + line += convert_1(instruction.getSourceOperand2(), 5); + line += convert_1(instruction.getDestinationOperand(), 5); + line += toBinaryOfSpecificPrecision(0, 12); + + } + else if (op == 29) { + line += toBinaryOfSpecificPrecision(0, 27); + } + else if (op == 24) { + // RI Type + if (instruction.destinationOperand.getOperandType() == Operand.OperandType.Register) { + line += convert(instruction.getDestinationOperand(), 5); + line += toBinaryOfSpecificPrecision(0, 22); + } else { + line += toBinaryOfSpecificPrecision(0, 5); + int value = Integer.parseInt(convert(instruction.getDestinationOperand(), 5), 2) - pc; + String bin = toBinaryOfSpecificPrecision(value, 22); + line += bin.substring(bin.length() - 22); + } + } + else { + // R2I Type + if (op >= 25 && op <= 28) { + int value = Integer.parseInt(convert(instruction.getDestinationOperand(), 5), 2) - pc; + line += convert(instruction.getSourceOperand1(), 5); + line += convert(instruction.getSourceOperand2(), 5); + String bin = toBinaryOfSpecificPrecision(value, 17); + line += bin.substring(bin.length() - 17); + } else { + line += convert(instruction.getSourceOperand1(), 5); + line += convert(instruction.getDestinationOperand(), 5); + line += convert(instruction.getSourceOperand2(), 17); + } + } + int instInteger = (int) Long.parseLong(line, 2); + byte[] instBinary = ByteBuffer.allocate(4).putInt(instInteger).array(); + file.write(instBinary); + } + file.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/assignment-2/200010024_200010033-1/src/generic/Statistics.java b/assignment-2/200010024_200010033-1/src/generic/Statistics.java new file mode 100644 index 0000000..059cb7a --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/generic/Statistics.java @@ -0,0 +1,13 @@ +package generic; + +public class Statistics { + + // TODO add your statistics here + + // TODO write functions to update statistics + + public static void printStatistics(String statFile) + { + // TODO add code here to print statistics in the output file + } +} diff --git a/assignment-2/200010024_200010033-1/src/main/Main.java b/assignment-2/200010024_200010033-1/src/main/Main.java new file mode 100644 index 0000000..fcb74c3 --- /dev/null +++ b/assignment-2/200010024_200010033-1/src/main/Main.java @@ -0,0 +1,19 @@ +package main; +import configuration.Configuration; +import generic.Misc; +import generic.Statistics; +import generic.Simulator; + +public class Main { + + public static void main(String[] args) { + if(args.length != 2) + { + Misc.printErrorAndExit("usage: java -jar \n"); + } + + Simulator.setupSimulation(args[0]); + Simulator.assemble(args[1]); + } + +} diff --git a/assignment-2/bin/1.asm b/assignment-2/bin/1.asm new file mode 100644 index 0000000..613ab49 --- /dev/null +++ b/assignment-2/bin/1.asm @@ -0,0 +1,9 @@ + .data +a: + 10 +b: + 20 + .text +main: + load %x0, $a, %x4 + end diff --git a/assignment-2/bin/1.out b/assignment-2/bin/1.out new file mode 100644 index 0000000000000000000000000000000000000000..57736dd0bdcc8b87d9e2925568fb6536c128c5b0 GIT binary patch literal 20 YcmZQzU|?ckVBi8`kqsOS3@?BT01Ctb!2kdN literal 0 HcmV?d00001 diff --git a/assignment-2/bin/configuration/config.xml b/assignment-2/bin/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-2/bin/configuration/config.xml @@ -0,0 +1,43 @@ + + + + + 2 + 1 + 1 + + + 1 + 4 + 1 + + + 1 + 10 + 1 + + + + + 256 + 2 + 4 + LRU + + + + 256 + 2 + 4 + LRU + + + + 2048 + 10 + 4 + LRU + + + 40 + \ No newline at end of file diff --git a/assignment-2/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob b/assignment-2/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob new file mode 100644 index 0000000..2fec9c8 --- /dev/null +++ b/assignment-2/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob @@ -0,0 +1 @@ +{"name":"Local: Simulator","url":"c:\\Users\\karth\\OneDrive\\Desktop\\new\\src\\generic\\Simulator.java","tests":[{"id":1662603879409,"input":"","output":""},{"id":1662603899098,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\karth\\OneDrive\\Desktop\\new\\src\\generic\\Simulator.java","group":"local","local":true} \ No newline at end of file diff --git a/assignment-2/build.xml b/assignment-2/build.xml new file mode 100644 index 0000000..f8164cd --- /dev/null +++ b/assignment-2/build.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assignment-2/descending.out b/assignment-2/descending.out new file mode 100644 index 0000000000000000000000000000000000000000..d8b44b865474dc97c856ee54bbee4b8c16dcd6d8 GIT binary patch literal 92 zcmZQzVBln6U~mKC03g-?Vi6$b0%AEJHUeTBAm-S>#=x*ah=D;Wh=GxV11JDg!Rf=m hxN!ml!$vqcT|q!)W2Q(7#J9AH*(nW0{}yZ22lV2 delta 17 ZcmX>qcT|q!)W2Q(7#JArHgeeV0{}ye22ub3 diff --git a/assignment-2/supporting_files/src/descending.out b/assignment-2/supporting_files/src/descending.out new file mode 100644 index 0000000000000000000000000000000000000000..dc103f037d9928cd1882b4b85c7ab8c722d64a66 GIT binary patch literal 120 zcmZQzVBln6U~mKC03g-?Vi6$b0%AEJHUeTBAm-S>2E;-P98y6Hj2s+50iX&_9|p#a z6Brma`T^Nq42&n;7}#&||Nkvn!@$VDi-B<$Q0LBH3=9YC|9|7;0Llw7Fag=0UH}aQ E05jnhH~;_u literal 0 HcmV?d00001 diff --git a/assignment-2/supporting_files/src/even-odd.out b/assignment-2/supporting_files/src/even-odd.out new file mode 100644 index 0000000000000000000000000000000000000000..5b81735756e65df4b3d01e179e887fcf9a4715d6 GIT binary patch literal 36 lcmZQzU|?imVBp@s#=u~4jDhL=0S0Cc5eCK=KtTy84FFuj1#bWV literal 0 HcmV?d00001 diff --git a/assignment-2/supporting_files/src/fibonacci.out b/assignment-2/supporting_files/src/fibonacci.out new file mode 100644 index 0000000000000000000000000000000000000000..9acd5aea3e99f3f5e3f391ac0319fdf80bd15309 GIT binary patch literal 100 zcmZQzU|?imVBq2q`Tu`|7y|=`1Q1IBu?zzvw*&(tb4&xn?hpnBsVNMMr*1Itg2Z-D hf%7?ci9pyOaiBaa_bi}cG7JnLeFyCS|9k;78~|YT6kz}W literal 0 HcmV?d00001 diff --git a/assignment-2/supporting_files/src/generic/Instruction$OperationType.class b/assignment-2/supporting_files/src/generic/Instruction$OperationType.class index 071572bd5549bba56ab7460171052d23133a3332..574b78449ecc858b61fb53912bcb94733e8f197f 100644 GIT binary patch delta 17 ZcmaDO^hSu|)W2Q(7#J9AH*%zK0su!x2B-i4 delta 17 ZcmaDO^hSu|)W2Q(7#JArHgcqJ0su!$2B`o5 diff --git a/assignment-2/supporting_files/src/generic/Instruction.class b/assignment-2/supporting_files/src/generic/Instruction.class index 00fc28ea93b1758380b648db97e89d7f6da93eed..1980ed393feb3d50bf9645eecccd19f33c79b218 100644 GIT binary patch delta 17 ZcmbOuI!Bb_)W2Q(7#J9AH*!qj0suY&20s7* delta 17 ZcmbOuI!Bb_)W2Q(7#JArHgZhi0suY-20#D+ diff --git a/assignment-2/supporting_files/src/generic/Misc.class b/assignment-2/supporting_files/src/generic/Misc.class index bb3ec21a8cb70fc78810e059b7aa4d703352a358..a294df3c67639ac068817dfa966b36eab580b7fd 100644 GIT binary patch delta 17 ZcmZ3^yquZi)W2Q(7#J9AH*(Bp1OPo#1~UKv delta 17 ZcmZ3^yquZi)W2Q(7#JArHge2o1OPo)1~dQw diff --git a/assignment-2/supporting_files/src/generic/Operand$OperandType.class b/assignment-2/supporting_files/src/generic/Operand$OperandType.class index 92d3d37d9caa57e9a5199358b48acb423e4137f2..4138e107652eca250946d8bc6d2a27c1feef6b9c 100644 GIT binary patch delta 28 kcmdnOv4un6)W2Q(7#J9A8O+%km>3zPCoYuUsLja&0D}w%m;e9( delta 28 kcmdnOv4un6)W2Q(7#JAr7|hujm>3zPCoYuUsLja&0D~L{nE(I) diff --git a/assignment-2/supporting_files/src/generic/Operand.class b/assignment-2/supporting_files/src/generic/Operand.class index 376bd1a45997dddae590f0a2c08b361923137580..2e977a0170958aaac41d342819711fc37abcf582 100644 GIT binary patch delta 17 Zcmcb{bB%}N)W2Q(7#J9AH*)x~0RTg!23G(8 delta 17 Zcmcb{bB%}N)W2Q(7#JArHgfo}0RTg(23P<9 diff --git a/assignment-2/supporting_files/src/generic/ParsedProgram$1.class b/assignment-2/supporting_files/src/generic/ParsedProgram$1.class index b3efba65b31b2554bfe53b5475c0b62321a3bca0..7f19502cb15381e62e894a83eb46e27b7b1a96eb 100644 GIT binary patch delta 17 Zcmcc4f1RJ>)W2Q(7#J9AH*%b32LMGO2H^kz delta 17 Zcmcc4f1RJ>)W2Q(7#JArHgcS22LMGT2I2q! diff --git a/assignment-2/supporting_files/src/generic/ParsedProgram.class b/assignment-2/supporting_files/src/generic/ParsedProgram.class index 8103baa2b78b3cf19db090298e5da01b03758213..00603f8648a0aed89eba77230b2c39beaf3302fe 100644 GIT binary patch delta 17 Zcmdm}xlxnj)W2Q(7#J9AH*%~L0RTfT2Cx7C delta 17 Zcmdm}xlxnj)W2Q(7#JArHgc>K0RTfY2C)DD diff --git a/assignment-2/supporting_files/src/generic/Simulator$1.class b/assignment-2/supporting_files/src/generic/Simulator$1.class index da583e3c7d7abcea3e84b2becf0c0229180054b8..9910c77e263f87a19bdabf6252f6a4185d047820 100644 GIT binary patch delta 17 ZcmdnVy_1{c)W2Q(7#J9AH*#!b1pq*+27Lek delta 17 ZcmdnVy_1{c)W2Q(7#JArHgara1pq*>27Ukl diff --git a/assignment-2/supporting_files/src/generic/Simulator.class b/assignment-2/supporting_files/src/generic/Simulator.class index 6484c6916b8f9bea7e0c3a5b3d45347ce724429d..ff4352ee2cff80fba3ad2c8a2e6a5d61fa16afe9 100644 GIT binary patch delta 17 YcmZouYE$Ak^>5cc1_lP(jU4|306n4xod5s; delta 17 YcmZouYE$Ak^>5cc1_lPZjU4|306nJ$o&W#< diff --git a/assignment-2/supporting_files/src/generic/Simulator.java b/assignment-2/supporting_files/src/generic/Simulator.java index eb55911..7384637 100644 --- a/assignment-2/supporting_files/src/generic/Simulator.java +++ b/assignment-2/supporting_files/src/generic/Simulator.java @@ -1,89 +1,68 @@ package generic; import java.io.FileInputStream; +import generic.Operand.OperandType; +import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.BufferedOutputStream; -import java.nio.ByteBuffer; import java.io.IOException; -import java.util.Arrays; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.Stack; - +import java.util.Scanner; +import java.io.ByteArrayInputStream; +import java.nio.ByteBuffer; public class Simulator { - + static FileInputStream inputcodeStream = null; - public static Map mapping = new HashMap(); - static { - mapping.put(Instruction.OperationType.add, "00000"); - mapping.put(Instruction.OperationType.addi, "00001"); - mapping.put(Instruction.OperationType.sub, "00010"); - mapping.put(Instruction.OperationType.subi, "00011"); - mapping.put(Instruction.OperationType.mul, "00100"); - mapping.put(Instruction.OperationType.muli, "00101"); - mapping.put(Instruction.OperationType.div, "00110"); - mapping.put(Instruction.OperationType.divi, "00111"); - mapping.put(Instruction.OperationType.and, "01000"); - mapping.put(Instruction.OperationType.andi, "01001"); - mapping.put(Instruction.OperationType.or, "01010"); - mapping.put(Instruction.OperationType.ori, "01011"); - mapping.put(Instruction.OperationType.xor, "01100"); - mapping.put(Instruction.OperationType.xori, "01101"); - mapping.put(Instruction.OperationType.slt, "01110"); - mapping.put(Instruction.OperationType.slti, "01111"); - mapping.put(Instruction.OperationType.sll, "10000"); - mapping.put(Instruction.OperationType.slli, "10001"); - mapping.put(Instruction.OperationType.srl, "10010"); - mapping.put(Instruction.OperationType.srli, "10011"); - mapping.put(Instruction.OperationType.sra, "10100"); - mapping.put(Instruction.OperationType.srai, "10101"); - mapping.put(Instruction.OperationType.load, "10110"); - mapping.put(Instruction.OperationType.end, "11101"); - mapping.put(Instruction.OperationType.beq, "11001"); - mapping.put(Instruction.OperationType.jmp, "11000"); - mapping.put(Instruction.OperationType.bne, "11010"); - mapping.put(Instruction.OperationType.blt, "11011"); - mapping.put(Instruction.OperationType.bgt, "11100"); - } + public static Map map = new HashMap<>() { + { + put("add", "00000"); + put("sub", "00010"); + put("mul", "00100"); + put("div", "00110"); + put("and", "01000"); + put("or", "01010"); + put("xor", "01100"); + put("slt", "01110"); + put("sll", "10000"); + put("srl", "10010"); + put("sra", "10100"); + put("beq", "11001"); + put("bne", "11010"); + put("blt", "11011"); + put("bgt", "11100"); + put("load", "10110"); + put("store", "10111"); + put("jmp", "11000"); + put("addi", "00001"); + put("subi", "00011"); + put("muli", "00101"); + put("divi", "00111"); + put("andi", "01001"); + put("ori", "01011"); + put("xori", "01101"); + put("slti", "01111"); + put("slli", "10001"); + put("srli", "10011"); + put("srai", "10101"); + put("end", "11101"); - public static void setupSimulation(String assemblyProgramFile) { - int firstCodeAddress = ParsedProgram.parseDataSection(assemblyProgramFile); - ParsedProgram.parseCodeSection(assemblyProgramFile, firstCodeAddress); - ParsedProgram.printState(); - } - + } + }; private static String toBinaryOfSpecificPrecision(int num, int lenOfTargetString) { String binary = String.format("%" + lenOfTargetString + "s", Integer.toBinaryString(num)).replace(' ', '0'); return binary; } - - private static int toSignedInteger(String binary) { - int n = 32 - binary.length(); - char[] sign_ext = new char[n]; - Arrays.fill(sign_ext, binary.charAt(0)); - int signedInteger = (int) Long.parseLong(new String(sign_ext) + binary, 2); - return signedInteger; + private static String convert_1(Operand inst, int precision) { + + return toBinaryOfSpecificPrecision(inst.getValue(), precision); + } - - private static String toBinaryString(int n) { - // Remove this conditional statement - // if (n >= 0) return String.valueOf(n); - - Stack bits = new Stack<>(); - do { - bits.push(n % 2); - n /= 2; - } while (n != 0); - - StringBuilder builder = new StringBuilder(); - while (!bits.isEmpty()) { - builder.append(bits.pop()); - } - return " " + builder.toString(); - } - private static String convert(Operand inst, int precision) { if (inst == null) return toBinaryOfSpecificPrecision(0, precision); @@ -91,106 +70,84 @@ public class Simulator { if (inst.getOperandType() == Operand.OperandType.Label) return toBinaryOfSpecificPrecision(ParsedProgram.symtab.get(inst.getLabelValue()), precision); - // write logic for converting to binary/ hex return toBinaryOfSpecificPrecision(inst.getValue(), precision); - // check if inst is a label, in that case, use its value - // return String.valueOf(inst.getValue()); + } - - public static void assemble(String objectProgramFile) { + public static void setupSimulation(String assemblyProgramFile) + { + int firstCodeAddress = ParsedProgram.parseDataSection(assemblyProgramFile); + ParsedProgram.parseCodeSection(assemblyProgramFile, firstCodeAddress); + ParsedProgram.printState(); + } + + public static void assemble(String objectProgramFile) + { + FileOutputStream file; try { - //1. open the objectProgramFile in binary mode file = new FileOutputStream(objectProgramFile); - BufferedOutputStream bfile = new BufferedOutputStream(file); - - //2. write the firstCodeAddress to the file - byte[] addressCode = ByteBuffer.allocate(4).putInt(ParsedProgram.firstCodeAddress).array(); - bfile.write(addressCode); - - //3. write the data to the file - for (int value: ParsedProgram.data) { + //write the firstCodeAddress to the file + file.write(ByteBuffer.allocate(4).putInt(ParsedProgram.firstCodeAddress).array()); + //write the data to the file + for (var value : ParsedProgram.data) { byte[] dataValue = ByteBuffer.allocate(4).putInt(value).array(); - bfile.write(dataValue); + file.write(dataValue); } - - //4. assemble one instruction at a time, and write to the file - for (Instruction inst: ParsedProgram.code) { - /** - * inst.getSourceOperand().getValue() will be passed to a function as f() - * that will change decimal to binary and then will return the string - * form of the binary. It will also check if the value is a label, - * in case it is a label, it would call ParsedProgram.symtab.get() - * to get the address corresponding to the label - */ - String binaryRep = ""; - - // print operation type, use toBinaryString() instead of convert() - // file.write(mapping.get(inst.getOperationType())); - binaryRep += mapping.get(inst.getOperationType()); - int opCode = Integer.parseInt(binaryRep, 2); - // System.out.println(inst.getOperationType() + " " + mapping.get(inst.getOperationType())); - // System.out.println(mapping); - // System.out.println(inst.getProgramCounter()); - int pc = inst.getProgramCounter(); + //assemble one instruction at a time, and write to the file + for (var instruction : ParsedProgram.code) { + String line = ""; + String optype=(instruction.operationType).toString(); + line += map.get(optype); + // int op = Integer.parseInt(line, 2); + int op=Integer.parseInt(map.get(optype),2); + int pc = instruction.getProgramCounter(); - if (opCode <= 20 && opCode % 2 == 0) { + if (op <= 20 && op % 2 == 0) { // R3 Type - binaryRep += convert(inst.getSourceOperand1(), 5); - binaryRep += convert(inst.getSourceOperand2(), 5); - binaryRep += convert(inst.getDestinationOperand(), 5); - binaryRep += toBinaryOfSpecificPrecision(0, 12); + line += convert_1(instruction.getSourceOperand1(), 5); + line += convert_1(instruction.getSourceOperand2(), 5); + line += convert_1(instruction.getDestinationOperand(), 5); + line += toBinaryOfSpecificPrecision(0, 12); + } - else if (opCode == 24) { + else if (op == 29) { + line += toBinaryOfSpecificPrecision(0, 27); + } + else if (op == 24) { // RI Type - if (inst.destinationOperand.getOperandType() == Operand.OperandType.Register) { - binaryRep += convert(inst.getDestinationOperand(), 5); - binaryRep += toBinaryOfSpecificPrecision(0, 22); - } - else { - binaryRep += toBinaryOfSpecificPrecision(0, 5); - int value = Integer.parseInt(convert(inst.getDestinationOperand(), 5), 2) - pc; + if (instruction.destinationOperand.getOperandType() == Operand.OperandType.Register) { + line += convert(instruction.getDestinationOperand(), 5); + line += toBinaryOfSpecificPrecision(0, 22); + } else { + line += toBinaryOfSpecificPrecision(0, 5); + int value = Integer.parseInt(convert(instruction.getDestinationOperand(), 5), 2) - pc; String bin = toBinaryOfSpecificPrecision(value, 22); - binaryRep += bin.substring(bin.length() - 22); + line += bin.substring(bin.length() - 22); } } - else if (opCode == 29) { - binaryRep += toBinaryOfSpecificPrecision(0, 27); - } else { // R2I Type - if (opCode >= 25 && opCode <= 28) { - int value = Integer.parseInt(convert(inst.getDestinationOperand(), 5), 2) - pc; - binaryRep += convert(inst.getSourceOperand1(), 5); - binaryRep += convert(inst.getSourceOperand2(), 5); + if (op >= 25 && op <= 28) { + int value = Integer.parseInt(convert(instruction.getDestinationOperand(), 5), 2) - pc; + line += convert(instruction.getSourceOperand1(), 5); + line += convert(instruction.getSourceOperand2(), 5); String bin = toBinaryOfSpecificPrecision(value, 17); - binaryRep += bin.substring(bin.length() - 17); - } - else { - binaryRep += convert(inst.getSourceOperand1(), 5); - binaryRep += convert(inst.getDestinationOperand(), 5); - binaryRep += convert(inst.getSourceOperand2(), 17); + line += bin.substring(bin.length() - 17); + } else { + line += convert(instruction.getSourceOperand1(), 5); + line += convert(instruction.getDestinationOperand(), 5); + line += convert(instruction.getSourceOperand2(), 17); } } - int instInteger = (int) Long.parseLong(binaryRep, 2); + int instInteger = (int) Long.parseLong(line, 2); byte[] instBinary = ByteBuffer.allocate(4).putInt(instInteger).array(); - bfile.write(instBinary); - - // System.out.println(instInteger); - // if (inst.getSourceOperand1() != null) - // file.write(convert(inst.getSourceOperand1())); - // if (inst.getSourceOperand2() != null) - // file.write(convert(inst.getSourceOperand2())); - // if (inst.getDestinationOperand() != null) - // file.write(convert(inst.getDestinationOperand())); - // file.write(inst.toString()); + file.write(instBinary); } - - //5. close the file - bfile.close(); + file.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); - } + } } } - diff --git a/assignment-2/supporting_files/src/generic/Statistics.class b/assignment-2/supporting_files/src/generic/Statistics.class index 863e86baa031c969c16efb310ff7fe0ebcfb60b4..77dbab089559fe2df867fc3cdb460bdc33010c81 100644 GIT binary patch delta 17 ZcmbQuG@FUz)W2Q(7#J9AH*yFw0suOl1&sgz delta 17 ZcmbQuG@FUz)W2Q(7#JArHgX6v0suOq1&#m! diff --git a/assignment-2/supporting_files/src/main/Main.class b/assignment-2/supporting_files/src/main/Main.class index 80b924a4617a7d9e537a85a5114185369c090c96..0511d992fd86483c195a5718765da277e9b747a2 100644 GIT binary patch delta 17 ZcmZ3>vX+J8)W2Q(7#J9AH*%;l0RTNd1;_vZ delta 17 ZcmZ3>vX+J8)W2Q(7#JArHgc#k0RTNi1<3#a diff --git a/assignment-2/supporting_files/src/palindrome.out b/assignment-2/supporting_files/src/palindrome.out new file mode 100644 index 0000000000000000000000000000000000000000..f3a5c6ecf218fac71c0e3cc7e62c368e36a9f326 GIT binary patch literal 92 zcmZQzU|?im+Lpb6je&vV8W2AMVmSsz4kZQ#i(?F2+V>c^P8?uhI$;0*uQiZub&Y{b h{hk2>i@N{=h<^$q_xnr|1GCf=2F4c*3=CXQ8UPLf7+3%R literal 0 HcmV?d00001 diff --git a/assignment-2/supporting_files/src/prime.out b/assignment-2/supporting_files/src/prime.out new file mode 100644 index 0000000000000000000000000000000000000000..641fca5885c51df73c40ca2e43cbddd5fb2bfc36 GIT binary patch literal 60 zcmZQzU|?imU=ZP8V_@1K#=vmv2m|M-BMfY(jxn$r9Xr5q{s03j$1w)R8z=w& x == opcode)) { - if(opcode != 24){ + if(opcode != 24){ // end Operand rd = new Operand(); rd.setOperandType(Operand.OperandType.Register); rd.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2)); instr.setDestinationOperand(rd); - - int imm = Integer.parseInt(bin_instr.substring(10, 32), 2); - if (bin_instr.charAt(10)=='1'){ - imm = -1*twoscompliment(bin_instr.substring(10, 32)); - System.out.println(bin_instr); - } - // if (checkdatahazard(new int[] { rd.getValue() })) { - // noDataHazard = false; - // }else{ - // containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter()-1); OF_EX_Latch.setInstruction(instr); - // OF_EX_Latch.setImm(imm); - // isEnd = true; - // } } else{ // opcode == 24 jmp Operand op = new Operand(); @@ -205,6 +193,7 @@ public class OperandFetch { if(!noDataHazard){ IF_EnableLatch.setFreeze(true); System.out.println("\n\nData Hazard - Interlock\n\n"); + Statistics.setDatahazards(Statistics.getDatahazards() + 1); } } else if (!Proceed) { diff --git a/assignment-4/supporting_files/descending.out b/assignment-4/supporting_files/descending.out new file mode 100644 index 0000000000000000000000000000000000000000..dc103f037d9928cd1882b4b85c7ab8c722d64a66 GIT binary patch literal 120 zcmZQzVBln6U~mKC03g-?Vi6$b0%AEJHUeTBAm-S>2E;-P98y6Hj2s+50iX&_9|p#a z6Brma`T^Nq42&n;7}#&||Nkvn!@$VDi-B<$Q0LBH3=9YC|9|7;0Llw7Fag=0UH}aQ E05jnhH~;_u literal 0 HcmV?d00001 diff --git a/assignment-4/supporting_files/even-odd.out b/assignment-4/supporting_files/even-odd.out new file mode 100644 index 0000000000000000000000000000000000000000..5b81735756e65df4b3d01e179e887fcf9a4715d6 GIT binary patch literal 36 lcmZQzU|?imVBp@s#=u~4jDhL=0S0Cc5eCK=KtTy84FFuj1#bWV literal 0 HcmV?d00001 diff --git a/assignment-4/supporting_files/fibonacci.out b/assignment-4/supporting_files/fibonacci.out new file mode 100644 index 0000000000000000000000000000000000000000..9acd5aea3e99f3f5e3f391ac0319fdf80bd15309 GIT binary patch literal 100 zcmZQzU|?imVBq2q`Tu`|7y|=`1Q1IBu?zzvw*&(tb4&xn?hpnBsVNMMr*1Itg2Z-D hf%7?ci9pyOaiBaa_bi}cG7JnLeFyCS|9k;78~|YT6kz}W literal 0 HcmV?d00001 diff --git a/assignment-4/supporting_files/palindrome.out b/assignment-4/supporting_files/palindrome.out new file mode 100644 index 0000000000000000000000000000000000000000..f3a5c6ecf218fac71c0e3cc7e62c368e36a9f326 GIT binary patch literal 92 zcmZQzU|?im+Lpb6je&vV8W2AMVmSsz4kZQ#i(?F2+V>c^P8?uhI$;0*uQiZub&Y{b h{hk2>i@N{=h<^$q_xnr|1GCf=2F4c*3=CXQ8UPLf7+3%R literal 0 HcmV?d00001 diff --git a/assignment-4/supporting_files/prime.out b/assignment-4/supporting_files/prime.out new file mode 100644 index 0000000000000000000000000000000000000000..641fca5885c51df73c40ca2e43cbddd5fb2bfc36 GIT binary patch literal 60 zcmZQzU|?imU=ZP8V_@1K#=vmv2m|M-BMfY(jxn$r9Xr5q{s03j$1w)R8z=w&