even odd.out is working

This commit is contained in:
karthikmurakonda 2022-10-07 04:29:58 +05:30
parent 3f38e8f432
commit 783e72c4ed
7 changed files with 209 additions and 43 deletions

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>

39
assignment-3/build.xml Executable file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
</javac>
</target>
<target name="make-jar" depends="build">
<mkdir dir="jars"/>
<jar destfile="jars/simulator.jar" basedir="bin">
<manifest>
<attribute name="Main-Class" value="main.Main"/>
</manifest>
</jar>
</target>
</project>

View File

@ -45,7 +45,7 @@ public class Processor {
MA_RW_Latch = new MA_RW_LatchType(); MA_RW_Latch = new MA_RW_LatchType();
IFUnit = new InstructionFetch(this, IF_EnableLatch, IF_OF_Latch, EX_IF_Latch); IFUnit = new InstructionFetch(this, IF_EnableLatch, IF_OF_Latch, EX_IF_Latch);
OFUnit = new OperandFetch(this, IF_OF_Latch, OF_EX_Latch); OFUnit = new OperandFetch(this, IF_OF_Latch, OF_EX_Latch, IF_EnableLatch);
EXUnit = new Execute(this, OF_EX_Latch, EX_MA_Latch, EX_IF_Latch); EXUnit = new Execute(this, OF_EX_Latch, EX_MA_Latch, EX_IF_Latch);
MAUnit = new MemoryAccess(this, EX_MA_Latch, MA_RW_Latch); MAUnit = new MemoryAccess(this, EX_MA_Latch, MA_RW_Latch);
RWUnit = new RegisterWrite(this, MA_RW_Latch, IF_EnableLatch); RWUnit = new RegisterWrite(this, MA_RW_Latch, IF_EnableLatch);

View File

@ -3,10 +3,12 @@ package processor.pipeline;
public class IF_EnableLatchType { public class IF_EnableLatchType {
boolean IF_enable; boolean IF_enable;
boolean freeze;
public IF_EnableLatchType() public IF_EnableLatchType()
{ {
IF_enable = true; IF_enable = true;
freeze = false;
} }
public boolean isIF_enable() { public boolean isIF_enable() {
@ -17,4 +19,12 @@ public class IF_EnableLatchType {
IF_enable = iF_enable; IF_enable = iF_enable;
} }
public boolean isFreeze() {
return freeze;
}
public void setFreeze(boolean freeze) {
this.freeze = freeze;
}
} }

View File

@ -18,7 +18,7 @@ public class InstructionFetch {
} }
public void performIF() public void performIF()
{ { if(!IF_EnableLatch.isFreeze()){
if(EX_IF_Latch.isIF_enable()){ if(EX_IF_Latch.isIF_enable()){
containingProcessor.getRegisterFile().setProgramCounter(EX_IF_Latch.getPC()-1); containingProcessor.getRegisterFile().setProgramCounter(EX_IF_Latch.getPC()-1);
EX_IF_Latch.setIF_enable(false); EX_IF_Latch.setIF_enable(false);
@ -34,6 +34,9 @@ public class InstructionFetch {
IF_EnableLatch.setIF_enable(true); IF_EnableLatch.setIF_enable(true);
IF_OF_Latch.setOF_enable(true); IF_OF_Latch.setOF_enable(true);
} }
}else{
IF_EnableLatch.setFreeze(false);
}
} }
} }

View File

@ -1,6 +1,8 @@
package processor.pipeline; package processor.pipeline;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import generic.Instruction; import generic.Instruction;
import processor.Processor; import processor.Processor;
@ -12,15 +14,38 @@ public class OperandFetch {
Processor containingProcessor; Processor containingProcessor;
IF_OF_LatchType IF_OF_Latch; IF_OF_LatchType IF_OF_Latch;
OF_EX_LatchType OF_EX_Latch; OF_EX_LatchType OF_EX_Latch;
IF_EnableLatchType IF_EnableLatch;
static OperationType[] opTypes = OperationType.values(); static OperationType[] opTypes = OperationType.values();
boolean Proceed; boolean Proceed;
Queue<Integer> queue;
boolean isEnd;
public 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, IF_EnableLatchType iF_EnableLatch)
{ {
this.containingProcessor = containingProcessor; this.containingProcessor = containingProcessor;
this.IF_OF_Latch = iF_OF_Latch; this.IF_OF_Latch = iF_OF_Latch;
this.OF_EX_Latch = oF_EX_Latch; this.OF_EX_Latch = oF_EX_Latch;
this.IF_EnableLatch = iF_EnableLatch;
isEnd = false;
Proceed = true; Proceed = true;
queue = new LinkedList<>();
queue.add(-1);
queue.add(-1);
queue.add(-1);
}
boolean checkdatahazard(int[] operands) {
for(int i=0;i<operands.length;i++) {
if(queue.contains(operands[i])) {
return true;
}
}
return false;
}
void updateQueue(int operand) {
queue.poll();
queue.add(operand);
} }
public static int twoscompliment(String s) { public static int twoscompliment(String s) {
@ -40,6 +65,14 @@ public class OperandFetch {
public void performOF() public void performOF()
{ {
if(isEnd){
IF_EnableLatch.setIF_enable(false);
IF_OF_Latch.setOF_enable(false);
OF_EX_Latch.setEX_enable(false);
return;
}
int addtoqueue = -1;
boolean noDataHazard = true;
if(IF_OF_Latch.isOF_enable() && Proceed) if(IF_OF_Latch.isOF_enable() && Proceed)
{ {
int instruction = IF_OF_Latch.getInstruction(); int instruction = IF_OF_Latch.getInstruction();
@ -76,7 +109,10 @@ public class OperandFetch {
int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue()); int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue());
int op2 = containingProcessor.getRegisterFile().getValue(rs2.getValue()); int op2 = containingProcessor.getRegisterFile().getValue(rs2.getValue());
if (checkdatahazard(new int[] { rs1.getValue(), rs2.getValue() })) {
noDataHazard = false;
}else{
addtoqueue = rd.getValue();
OF_EX_Latch.setInstruction(instr); OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setOp1(op1); OF_EX_Latch.setOp1(op1);
OF_EX_Latch.setOp2(op2); OF_EX_Latch.setOp2(op2);
@ -84,6 +120,7 @@ public class OperandFetch {
instr.setSourceOperand1(rs1); instr.setSourceOperand1(rs1);
instr.setSourceOperand2(rs2); instr.setSourceOperand2(rs2);
} }
}
else if (Arrays.stream(R2I_type_operators).anyMatch(x -> x == opcode)) { else if (Arrays.stream(R2I_type_operators).anyMatch(x -> x == opcode)) {
Operand rs1 = new Operand(); Operand rs1 = new Operand();
Operand rd = new Operand(); Operand rd = new Operand();
@ -100,18 +137,25 @@ public class OperandFetch {
} }
int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue()); int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue());
int op2 = containingProcessor.getRegisterFile().getValue(rd.getValue()); int op2 = containingProcessor.getRegisterFile().getValue(rd.getValue());
System.out.println("imm: " + imm); // System.out.println("imm: " + imm);
if (checkdatahazard(new int[] { rs1.getValue() })) {
noDataHazard = false;
}else{
if(opcode <= 22) { // > 21 means it is a branch instruction so no need to update queue
addtoqueue = rd.getValue();
}
OF_EX_Latch.setInstruction(instr); OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm); OF_EX_Latch.setImm(imm);
OF_EX_Latch.setOp1(op1); OF_EX_Latch.setOp1(op1);
OF_EX_Latch.setOp2(op2); OF_EX_Latch.setOp2(op2);
System.out.println("op1: " + op1);
System.out.println("op2: " + rd);
instr.setDestinationOperand(rd); instr.setDestinationOperand(rd);
instr.setSourceOperand1(rs1); instr.setSourceOperand1(rs1);
} }
// if(opcode == 22){
// }
}
else if (Arrays.stream(R1I_type_operators).anyMatch(x -> x == opcode)) { else if (Arrays.stream(R1I_type_operators).anyMatch(x -> x == opcode)) {
if(opcode != 24){ if(opcode != 24){
Operand rd = new Operand(); Operand rd = new Operand();
@ -125,11 +169,16 @@ public class OperandFetch {
imm = -1*twoscompliment(bin_instr.substring(10, 32)); imm = -1*twoscompliment(bin_instr.substring(10, 32));
System.out.println(bin_instr); System.out.println(bin_instr);
} }
System.out.println("imm: " + imm); // if (checkdatahazard(new int[] { rd.getValue() })) {
// noDataHazard = false;
// }else{
containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter()-1);
OF_EX_Latch.setInstruction(instr); OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm); OF_EX_Latch.setImm(imm);
isEnd = true;
// }
} }
else{ else{ // opcode == 24 jmp
Operand op = new Operand(); Operand op = new Operand();
String imm = bin_instr.substring(10, 32); String imm = bin_instr.substring(10, 32);
int imm_val = Integer.parseInt(imm, 2); int imm_val = Integer.parseInt(imm, 2);
@ -146,16 +195,26 @@ public class OperandFetch {
op.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2)); op.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
instr.setSourceOperand1(op); instr.setSourceOperand1(op);
} }
if (checkdatahazard(new int[] { op.getValue() })) {
noDataHazard = false;
}else{
OF_EX_Latch.setInstruction(instr); OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm_val); OF_EX_Latch.setImm(imm_val);
} }
} }
}
OF_EX_Latch.setEX_enable(true); OF_EX_Latch.setEX_enable(noDataHazard);
if(!noDataHazard){
IF_EnableLatch.setFreeze(true);
System.out.println("\n\nData Hazard - Interlock\n\n");
}
} }
else if (!Proceed) { else if (!Proceed) {
Proceed = true; Proceed = true;
System.out.println("\n\nControl Hazard - Interlock\n\n");
} }
updateQueue(addtoqueue);
} }
public void setProceed(boolean proceed) { public void setProceed(boolean proceed) {

View File

@ -24,7 +24,7 @@ public class RegisterWrite {
Instruction instruction = MA_RW_Latch.getInstruction(); Instruction instruction = MA_RW_Latch.getInstruction();
OperationType op_type = instruction.getOperationType(); OperationType op_type = instruction.getOperationType();
int alu_result = MA_RW_Latch.getALU_result(); int alu_result = MA_RW_Latch.getALU_result();
boolean proceed = true;
if (op_type==OperationType.load) if (op_type==OperationType.load)
{ {
int load_result = MA_RW_Latch.getLoad_result(); int load_result = MA_RW_Latch.getLoad_result();
@ -34,6 +34,7 @@ public class RegisterWrite {
else if (op_type==OperationType.end) else if (op_type==OperationType.end)
{ {
Simulator.setSimulationComplete(true); Simulator.setSimulationComplete(true);
proceed = false;
} }
else else
{ {
@ -44,8 +45,19 @@ public class RegisterWrite {
containingProcessor.getRegisterFile().setValue(rd, alu_result); containingProcessor.getRegisterFile().setValue(rd, alu_result);
} }
} }
IF_EnableLatch.setIF_enable(proceed);
}else{
try{
if(MA_RW_Latch.getInstruction().getOperationType() == OperationType.end){
IF_EnableLatch.setIF_enable(false);
}
else{
IF_EnableLatch.setIF_enable(true); IF_EnableLatch.setIF_enable(true);
} }
} catch(Exception e){
IF_EnableLatch.setIF_enable(true);
}
}
} }
} }