Question: Failed Tests 7 : Program simulates two instructions addi $ 3 , $ 3 , 8 and Iw $ 3 , 8 (

Failed Tests
7: Program simulates two instructions "addi $3,
$3,8" and "Iw $3,8($4)" correctly (0/10)
8: Program simulates two instructions "addi $2,
$2,16" and "sw $2,8($2)" correctly (0/10)
9: Program simulates two instructions "addi $2,
$2,16" then "beq $3,$4,20" correctly (0//10)
10: Program simulates a set of instruction "addi
$7, $7,5", "addi $3, $3,1", and "bne $7, $3,-8"
correctly (0/20)
Here is my code this is in python ```
import argparse
# Set up command-line argument parsing
parser = argparse.ArgumentParser(description="Simulate single-cycle MIPS datapath (beta version)")
parser.add_argument("--program", required=True, help="Path to machine-language input file")
parser.add_argument("--memory", required=True, help="Path to memory file")
args = parser.parse_args()
program_file_path = args.program
memory_file_path = args.memory
# File paths for output
control_output_file_path = 'out_control.txt'
registers_output_file_path = 'out_registers.txt'
memory_output_file_path = 'out_memory.txt'
# Initialize registers, program counter, and memory
registers =[0]*8
pc}=6553
memory =[]
# Load memory from file
with open(memory_file_path, 'r') as f:
memory =[int(line.strip()) for line in f.readlines()]
# Define control signals and ALU operations
CONTROL_SIGNALS ={
'add': '1001000100',
'sub': '1001000100',
'addi': '0101000000',
'lw': '0111100000',
'SW': 'X1X0010000',
'beq': 'X0X0001011',
'bne': 'x0x0001110'
}
ALU_OPCODES ={
'add': lambda x, y: x + y,
'sub': lambda x, y: x - y,
``````
'addi': lambda x, imm: x + imm
}
# Parse an instruction to get opcode and fields
def parse_instruction(instruction):
opcode = instruction[0:6]
rs = int(instruction[6:11],2)
rt = int(instruction[11:16],2)
rd = int(instruction[16:21],2)
immediate = int(instruction[16:],2) if instruction[16]=='0' else int(instruction[16:],2)-(116)
funct = instruction[26:]
return opcode, rs, rt, rd, immediate, funct
# Execute an instruction
def execute_instruction(instruction):
global pc, registers, memory
opcode, rs, rt, rd, immediate, funct = parse_instruction(instruction)
zero =0 # Zero flag for branch instructions
if opcode =='000000': # R-type instructions
if funct =='100000': # add
control_signals = CONTROL_SIGNALS['add']
registers[rd]= ALU_OPCODES['add'](registers[rs], registers[rt])
elif funct =='100010': # sub
control_signals = CONTROL_SIGNALS['sub']
registers[rd]= ALU_OPCODES['sub'](registers[rs], registers[rt])
else:
return None, None
elif opcode =='001000': # addi
control_signals = CONTROL_SIGNALS['addi']
registers[rt]= ALU_OPCODES['addi'](registers[rs], immediate)
elif opcode =='100011': # lw
control_signals = CONTROL_SIGNALS['lw']
address = registers[rs]+ immediate
if address <0 or address >= len(memory): # Validate address
``````
return control_signals, "segfault"
value = memory[address]
if value >=(1<<31): # Sign-extend 32-bit value
value -=(1<<32)
registers[rt]= value
elif opcode =='101011': # sw
control_signals = CONTROL_SIGNALS['sw']
address = registers[rs]+ immediate
if address <0 or address >= len(memory): # Validate address
return control_signals, "segfault"
memory[address]= registers[rt]
elif opcode =='000100': # beq
control_signals = CONTROL_SIGNALS['beq']
zero =1 if registers[rs]== registers[rt] else 0
branch_offset = immediate <<2 # Shift left by 2 for byte offset
if zero:
pc += branch_offset
else:
pc +=4
elif opcode =='000101': # bne
control_signals = CONTROL_SIGNALS['bne']
zero =1 if registers[rs]!= registers[rt] else 0
branch_offset = immediate <<2 # Shift left by 2 for byte offset
if zero:
pc += branch_offset
else:
pc +=4
else:
return None, None
pc +=4
return control_signals + str(zero), None
# Ensure control signal is exactly 10 bits
def pad control signal(signal):
``````
return signal[:10]
# Simulate instructions
def simulate(instructions):
instruction_memory =[(pc + i *4, line.strip()) for i, line in enumerate(instructions)]
with open(control_output_file_path, 'w') as control_file, \
open(registers_output_file_path, 'w') as registers_file:
registers_file.write(f"{pc}|{'|'.join(map(str, registers))}
")
for current_pc, instruction in instruction_memory:
if pc <65536 or pc >=65536+ len(instructions)*4:
control_file.write("!!! Segmentation Fault !!!
")
registers_file.write("!!! Segmentation Fault !!!
")
return
control_signals, fault = execute_instruction(instruction)
if fault == "segfault":
control_file.write("!!! Segmentation Fault !!!
")
registers_file.write("!!! Segmentation Fault !!!
")
return
control_signals = pad_control_signal(control_signals)
control_file.write(f"{control_signals}
")
registers_file.write(f"{pc}|{'|'.join(map(str, registers))}
")
with open(memory_output_file_path, 'w') as memory_file:
memory_file.writelines(f"{word}
" for word in memory)
# Load instructions from the program file
with open(program_file_path, 'r') as f:
instructions = f.readlines()
# Run the simulat Failed Tests
7: Program simulates two instructions "addi \$3,\(\$ 3,8\)" and "Iw \$3,8(\$4)" correctly (0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!