diff --git a/.vscode/launch.json b/.vscode/launch.json index f82083f..8667d0a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "name": "Launch Current File", "request": "launch", "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", diff --git a/assignment-4/src/hello.txt b/assignment-4/src/hello.txt index 8fcd074..f9829fd 100644 --- a/assignment-4/src/hello.txt +++ b/assignment-4/src/hello.txt @@ -1,4 +1,4 @@ -Number of instructions executed = 5 -Number of cycles taken = 14 -Number of data hazards = 3 -Number of control hazards = 2 +Number of instructions executed = 6 +Number of cycles taken = 16 +Number of data hazards = 6 +Number of control hazards = 0 diff --git a/assignment-4/src/processor/pipeline/OperandFetch.java b/assignment-4/src/processor/pipeline/OperandFetch.java index 175fa60..6dc8305 100644 --- a/assignment-4/src/processor/pipeline/OperandFetch.java +++ b/assignment-4/src/processor/pipeline/OperandFetch.java @@ -17,7 +17,7 @@ public class OperandFetch { OF_EX_LatchType OF_EX_Latch; IF_EnableLatchType IF_EnableLatch; static OperationType[] opTypes = OperationType.values(); - boolean Proceed; + public boolean Proceed; Queue queue; boolean isEnd; @@ -197,7 +197,7 @@ public class OperandFetch { } } else if (!Proceed) { - Proceed = true; + // Proceed = true; System.out.println("\n\nControl Hazard - Interlock\n\n"); } updateQueue(addtoqueue); diff --git a/assignment-5/assignment4.pdf b/assignment-5/assignment4.pdf deleted file mode 100644 index adeff03..0000000 Binary files a/assignment-5/assignment4.pdf and /dev/null differ diff --git a/assignment-5/assignment5-1.pdf b/assignment-5/assignment5-1.pdf new file mode 100644 index 0000000..1f7ad85 Binary files /dev/null and b/assignment-5/assignment5-1.pdf differ diff --git a/assignment-5/bin/hello.txt b/assignment-5/bin/hello.txt index c7dc2e4..9fcef70 100644 --- a/assignment-5/bin/hello.txt +++ b/assignment-5/bin/hello.txt @@ -1,4 +1,4 @@ -Number of instructions executed = 421 -Number of cycles taken = 992 -Number of data hazards = 441 -Number of control hazards = 63 +Number of instructions executed = 7609 +Number of cycles taken = 11172 +Number of data hazards = 0 +Number of control hazards = 176 diff --git a/assignment-5/src/generic/Simulator.java b/assignment-5/src/generic/Simulator.java index d420906..d19980e 100644 --- a/assignment-5/src/generic/Simulator.java +++ b/assignment-5/src/generic/Simulator.java @@ -69,6 +69,7 @@ public class Simulator { processor.getRWUnit().performRW(); processor.getMAUnit().performMA(); processor.getEXUnit().performEX(); + eventQueue.processEvents(); processor.getOFUnit().performOF(); processor.getIFUnit().performIF(); Clock.incrementClock(); diff --git a/assignment-5/src/hello.txt b/assignment-5/src/hello.txt index 8fcd074..db49d84 100644 --- a/assignment-5/src/hello.txt +++ b/assignment-5/src/hello.txt @@ -1,4 +1,4 @@ -Number of instructions executed = 5 -Number of cycles taken = 14 -Number of data hazards = 3 -Number of control hazards = 2 +Number of instructions executed = 926 +Number of cycles taken = 1169 +Number of data hazards = 0 +Number of control hazards = 10 diff --git a/assignment-5/src/processor/memorysystem/MainMemory.java b/assignment-5/src/processor/memorysystem/MainMemory.java index 94b7bd4..3954edb 100644 --- a/assignment-5/src/processor/memorysystem/MainMemory.java +++ b/assignment-5/src/processor/memorysystem/MainMemory.java @@ -1,6 +1,15 @@ 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; public MainMemory() @@ -32,4 +41,37 @@ public class MainMemory { sb.append("\n"); 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()) + ); + } + } + + } diff --git a/assignment-5/src/processor/pipeline/Execute.java b/assignment-5/src/processor/pipeline/Execute.java index b0b7365..269f124 100644 --- a/assignment-5/src/processor/pipeline/Execute.java +++ b/assignment-5/src/processor/pipeline/Execute.java @@ -160,7 +160,7 @@ public class Execute { break; case end: { - containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter()-1); + // containingProcessor.getRegisterFile().setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter()-1); containingProcessor.getOFUnit().setisEnd(true); break; } diff --git a/assignment-5/src/processor/pipeline/IF_OF_LatchType.java b/assignment-5/src/processor/pipeline/IF_OF_LatchType.java index e3fb477..240b5f4 100644 --- a/assignment-5/src/processor/pipeline/IF_OF_LatchType.java +++ b/assignment-5/src/processor/pipeline/IF_OF_LatchType.java @@ -4,6 +4,8 @@ public class IF_OF_LatchType { boolean OF_enable; int instruction; + boolean IF_busy=false; + boolean IF_branching_busy=false; public IF_OF_LatchType() { @@ -26,4 +28,19 @@ public class IF_OF_LatchType { 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; + } } diff --git a/assignment-5/src/processor/pipeline/InstructionFetch.java b/assignment-5/src/processor/pipeline/InstructionFetch.java index 253bb4c..03ac60d 100644 --- a/assignment-5/src/processor/pipeline/InstructionFetch.java +++ b/assignment-5/src/processor/pipeline/InstructionFetch.java @@ -1,8 +1,15 @@ 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; -public class InstructionFetch { +public class InstructionFetch implements Element{ Processor containingProcessor; IF_EnableLatchType IF_EnableLatch; @@ -18,27 +25,65 @@ public class InstructionFetch { } public void performIF() - { if(!IF_EnableLatch.isFreeze()){ - if(EX_IF_Latch.isIF_enable()){ - containingProcessor.getRegisterFile().setProgramCounter(EX_IF_Latch.getPC()-1); + { + if(EX_IF_Latch.isIF_enable() && !IF_OF_Latch.isIF_branching_busy()){ + 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); IF_OF_Latch.setOF_enable(false); 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) - 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 newInstruction = containingProcessor.getMainMemory().getWord(currentPC); - IF_OF_Latch.setInstruction(newInstruction); - containingProcessor.getRegisterFile().setProgramCounter(currentPC + 1); + // int newInstruction = containingProcessor.getMainMemory().getWord(currentPC); + // IF_OF_Latch.setInstruction(newInstruction); + // containingProcessor.getRegisterFile().setProgramCounter(currentPC + 1); - IF_EnableLatch.setIF_enable(true); - IF_OF_Latch.setOF_enable(true); + 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); + } - }else{ + } + + @Override + public void handleEvent(Event e) { + if(IF_OF_Latch.isIF_branching_busy()){ 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); } } diff --git a/assignment-5/src/processor/pipeline/OperandFetch.java b/assignment-5/src/processor/pipeline/OperandFetch.java index 175fa60..6dc8305 100644 --- a/assignment-5/src/processor/pipeline/OperandFetch.java +++ b/assignment-5/src/processor/pipeline/OperandFetch.java @@ -17,7 +17,7 @@ public class OperandFetch { OF_EX_LatchType OF_EX_Latch; IF_EnableLatchType IF_EnableLatch; static OperationType[] opTypes = OperationType.values(); - boolean Proceed; + public boolean Proceed; Queue queue; boolean isEnd; @@ -197,7 +197,7 @@ public class OperandFetch { } } else if (!Proceed) { - Proceed = true; + // Proceed = true; System.out.println("\n\nControl Hazard - Interlock\n\n"); } updateQueue(addtoqueue);