diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f66c74 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.zip \ No newline at end of file diff --git a/assignment-1/assignment1-1.pdf b/assignment-1/assignment1-1.pdf new file mode 100644 index 0000000..8d64f81 Binary files /dev/null and b/assignment-1/assignment1-1.pdf differ diff --git a/assignment-1/src/descending.asm b/assignment-1/src/descending.asm new file mode 100644 index 0000000..0445108 --- /dev/null +++ b/assignment-1/src/descending.asm @@ -0,0 +1,40 @@ + .data +a: + 70 + 80 + 40 + 20 + 10 + 30 + 50 + 60 +n: + 8 + .text +main: + load %x0, $n, %x3 + load %x0, $n, %x9 +init: + subi %x9, 1, %x9 + addi %x0, 0, %x4 +loopi: + addi %x0, 0, %x5 +loopj: + addi %x5, 1, %x6 + load %x6, $a, %x8 + load %x5, $a, %x7 + addi %x5, 1, %x5 +check: + beq %x5, %x3, loopout + blt %x8, %x7, loopj + subi %x5, 1, %x30 + addi %x30, 1, %x29 + store %x8, $a, %x30 + store %x7, $a, %x29 + jmp loopj +loopout: + addi %x4, 1, %x4 + beq %x4, %x9, escape + jmp loopi +escape: + end \ No newline at end of file diff --git a/assignment-1/src/even-odd.asm b/assignment-1/src/even-odd.asm new file mode 100644 index 0000000..08e7a4e --- /dev/null +++ b/assignment-1/src/even-odd.asm @@ -0,0 +1,13 @@ + .data +a: + 11 + .text +main: + load 0, $a, %x3 + divi %x3, 2, %x3 + beq %x31, 0, even + addi %x0, 1, %x10 + end +even: + subi %x0, 1, %x10 + end \ No newline at end of file diff --git a/assignment-1/src/fibonacci.asm b/assignment-1/src/fibonacci.asm new file mode 100644 index 0000000..797756a --- /dev/null +++ b/assignment-1/src/fibonacci.asm @@ -0,0 +1,30 @@ + .data +n: + 10 + .text +main: + addi %x0, 65535, %x10 + load %x0, $n, %x11 + addi %x0, 0, %x12 + addi %x0, 0, %x13 + addi %x0, 1, %x14 + addi %x12, 1, %x12 + add %x13, %x14, %x8 + store %x13, $n, %x10 + subi %x10, 1, %x10 + beq %x11, %x12, endl + addi %x12, 1, %x12 + store %x14, $n, %x10 + subi %x10, 1, %x10 + beq %x11, %x12, endl +forloop: + store %x8, $n, %x10 + subi %x10, 1, %x10 + addi %x12, 1, %x12 + beq %x11, %x12, endl + addi %x14, 0, %x13 + addi %x8, 0, %x14 + add %x13, %x14, %x8 + jmp forloop +endl: + end \ No newline at end of file diff --git a/assignment-1/src/palindrome.asm b/assignment-1/src/palindrome.asm new file mode 100644 index 0000000..deb75a6 --- /dev/null +++ b/assignment-1/src/palindrome.asm @@ -0,0 +1,32 @@ + .data +a: + 177771 + .text +main: + load %x0, $a, %x3 +init: + addi %x3, 0, %x11 + addi %x3, 0, %x17 + addi %x0, 1, %x15 + addi %x0, 0, %x17 +forloop: + divi %x3, 10, %x3 + muli %x15, 10, %x15 + beq %x3, 0, end1 + jmp forloop +end1: + divi %x15, 10, %x15 +forloop1: + divi %x11, 10, %x11 + mul %x31, %x15, %x3 + add %x17, %x3, %x17 + divi %x15, 10, %x15 + beq %x11, 0, end2 + jmp forloop1 +end2: + beq %x17, %x17, write1 + subi %x10, 1, %x10 + end +write1: + addi %x10, 1, %x10 + end \ No newline at end of file diff --git a/assignment-1/src/prime.asm b/assignment-1/src/prime.asm new file mode 100644 index 0000000..d95d6d4 --- /dev/null +++ b/assignment-1/src/prime.asm @@ -0,0 +1,23 @@ +.data +a: + 20 + .text +main: + addi %x0, 2, $x3 + load %x0, $a, %x11 + blt %x11, 2, notAnswer +camparision: + beq %x11, 2, Answer + beq %x11, 3, Answer +round: + div %x11, %x3, %x12 +check: + beq %x31, 0, notAnswer + addi %x3, 1, %x3 + blt %x3, %x4, round +Answer: + addi %x0, 1, %x10 + end +notAnswer: + subi %x0, 1, %x10 + end \ No newline at end of file diff --git a/assignment-2/supporting_files/bin/configuration/Configuration.class b/assignment-2/supporting_files/bin/configuration/Configuration.class new file mode 100644 index 0000000..9ddf3a1 Binary files /dev/null and b/assignment-2/supporting_files/bin/configuration/Configuration.class differ diff --git a/assignment-2/supporting_files/bin/configuration/config.xml b/assignment-2/supporting_files/bin/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob b/assignment-2/supporting_files/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob new file mode 100644 index 0000000..2fec9c8 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/bin/generic/Instruction$OperationType.class b/assignment-2/supporting_files/bin/generic/Instruction$OperationType.class new file mode 100644 index 0000000..f719b5b Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Instruction$OperationType.class differ diff --git a/assignment-2/supporting_files/bin/generic/Instruction.class b/assignment-2/supporting_files/bin/generic/Instruction.class new file mode 100644 index 0000000..3b6d90b Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Instruction.class differ diff --git a/assignment-2/supporting_files/bin/generic/Misc.class b/assignment-2/supporting_files/bin/generic/Misc.class new file mode 100644 index 0000000..f0604ac Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Misc.class differ diff --git a/assignment-2/supporting_files/bin/generic/Operand$OperandType.class b/assignment-2/supporting_files/bin/generic/Operand$OperandType.class new file mode 100644 index 0000000..b65e5f6 Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Operand$OperandType.class differ diff --git a/assignment-2/supporting_files/bin/generic/Operand.class b/assignment-2/supporting_files/bin/generic/Operand.class new file mode 100644 index 0000000..47fb03a Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Operand.class differ diff --git a/assignment-2/supporting_files/bin/generic/ParsedProgram.class b/assignment-2/supporting_files/bin/generic/ParsedProgram.class new file mode 100644 index 0000000..5335683 Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/ParsedProgram.class differ diff --git a/assignment-2/supporting_files/bin/generic/Simulator.class b/assignment-2/supporting_files/bin/generic/Simulator.class new file mode 100644 index 0000000..af46ed6 Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Simulator.class differ diff --git a/assignment-2/supporting_files/bin/generic/Statistics.class b/assignment-2/supporting_files/bin/generic/Statistics.class new file mode 100644 index 0000000..5ae964d Binary files /dev/null and b/assignment-2/supporting_files/bin/generic/Statistics.class differ diff --git a/assignment-2/supporting_files/bin/main/Main.class b/assignment-2/supporting_files/bin/main/Main.class new file mode 100644 index 0000000..1d66845 Binary files /dev/null and b/assignment-2/supporting_files/bin/main/Main.class differ diff --git a/assignment-2/supporting_files/build.xml b/assignment-2/supporting_files/build.xml new file mode 100644 index 0000000..f8164cd --- /dev/null +++ b/assignment-2/supporting_files/build.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assignment-2/supporting_files/src/configuration/Configuration.class b/assignment-2/supporting_files/src/configuration/Configuration.class new file mode 100644 index 0000000..469528b Binary files /dev/null and b/assignment-2/supporting_files/src/configuration/Configuration.class differ diff --git a/assignment-2/supporting_files/src/configuration/Configuration.java b/assignment-2/supporting_files/src/configuration/Configuration.java new file mode 100644 index 0000000..d7b43e1 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/configuration/config.xml b/assignment-2/supporting_files/src/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob b/assignment-2/supporting_files/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob new file mode 100644 index 0000000..2fec9c8 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/generic/Instruction$OperationType.class b/assignment-2/supporting_files/src/generic/Instruction$OperationType.class new file mode 100644 index 0000000..071572b Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Instruction$OperationType.class differ diff --git a/assignment-2/supporting_files/src/generic/Instruction.class b/assignment-2/supporting_files/src/generic/Instruction.class new file mode 100644 index 0000000..00fc28e Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Instruction.class differ diff --git a/assignment-2/supporting_files/src/generic/Instruction.java b/assignment-2/supporting_files/src/generic/Instruction.java new file mode 100644 index 0000000..ec3ef99 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/generic/Misc.class b/assignment-2/supporting_files/src/generic/Misc.class new file mode 100644 index 0000000..bb3ec21 Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Misc.class differ diff --git a/assignment-2/supporting_files/src/generic/Misc.java b/assignment-2/supporting_files/src/generic/Misc.java new file mode 100644 index 0000000..0be690a --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/generic/Operand$OperandType.class b/assignment-2/supporting_files/src/generic/Operand$OperandType.class new file mode 100644 index 0000000..92d3d37 Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Operand$OperandType.class differ diff --git a/assignment-2/supporting_files/src/generic/Operand.class b/assignment-2/supporting_files/src/generic/Operand.class new file mode 100644 index 0000000..376bd1a Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Operand.class differ diff --git a/assignment-2/supporting_files/src/generic/Operand.java b/assignment-2/supporting_files/src/generic/Operand.java new file mode 100644 index 0000000..3224099 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/generic/ParsedProgram$1.class b/assignment-2/supporting_files/src/generic/ParsedProgram$1.class new file mode 100644 index 0000000..b3efba6 Binary files /dev/null and b/assignment-2/supporting_files/src/generic/ParsedProgram$1.class differ diff --git a/assignment-2/supporting_files/src/generic/ParsedProgram.class b/assignment-2/supporting_files/src/generic/ParsedProgram.class new file mode 100644 index 0000000..8103baa Binary files /dev/null and b/assignment-2/supporting_files/src/generic/ParsedProgram.class differ diff --git a/assignment-2/supporting_files/src/generic/ParsedProgram.java b/assignment-2/supporting_files/src/generic/ParsedProgram.java new file mode 100644 index 0000000..7cc69f0 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/generic/Simulator$1.class b/assignment-2/supporting_files/src/generic/Simulator$1.class new file mode 100644 index 0000000..da583e3 Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Simulator$1.class differ diff --git a/assignment-2/supporting_files/src/generic/Simulator.class b/assignment-2/supporting_files/src/generic/Simulator.class new file mode 100644 index 0000000..6484c69 Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Simulator.class differ diff --git a/assignment-2/supporting_files/src/generic/Simulator.java b/assignment-2/supporting_files/src/generic/Simulator.java new file mode 100644 index 0000000..eb55911 --- /dev/null +++ b/assignment-2/supporting_files/src/generic/Simulator.java @@ -0,0 +1,196 @@ +package generic; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.BufferedOutputStream; +import java.nio.ByteBuffer; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + + +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 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 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); + + 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) { + 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) { + byte[] dataValue = ByteBuffer.allocate(4).putInt(value).array(); + bfile.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(); + + if (opCode <= 20 && opCode % 2 == 0) { + // R3 Type + binaryRep += convert(inst.getSourceOperand1(), 5); + binaryRep += convert(inst.getSourceOperand2(), 5); + binaryRep += convert(inst.getDestinationOperand(), 5); + binaryRep += toBinaryOfSpecificPrecision(0, 12); + } + else if (opCode == 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; + String bin = toBinaryOfSpecificPrecision(value, 22); + binaryRep += 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); + 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); + } + } + int instInteger = (int) Long.parseLong(binaryRep, 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()); + } + + //5. close the file + bfile.close(); + } 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 new file mode 100644 index 0000000..863e86b Binary files /dev/null and b/assignment-2/supporting_files/src/generic/Statistics.class differ diff --git a/assignment-2/supporting_files/src/generic/Statistics.java b/assignment-2/supporting_files/src/generic/Statistics.java new file mode 100644 index 0000000..059cb7a --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/src/main/Main.class b/assignment-2/supporting_files/src/main/Main.class new file mode 100644 index 0000000..80b924a Binary files /dev/null and b/assignment-2/supporting_files/src/main/Main.class differ diff --git a/assignment-2/supporting_files/src/main/Main.java b/assignment-2/supporting_files/src/main/Main.java new file mode 100644 index 0000000..fcb74c3 --- /dev/null +++ b/assignment-2/supporting_files/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/supporting_files/submissions/1.observedoutput b/assignment-2/supporting_files/submissions/1.observedoutput new file mode 100644 index 0000000..57736dd Binary files /dev/null and b/assignment-2/supporting_files/submissions/1.observedoutput differ diff --git a/assignment-2/supporting_files/submissions/2.observedoutput b/assignment-2/supporting_files/submissions/2.observedoutput new file mode 100644 index 0000000..d4fbf39 Binary files /dev/null and b/assignment-2/supporting_files/submissions/2.observedoutput differ diff --git a/assignment-2/supporting_files/submissions/3.observedoutput b/assignment-2/supporting_files/submissions/3.observedoutput new file mode 100644 index 0000000..b157782 Binary files /dev/null and b/assignment-2/supporting_files/submissions/3.observedoutput differ diff --git a/assignment-2/supporting_files/submissions/bin/1.asm b/assignment-2/supporting_files/submissions/bin/1.asm new file mode 100644 index 0000000..613ab49 --- /dev/null +++ b/assignment-2/supporting_files/submissions/bin/1.asm @@ -0,0 +1,9 @@ + .data +a: + 10 +b: + 20 + .text +main: + load %x0, $a, %x4 + end diff --git a/assignment-2/supporting_files/submissions/bin/1.out b/assignment-2/supporting_files/submissions/bin/1.out new file mode 100644 index 0000000..57736dd Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/1.out differ diff --git a/assignment-2/supporting_files/submissions/bin/configuration/Configuration.class b/assignment-2/supporting_files/submissions/bin/configuration/Configuration.class new file mode 100644 index 0000000..469528b Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/configuration/Configuration.class differ diff --git a/assignment-2/supporting_files/submissions/bin/configuration/config.xml b/assignment-2/supporting_files/submissions/bin/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob b/assignment-2/supporting_files/submissions/bin/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob new file mode 100644 index 0000000..2fec9c8 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/bin/generic/Instruction$OperationType.class b/assignment-2/supporting_files/submissions/bin/generic/Instruction$OperationType.class new file mode 100644 index 0000000..071572b Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Instruction$OperationType.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Instruction.class b/assignment-2/supporting_files/submissions/bin/generic/Instruction.class new file mode 100644 index 0000000..00fc28e Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Instruction.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Misc.class b/assignment-2/supporting_files/submissions/bin/generic/Misc.class new file mode 100644 index 0000000..bb3ec21 Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Misc.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Operand$OperandType.class b/assignment-2/supporting_files/submissions/bin/generic/Operand$OperandType.class new file mode 100644 index 0000000..92d3d37 Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Operand$OperandType.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Operand.class b/assignment-2/supporting_files/submissions/bin/generic/Operand.class new file mode 100644 index 0000000..376bd1a Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Operand.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/ParsedProgram$1.class b/assignment-2/supporting_files/submissions/bin/generic/ParsedProgram$1.class new file mode 100644 index 0000000..b3efba6 Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/ParsedProgram$1.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/ParsedProgram.class b/assignment-2/supporting_files/submissions/bin/generic/ParsedProgram.class new file mode 100644 index 0000000..8103baa Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/ParsedProgram.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Simulator$1.class b/assignment-2/supporting_files/submissions/bin/generic/Simulator$1.class new file mode 100644 index 0000000..da583e3 Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Simulator$1.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Simulator.class b/assignment-2/supporting_files/submissions/bin/generic/Simulator.class new file mode 100644 index 0000000..6484c69 Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Simulator.class differ diff --git a/assignment-2/supporting_files/submissions/bin/generic/Statistics.class b/assignment-2/supporting_files/submissions/bin/generic/Statistics.class new file mode 100644 index 0000000..863e86b Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/generic/Statistics.class differ diff --git a/assignment-2/supporting_files/submissions/bin/main/Main.class b/assignment-2/supporting_files/submissions/bin/main/Main.class new file mode 100644 index 0000000..80b924a Binary files /dev/null and b/assignment-2/supporting_files/submissions/bin/main/Main.class differ diff --git a/assignment-2/supporting_files/submissions/build.xml b/assignment-2/supporting_files/submissions/build.xml new file mode 100644 index 0000000..f8164cd --- /dev/null +++ b/assignment-2/supporting_files/submissions/build.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assignment-2/supporting_files/submissions/jars/assembler.jar b/assignment-2/supporting_files/submissions/jars/assembler.jar new file mode 100644 index 0000000..7e5ec04 Binary files /dev/null and b/assignment-2/supporting_files/submissions/jars/assembler.jar differ diff --git a/assignment-2/supporting_files/submissions/src/1.asm b/assignment-2/supporting_files/submissions/src/1.asm new file mode 100644 index 0000000..613ab49 --- /dev/null +++ b/assignment-2/supporting_files/submissions/src/1.asm @@ -0,0 +1,9 @@ + .data +a: + 10 +b: + 20 + .text +main: + load %x0, $a, %x4 + end diff --git a/assignment-2/supporting_files/submissions/src/1.out b/assignment-2/supporting_files/submissions/src/1.out new file mode 100644 index 0000000..57736dd Binary files /dev/null and b/assignment-2/supporting_files/submissions/src/1.out differ diff --git a/assignment-2/supporting_files/submissions/src/configuration/.gitignore b/assignment-2/supporting_files/submissions/src/configuration/.gitignore new file mode 100644 index 0000000..0bc28e3 --- /dev/null +++ b/assignment-2/supporting_files/submissions/src/configuration/.gitignore @@ -0,0 +1 @@ +/Configuration.class diff --git a/assignment-2/supporting_files/submissions/src/configuration/Configuration.java b/assignment-2/supporting_files/submissions/src/configuration/Configuration.java new file mode 100644 index 0000000..d7b43e1 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/configuration/config.xml b/assignment-2/supporting_files/submissions/src/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob b/assignment-2/supporting_files/submissions/src/generic/.cph/.Simulator.java_74fc5dae77a3b9a48197eb148ed1e37a.prob new file mode 100644 index 0000000..2fec9c8 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/.gitignore b/assignment-2/supporting_files/submissions/src/generic/.gitignore new file mode 100644 index 0000000..d78f9a2 --- /dev/null +++ b/assignment-2/supporting_files/submissions/src/generic/.gitignore @@ -0,0 +1,8 @@ +/Instruction$OperationType.class +/Instruction.class +/Misc.class +/Operand$OperandType.class +/Operand.class +/ParsedProgram.class +/Simulator.class +/Statistics.class diff --git a/assignment-2/supporting_files/submissions/src/generic/Instruction.java b/assignment-2/supporting_files/submissions/src/generic/Instruction.java new file mode 100644 index 0000000..ec3ef99 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/Misc.java b/assignment-2/supporting_files/submissions/src/generic/Misc.java new file mode 100644 index 0000000..0be690a --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/Operand.java b/assignment-2/supporting_files/submissions/src/generic/Operand.java new file mode 100644 index 0000000..3224099 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/ParsedProgram$1.class b/assignment-2/supporting_files/submissions/src/generic/ParsedProgram$1.class new file mode 100644 index 0000000..b3efba6 Binary files /dev/null and b/assignment-2/supporting_files/submissions/src/generic/ParsedProgram$1.class differ diff --git a/assignment-2/supporting_files/submissions/src/generic/ParsedProgram.java b/assignment-2/supporting_files/submissions/src/generic/ParsedProgram.java new file mode 100644 index 0000000..7cc69f0 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/Simulator$1.class b/assignment-2/supporting_files/submissions/src/generic/Simulator$1.class new file mode 100644 index 0000000..da583e3 Binary files /dev/null and b/assignment-2/supporting_files/submissions/src/generic/Simulator$1.class differ diff --git a/assignment-2/supporting_files/submissions/src/generic/Simulator.java b/assignment-2/supporting_files/submissions/src/generic/Simulator.java new file mode 100644 index 0000000..7384637 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/generic/Statistics.java b/assignment-2/supporting_files/submissions/src/generic/Statistics.java new file mode 100644 index 0000000..059cb7a --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/src/main/.gitignore b/assignment-2/supporting_files/submissions/src/main/.gitignore new file mode 100644 index 0000000..a66c73a --- /dev/null +++ b/assignment-2/supporting_files/submissions/src/main/.gitignore @@ -0,0 +1 @@ +/Main.class diff --git a/assignment-2/supporting_files/submissions/src/main/Main.java b/assignment-2/supporting_files/submissions/src/main/Main.java new file mode 100644 index 0000000..fcb74c3 --- /dev/null +++ b/assignment-2/supporting_files/submissions/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/supporting_files/submissions/tmp.output b/assignment-2/supporting_files/submissions/tmp.output new file mode 100644 index 0000000..df70aab --- /dev/null +++ b/assignment-2/supporting_files/submissions/tmp.output @@ -0,0 +1,398 @@ +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 14 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 14 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 12 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Symbol Table : +[{a=0, b=1, main=2}] + +Parsed instructions : +[[PC=2 load [Register:0] [Label:a] [Register:4] +, PC=3 end null null null +]] +Symbol Table : +[{prime=11, a=0, for=6, main=1, notprime=14}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:4] [Register:4] [Register:4] +, PC=3 divi [Register:3] [Immediate:2] [Register:4] +, PC=4 sub [Register:6] [Register:6] [Register:6] +, PC=5 addi [Register:6] [Immediate:2] [Register:6] +, PC=6 bgt [Register:6] [Register:4] [Label:prime] +, PC=7 div [Register:3] [Register:6] [Register:7] +, PC=8 beq [Register:0] [Register:31] [Label:notprime] +, PC=9 addi [Register:6] [Immediate:1] [Register:6] +, PC=10 jmp null null [Label:for] +, PC=11 sub [Register:10] [Register:10] [Register:10] +, PC=12 addi [Register:10] [Immediate:1] [Register:10] +, PC=13 end null null null +, PC=14 sub [Register:10] [Register:10] [Register:10] +, PC=15 subi [Register:10] [Immediate:1] [Register:10] +, PC=16 end null null null +]] +Symbol Table : +[{a=0, palindrome=13, loop=3, main=1}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:7] [Register:7] [Register:7] +, PC=3 divi [Register:3] [Immediate:10] [Register:4] +, PC=4 muli [Register:7] [Immediate:10] [Register:7] +, PC=5 add [Register:7] [Register:31] [Register:7] +, PC=6 divi [Register:3] [Immediate:10] [Register:3] +, PC=7 bgt [Register:3] [Register:0] [Label:loop] +, PC=8 load [Register:0] [Label:a] [Register:5] +, PC=9 beq [Register:5] [Register:7] [Label:palindrome] +, PC=10 sub [Register:10] [Register:10] [Register:10] +, PC=11 subi [Register:10] [Immediate:1] [Register:10] +, PC=12 end null null null +, PC=13 sub [Register:10] [Register:10] [Register:10] +, PC=14 addi [Register:10] [Immediate:1] [Register:10] +, PC=15 end null null null +]] +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 14 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 12 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Symbol Table : +[{a=0, b=1, main=2}] + +Parsed instructions : +[[PC=2 load [Register:0] [Label:a] [Register:4] +, PC=3 end null null null +]] +Symbol Table : +[{prime=11, a=0, for=6, main=1, notprime=14}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:4] [Register:4] [Register:4] +, PC=3 divi [Register:3] [Immediate:2] [Register:4] +, PC=4 sub [Register:6] [Register:6] [Register:6] +, PC=5 addi [Register:6] [Immediate:2] [Register:6] +, PC=6 bgt [Register:6] [Register:4] [Label:prime] +, PC=7 div [Register:3] [Register:6] [Register:7] +, PC=8 beq [Register:0] [Register:31] [Label:notprime] +, PC=9 addi [Register:6] [Immediate:1] [Register:6] +, PC=10 jmp null null [Label:for] +, PC=11 sub [Register:10] [Register:10] [Register:10] +, PC=12 addi [Register:10] [Immediate:1] [Register:10] +, PC=13 end null null null +, PC=14 sub [Register:10] [Register:10] [Register:10] +, PC=15 subi [Register:10] [Immediate:1] [Register:10] +, PC=16 end null null null +]] +Symbol Table : +[{a=0, palindrome=13, loop=3, main=1}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:7] [Register:7] [Register:7] +, PC=3 divi [Register:3] [Immediate:10] [Register:4] +, PC=4 muli [Register:7] [Immediate:10] [Register:7] +, PC=5 add [Register:7] [Register:31] [Register:7] +, PC=6 divi [Register:3] [Immediate:10] [Register:3] +, PC=7 bgt [Register:3] [Register:0] [Label:loop] +, PC=8 load [Register:0] [Label:a] [Register:5] +, PC=9 beq [Register:5] [Register:7] [Label:palindrome] +, PC=10 sub [Register:10] [Register:10] [Register:10] +, PC=11 subi [Register:10] [Immediate:1] [Register:10] +, PC=12 end null null null +, PC=13 sub [Register:10] [Register:10] [Register:10] +, PC=14 addi [Register:10] [Immediate:1] [Register:10] +, PC=15 end null null null +]] +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 12 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Symbol Table : +[{a=0, b=1, main=2}] + +Parsed instructions : +[[PC=2 load [Register:0] [Label:a] [Register:4] +, PC=3 end null null null +]] +Symbol Table : +[{prime=11, a=0, for=6, main=1, notprime=14}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:4] [Register:4] [Register:4] +, PC=3 divi [Register:3] [Immediate:2] [Register:4] +, PC=4 sub [Register:6] [Register:6] [Register:6] +, PC=5 addi [Register:6] [Immediate:2] [Register:6] +, PC=6 bgt [Register:6] [Register:4] [Label:prime] +, PC=7 div [Register:3] [Register:6] [Register:7] +, PC=8 beq [Register:0] [Register:31] [Label:notprime] +, PC=9 addi [Register:6] [Immediate:1] [Register:6] +, PC=10 jmp null null [Label:for] +, PC=11 sub [Register:10] [Register:10] [Register:10] +, PC=12 addi [Register:10] [Immediate:1] [Register:10] +, PC=13 end null null null +, PC=14 sub [Register:10] [Register:10] [Register:10] +, PC=15 subi [Register:10] [Immediate:1] [Register:10] +, PC=16 end null null null +]] +Symbol Table : +[{a=0, palindrome=13, loop=3, main=1}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:7] [Register:7] [Register:7] +, PC=3 divi [Register:3] [Immediate:10] [Register:4] +, PC=4 muli [Register:7] [Immediate:10] [Register:7] +, PC=5 add [Register:7] [Register:31] [Register:7] +, PC=6 divi [Register:3] [Immediate:10] [Register:3] +, PC=7 bgt [Register:3] [Register:0] [Label:loop] +, PC=8 load [Register:0] [Label:a] [Register:5] +, PC=9 beq [Register:5] [Register:7] [Label:palindrome] +, PC=10 sub [Register:10] [Register:10] [Register:10] +, PC=11 subi [Register:10] [Immediate:1] [Register:10] +, PC=12 end null null null +, PC=13 sub [Register:10] [Register:10] [Register:10] +, PC=14 addi [Register:10] [Immediate:1] [Register:10] +, PC=15 end null null null +]] +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 12 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Symbol Table : +[{a=0, b=1, main=2}] + +Parsed instructions : +[[PC=2 load [Register:0] [Label:a] [Register:4] +, PC=3 end null null null +]] +Symbol Table : +[{prime=11, a=0, for=6, main=1, notprime=14}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:4] [Register:4] [Register:4] +, PC=3 divi [Register:3] [Immediate:2] [Register:4] +, PC=4 sub [Register:6] [Register:6] [Register:6] +, PC=5 addi [Register:6] [Immediate:2] [Register:6] +, PC=6 bgt [Register:6] [Register:4] [Label:prime] +, PC=7 div [Register:3] [Register:6] [Register:7] +, PC=8 beq [Register:0] [Register:31] [Label:notprime] +, PC=9 addi [Register:6] [Immediate:1] [Register:6] +, PC=10 jmp null null [Label:for] +, PC=11 sub [Register:10] [Register:10] [Register:10] +, PC=12 addi [Register:10] [Immediate:1] [Register:10] +, PC=13 end null null null +, PC=14 sub [Register:10] [Register:10] [Register:10] +, PC=15 subi [Register:10] [Immediate:1] [Register:10] +, PC=16 end null null null +]] +Symbol Table : +[{a=0, palindrome=13, loop=3, main=1}] + +Parsed instructions : +[[PC=1 load [Register:0] [Label:a] [Register:3] +, PC=2 sub [Register:7] [Register:7] [Register:7] +, PC=3 divi [Register:3] [Immediate:10] [Register:4] +, PC=4 muli [Register:7] [Immediate:10] [Register:7] +, PC=5 add [Register:7] [Register:31] [Register:7] +, PC=6 divi [Register:3] [Immediate:10] [Register:3] +, PC=7 bgt [Register:3] [Register:0] [Label:loop] +, PC=8 load [Register:0] [Label:a] [Register:5] +, PC=9 beq [Register:5] [Register:7] [Label:palindrome] +, PC=10 sub [Register:10] [Register:10] [Register:10] +, PC=11 subi [Register:10] [Immediate:1] [Register:10] +, PC=12 end null null null +, PC=13 sub [Register:10] [Register:10] [Register:10] +, PC=14 addi [Register:10] [Immediate:1] [Register:10] +, PC=15 end null null null +]] +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 14 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 17 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Buildfile: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build-subprojects: + +init: + [copy] Copying 17 files to /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/bin + +build-project: + [echo] CustomISAAssembler: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/build.xml + +build: + +make-jar: + [jar] Building jar: /home/chasingcar/acad/sem-5/ca/cs311-lab/assignment-2/supporting_files/submissions/jars/assembler.jar + +BUILD SUCCESSFUL +Total time: 0 seconds +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 +Error: LinkageError occurred while loading main class main.Main + java.lang.UnsupportedClassVersionError: main/Main has been compiled by a more recent version of the Java Runtime (class file version 62.0), this version of the Java Runtime only recognizes class file versions up to 61.0 diff --git a/assignment-2/supporting_files/test_cases/1.asm b/assignment-2/supporting_files/test_cases/1.asm new file mode 100755 index 0000000..ee2d17a --- /dev/null +++ b/assignment-2/supporting_files/test_cases/1.asm @@ -0,0 +1,9 @@ + .data +a: + 10 +b: + 20 + .text +main: + load %x0, $a, %x4 + end diff --git a/assignment-2/supporting_files/test_cases/1.out b/assignment-2/supporting_files/test_cases/1.out new file mode 100755 index 0000000..57736dd Binary files /dev/null and b/assignment-2/supporting_files/test_cases/1.out differ diff --git a/assignment-2/supporting_files/test_cases/2.asm b/assignment-2/supporting_files/test_cases/2.asm new file mode 100755 index 0000000..20993c2 --- /dev/null +++ b/assignment-2/supporting_files/test_cases/2.asm @@ -0,0 +1,22 @@ + .data +a: + 12321 + .text +main: + load %x0, $a, %x3 + sub %x7, %x7, %x7 +loop: + divi %x3, 10, %x4 + muli %x7, 10, %x7 + add %x7, %x31, %x7 + divi %x3, 10, %x3 + bgt %x3, %x0, loop + load %x0, $a, %x5 + beq %x5, %x7, palindrome + sub %x10, %x10, %x10 + subi %x10, 1, %x10 + end +palindrome: + sub %x10, %x10, %x10 + addi %x10, 1, %x10 + end diff --git a/assignment-2/supporting_files/test_cases/2.out b/assignment-2/supporting_files/test_cases/2.out new file mode 100755 index 0000000..d4fbf39 Binary files /dev/null and b/assignment-2/supporting_files/test_cases/2.out differ diff --git a/assignment-2/supporting_files/test_cases/3.asm b/assignment-2/supporting_files/test_cases/3.asm new file mode 100755 index 0000000..e857ac6 --- /dev/null +++ b/assignment-2/supporting_files/test_cases/3.asm @@ -0,0 +1,24 @@ +.data +a: + 11 + .text +main: + load %x0, $a, %x3 + sub %x4, %x4, %x4 + divi %x3, 2, %x4 + sub %x6, %x6, %x6 + addi %x6, 2, %x6 +for: + bgt %x6, %x4, prime + div %x3, %x6, %x7 + beq %x0, %x31, notprime + addi %x6, 1, %x6 + jmp for +prime: + sub %x10, %x10, %x10 + addi %x10, 1, %x10 + end +notprime: + sub %x10, %x10, %x10 + subi %x10, 1, %x10 + end diff --git a/assignment-2/supporting_files/test_cases/3.out b/assignment-2/supporting_files/test_cases/3.out new file mode 100755 index 0000000..b157782 Binary files /dev/null and b/assignment-2/supporting_files/test_cases/3.out differ diff --git a/assignment-2/supporting_files/test_cases/test.out b/assignment-2/supporting_files/test_cases/test.out new file mode 100644 index 0000000..b157782 Binary files /dev/null and b/assignment-2/supporting_files/test_cases/test.out differ diff --git a/assignment-2/supporting_files/test_zip.py b/assignment-2/supporting_files/test_zip.py new file mode 100644 index 0000000..76cdb87 --- /dev/null +++ b/assignment-2/supporting_files/test_zip.py @@ -0,0 +1,76 @@ +#!/bin/python + +import sys +import os +import zipfile +import shutil +import filecmp +import subprocess +from threading import Timer + +zip_file = sys.argv[1] + +l = len(zip_file.split("/")) +print("Students :\t" + zip_file.split("/")[l-1].split("_")[0] + " and " + zip_file.split("/")[l-1].split("_")[1].split(".")[0]) +print("") + +submissions_temp_dir = "./submissions/" + +if not os.path.exists(submissions_temp_dir): + os.mkdir(submissions_temp_dir) + +zip_ref = zipfile.ZipFile(zip_file, 'r') +zip_ref.extractall(submissions_temp_dir) +zip_ref.close() + +shutil.copyfile("build.xml", submissions_temp_dir + "/build.xml") + +os.chdir(submissions_temp_dir) + +stdout_file = open("./tmp.output", 'a') +popen_args = ["ant", "make-jar"] +proc = subprocess.Popen(popen_args, stdout = stdout_file, stderr = stdout_file) +timer = Timer(5, proc.kill) +try: + timer.start() + stdout, stderr = proc.communicate() +finally: + timer.cancel() +stdout_file.close() + +if not os.path.exists("jars/assembler.jar"): + print("compilation failed. jar file not created") + sys.exit(0) + +test_cases_dir = "../test_cases" +total_marks = 0 +scored_marks = 0 +for testcase in os.listdir(test_cases_dir): + if ".asm" in testcase: + total_marks = total_marks + 1 + + stdout_file = open("./tmp.output", 'a') + popen_args = ["java", "-Xmx1g", "-jar", "jars/assembler.jar", test_cases_dir + "/" + testcase, "./" + testcase.split(".")[0] + ".observedoutput"] + proc = subprocess.Popen(popen_args, stdout = stdout_file, stderr = stdout_file) + timer = Timer(5, proc.kill) + try: + timer.start() + stdout, stderr = proc.communicate() + finally: + timer.cancel() + stdout_file.close() + + if os.path.exists("./" + testcase.split(".")[0] + ".observedoutput"): + if filecmp.cmp(test_cases_dir + "/" + testcase.split(".")[0] + ".out", "./" + testcase.split(".")[0] + ".observedoutput") == True: + scored_marks = scored_marks + 1 + print(testcase + " : PASS!") + else: + print(testcase + " : fail - incorrect object file contents") + else: + print(testcase + " : fail - file not created") + +os.chdir("..") + +# shutil.rmtree(submissions_temp_dir) + +print("\ntotal score = " + str(scored_marks) + " out of " + str(total_marks)) diff --git a/assignment-3/src/configuration/Configuration.java b/assignment-3/src/configuration/Configuration.java new file mode 100644 index 0000000..d7b43e1 --- /dev/null +++ b/assignment-3/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-3/src/configuration/config.xml b/assignment-3/src/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/assignment-3/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-3/src/generic/Instruction.java b/assignment-3/src/generic/Instruction.java new file mode 100644 index 0000000..ec3ef99 --- /dev/null +++ b/assignment-3/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-3/src/generic/Misc.java b/assignment-3/src/generic/Misc.java new file mode 100644 index 0000000..0be690a --- /dev/null +++ b/assignment-3/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-3/src/generic/Operand.java b/assignment-3/src/generic/Operand.java new file mode 100644 index 0000000..3224099 --- /dev/null +++ b/assignment-3/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-3/src/generic/Simulator.java b/assignment-3/src/generic/Simulator.java new file mode 100644 index 0000000..d093f82 --- /dev/null +++ b/assignment-3/src/generic/Simulator.java @@ -0,0 +1,57 @@ +package generic; + +import processor.Clock; +import processor.Processor; + +public class Simulator { + + static Processor processor; + static boolean simulationComplete; + + public static void setupSimulation(String assemblyProgramFile, Processor p) + { + Simulator.processor = p; + loadProgram(assemblyProgramFile); + + simulationComplete = false; + } + + static void loadProgram(String assemblyProgramFile) + { + /* + * TODO + * 1. load the program into memory according to the program layout described + * in the ISA specification + * 2. set PC to the address of the first instruction in the main + * 3. set the following registers: + * x0 = 0 + * x1 = 65535 + * x2 = 65535 + */ + } + + public static void simulate() + { + while(simulationComplete == false) + { + processor.getIFUnit().performIF(); + Clock.incrementClock(); + processor.getOFUnit().performOF(); + Clock.incrementClock(); + processor.getEXUnit().performEX(); + Clock.incrementClock(); + processor.getMAUnit().performMA(); + Clock.incrementClock(); + processor.getRWUnit().performRW(); + Clock.incrementClock(); + } + + // TODO + // set statistics + } + + public static void setSimulationComplete(boolean value) + { + simulationComplete = value; + } +} diff --git a/assignment-3/src/generic/Statistics.java b/assignment-3/src/generic/Statistics.java new file mode 100644 index 0000000..20125e6 --- /dev/null +++ b/assignment-3/src/generic/Statistics.java @@ -0,0 +1,39 @@ +package generic; + +import java.io.PrintWriter; + +public class Statistics { + + // TODO add your statistics here + static int numberOfInstructions; + static int numberOfCycles; + + + public static void printStatistics(String statFile) + { + try + { + PrintWriter writer = new PrintWriter(statFile); + + writer.println("Number of instructions executed = " + numberOfInstructions); + writer.println("Number of cycles taken = " + numberOfCycles); + + // TODO add code here to print statistics in the output file + + writer.close(); + } + catch(Exception e) + { + Misc.printErrorAndExit(e.getMessage()); + } + } + + // TODO write functions to update statistics + public void setNumberOfInstructions(int numberOfInstructions) { + Statistics.numberOfInstructions = numberOfInstructions; + } + + public void setNumberOfCycles(int numberOfCycles) { + Statistics.numberOfCycles = numberOfCycles; + } +} diff --git a/assignment-3/src/main/Main.java b/assignment-3/src/main/Main.java new file mode 100644 index 0000000..237c66e --- /dev/null +++ b/assignment-3/src/main/Main.java @@ -0,0 +1,50 @@ +package main; +import java.util.ArrayList; + +import configuration.Configuration; +import generic.Misc; +import generic.Statistics; +import processor.Processor; +import processor.memorysystem.MainMemory; +import processor.pipeline.RegisterFile; +import generic.Simulator; + +public class Main { + + public static void main(String[] args) { + if(args.length != 3) + { + Misc.printErrorAndExit("usage: java -jar \n"); + } + + Configuration.parseConfiguratioFile(args[0]); + + Processor processor = new Processor(); + + Simulator.setupSimulation(args[2], processor); + Simulator.simulate(); + + processor.printState(0, 30); // ((0, 0) refers to the range of main memory addresses we wish to print. this is an empty set. + + Statistics.printStatistics(args[1]); + + System.out.println("Hash of the Processor State = "+getHashCode(processor.getRegisterFile(), processor.getMainMemory())); + } + + static int getHashCode(RegisterFile registerState, MainMemory memoryState) { + ArrayList hash = new ArrayList(); + + hash.add(registerState.getProgramCounter()); + + for(int i=0;i<32;i++) { + hash.add(registerState.getValue(i)); + } + + for(int i=0;i<65536;i++) { + hash.add(memoryState.getWord(i)); + } + + return hash.hashCode(); + } + +} diff --git a/assignment-3/src/processor/Clock.java b/assignment-3/src/processor/Clock.java new file mode 100644 index 0000000..a19b8a8 --- /dev/null +++ b/assignment-3/src/processor/Clock.java @@ -0,0 +1,15 @@ +package processor; + +public class Clock { + static long currentTime = 0; + + public static void incrementClock() + { + currentTime++; + } + + public static long getCurrentTime() + { + return currentTime; + } +} diff --git a/assignment-3/src/processor/Processor.java b/assignment-3/src/processor/Processor.java new file mode 100644 index 0000000..33e8188 --- /dev/null +++ b/assignment-3/src/processor/Processor.java @@ -0,0 +1,97 @@ +package processor; + +import processor.memorysystem.MainMemory; +import processor.pipeline.EX_IF_LatchType; +import processor.pipeline.EX_MA_LatchType; +import processor.pipeline.Execute; +import processor.pipeline.IF_EnableLatchType; +import processor.pipeline.IF_OF_LatchType; +import processor.pipeline.InstructionFetch; +import processor.pipeline.MA_RW_LatchType; +import processor.pipeline.MemoryAccess; +import processor.pipeline.OF_EX_LatchType; +import processor.pipeline.OperandFetch; +import processor.pipeline.RegisterFile; +import processor.pipeline.RegisterWrite; + +public class Processor { + + RegisterFile registerFile; + MainMemory mainMemory; + + IF_EnableLatchType IF_EnableLatch; + IF_OF_LatchType IF_OF_Latch; + OF_EX_LatchType OF_EX_Latch; + EX_MA_LatchType EX_MA_Latch; + EX_IF_LatchType EX_IF_Latch; + MA_RW_LatchType MA_RW_Latch; + + InstructionFetch IFUnit; + OperandFetch OFUnit; + Execute EXUnit; + MemoryAccess MAUnit; + RegisterWrite RWUnit; + + public Processor() + { + registerFile = new RegisterFile(); + mainMemory = new MainMemory(); + + IF_EnableLatch = new IF_EnableLatchType(); + IF_OF_Latch = new IF_OF_LatchType(); + OF_EX_Latch = new OF_EX_LatchType(); + EX_MA_Latch = new EX_MA_LatchType(); + EX_IF_Latch = new EX_IF_LatchType(); + MA_RW_Latch = new MA_RW_LatchType(); + + IFUnit = new InstructionFetch(this, IF_EnableLatch, IF_OF_Latch, EX_IF_Latch); + OFUnit = new OperandFetch(this, IF_OF_Latch, OF_EX_Latch); + EXUnit = new Execute(this, OF_EX_Latch, EX_MA_Latch, EX_IF_Latch); + MAUnit = new MemoryAccess(this, EX_MA_Latch, MA_RW_Latch); + RWUnit = new RegisterWrite(this, MA_RW_Latch, IF_EnableLatch); + } + + public void printState(int memoryStartingAddress, int memoryEndingAddress) + { + System.out.println(registerFile.getContentsAsString()); + + System.out.println(mainMemory.getContentsAsString(memoryStartingAddress, memoryEndingAddress)); + } + + public RegisterFile getRegisterFile() { + return registerFile; + } + + public void setRegisterFile(RegisterFile registerFile) { + this.registerFile = registerFile; + } + + public MainMemory getMainMemory() { + return mainMemory; + } + + public void setMainMemory(MainMemory mainMemory) { + this.mainMemory = mainMemory; + } + + public InstructionFetch getIFUnit() { + return IFUnit; + } + + public OperandFetch getOFUnit() { + return OFUnit; + } + + public Execute getEXUnit() { + return EXUnit; + } + + public MemoryAccess getMAUnit() { + return MAUnit; + } + + public RegisterWrite getRWUnit() { + return RWUnit; + } + +} diff --git a/assignment-3/src/processor/memorysystem/MainMemory.java b/assignment-3/src/processor/memorysystem/MainMemory.java new file mode 100644 index 0000000..94b7bd4 --- /dev/null +++ b/assignment-3/src/processor/memorysystem/MainMemory.java @@ -0,0 +1,35 @@ +package processor.memorysystem; + +public class MainMemory { + int[] memory; + + public MainMemory() + { + memory = new int[65536]; + } + + public int getWord(int address) + { + return memory[address]; + } + + public void setWord(int address, int value) + { + memory[address] = value; + } + + public String getContentsAsString(int startingAddress, int endingAddress) + { + if(startingAddress == endingAddress) + return ""; + + StringBuilder sb = new StringBuilder(); + sb.append("\nMain Memory Contents:\n\n"); + for(int i = startingAddress; i <= endingAddress; i++) + { + sb.append(i + "\t\t: " + memory[i] + "\n"); + } + sb.append("\n"); + return sb.toString(); + } +} diff --git a/assignment-3/src/processor/pipeline/EX_IF_LatchType.java b/assignment-3/src/processor/pipeline/EX_IF_LatchType.java new file mode 100644 index 0000000..6ca9646 --- /dev/null +++ b/assignment-3/src/processor/pipeline/EX_IF_LatchType.java @@ -0,0 +1,10 @@ +package processor.pipeline; + +public class EX_IF_LatchType { + + public EX_IF_LatchType() + { + + } + +} diff --git a/assignment-3/src/processor/pipeline/EX_MA_LatchType.java b/assignment-3/src/processor/pipeline/EX_MA_LatchType.java new file mode 100644 index 0000000..cf74f38 --- /dev/null +++ b/assignment-3/src/processor/pipeline/EX_MA_LatchType.java @@ -0,0 +1,20 @@ +package processor.pipeline; + +public class EX_MA_LatchType { + + boolean MA_enable; + + public EX_MA_LatchType() + { + MA_enable = false; + } + + public boolean isMA_enable() { + return MA_enable; + } + + public void setMA_enable(boolean mA_enable) { + MA_enable = mA_enable; + } + +} diff --git a/assignment-3/src/processor/pipeline/Execute.java b/assignment-3/src/processor/pipeline/Execute.java new file mode 100644 index 0000000..df6e251 --- /dev/null +++ b/assignment-3/src/processor/pipeline/Execute.java @@ -0,0 +1,24 @@ +package processor.pipeline; + +import processor.Processor; + +public class Execute { + Processor containingProcessor; + OF_EX_LatchType OF_EX_Latch; + EX_MA_LatchType EX_MA_Latch; + EX_IF_LatchType EX_IF_Latch; + + public Execute(Processor containingProcessor, OF_EX_LatchType oF_EX_Latch, EX_MA_LatchType eX_MA_Latch, EX_IF_LatchType eX_IF_Latch) + { + this.containingProcessor = containingProcessor; + this.OF_EX_Latch = oF_EX_Latch; + this.EX_MA_Latch = eX_MA_Latch; + this.EX_IF_Latch = eX_IF_Latch; + } + + public void performEX() + { + //TODO + } + +} diff --git a/assignment-3/src/processor/pipeline/IF_EnableLatchType.java b/assignment-3/src/processor/pipeline/IF_EnableLatchType.java new file mode 100644 index 0000000..e0198a5 --- /dev/null +++ b/assignment-3/src/processor/pipeline/IF_EnableLatchType.java @@ -0,0 +1,20 @@ +package processor.pipeline; + +public class IF_EnableLatchType { + + boolean IF_enable; + + public IF_EnableLatchType() + { + IF_enable = true; + } + + public boolean isIF_enable() { + return IF_enable; + } + + public void setIF_enable(boolean iF_enable) { + IF_enable = iF_enable; + } + +} diff --git a/assignment-3/src/processor/pipeline/IF_OF_LatchType.java b/assignment-3/src/processor/pipeline/IF_OF_LatchType.java new file mode 100644 index 0000000..e3fb477 --- /dev/null +++ b/assignment-3/src/processor/pipeline/IF_OF_LatchType.java @@ -0,0 +1,29 @@ +package processor.pipeline; + +public class IF_OF_LatchType { + + boolean OF_enable; + int instruction; + + public IF_OF_LatchType() + { + OF_enable = false; + } + + public boolean isOF_enable() { + return OF_enable; + } + + public void setOF_enable(boolean oF_enable) { + OF_enable = oF_enable; + } + + public int getInstruction() { + return instruction; + } + + public void setInstruction(int instruction) { + this.instruction = instruction; + } + +} diff --git a/assignment-3/src/processor/pipeline/InstructionFetch.java b/assignment-3/src/processor/pipeline/InstructionFetch.java new file mode 100644 index 0000000..f6d85d0 --- /dev/null +++ b/assignment-3/src/processor/pipeline/InstructionFetch.java @@ -0,0 +1,34 @@ +package processor.pipeline; + +import processor.Processor; + +public class InstructionFetch { + + Processor containingProcessor; + IF_EnableLatchType IF_EnableLatch; + IF_OF_LatchType IF_OF_Latch; + EX_IF_LatchType EX_IF_Latch; + + public InstructionFetch(Processor containingProcessor, IF_EnableLatchType iF_EnableLatch, IF_OF_LatchType iF_OF_Latch, EX_IF_LatchType eX_IF_Latch) + { + this.containingProcessor = containingProcessor; + this.IF_EnableLatch = iF_EnableLatch; + this.IF_OF_Latch = iF_OF_Latch; + this.EX_IF_Latch = eX_IF_Latch; + } + + public void performIF() + { + if(IF_EnableLatch.isIF_enable()) + { + int currentPC = containingProcessor.getRegisterFile().getProgramCounter(); + int newInstruction = containingProcessor.getMainMemory().getWord(currentPC); + IF_OF_Latch.setInstruction(newInstruction); + containingProcessor.getRegisterFile().setProgramCounter(currentPC + 1); + + IF_EnableLatch.setIF_enable(false); + IF_OF_Latch.setOF_enable(true); + } + } + +} diff --git a/assignment-3/src/processor/pipeline/MA_RW_LatchType.java b/assignment-3/src/processor/pipeline/MA_RW_LatchType.java new file mode 100644 index 0000000..7e9f76a --- /dev/null +++ b/assignment-3/src/processor/pipeline/MA_RW_LatchType.java @@ -0,0 +1,20 @@ +package processor.pipeline; + +public class MA_RW_LatchType { + + boolean RW_enable; + + public MA_RW_LatchType() + { + RW_enable = false; + } + + public boolean isRW_enable() { + return RW_enable; + } + + public void setRW_enable(boolean rW_enable) { + RW_enable = rW_enable; + } + +} diff --git a/assignment-3/src/processor/pipeline/MemoryAccess.java b/assignment-3/src/processor/pipeline/MemoryAccess.java new file mode 100644 index 0000000..1f1684c --- /dev/null +++ b/assignment-3/src/processor/pipeline/MemoryAccess.java @@ -0,0 +1,22 @@ +package processor.pipeline; + +import processor.Processor; + +public class MemoryAccess { + Processor containingProcessor; + EX_MA_LatchType EX_MA_Latch; + MA_RW_LatchType MA_RW_Latch; + + public MemoryAccess(Processor containingProcessor, EX_MA_LatchType eX_MA_Latch, MA_RW_LatchType mA_RW_Latch) + { + this.containingProcessor = containingProcessor; + this.EX_MA_Latch = eX_MA_Latch; + this.MA_RW_Latch = mA_RW_Latch; + } + + public void performMA() + { + //TODO + } + +} diff --git a/assignment-3/src/processor/pipeline/OF_EX_LatchType.java b/assignment-3/src/processor/pipeline/OF_EX_LatchType.java new file mode 100644 index 0000000..0339fae --- /dev/null +++ b/assignment-3/src/processor/pipeline/OF_EX_LatchType.java @@ -0,0 +1,20 @@ +package processor.pipeline; + +public class OF_EX_LatchType { + + boolean EX_enable; + + public OF_EX_LatchType() + { + EX_enable = false; + } + + public boolean isEX_enable() { + return EX_enable; + } + + public void setEX_enable(boolean eX_enable) { + EX_enable = eX_enable; + } + +} diff --git a/assignment-3/src/processor/pipeline/OperandFetch.java b/assignment-3/src/processor/pipeline/OperandFetch.java new file mode 100644 index 0000000..9620261 --- /dev/null +++ b/assignment-3/src/processor/pipeline/OperandFetch.java @@ -0,0 +1,28 @@ +package processor.pipeline; + +import processor.Processor; + +public class OperandFetch { + Processor containingProcessor; + IF_OF_LatchType IF_OF_Latch; + OF_EX_LatchType OF_EX_Latch; + + public OperandFetch(Processor containingProcessor, IF_OF_LatchType iF_OF_Latch, OF_EX_LatchType oF_EX_Latch) + { + this.containingProcessor = containingProcessor; + this.IF_OF_Latch = iF_OF_Latch; + this.OF_EX_Latch = oF_EX_Latch; + } + + public void performOF() + { + if(IF_OF_Latch.isOF_enable()) + { + //TODO + + IF_OF_Latch.setOF_enable(false); + OF_EX_Latch.setEX_enable(true); + } + } + +} diff --git a/assignment-3/src/processor/pipeline/RegisterFile.java b/assignment-3/src/processor/pipeline/RegisterFile.java new file mode 100644 index 0000000..4b95b95 --- /dev/null +++ b/assignment-3/src/processor/pipeline/RegisterFile.java @@ -0,0 +1,52 @@ +package processor.pipeline; + +public class RegisterFile { + int[] registerFile; + int programCounter; + + public RegisterFile() + { + registerFile = new int[32]; + registerFile[0]=0; //%xo is always 0 [RISC V] + } + + public int getValue(int registerNumber) + { + return registerFile[registerNumber]; + } + + public void setValue(int registerNumber, int value) + { + registerFile[registerNumber] = value; + } + + public int getProgramCounter() + { + return programCounter; + } + + public void setProgramCounter(int programCounter) + { + this.programCounter = programCounter; + } + + public void incrementProgramCounter() + { + this.programCounter++; + } + + public String getContentsAsString() + { + StringBuilder sb = new StringBuilder(); + sb.append("\nRegister File Contents:\n\n"); + sb.append("PC" + "\t: " + programCounter + "\n\n"); + + sb.append("x" + 0 + "\t: " + registerFile[0]+ "\n"); //%xo is always 0 [RISC V] + for(int i = 1; i < 32; i++) + { + sb.append("x" + i + "\t: " + registerFile[i] + "\n"); + } + sb.append("\n"); + return sb.toString(); + } +} diff --git a/assignment-3/src/processor/pipeline/RegisterWrite.java b/assignment-3/src/processor/pipeline/RegisterWrite.java new file mode 100644 index 0000000..21cba98 --- /dev/null +++ b/assignment-3/src/processor/pipeline/RegisterWrite.java @@ -0,0 +1,31 @@ +package processor.pipeline; + +import generic.Simulator; +import processor.Processor; + +public class RegisterWrite { + Processor containingProcessor; + MA_RW_LatchType MA_RW_Latch; + IF_EnableLatchType IF_EnableLatch; + + public RegisterWrite(Processor containingProcessor, MA_RW_LatchType mA_RW_Latch, IF_EnableLatchType iF_EnableLatch) + { + this.containingProcessor = containingProcessor; + this.MA_RW_Latch = mA_RW_Latch; + this.IF_EnableLatch = iF_EnableLatch; + } + + public void performRW() + { + if(MA_RW_Latch.isRW_enable()) + { + //TODO + + // if instruction being processed is an end instruction, remember to call Simulator.setSimulationComplete(true); + + MA_RW_Latch.setRW_enable(false); + IF_EnableLatch.setIF_enable(true); + } + } + +} diff --git a/isa_specification-1.pdf b/isa_specification-1.pdf new file mode 100644 index 0000000..9f76463 Binary files /dev/null and b/isa_specification-1.pdf differ