11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380 | class Simulator:
def __init__(self):
self.registers = Registers()
self.memory = Memory()
self.assembler = Assembler()
self.current_pc = 0
self.program_loaded = False
self.program_length = 0
self.debug_mode = False
self.execution_history = []
def load(self, program, translation_option='binary'):
"""Load and assemble program from string"""
# Returns: Translated binary code as default
self.registers.reset()
self.memory.reset()
self.current_pc = 0
self.registers.pc = 0
self.execution_history.clear()
machine_code = self.assembler.assemble(program)
for i, instruction in enumerate(machine_code):
self.memory.load_instruction(i, instruction)
self.program_length = len(machine_code)
self.program_loaded = True
translations = self.assembler.get_translations(translation_option)
result = '\n'.join(translations)
return result
def load_from_file(self, filename):
"""Load and assemble program from file"""
with open(filename, 'r') as f:
program = f.read()
return self.load(program)
def step(self):
"""Execute single instruction and return detailed state"""
# Returns: Status KV pair from the step executed
if not self.program_loaded:
return {
'status': 'error',
'message': 'No program loaded',
'state': self.get_state()
}
if self.registers.pc >= self.program_length * 4:
return {
'status': 'completed',
'message': 'Program completed - reached end of instructions',
'state': self.get_state()
}
instruction = self.memory.get_instruction(self.registers.pc)
if instruction == 0:
return {
'status': 'halted',
'message': 'Program halted - reached null instruction',
'state': self.get_state()
}
# Record pre-execution state
old_pc = self.registers.pc
pre_state = self.get_register_state()
# Execute instruction
continue_execution = self.execute_instruction(instruction)
# Record execution in history
instruction_info = self._get_instruction_info(instruction)
execution_record = {
'pc': old_pc,
'instruction': f"0x{instruction:08x}",
'instruction_info': instruction_info,
'pre_state': pre_state,
'post_state': self.get_register_state()
}
self.execution_history.append(execution_record)
# Update PC if needed
if continue_execution:
self.registers.pc += 4
self.current_pc = self.registers.pc
result = {
'status': 'running',
'pc': self.registers.pc,
'previous_pc': old_pc,
'instruction': f"0x{instruction:08x}",
'instruction_info': instruction_info,
'state': self.get_state(),
'message': 'Instruction executed successfully'
}
if self.debug_mode:
self._print_step_debug(result)
return result
def run(self):
"""Run the program with detailed execution tracking"""
# Returns: Status KV pair from the whole program
if not self.program_loaded:
return {
'status': 'error',
'message': 'No program loaded',
'execution_history': self.execution_history
}
print("\nStarting program execution...")
instruction_count = 0
max_iterations = 1000000 # Prevent infinite loops
execution_results = []
while instruction_count < max_iterations:
step_result = self.step()
execution_results.append(step_result)
if step_result['status'] in ['completed', 'halted', 'error']:
break
instruction_count += 1
final_result = {
'status': 'completed',
'instructions_executed': instruction_count,
'execution_results': execution_results,
'final_state': self.get_state(),
'message': f"Program execution completed with {instruction_count} instructions"
}
if instruction_count >= max_iterations:
final_result.update({
'status': 'error',
'message': 'Program terminated - reached maximum instruction limit'
})
if self.debug_mode:
self._print_run_debug(final_result)
return final_result
def get_state(self):
"""Get current simulator state"""
return {
'pc': self.registers.pc,
'registers': self.get_register_state(),
'memory': self.get_memory_state(),
'history': self.execution_history,
'reg_labels': self.get_register_state(label=True)
}
def get_register_state(self, label=False):
"""Get register contents with sign extension using register names as keys"""
register_state = {}
for i in range(32):
value = self.registers.read_register(i)
# Convert 2's complement to signed
if value & 0x80000000: # If negative (MSB is 1)
value = -((~value + 1) & 0xFFFFFFFF)
if label:
register_state[self.registers.get_register_name(i)] = value
else:
register_state[i] = value
return register_state
def get_memory_state(self):
"""Get memory contents"""
return {
'instructions': self.memory.read_instruction_memory(),
'data': self.memory.read_data_memory()
}
def execute_r_type(self, instruction, rs, rt, rd, shamt, funct):
"""Handle R-type instructions"""
rs_val = self.registers.read_register(rs)
rt_val = self.registers.read_register(rt)
if funct == 0x20: # add
result = (rs_val + rt_val) & 0xFFFFFFFF # Add truncation
self.registers.write_register(rd, result)
elif funct == 0x22: # sub
result = (rs_val - rt_val) & 0xFFFFFFFF # Add truncation
self.registers.write_register(rd, result)
elif funct == 0x24: # and
result = rs_val & rt_val
self.registers.write_register(rd, result)
elif funct == 0x25: # or
result = rs_val | rt_val
self.registers.write_register(rd, result)
elif funct == 0x2A: # slt
# Sign comparison for SLT
rs_signed = rs_val if rs_val < 0x80000000 else rs_val - 0x100000000
rt_signed = rt_val if rt_val < 0x80000000 else rt_val - 0x100000000
result = 1 if rs_signed < rt_signed else 0
self.registers.write_register(rd, result)
elif funct == 0x00: # sll
result = (rt_val << shamt) & 0xFFFFFFFF
self.registers.write_register(rd, result)
elif funct == 0x02: # srl
result = (rt_val >> shamt) & 0xFFFFFFFF
self.registers.write_register(rd, result)
elif funct == 0x08: # jr
self.current_pc = rs_val
self.registers.pc = self.current_pc
return False
return True
def execute_i_type(self, op, rs, rt, imm):
"""Handle I-type instructions"""
rs_val = self.registers.read_register(rs)
# Sign extend immediate value
if imm & 0x8000:
imm |= 0xFFFF0000
if op == 0x08: # addi
result = (rs_val + imm) & 0xFFFFFFFF
self.registers.write_register(rt, result)
elif op == 0x23: # lw
addr = (rs_val + imm) & 0xFFFFFFFF
value = self.memory.read_word(addr)
self.registers.write_register(rt, value)
elif op == 0x2B: # sw
addr = (rs_val + imm) & 0xFFFFFFFF
rt_val = self.registers.read_register(rt)
self.memory.write_word(addr, rt_val)
elif op == 0x04: # beq
rt_val = self.registers.read_register(rt)
if rs_val == rt_val:
target = self.registers.pc + 4 + (imm << 2)
self.current_pc = target
self.registers.pc = target
return False
elif op == 0x05: # bne
rt_val = self.registers.read_register(rt)
if rs_val != rt_val:
target = self.registers.pc + 4 + (imm << 2)
self.current_pc = target
self.registers.pc = target
return False
return True
def execute_j_type(self, op, address):
"""Handle J-type instructions"""
if op == 0x02: # j
target = (self.registers.pc & 0xF0000000) | (address << 2)
self.current_pc = target
self.registers.pc = target
return False
elif op == 0x03: # jal
target = (self.registers.pc & 0xF0000000) | (address << 2)
self.registers.write_register(31, self.registers.pc + 4) # Store return address
self.current_pc = target
self.registers.pc = target
return False
return True
def execute_instruction(self, instruction):
"""Execute a single instruction"""
# Extract operation fields
op = (instruction >> 26) & 0x3F
rs = (instruction >> 21) & 0x1F
rt = (instruction >> 16) & 0x1F
rd = (instruction >> 11) & 0x1F
shamt = (instruction >> 6) & 0x1F
funct = instruction & 0x3F
imm = instruction & 0xFFFF
address = instruction & 0x3FFFFFF
# Handle instruction based on type
if op == 0: # R-type
return self.execute_r_type(instruction, rs, rt, rd, shamt, funct)
elif op in [0x02, 0x03]: # J-type
return self.execute_j_type(op, address)
else: # I-type
return self.execute_i_type(op, rs, rt, imm)
def _get_instruction_info(self, instruction):
"""Decode and return instruction information"""
op = (instruction >> 26) & 0x3F
rs = (instruction >> 21) & 0x1F
rt = (instruction >> 16) & 0x1F
rd = (instruction >> 11) & 0x1F
shamt = (instruction >> 6) & 0x1F
funct = instruction & 0x3F
imm = instruction & 0xFFFF
address = instruction & 0x3FFFFFF
return {
'opcode': f"0x{op:02x}",
'type': 'R-type' if op == 0 else 'J-type' if op in [0x02, 0x03] else 'I-type',
'fields': {
'rs': f"${rs} ({self.registers.get_register_name(rs)})",
'rt': f"${rt} ({self.registers.get_register_name(rt)})",
'rd': f"${rd} ({self.registers.get_register_name(rd)})",
'shamt': shamt,
'funct': f"0x{funct:02x}",
'immediate': f"0x{imm:04x}",
'address': f"0x{address:07x}"
}
}
def _print_step_debug(self, result):
"""Print debug information for step execution"""
print("\nInstruction Execution Details:")
print(f"PC: 0x{result['pc']:08x} (Previous: 0x{result['previous_pc']:08x})")
print(f"Instruction: {result['instruction']}")
print("\nInstruction Info:")
info = result['instruction_info']
print(f"Type: {info['type']}")
print(f"Opcode: {info['opcode']}")
print("\nFields:")
for field, value in info['fields'].items():
print(f" {field}: {value}")
if self.debug_mode == 2: # More detailed debug level
print("\nRegister Changes:")
pre_state = result['state']['history'][-1]['pre_state']
post_state = result['state']['history'][-1]['post_state']
for reg in range(32):
if pre_state[reg] != post_state[reg]:
reg_name = self.registers.get_register_name(reg)
print(f" ${reg} ({reg_name}): 0x{pre_state[reg]:08x} -> 0x{post_state[reg]:08x}")
def _print_run_debug(self, result):
"""Print debug information for complete program execution"""
print("\nProgram Execution Summary:")
print(f"Status: {result['status']}")
print(f"Instructions Executed: {result['instructions_executed']}")
print(f"Message: {result['message']}")
if self.debug_mode == 2: # More detailed debug level
print("\nFinal Register State:")
for reg in range(32):
reg_name = self.registers.get_register_name(reg)
value = result['final_state']['registers'][reg]
print(f" ${reg} ({reg_name}): 0x{value:08x}")
print("\nNon-Zero Memory Contents:")
for addr, value in result['final_state']['memory']['data'].items():
if value != 0:
print(f" [0x{int(addr):08x}]: 0x{value:08x}")
def _print_info(self):
"""Print the current state of registers and memory"""
reg_state = self.get_register_state()
print("\nRegister Contents:")
for reg_num, value in reg_state.items():
print(f"{self.registers.get_register_name(reg_num)}: {value}")
print("\nInstruction Memory Contents:")
for i, instruction in enumerate(self.memory.instruction_memory):
if instruction != 0: # Only print non-zero instructions
print(f"[0x{i*4:04x}] 0x{instruction:08x}")
print("\nData Memory Contents:")
for i, data in enumerate(self.memory.data_memory):
if data != 0: # Only print non-zero data
print(f"{i}[0x{i*4:04x}] 0x{data:08x}")
|