added discrete event simulator for instruction fetch
This commit is contained in:
parent
a1213b7e7f
commit
42f0925c13
|
@ -9,7 +9,7 @@
|
||||||
"name": "Launch Current File",
|
"name": "Launch Current File",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "${file}",
|
"mainClass": "${file}",
|
||||||
"args": ["assignment-1/src/descending.asm", "descending.out"]
|
"args": ["assignment-5/src/configuration/config.xml", "assignment-5/src/hello.txt", "assignment-5/supporting_files/test_cases/descending.out"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "java",
|
"type": "java",
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Number of instructions executed = 5
|
Number of instructions executed = 6
|
||||||
Number of cycles taken = 14
|
Number of cycles taken = 16
|
||||||
Number of data hazards = 3
|
Number of data hazards = 6
|
||||||
Number of control hazards = 2
|
Number of control hazards = 0
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class OperandFetch {
|
||||||
OF_EX_LatchType OF_EX_Latch;
|
OF_EX_LatchType OF_EX_Latch;
|
||||||
IF_EnableLatchType IF_EnableLatch;
|
IF_EnableLatchType IF_EnableLatch;
|
||||||
static OperationType[] opTypes = OperationType.values();
|
static OperationType[] opTypes = OperationType.values();
|
||||||
boolean Proceed;
|
public boolean Proceed;
|
||||||
Queue<Integer> queue;
|
Queue<Integer> queue;
|
||||||
boolean isEnd;
|
boolean isEnd;
|
||||||
|
|
||||||
|
@ -197,7 +197,7 @@ public class OperandFetch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!Proceed) {
|
else if (!Proceed) {
|
||||||
Proceed = true;
|
// Proceed = true;
|
||||||
System.out.println("\n\nControl Hazard - Interlock\n\n");
|
System.out.println("\n\nControl Hazard - Interlock\n\n");
|
||||||
}
|
}
|
||||||
updateQueue(addtoqueue);
|
updateQueue(addtoqueue);
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,4 +1,4 @@
|
||||||
Number of instructions executed = 421
|
Number of instructions executed = 7609
|
||||||
Number of cycles taken = 992
|
Number of cycles taken = 11172
|
||||||
Number of data hazards = 441
|
Number of data hazards = 0
|
||||||
Number of control hazards = 63
|
Number of control hazards = 176
|
||||||
|
|
|
@ -69,6 +69,7 @@ public class Simulator {
|
||||||
processor.getRWUnit().performRW();
|
processor.getRWUnit().performRW();
|
||||||
processor.getMAUnit().performMA();
|
processor.getMAUnit().performMA();
|
||||||
processor.getEXUnit().performEX();
|
processor.getEXUnit().performEX();
|
||||||
|
eventQueue.processEvents();
|
||||||
processor.getOFUnit().performOF();
|
processor.getOFUnit().performOF();
|
||||||
processor.getIFUnit().performIF();
|
processor.getIFUnit().performIF();
|
||||||
Clock.incrementClock();
|
Clock.incrementClock();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Number of instructions executed = 5
|
Number of instructions executed = 926
|
||||||
Number of cycles taken = 14
|
Number of cycles taken = 1169
|
||||||
Number of data hazards = 3
|
Number of data hazards = 0
|
||||||
Number of control hazards = 2
|
Number of control hazards = 10
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
package processor.memorysystem;
|
package processor.memorysystem;
|
||||||
|
|
||||||
public class MainMemory {
|
import generic.Element;
|
||||||
|
import generic.Event;
|
||||||
|
import generic.ExecutionCompleteEvent;
|
||||||
|
import generic.MemoryReadEvent;
|
||||||
|
import generic.MemoryResponseEvent;
|
||||||
|
import generic.MemoryWriteEvent;
|
||||||
|
import generic.Simulator;
|
||||||
|
import processor.Clock;
|
||||||
|
|
||||||
|
public class MainMemory implements Element{
|
||||||
int[] memory;
|
int[] memory;
|
||||||
|
|
||||||
public MainMemory()
|
public MainMemory()
|
||||||
|
@ -32,4 +41,37 @@ public class MainMemory {
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleEvent(Event e) {
|
||||||
|
if (e.getEventType() == Event.EventType.MemoryRead) {
|
||||||
|
System.out.println("Memory Read Event Happening");
|
||||||
|
MemoryReadEvent event = (MemoryReadEvent) e ;
|
||||||
|
|
||||||
|
System.out.println(getWord(event.getAddressToReadFrom()));
|
||||||
|
Simulator.getEventQueue().addEvent(
|
||||||
|
new MemoryResponseEvent(
|
||||||
|
Clock.getCurrentTime(),
|
||||||
|
this,
|
||||||
|
event.getRequestingElement(),
|
||||||
|
getWord(event.getAddressToReadFrom())
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if(e.getEventType() == Event.EventType.MemoryWrite) {
|
||||||
|
System.out.println("Memory Write Event Happening");
|
||||||
|
MemoryWriteEvent event = (MemoryWriteEvent) e ;
|
||||||
|
System.out.println(getWord(event.getAddressToWriteTo()));
|
||||||
|
|
||||||
|
this.setWord(event.getAddressToWriteTo(), event.getValue());
|
||||||
|
Simulator.getEventQueue().addEvent(
|
||||||
|
new ExecutionCompleteEvent(
|
||||||
|
Clock.getCurrentTime(),
|
||||||
|
this,
|
||||||
|
event.getRequestingElement())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,7 +160,7 @@ public class Execute {
|
||||||
break;
|
break;
|
||||||
case end:
|
case end:
|
||||||
{
|
{
|
||||||
containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter()-1);
|
// containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter()-1);
|
||||||
containingProcessor.getOFUnit().setisEnd(true);
|
containingProcessor.getOFUnit().setisEnd(true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ public class IF_OF_LatchType {
|
||||||
|
|
||||||
boolean OF_enable;
|
boolean OF_enable;
|
||||||
int instruction;
|
int instruction;
|
||||||
|
boolean IF_busy=false;
|
||||||
|
boolean IF_branching_busy=false;
|
||||||
|
|
||||||
public IF_OF_LatchType()
|
public IF_OF_LatchType()
|
||||||
{
|
{
|
||||||
|
@ -26,4 +28,19 @@ public class IF_OF_LatchType {
|
||||||
this.instruction = instruction;
|
this.instruction = instruction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isIF_busy() {
|
||||||
|
return IF_busy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIF_busy(boolean iF_busy) {
|
||||||
|
IF_busy = iF_busy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIF_branching_busy() {
|
||||||
|
return IF_branching_busy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIF_branching_busy(boolean iF_branching_busy) {
|
||||||
|
IF_branching_busy = iF_branching_busy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
package processor.pipeline;
|
package processor.pipeline;
|
||||||
|
|
||||||
|
import configuration.Configuration;
|
||||||
|
import generic.Element;
|
||||||
|
import generic.Event;
|
||||||
|
import generic.MemoryReadEvent;
|
||||||
|
import generic.MemoryResponseEvent;
|
||||||
|
import generic.Simulator;
|
||||||
|
import processor.Clock;
|
||||||
import processor.Processor;
|
import processor.Processor;
|
||||||
|
|
||||||
public class InstructionFetch {
|
public class InstructionFetch implements Element{
|
||||||
|
|
||||||
Processor containingProcessor;
|
Processor containingProcessor;
|
||||||
IF_EnableLatchType IF_EnableLatch;
|
IF_EnableLatchType IF_EnableLatch;
|
||||||
|
@ -18,27 +25,65 @@ 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() && !IF_OF_Latch.isIF_branching_busy()){
|
||||||
containingProcessor.getRegisterFile().setProgramCounter(EX_IF_Latch.getPC()-1);
|
containingProcessor.getRegisterFile().setProgramCounter(EX_IF_Latch.getPC());
|
||||||
|
int currentPC = containingProcessor.getRegisterFile().getProgramCounter();
|
||||||
|
|
||||||
|
Simulator.getEventQueue().addEvent(
|
||||||
|
new MemoryReadEvent(
|
||||||
|
Clock.getCurrentTime()+Configuration.mainMemoryLatency,
|
||||||
|
this,
|
||||||
|
containingProcessor.getMainMemory(),
|
||||||
|
currentPC
|
||||||
|
)
|
||||||
|
);
|
||||||
|
IF_OF_Latch.setIF_branching_busy(true);
|
||||||
|
|
||||||
|
|
||||||
EX_IF_Latch.setIF_enable(false);
|
EX_IF_Latch.setIF_enable(false);
|
||||||
IF_OF_Latch.setOF_enable(false);
|
IF_OF_Latch.setOF_enable(false);
|
||||||
System.out.println("IF: PC set to " + EX_IF_Latch.getPC());
|
System.out.println("IF: PC set to " + EX_IF_Latch.getPC());
|
||||||
|
|
||||||
} // if EX_IF_Latch is enabled, set PC to EX_IF_Latch's PC and wait for next cycle (1 nop)
|
} // if EX_IF_Latch is enabled, set PC to EX_IF_Latch's PC and wait for next cycle (1 nop)
|
||||||
else if(IF_EnableLatch.isIF_enable() || EX_IF_Latch.isIF_enable())
|
else if(IF_EnableLatch.isIF_enable())
|
||||||
{
|
{
|
||||||
|
if(IF_OF_Latch.isIF_busy()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
int currentPC = containingProcessor.getRegisterFile().getProgramCounter();
|
int currentPC = containingProcessor.getRegisterFile().getProgramCounter();
|
||||||
int newInstruction = containingProcessor.getMainMemory().getWord(currentPC);
|
// int newInstruction = containingProcessor.getMainMemory().getWord(currentPC);
|
||||||
IF_OF_Latch.setInstruction(newInstruction);
|
// IF_OF_Latch.setInstruction(newInstruction);
|
||||||
containingProcessor.getRegisterFile().setProgramCounter(currentPC + 1);
|
// containingProcessor.getRegisterFile().setProgramCounter(currentPC + 1);
|
||||||
|
|
||||||
|
Simulator.getEventQueue().addEvent(
|
||||||
|
new MemoryReadEvent(
|
||||||
|
Clock.getCurrentTime()+ Configuration.mainMemoryLatency,
|
||||||
|
this,
|
||||||
|
containingProcessor.getMainMemory(),
|
||||||
|
currentPC
|
||||||
|
)
|
||||||
|
);
|
||||||
|
IF_OF_Latch.setIF_busy(true);
|
||||||
|
IF_OF_Latch.setOF_enable(false);
|
||||||
|
|
||||||
IF_EnableLatch.setIF_enable(true);
|
|
||||||
IF_OF_Latch.setOF_enable(true);
|
|
||||||
}
|
}
|
||||||
}else{
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleEvent(Event e) {
|
||||||
|
if(IF_OF_Latch.isIF_branching_busy()){
|
||||||
IF_EnableLatch.setFreeze(false);
|
IF_EnableLatch.setFreeze(false);
|
||||||
|
IF_OF_Latch.setIF_branching_busy(false);
|
||||||
|
containingProcessor.getOFUnit().Proceed = true;
|
||||||
|
System.out.println("IF: Unfreezing");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
MemoryResponseEvent event = (MemoryResponseEvent) e;
|
||||||
|
IF_OF_Latch.setInstruction(event.getValue());
|
||||||
|
containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter() + 1);
|
||||||
|
IF_OF_Latch.setOF_enable(true);
|
||||||
|
IF_OF_Latch.setIF_busy(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class OperandFetch {
|
||||||
OF_EX_LatchType OF_EX_Latch;
|
OF_EX_LatchType OF_EX_Latch;
|
||||||
IF_EnableLatchType IF_EnableLatch;
|
IF_EnableLatchType IF_EnableLatch;
|
||||||
static OperationType[] opTypes = OperationType.values();
|
static OperationType[] opTypes = OperationType.values();
|
||||||
boolean Proceed;
|
public boolean Proceed;
|
||||||
Queue<Integer> queue;
|
Queue<Integer> queue;
|
||||||
boolean isEnd;
|
boolean isEnd;
|
||||||
|
|
||||||
|
@ -197,7 +197,7 @@ public class OperandFetch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!Proceed) {
|
else if (!Proceed) {
|
||||||
Proceed = true;
|
// Proceed = true;
|
||||||
System.out.println("\n\nControl Hazard - Interlock\n\n");
|
System.out.println("\n\nControl Hazard - Interlock\n\n");
|
||||||
}
|
}
|
||||||
updateQueue(addtoqueue);
|
updateQueue(addtoqueue);
|
||||||
|
|
Loading…
Reference in New Issue