OOO-Processor-Simulator / src / processor.py
processor.py
Raw
from datastructures import *
from stages import *
import json

output_dict=[]

class Processor:
    def __init__(self, instructions):
        self.instructions = []
        instructions
        for i in range(len(instructions)):
            instruction = Instruction.parse(instructions[i], i)
            self.instructions.append(instruction)
        self.busy_bit_table = BusyBitTable(64)
        self.decoded_pcs = DecodedPCs()
        self.exception_flag = ExceptionFlag()
        self.exception_pc = ExceptionPC()
        self.free_list = FreeList()
        self.integer_queue = IntegerQueue()
        self.program_counter = ProgramCounter()
        self.physical_register_file = PhysicalRegisterFile(64)
        self.register_map_table = RegisterMapTable()
        self.active_list = ActiveList()
        self.forwarding_paths = [None] * 64
        self.fetch_and_decode = FetchAndDecode(self.decoded_pcs, self.program_counter, self.instructions, self.exception_flag)
        self.rename_dispatch = RenameDispatch(self.physical_register_file, self.register_map_table,
                                                 self.active_list, self.integer_queue, self.free_list,
                                                  self.busy_bit_table, self.forwarding_paths, self.instructions,
                                                   self.decoded_pcs)
        self.issue = Issue(self.integer_queue, self.forwarding_paths)
        self.execution = Execution(self.physical_register_file, self.active_list,
                                     self.integer_queue, self.busy_bit_table, self.forwarding_paths, self.register_map_table)
        self.commit = Commit(self.active_list, self.physical_register_file, self.register_map_table,
                             self.free_list, self.exception_flag, self.exception_pc, self.forwarding_paths,
                              self.busy_bit_table, self.integer_queue, self.program_counter)



    def run_simulation(self, output_file):
        issued_instructions0 = []
        issued_instructions1 = []
        output_dict=[]
        count= -1
        num1=-1
        output_dict.append(self.write_output())
        while (True):
            count+=1
            #if count > 1000:
             #   break
            # Execute Commit stage
            if count >= 5:
                committed_pc = self.commit.execute(num1)
            if count >= 4:
                # Execute Execution stage
                num1 = self.execution.execute_cycle2()
            if count >= 3:
                # Execute Execution stage
                issued_instructions1 = issued_instructions0
                self.execution.execute_cycle1(issued_instructions1)
            if count >= 2:
                # Execute Issue stage
                
                issued_instructions0 = self.issue.execute()
            if count >= 1:
                # Execute Rename and Dispatch stage
                self.rename_dispatch.execute()  
                if self.rename_dispatch.backpressure:
                    continue
            self.fetch_and_decode.fetch_and_decode()

            if self.active_list.is_empty() and self.exception_flag.is_set():
                output_dict.append(self.write_output())
                self.commit.quit_exception_mode()
                self.execution.reset_execution_stage()
                
                break
            if self.decoded_pcs.get_data() == [] and self.active_list.is_empty():
                break
            
            output_dict.append(self.write_output()) 
        output_dict.append(self.write_output())
        with open(output_file, 'a') as f:
            json.dump(output_dict, f, indent=2)

    def write_output(self):
        output_data = {
            "ActiveList": [entry.to_dict() for entry in self.active_list.entries].copy(),
            "BusyBitTable": copy.copy(self.busy_bit_table.get_data()),
            "DecodedPCs": self.decoded_pcs.get_data(),
            "Exception": self.exception_flag.get_data(),
            "ExceptionPC": self.exception_pc.get_data(),
            "FreeList": self.free_list.get_data().copy(),
            "IntegerQueue": [entry.to_dict() for entry in self.integer_queue.entries].copy(),
            "PC": self.program_counter.get_data(),
            "PhysicalRegisterFile": self.physical_register_file.get_data().copy(),
            "RegisterMapTable": self.register_map_table.get_data().copy()
        }
        return (output_data.copy())