Create the complete code for a MIPS processor simulator based on the following specifications from a computer architecture assignment. The simulator should be able to:
Task 1: MIPS Compiler
Implement a MIPS compiler that reads MIPS assembly instructions and converts them into binary machine code (hardcoded).
The compiler should handle .data and .text sections, supporting memory allocation, and around 10 MIPS instructions including:
Read MIPS assembly instructions from a text file.
Parse each line of assembly code, identifying the type of instruction (R-type, I-type, J-type) and extracting registers and values.
Convert each instruction into the corresponding binary machine code (opcode, funct, registers, immediate values, etc.).
Output the binary code in a text file.
Example: For the assembly instruction add t1,t2, $t3, the compiler should output:
Simulate the execution of the binary MIPS instructions using a simulated processor that follows the standard MIPS datapath.
Simulation pipeline:
Instruction Fetch: Retrieve the next instruction from memory.
Instruction Decode: Decode the instruction and identify control signals.
Execution: Perform ALU operations (addition, subtraction, etc.).
Memory Access: For lw and sw, simulate memory read/write operations, including offset handling.
Write Back: Write the result back to the register file.
Requirements:
Create 32 MIPS registers and simulate memory (hardcoded).
Implement ALU operations like addition and subtraction.
Generate and apply control signals during the instruction cycle.
Simulate proper program counter (PC) updates, including branching behavior for beq.
Ensure proper reads and writes to registers and memory.
All components, including registers, ALU, control unit, and memory, should be hardcoded and modular.
Handle at least the following instructions:
R-type: add, sub, and, or, slt
I-type: lw, beq, addi
J-type: j
Task 3: Testing
Test the simulator with 5 MIPS programs in total, including 2 provided programs and 3 additional custom programs.
Analyze the results of these programs by printing the final state of the registers after execution.
Ensure that the programs are sufficiently complex to test the robustness of the simulator.
Additional Requirements:
Assistant
The program should be written in either C, C++, or Python.
No external libraries should be used for register management, memory simulation, or instruction parsing—everything should be hardcoded.
Ensure proper formatting and modularity of code (e.g., separate functions for instruction decoding, execution, memory access, etc.).
Report Generation:
After executing the MIPS programs, generate a report including:
A brief description of the MIPS architecture simulated.
Challenges faced during implementation.
Detailed working of the simulator.
Results of the MIPS programs with explanations.
Contributions of each group member.
Ensure that the MIPS simulator is capable of handling the entire instruction pipeline, managing 32 registers, and properly simulating memory and control signals, while adhering to the MIPS instruction format. The simulator should output the state of registers and memory after executing each instruction."
Certainly! Below is a complete Python implementation of a MIPS processor simulator based on your specifications. The simulator is divided into three main parts:
MIPS Compiler: Reads MIPS assembly code, parses it, and converts it into binary machine code.
MIPS Simulator: Executes the binary machine code, simulating the MIPS pipeline and updating registers and memory accordingly.
Testing: Includes sample MIPS programs to test the compiler and simulator.
REGISTER_MAP: A dictionary mapping MIPS register names to their 5-bit binary representations.
OPCODE_MAP: A dictionary mapping instruction mnemonics to their opcode and function codes.
2. MIPSCompiler Class
Initialization: Takes the assembly file as input and initializes necessary variables like labels, data, and text.
first_pass Method:
Reads the assembly lines.
Processes the .data section to handle data declarations and labels.
Processes the .text section to handle instructions and labels.
second_pass Method:
Converts each instruction into its binary representation.
parse_instruction Method:
Parses individual instructions based on their type (R, I, J).
Converts registers and immediate values into their binary forms.
Handles labels for branching and jumping.
compile Method: Executes the first and second passes.
write_binary Method: Writes the binary instructions to an output file.
3. MIPSSimulator Class
Initialization: Takes the binary file as input and initializes registers, memory, and the Program Counter (PC).
load_instructions Method: Loads binary instructions from the binary file into the simulator's instruction memory.
fetch Method: Fetches the current instruction based on the PC.
decode Method: Decodes the fetched binary instruction into its components.
execute Method: Executes the decoded instruction, performing ALU operations, memory access, or PC updates.
simulate Method: Runs the simulation loop, fetching, decoding, and executing instructions until the PC points outside the instruction memory or an execution limit is reached.
print_registers Method: Prints the final state of all registers after execution.
4. main Function
Handles command-line arguments to either compile an assembly file or simulate a binary file.
Labels Handling: The compiler handles labels for both the .data and .text sections, enabling proper addressing for beq and j instructions.
Immediate Values: Supports both positive and negative immediate values through sign extension.
Memory Simulation: The simulator uses a dictionary to simulate memory addresses and their contents.
Execution Limit: To prevent infinite loops, the simulator stops after executing 1000 instructions.
Testing
To thoroughly test the simulator, create multiple assembly files covering different instructions and scenarios, including:
Arithmetic Operations: Test add, sub, and, or, slt.
Memory Access: Test lw and sw with different addresses and offsets.
Branching and Jumping: Test beq and j with various labels and conditions.
Edge Cases: Test with zero registers, maximum and minimum immediate values, and undefined labels.
Ensure that after each simulation, the final state of the registers matches the expected outcome based on the instructions executed.
Conclusion
This Python implementation provides a foundational MIPS processor simulator capable of compiling assembly code into binary machine code and simulating its execution. While it handles the specified instructions and features, further enhancements can be made to support more instructions, better error handling, and a more detailed simulation of the MIPS pipeline.