-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_instruction.py
More file actions
179 lines (166 loc) · 5.76 KB
/
Copy pathsim_instruction.py
File metadata and controls
179 lines (166 loc) · 5.76 KB
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
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
## Functions to execute the different instructions of the E20 processot
from sim_helpers import *
def addi(prog_rg1,prog_rg2,imm,next_pc,mem_array,reg_array):
"""
Executes the addi instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> str -> int -> list(int) -> list(int) -> int
"""
signed_imm = sign_extend(bin_to_dec((imm)))
if prog_rg2 == 0:
reg_array[prog_rg2] = 0
else:
storeval = reg_array[prog_rg1] + signed_imm
if storeval > 65535:
storeval = storeval - 65536
reg_array[prog_rg2] = storeval
next_pc += 1
return next_pc
def jeq(prog_rg1,prog_rg2,imm,next_pc,mem_array,reg_array):
"""
Executes the jeq instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> str -> int -> list(int) -> list(int) -> int
"""
signed_imm = bin_to_dec(imm)
if reg_array[prog_rg1] == reg_array[prog_rg2]:
next_pc = next_pc + 1 + sign_extend(signed_imm)
else:
next_pc += 1
return next_pc
def sw(prog_rg1,prog_rg2,imm,next_pc,mem_array,reg_array):
"""
Executes the sw instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> str -> int -> list(int) -> list(int) -> int
"""
mem_num = mem_addr(reg_array[prog_rg1],imm)
mem_array[mem_num] = reg_array[prog_rg2]
next_pc += 1
return next_pc
def lw(prog_rg1,prog_rg2,imm,next_pc,mem_array,reg_array):
"""
Executes the lw instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> str -> int -> list(int) -> list(int) -> int
"""
mem_num = mem_addr(reg_array[prog_rg1],imm)
if prog_rg2 == 0:
reg_array[prog_rg2] = 0
else:
reg_array[prog_rg2] = mem_array[mem_num]
next_pc += 1
return next_pc
def slti(prog_rg1,prog_rg2,imm,next_pc,mem_array,reg_array):
"""
Executes the slti instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> str -> int -> list(int) -> list(int) -> int
"""
unsigned_imm = sign_extend(bin_to_dec(imm))
if reg_array[prog_rg1] < unsigned_imm:
if prog_rg2 != 0:
reg_array[prog_rg2] = 1
else:
reg_array[prog_rg2] = 0
next_pc += 1
return next_pc
def add(prog_rg1,prog_rg2,prog_rg3,next_pc,mem_array,reg_array):
"""
Executes the add instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int -> int -> list(int) -> list(int) -> int
"""
store_val = 0
if reg_array[prog_rg1] + reg_array[prog_rg2] > 65535:
store_val = reg_array[prog_rg1] + reg_array[prog_rg2] - 65536
else:
store_val = reg_array[prog_rg1] + reg_array[prog_rg2]
if prog_rg3 == 0:
reg_array[prog_rg3] = 0
else:
reg_array[prog_rg3] = store_val
next_pc += 1
return next_pc
def sub(prog_rg1,prog_rg2,prog_rg3,next_pc,mem_array,reg_array):
"""
Executes the sub instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int -> int -> list(int) -> list(int) -> int
"""
store_val = 0
if reg_array[prog_rg1] - reg_array[prog_rg2] < 0:
store_val = 65536 + (reg_array[prog_rg1] - reg_array[prog_rg2])
else:
store_val = reg_array[prog_rg1] - reg_array[prog_rg2]
if prog_rg3 == 0:
reg_array[prog_rg3] = 0
else:
reg_array[prog_rg3] = store_val
next_pc += 1
return next_pc
def orInstr(prog_rg1,prog_rg2,prog_rg3,next_pc,mem_array,reg_array):
"""
Executes the or instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int -> int -> list(int) -> list(int) -> int
"""
if prog_rg3 == 0:
reg_array[prog_rg3] = 0
else:
reg_array[prog_rg3] = reg_array[prog_rg1] | reg_array[prog_rg2]
next_pc += 1
return next_pc
def andInstr(prog_rg1,prog_rg2,prog_rg3,next_pc,mem_array,reg_array):
"""
Executes the and instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int -> int -> list(int) -> list(int) -> int
"""
if prog_rg3 == 0:
reg_array[prog_rg3] = 0
else:
reg_array[prog_rg3] = reg_array[prog_rg1] & reg_array[prog_rg2]
next_pc += 1
return next_pc
def slt(prog_rg1,prog_rg2,prog_rg3,next_pc,mem_array,reg_array):
"""
Executes the slt instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int -> int -> list(int) -> list(int) -> int
"""
if reg_array[prog_rg1] < reg_array[prog_rg2]:
if prog_rg3 != 0:
reg_array[prog_rg3] = 1
else:
reg_array[prog_rg3] = 0
next_pc += 1
return next_pc
def jr(prog_rg1,prog_rg2,prog_rg3,next_pc,mem_array,reg_array):
"""
Executes the jr instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int -> int -> list(int) -> list(int) -> int
"""
next_pc = reg_array[prog_rg1]
return next_pc
def j(imm,next_pc):
"""
Executes the j instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> int
"""
next_pc = imm
return next_pc
def jal(imm,next_pc,reg_array):
"""
Executes the jal instruction as described in the E20 manual.
Returns the program counter after execution
sig: int -> int -> list(int) -> int
"""
store = next_pc + 1
if store > 65535:
store = store - 65536
reg_array[7] = store
next_pc = imm
return next_pc