From a142b0799b73fc8c4c3c04d2bf6535d80e055a9d Mon Sep 17 00:00:00 2001 From: "rajshekar.k" Date: Thu, 29 Aug 2019 17:25:31 +0530 Subject: [PATCH] first commit --- src/configuration/Configuration.java | 116 ++++++++++++++++++ src/configuration/config.xml | 43 +++++++ src/generic/Instruction.java | 96 +++++++++++++++ src/generic/Misc.java | 10 ++ src/generic/Operand.java | 41 +++++++ src/generic/Simulator.java | 57 +++++++++ src/generic/Statistics.java | 39 ++++++ src/main/Main.java | 50 ++++++++ src/processor/Clock.java | 15 +++ src/processor/Processor.java | 97 +++++++++++++++ src/processor/memorysystem/MainMemory.java | 35 ++++++ src/processor/pipeline/EX_IF_LatchType.java | 10 ++ src/processor/pipeline/EX_MA_LatchType.java | 20 +++ src/processor/pipeline/Execute.java | 24 ++++ .../pipeline/IF_EnableLatchType.java | 20 +++ src/processor/pipeline/IF_OF_LatchType.java | 29 +++++ src/processor/pipeline/InstructionFetch.java | 34 +++++ src/processor/pipeline/MA_RW_LatchType.java | 20 +++ src/processor/pipeline/MemoryAccess.java | 22 ++++ src/processor/pipeline/OF_EX_LatchType.java | 20 +++ src/processor/pipeline/OperandFetch.java | 28 +++++ src/processor/pipeline/RegisterFile.java | 52 ++++++++ src/processor/pipeline/RegisterWrite.java | 31 +++++ 23 files changed, 909 insertions(+) create mode 100644 src/configuration/Configuration.java create mode 100644 src/configuration/config.xml create mode 100644 src/generic/Instruction.java create mode 100644 src/generic/Misc.java create mode 100644 src/generic/Operand.java create mode 100644 src/generic/Simulator.java create mode 100644 src/generic/Statistics.java create mode 100644 src/main/Main.java create mode 100644 src/processor/Clock.java create mode 100644 src/processor/Processor.java create mode 100644 src/processor/memorysystem/MainMemory.java create mode 100644 src/processor/pipeline/EX_IF_LatchType.java create mode 100644 src/processor/pipeline/EX_MA_LatchType.java create mode 100644 src/processor/pipeline/Execute.java create mode 100644 src/processor/pipeline/IF_EnableLatchType.java create mode 100644 src/processor/pipeline/IF_OF_LatchType.java create mode 100644 src/processor/pipeline/InstructionFetch.java create mode 100644 src/processor/pipeline/MA_RW_LatchType.java create mode 100644 src/processor/pipeline/MemoryAccess.java create mode 100644 src/processor/pipeline/OF_EX_LatchType.java create mode 100644 src/processor/pipeline/OperandFetch.java create mode 100644 src/processor/pipeline/RegisterFile.java create mode 100644 src/processor/pipeline/RegisterWrite.java diff --git a/src/configuration/Configuration.java b/src/configuration/Configuration.java new file mode 100644 index 0000000..d7b43e1 --- /dev/null +++ b/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/src/configuration/config.xml b/src/configuration/config.xml new file mode 100644 index 0000000..002c97c --- /dev/null +++ b/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/src/generic/Instruction.java b/src/generic/Instruction.java new file mode 100644 index 0000000..ec3ef99 --- /dev/null +++ b/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/src/generic/Misc.java b/src/generic/Misc.java new file mode 100644 index 0000000..0be690a --- /dev/null +++ b/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/src/generic/Operand.java b/src/generic/Operand.java new file mode 100644 index 0000000..3224099 --- /dev/null +++ b/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/src/generic/Simulator.java b/src/generic/Simulator.java new file mode 100644 index 0000000..d093f82 --- /dev/null +++ b/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/src/generic/Statistics.java b/src/generic/Statistics.java new file mode 100644 index 0000000..20125e6 --- /dev/null +++ b/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/src/main/Main.java b/src/main/Main.java new file mode 100644 index 0000000..237c66e --- /dev/null +++ b/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/src/processor/Clock.java b/src/processor/Clock.java new file mode 100644 index 0000000..a19b8a8 --- /dev/null +++ b/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/src/processor/Processor.java b/src/processor/Processor.java new file mode 100644 index 0000000..33e8188 --- /dev/null +++ b/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/src/processor/memorysystem/MainMemory.java b/src/processor/memorysystem/MainMemory.java new file mode 100644 index 0000000..94b7bd4 --- /dev/null +++ b/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/src/processor/pipeline/EX_IF_LatchType.java b/src/processor/pipeline/EX_IF_LatchType.java new file mode 100644 index 0000000..6ca9646 --- /dev/null +++ b/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/src/processor/pipeline/EX_MA_LatchType.java b/src/processor/pipeline/EX_MA_LatchType.java new file mode 100644 index 0000000..cf74f38 --- /dev/null +++ b/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/src/processor/pipeline/Execute.java b/src/processor/pipeline/Execute.java new file mode 100644 index 0000000..df6e251 --- /dev/null +++ b/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/src/processor/pipeline/IF_EnableLatchType.java b/src/processor/pipeline/IF_EnableLatchType.java new file mode 100644 index 0000000..e0198a5 --- /dev/null +++ b/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/src/processor/pipeline/IF_OF_LatchType.java b/src/processor/pipeline/IF_OF_LatchType.java new file mode 100644 index 0000000..e3fb477 --- /dev/null +++ b/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/src/processor/pipeline/InstructionFetch.java b/src/processor/pipeline/InstructionFetch.java new file mode 100644 index 0000000..f6d85d0 --- /dev/null +++ b/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/src/processor/pipeline/MA_RW_LatchType.java b/src/processor/pipeline/MA_RW_LatchType.java new file mode 100644 index 0000000..7e9f76a --- /dev/null +++ b/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/src/processor/pipeline/MemoryAccess.java b/src/processor/pipeline/MemoryAccess.java new file mode 100644 index 0000000..1f1684c --- /dev/null +++ b/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/src/processor/pipeline/OF_EX_LatchType.java b/src/processor/pipeline/OF_EX_LatchType.java new file mode 100644 index 0000000..0339fae --- /dev/null +++ b/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/src/processor/pipeline/OperandFetch.java b/src/processor/pipeline/OperandFetch.java new file mode 100644 index 0000000..9620261 --- /dev/null +++ b/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/src/processor/pipeline/RegisterFile.java b/src/processor/pipeline/RegisterFile.java new file mode 100644 index 0000000..4b95b95 --- /dev/null +++ b/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/src/processor/pipeline/RegisterWrite.java b/src/processor/pipeline/RegisterWrite.java new file mode 100644 index 0000000..21cba98 --- /dev/null +++ b/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); + } + } + +}