-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.py
132 lines (117 loc) · 4.78 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
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
import numpy as np
# Used for debugging
instructionText = {
0x00 : "NOP", 0x01 : "MEM",
0x02 : "ADD", 0x03 : "ADDI",
0x04 : "SUB", 0x05 : "SUBI",
0x06 : "MUL", 0x07 : "MULI",
0x08 : "AND", 0x09 : "ANDI",
0x0A : "OR", 0x0B : "ORI",
0x0C : "LS", 0x0D : "LSI",
0x0E : "RS", 0x0F : "RSI",
0x22 : "LD", 0x23 : "LDI",
0x24 : "LDA", 0x25 : "LDAI",
0x26 : "MV", 0x27 : "MVI",
0x28 : "ST", 0x29 : "STI",
0x2A : "STA", 0x2B : "STAI",
0x42 : "BR", 0x43 : "BRI",
0x44 : "BRE", 0x45 : "BREI",
0x46 : "BRN", 0x47 : "BRNI",
0x48 : "BRL", 0x49 : "BRLI",
0x4A : "BRG", 0x4B : "BRGI",
0x4C : "BRZ", 0x4D : "BRZI",
0xFF : "TRM"}
class Instruction():
def __init__(self, instruction=None):
self.instruction = instruction
self.opcode = np.uint8(int(format(int(self.instruction), "032b")[:8], 2))
self.registers = []
self.immediate = None
self.numRegs = None
self.finished = False # Has instruction finished executing
self.started = False # Has it been issued
self.waitingFor = {} # Any instructions that this one depends on, maps to how long to wait once that instr has started
self.instrType = None # Can be LOAD, STORE, ARITH, BRANCH. MV counts as arith
self.reads = [] # What registers it reads from
self.writes = [] # What registers it writes to
def parse(self):
self.determineType()
instructionString = format(int(self.instruction), "032b")
if self.opcode == 0x00 or self.opcode == 0xFF: # NOP, TRM
self.numRegs = 0
return
if self.opcode | 1 == 0x43: # BR, BRI
self.numRegs = 1
elif self.opcode | 1 in [0x23, 0x27, 0x29, 0x4D]: # LD, LDI, MV, MVI, ST, STI
self.numRegs = 2
else:
self.numRegs = 3
if self.opcode % 2 == 1:
self.numRegs -= 1
self.immediate = np.int32(int(instructionString[8 + 4 * self.numRegs:], 2))
for i in range(self.numRegs):
self.registers.append(np.uint8(int(instructionString[i*4 + 8: i*4 + 12], 2)))
if self.immediate != None:
self.immediate = np.int32(int(instructionString[self.numRegs*4 + 8:], 2))
return
def determineType(self):
if self.opcode in [0x00, 0xFF]:
return
if self.opcode < 0x20 or self.opcode | 1 == 0x27:
self.instrType = "ARITH"
elif self.opcode < 0x26:
self.instrType = "LOAD"
elif self.opcode < 0x40:
self.instrType = "STORE"
else:
self.instrType = "BRANCH"
return
def getReadsWrites(self):
self.reads = []
self.writes = []
if self.instrType == "ARITH" or self.instrType == "LOAD":
self.writes.append(self.registers[0])
for reg in self.registers[1:]:
self.reads.append(reg)
else:
for reg in self.registers:
self.reads.append(reg)
return
'''def getDependencies(self):
reads = []
writes = []
if self.opcode in [0x00, 0xFF, 0x43]: # NOP, TRM, BRI
return [[], []]
elif self.opcode < 0x20: # Arithmetic instructions
writes.append("R" + str(self.registers[0]))
for i in range(1, self.numRegs):
reads.append("R" + str(self.registers[i]))
elif self.opcode < 0x28: # LD*, MV*
writes.append("R" + str(self.registers[0]))
for i in range(1, self.numRegs):
reads.append("R" + str(self.registers[i]))
elif self.opcode < 0x40: # ST*
for i in range(self.numRegs):
reads.append("R" + str(self.registers[i]))
else: # Branch
for i in range(self.numRegs):
reads.append("R" + str(self.registers[i]))
return [reads, writes]'''
def updateWaiting(self):
for instruction in self.waitingFor:
if instruction.started:
self.waitingFor[instruction] = max(0, self.waitingFor[instruction] - 1)
return
def addWait(self, instruct, waitAmount):
if instruct in self.waitingFor:
self.waitingFor[instruct] = max(self.waitingFor[instruct], waitAmount)
else:
self.waitingFor[instruct] = waitAmount
return
def __str__(self):
string = format(sum(self.waitingFor.values()), " >2d") + " " + instructionText[self.opcode].ljust(4) + " "
for i in self.registers:
string += ("R" + str(i)).ljust(4)
if self.immediate != None:
string += hex(self.immediate)
return string