forked from Coderzs/cs204-riscv_simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead_write.py
More file actions
225 lines (189 loc) · 7.41 KB
/
Copy pathRead_write.py
File metadata and controls
225 lines (189 loc) · 7.41 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
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
reg=[[0]*32]*32
MEM = []*100000
# comment reg,MEM when mergred
def write_to_memory(start, len, reg_id):
for i in range(len):
MEM[i+start] = reg[reg_id][31-i]
def write_from_memory(start, len, reg_id):
for i in range(32):
reg[i] = 0
for i in range(len):
reg[32-len+i] = MEM[start+31-i]
def RW(machine_code, aluVal,PC = 0):
def binary(arr):
sum=0
for i in range(len(arr)):
sum+=arr[i]*(2*(len(arr)-1-i))
return sum
SIZE = 1<<32
SIZE -= 1
def add(m):
n = 0
carr = 1
pow = 1
while m>0:
sum = m%2 + carr
carr = 0
if sum==2:
sum = 0
carr = 1
n += sum*pow
pow *= 2
m = int(m/2)
n %= (SIZE+1)
return n
def _2C(n):
m = SIZE
m = m^n
return add(m)
def toBinary(n):
val = ""
if n<0:
n = _2C(-n)
while n>0 or len(val)<32:
if n%2:
val += "1"
else:
val += "0"
k = int(n/2)
n = int(k)
string = "".join(reversed(val))
return string
add_op=[0,1,1,0,0,1,1]
addi_op=[0,0,1,0,0,1,1]
add_funct7=[0,0,0,0,0,0,0]
sub_funct7=[0,1,0,0,0,0,0]
mul_funct7=[0,0,0,0,0,0,1]
add_funct3=[0,0,0]
and_funct3=[1,1,1]
or_funct3=[1,1,0]
sll_funct3=[0,0,1]
slt_funct3=[0,1,0]
sra_funct3=[1,0,1]
xor_funct3=[1,0,0]
andi_funct3=[1,1,1]
# I-format
lb_op = [0,0,0,0,0,1,1]
lb_funct3 = [0,0,0]
ld_op = [0,0,0,0,0,1,1]
ld_funct3 = [0,1,1]
lh_op = [0,0,0,0,0,1,1]
lh_funct3 = [0,0,1]
lw_op = [0,0,0,0,0,1,1]
lw_funct3 = [0,1,0]
jalr_op = [1,1,0,0,1,1,1]
jalr_funct3 = [0,0,0]
# S-format
sb_op = [0,1,0,0,0,1,1]
sb_funct3 = [0,0,0]
sw_op = [0,1,0,0,0,1,1]
sw_funct3 = [0,1,0]
sh_op = [0,1,0,0,0,1,1]
sh_funct3 = [0,0,1]
sd_op = [0,1,0,0,0,1,1] # I or S ? ? ? ? ?
sd_funct3 = [0,1,1]
# SB-format
beq_op = [1,1,0,0,0,1,1]
beq_funct3 = [0,0,0]
bne_op = [1,1,0,0,0,1,1]
bne_funct3 = [0,0,1]
bge_op = [1,1,0,0,0,1,1]
bge_funct3 = [1,0,1]
blt_op = [1,1,0,0,0,1,1]
blt_funct3 = [1,0,0]
# U-format
auipc_op = [0,0,1,0,1,1,1]
lui_op = [0,1,1,0,1,1,1]
# UJ-format
jal_op = [1,1,0,1,1,1,1]
reg_id = binary(machine_code[20:25])
start = binary(aluVal)
if(machine_code[25:32]==ld_op and machine_code[17:20]==ld_funct3):
# NOT SUPPORTED
print("Error, 64 bit operation")
return
if(machine_code[25:32]==lb_op and machine_code[17:20]==lb_funct3):
write_from_memory(start,8,reg_id)
if(machine_code[25:32]==lh_op and machine_code[17:20]==lh_funct3):
write_from_memory(start,16,reg_id)
if(machine_code[25:32]==lw_op and machine_code[17:20]==lw_funct3):
write_from_memory(start,32,reg_id)
if(machine_code[25:32]==jalr_op and machine_code[17:20]==jalr_funct3):
reg[reg_id] = aluVal
if(machine_code[25:32]==sb_op and machine_code[17:20]==sb_funct3):
write_to_memory(start,8,reg_id)
if(machine_code[25:32]==sw_op and machine_code[17:20]==sw_funct3):
write_to_memory(start,32,reg_id)
if(machine_code[25:32]==sd_op and machine_code[17:20]==sd_funct3):
# NOT SUPPORTED
print("Error, 64 bit operation")
return
if(machine_code[25:32]==sh_op and machine_code[17:20]==sh_funct3):
write_to_memory(start,16,reg_id)
if(machine_code[25:32]==add_op and machine_code[17:20]==add_funct3 and machine_code[0:7]==add_funct7): #add
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==add_funct3 and machine_code[0:7]==sub_funct7): #sub
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==and_funct3 and machine_code[0:7]==add_funct7): #and
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==or_funct3 and machine_code[0:7]==add_funct7): #or
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==sll_funct3 and machine_code[0:7]==add_funct7): #sll
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==slt_funct3 and machine_code[0:7]==add_funct7): #slt
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==sra_funct3 and machine_code[0:7]==sub_funct7): #sra
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==sra_funct3 and machine_code[0:7]==add_funct7): #srl
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==xor_funct3 and machine_code[0:7]==add_funct7): #xor
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==add_funct3 and machine_code[0:7]==mul_funct7): #mul
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==xor_funct3 and machine_code[0:7]==mul_funct7): #div
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==addi_op and machine_code[17:20]==add_funct3): #addi
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==addi_op and machine_code[17:20]==andi_funct3): #andi
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==addi_op and machine_code[17:20]==or_funct3): #ori
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==add_op and machine_code[17:20]==or_funct3 and machine_code[0:7]==mul_funct7): #rem
reg[binary(machine_code[20:25])]=aluVal
elif(machine_code[25:32]==lui_op): #lui
for i in range(20):
reg[binary(machine_code[20:25])][i]=machine_code[i]
for i in range(20,32):
reg[binary(machine_code[20:32])][i]=0
if(machine_code[25:32]==jal_op):
# PC = []*32 # comment when merged
reg[binary(machine_code[20:25])] = toBinary(PC) # Global PC
if(machine_code[25:32]==auipc_op):
imm = binary(machine_code[0:20])
imm = imm<<12
reg[binary(machine_code[20:25])] = toBinary(imm + PC)
from ALU import alu
def split(s):
return [int(char) for char in s]
def run():
while 1 :
x = input()
x = split(x)
# print(len(x))
machine_code = []
for i in range(32-len(x)):
machine_code.append(int(0))
# print(len(machine_code))
# print(machine_code)
for i in range(len(x)):
machine_code.append(int(x[i]))
# print(machine_code[12:17])
aluVal = alu(machine_code)
# print(aluVal)
aluVal = split(aluVal)
print(aluVal)
RW(machine_code,aluVal,0)
for i in range(3):
for j in range(32):
print (reg[i][j],end = " ")
run()