Skip to content

Commit 6d6e030

Browse files
committed
A: add utils.py about some tool func
1 parent dbd6b78 commit 6d6e030

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

utils.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from instructions_54 import instr_set
2+
3+
'''
4+
OP RS RT RD SA FUNCT
5+
000000 00000 00000 00000 00000 000000
6+
7+
separate an Integer into several parts:
8+
OP, RS, RT, RD, SA, FUNCT
9+
10+
'''
11+
def separate(input: int) -> dict:
12+
OP_bit, RS_bit, RT_bit, RD_bit, SA_bit, FUNCT_bit = 6, 5, 5, 5, 5, 6
13+
OP = (input & 0b11111100000000000000000000000000) >> (RS_bit + RT_bit + RD_bit + SA_bit + FUNCT_bit)
14+
RS = (input & 0b00000011111000000000000000000000) >> (RT_bit + RD_bit + SA_bit + FUNCT_bit)
15+
RT = (input & 0b00000000000111110000000000000000) >> (RD_bit + SA_bit + FUNCT_bit)
16+
RD = (input & 0b00000000000000001111100000000000) >> (SA_bit + FUNCT_bit)
17+
SA = (input & 0b00000000000000000000011111000000) >> (FUNCT_bit)
18+
FUNCT = (input & 0b00000000000000000000000000111111)
19+
return { "OP": OP, "RS": RS, "RT": RT, "RD": RD, "SA": SA, "FUNCT": FUNCT }
20+
21+
'''
22+
trans an dict { "OP", "RS", "RT", "RD", "SA", "FUNCT" }
23+
to an dict about { "intro", ... }
24+
25+
this function aims to find instruction type of input
26+
'''
27+
def trans(instr_sepa: dict) -> dict:
28+
OP, FUNCT, SA = instr_sepa["OP"], instr_sepa["FUNCT"], instr_sepa["SA"]
29+
RS, RT, RD = instr_sepa["RS"], instr_sepa["RT"], instr_sepa["RD"]
30+
31+
res_instr = []
32+
for instr in instr_set:
33+
# OP == 0, FUNCT can figure... such as "sllv", "srlv"...
34+
# almost all instrs that enjoy the same "OP" 0b000000
35+
# but clz and mul enjoy the same "OP" 0b011100 instead of 0b000000
36+
flag1 = (instr_set[instr]["OP"] == OP and OP != 0 and OP != 0b011100)
37+
38+
# OP and FUNCT must be able to figure out instruction type
39+
flag2 = (instr_set[instr]["OP"] == OP and instr_set[instr]["FUNCT"] == FUNCT)
40+
41+
if flag1 or flag2:
42+
res_instr.append({
43+
"instr": instr, "Rs": instr_sepa["RS"], "Rt": instr_sepa["RT"],
44+
"Rd": instr_sepa["RD"], "SA": instr_sepa["SA"], "FUNCT": instr_sepa["FUNCT"]
45+
})
46+
else:
47+
if len(res_instr) != 1:
48+
raise Exception("Instruction parse failed!", instr_set[instr])
49+
50+
return res_instr

0 commit comments

Comments
 (0)