first commit

This commit is contained in:
Rajshekar K K 2019-08-29 17:25:31 +05:30
commit a142b0799b
23 changed files with 909 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Configuration>
<FunctionalUnits>
<ALU>
<Count>2</Count>
<Latency>1</Latency>
<ReciprocalOfThroughput>1</ReciprocalOfThroughput>
</ALU>
<Multiplier>
<Count>1</Count>
<Latency>4</Latency>
<ReciprocalOfThroughput>1</ReciprocalOfThroughput>
</Multiplier>
<Divider>
<Count>1</Count>
<Latency>10</Latency>
<ReciprocalOfThroughput>1</ReciprocalOfThroughput>
</Divider>
</FunctionalUnits>
<L1iCache>
<NumberOfLines>256</NumberOfLines>
<Latency>2</Latency>
<Associativity>4</Associativity>
<ReplacementPolicy>LRU</ReplacementPolicy>
</L1iCache>
<L1dCache>
<NumberOfLines>256</NumberOfLines>
<Latency>2</Latency>
<Associativity>4</Associativity>
<ReplacementPolicy>LRU</ReplacementPolicy>
</L1dCache>
<L2Cache>
<NumberOfLines>2048</NumberOfLines>
<Latency>10</Latency>
<Associativity>4</Associativity>
<ReplacementPolicy>LRU</ReplacementPolicy>
</L2Cache>
<MainMemoryLatency>40</MainMemoryLatency>
</Configuration>

View File

@ -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";
}
}
}
}
}

10
src/generic/Misc.java Normal file
View File

@ -0,0 +1,10 @@
package generic;
public class Misc {
public static void printErrorAndExit(String message)
{
System.err.println(message);
System.exit(1);
}
}

41
src/generic/Operand.java Normal file
View File

@ -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 + "]";
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

50
src/main/Main.java Normal file
View File

@ -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 <path-to-jar-file> <path-to-config-file> <path-to-stat-file> <path-to-object-file>\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<Integer> hash = new ArrayList<Integer>();
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();
}
}

15
src/processor/Clock.java Normal file
View File

@ -0,0 +1,15 @@
package processor;
public class Clock {
static long currentTime = 0;
public static void incrementClock()
{
currentTime++;
}
public static long getCurrentTime()
{
return currentTime;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,10 @@
package processor.pipeline;
public class EX_IF_LatchType {
public EX_IF_LatchType()
{
}
}

View File

@ -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;
}
}

View File

@ -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
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}