Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print ("\n\n\n==================================================ENCODING==================================================")
- # Temp Create for Complier
- temp = open("temp.txt", "w")
- # check type
- def checkType(instructionCode) :
- Rtype = ["add","nand"]
- Itype = ["lw","sw","beq"]
- Jtype = ["jalr"]
- Otype = ["halt","noop"]
- if instructionCode in Rtype : #check R Type
- return "R"
- elif instructionCode in Itype : #check I Type
- return "I"
- elif instructionCode in Jtype : #check J Type
- return "J"
- elif instructionCode in Otype : #check O Type
- return "O"
- elif instructionCode==".fill" : #check Fill
- return "FILL"
- else : #else
- return "ERROR"
- # convert decimal to binary
- def Dectobin(n, bits):
- n = int(n)
- s = bin(n & int("1"*bits, 2))[2:]
- return ("{0:0>%s}" % (bits)).format(s)
- # Fill
- def Fill(label,inst1):
- # inst1 = int(inst1)
- label = inst1
- return label
- # opcode R-type
- def Ropcode(type):
- op = ""
- if type == "add":
- op = "000"
- elif type == "nand":
- op = "001"
- return op
- # opcode J-type
- def Jopcode(type):
- op = ""
- if type == "jalr":
- op = "101"
- return op
- # opcode O-type
- def Oopcode(type):
- op = ""
- if type == "halt":
- op = "110"
- elif type == "noop":
- op = "111"
- return op
- # opcode I-type
- def Iopcode(type):
- op = ""
- if type == "lw":
- op = "010"
- elif type == "sw":
- op = "011"
- elif type == "beq":
- op = "100"
- return op
- #Set Path
- filename = "ComArch.txt"
- #All instruction
- instruction = list()
- variableOfFill = list()
- adrlabel = list()
- countadr = 0
- myadr = 0
- k = 0
- #split instruction
- with open (filename) as fin :
- for line in fin :
- instruction.append (line)
- for i in instruction : # วนลูปรันคำสั่งทุกคำสั่ง โดยจะรับไปทีละบรรทัด
- inst = i.split("\t") # นำ ตัวแปลcommad มาแบ่งโดยใช้ช่องว่าง ถ้าเจอช่องว่างก็แบ่ง ไปเก็บในตัวแปล inst ที่เป็น array
- if len(inst[0]) > 6 :
- print ("ERROR : label > 6")
- break
- instType = checkType(inst[1]) # เอาไปเข้า ฟังชั่น chrcktype อยู่ข้างบน ฟังชั่นนี้จะ return ออกมาเพื่อนแบ่ง type R I S บลาๆ
- adrlabel.append(inst[0])
- adrlabel.append(countadr)
- countadr = countadr + 1
- # if inst[0] != None:
- # print ("few")
- if instType == "FILL" :
- k = 0
- z = inst[2][:-1]
- while k < len(adrlabel) :
- if z == adrlabel[k] :
- z = adrlabel[k+1]
- z = str(z)
- k = k + 2
- else :
- k = k + 2
- variableOfFill.append(inst[0])
- variableOfFill.append(z)
- # print(adrlabel)
- # print(variableOfFill)
- for i in instruction :
- inst = i.split("\t")
- instType = checkType(inst[1])
- if len(inst[0]) > 6 :
- print ("ERROR : label > 6")
- break
- if instType == "R" : # ถ้าเจอคำสั่ง r type ฟังชั่น instType จะ return ตัว "R" ออกมา นำมาเช็คว่าเป็น R type รึป่าว
- few = Ropcode(inst[1]) # สร้างตัวแปล เรียนใช้ฟังชัน Ropcode อยู่ข้างบน โดยจะส่ง inst[0](ตัวนี้จะเป็นตัวบอกคำสั่งเช่น add,nand,addi,lw) เอาไปเปลี่ยนเป็น opcode เช่น add => 000
- x = Dectobin(inst[2],3) # สร้างตัวแปลเรียกใช้ฟังชัน Dectobin อยู่ข้างบน โดนส่ง inst[1](x1 xต่างๆไป) เอาไปเปลี่ยนจาก x1 เป็น 1ในเลขฐาน2 001
- y = Dectobin(inst[3],3)
- z = Dectobin(inst[4],3)
- maichi = "0000000000000"
- maichi2 = "0000000"
- temp.write(maichi2+few+x+y+maichi+z+"\n") # เอาตัวแปลมาต่อกันออกเป็นเลขฐาน2 32 bit
- print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
- if instType == "I" : # เรียกใช้ I type ยังไม่เสร็จจ้าา
- few = Iopcode(inst[1])
- x = Dectobin(inst[2],3)
- y = Dectobin(inst[3],3)
- z = (inst[4][:-1])
- w = (inst[4][1:-1])
- check = 0
- maichi2 = "0000000"
- k = 0
- while k < len(adrlabel) :
- if z == adrlabel[k] :
- if few == "100" :
- z = int(adrlabel[k+1]) - myadr
- z = z -1
- z = str(z)
- else :
- z = int(adrlabel[k+1]) - myadr
- z = str(z)
- k = k + 2
- elif z != adrlabel[k] :
- k = k + 2
- c = Dectobin(z,16)
- temp.write(maichi2+few+x+y+c+"\n")
- print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
- if instType == "J" :
- few = Jopcode(inst[1])
- x = Dectobin(inst[2],3)
- y = Dectobin(inst[3],3)
- maichi = "0000000000000000"
- maichi2 = "0000000"
- temp.write(maichi2+few+x+y+maichi+"\n")
- print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
- if instType == "O" :
- few = Oopcode(inst[1])
- maichi = "0000000000000000000000"
- maichi2 = "0000000"
- temp.write(maichi2+few+maichi+"\n") # เอาตัวแปลมาต่อกันออกเป็นเลขฐาน2 32 bit
- print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
- if instType == "FILL" :
- x = (inst[2])[:-1]
- k = 0
- while k < len(adrlabel) :
- if x == adrlabel[k] :
- x = adrlabel[k+1]
- k = k+2
- else :
- k = k+2
- if int(x) > 0 :
- x = Dectobin(x,3)
- temp.write(str(int(x,2))+"\n")
- print ("Finsish ", myadr,"Instruction(s) : FILL")
- else :
- temp.write(x +"\n")
- print ("Finsish ", myadr,"Instruction(s) : FILL")
- myadr = myadr +1
- temp.close
- print ("===============================================FINISH ENCODING===============================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement