forked from piyush-nimbalkar/MIPS-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.py
83 lines (67 loc) · 2.37 KB
/
instruction.py
1
2
3
4
5
6
7
8
9
10
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
from config import *
class Instruction:
count = 0
def __init__(self, instr_name, operands):
self._full_name = instr_name + ' ' + ', '.join(operands)
self.name = instr_name
self._store_registers(operands)
self.address = Instruction.count * WORD_SIZE
Instruction.count += 1
def __str__(self):
return self._full_name
def type(self):
if self.name in ['LW', 'SW', 'L.D', 'S.D']:
return 'DATA'
elif self.name in ['J', 'BEQ', 'BNE']:
return 'BRANCH'
elif self.name == 'HLT':
return 'SPECIAL'
else:
return 'ALU'
def functional_unit(self):
if self.name in ['ADD.D', 'SUB.D']:
return 'FP_ADD'
elif self.name == 'MUL.D':
return 'FP_MUL'
elif self.name == 'DIV.D':
return 'FP_DIV'
elif self.name in ['J', 'BNE', 'BEQ', 'HLT']:
return 'NONE'
else:
return 'IU'
def _store_registers(self, operands_):
operands = self._extract_operands(filter(None, operands_))
self.dest_reg = ''
self.src_reg = []
self.immediate = ''
self.offset = ''
if self.name == 'HLT':
return
if self.name not in ['SW', 'S.D', 'J', 'BNE', 'BEQ']:
self.dest_reg = operands[0]
if self.name in ['LW', 'L.D']:
self.offset = operands[1]
self.src_reg = [operands[2]]
elif self.name in ['SW', 'S.D']:
self.offset = operands[1]
self.src_reg = [operands[0], operands[2]]
elif self.name in ['DADDI', 'DSUBI', 'ANDI', 'ORI']:
self.src_reg = [operands[1]]
self.immediate = operands[2]
elif self.name in ['BNE', 'BEQ']:
self.src_reg = operands[:2]
self.immediate = operands[2]
elif self.name in ['J']:
self.immediate = operands[0]
else:
self.src_reg = operands[1:]
def _extract_operands(self, operands_):
if len(operands_) > 1 and '(' in operands_[1]:
operands = [operands_[0]]
operands.append(operands_[1].split('(')[0])
operands.append(operands_[1].split('(')[1].split(')')[0])
else:
operands = operands_
return operands
def set_immediate(self, value):
self.immediate = value