Exemplo n.º 1
0
    def run(self):

        instructions = []
        instructions = SetUp.import_data_file()
        outputFilename = SetUp.get_output_filename()

        print("raw output filename is", outputFilename)

        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            if opcode[i] == 1112: #ADD
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1624: #SUB
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1104: #AND
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1360: #ORR
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1690: #LSR
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]) + ", ")
                self.arg2Str.append("R" + str(self.arg1[i]) + ", ")
                self.arg3Str.append("#" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1691: #LSL
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]) + ", ")
                self.arg2Str.append("R" + str(self.arg1[i]) + ", ")
                self.arg3Str.append("#" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1872: #EOR
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif 160 <= opcode[i] <= 191: #B
                self.instrSpaced.append(SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append(SetUp.imm_bit_to_32_bit_converter((int(instructions[i], base=2) & self.bMask), 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.destReg.append(-2)
                self.src1Reg.append(-3)
                self.src2Reg.append(-4)
            elif opcode[i] == 1160 or opcode[i] == 1161: #ADDI
                self.instrSpaced.append(SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2) & self.imMask) >> 10)
                self.arg2.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "")
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg2[i])
                self.src2Reg.append(-5)
            elif opcode[i] == 1672 or opcode[i] == 1673: #SUBI
                self.instrSpaced.append(SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2) & self.imMask) >> 10)
                self.arg2.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "")
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg2[i])
                self.src2Reg.append(-6)
            elif 1440 <= opcode[i] <= 1447: #CBZ
                self.instrSpaced.append(SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                self.arg1.append(SetUp.imm_bit_to_32_bit_converter(((int(instructions[i], base=2) & self.addr2Mask) >> 5), 19))
                self.arg2.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg3.append('')
                self.arg1Str.append("R" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
                self.src1Reg.append(arg2[i])
                self.src2Reg.append(-7)
                self.destReg.append(-8)
            elif 1448 <= opcode[i] <= 1455: #CBNZ
                # CBNZ offsetArg, conditionArg
                self.instrSpaced.append(SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append(SetUp.imm_bit_to_32_bit_converter(((int(instructions[i], base=2) & self.addr2Mask) >> 5), 19))
                self.arg2.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg3.append('')
                self.arg1Str.append("R" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
                self.src1Reg.append(arg2[i])
                self.src2Reg.append(-9)
                self.destReg.append(-10)
            elif 1684 <= opcode[i] <= 1687: #MOVZ
                self.instrSpaced.append(SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append((int(instructions[i], base=2) & self.imsftMask) >> 17)
                self.arg2.append((int(instructions[i], base=2) & self.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
                self.destReg.append(arg3[i])
                self.src1Reg.append(-11)
                self.src2Reg.append(-12)

            elif 1940 <= opcode[i] <= 1943: #MOVK
                self.instrSpaced.append(SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append((int(instructions[i], base=2) & self.imsftMask) >> 17)
                self.arg2.append((int(instructions[i], base=2) & self.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
                self.destReg.append(arg3[i])
                self.src1Reg.append(-13)
                self.src2Reg.append(-14)
            elif opcode[i] == 1984: #STUR
                self.instrSpaced.append(SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.imMask) >> 12)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]) + ", ")
                self.arg2Str.append("[R" + str(self.arg1[i]) + ", ")
                self.arg3Str.append("#" + str(self.arg2[i]) + "]")
                self.src1Reg.append(arg1[i])
                self.src2Reg.append(-15)
                self.destReg.append(arg3[i])
            elif opcode[i] == 1986: #LDUR
                self.instrSpaced.append(SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2) & self.imMask) >> 12)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]) + ", ")
                self.arg2Str.append("[R" + str(self.arg1[i]) + ", ")
                self.arg3Str.append("#" + str(self.arg2[i]) + "]")
                self.src1Reg.append(arg1[i])
                self.src2Reg.append(-16)
                self.destReg.append(arg3[i])
            elif opcode[i] == 1692: #ASR
                self.instrSpaced.append(SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2) & self.shmtMask) >> 10)
                self.arg2.append((int(instructions[i], base=2) & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2) & self.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]))
                self.src1Reg.append(arg2[i])
                self.src2Reg.append(-17)
                self.destReg.append(arg3[i])
            elif opcode[i] == 0:
                self.instrSpaced.append(SetUp.bin2StringSpaced(instructions[i]))
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.src1Reg.append(-18)
                self.src2Reg.append(-19)
                self.destReg.append(-20)

            elif opcode[i] == 2038 and (int(instructions[i], base=2) & self.specialMask) == 2031591:
                self.instrSpaced.append(SetUp.bin2StringSpaced(instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.src1Reg.append(-21)
                self.src2Reg.append(-22)
                self.destReg.append(-23)
                print("breaking")
                break
            else:
                self.opcodeStr.append("unknown")
                self.arg2.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.src1Reg.append(-24)
                self.src2Reg.append(-25)
                self.destReg.append(-26)
                print("i =:  " + str(i))
                print("opcode =:  " + str(opcode[i]))
                sys.exit("You have found an unknown instruction, investigate NOW")


            # now read the memory lines

        k = 0 #used to calculate how far from the B instruct
        for i in range ((self.numInstructs), (len(instructions))):
            #reads lines from break instruction to end of input file
            self.rawdata.append(instructions[i]) #adds the binary to rawdata list
            self.dataval.append(SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(int(instructions[i], base=2))) #converts to 2comp
            self.addressdata.append(str(int(self.address[i])))
        return  {#"instructions": instructions,
            "opcode":opcode,
            "opcodeStr":self.opcodeStr,
            "arg1":self.arg1,
            "arg1Str":self.arg1Str,
            "arg2":self.arg2,
            "arg2Str":self.arg2Str,
            "arg3":self.arg3,
            "arg3Str":self.arg3Str,
            "dataval":self.dataval,
            "address":self.address,
            "numInstructs":self.numInstructs,
            "instructions":instructions,
            "destReg":self.destReg,
            "src1Reg":self.src1Reg,
            "src2Reg":self.src2Reg}
Exemplo n.º 2
0
    def run(self):

        instructions = []
        instructions = SetUp.import_data_file()

        outputFilename = SetUp.get_output_filename()

        print("raw output filename is ", outputFilename)

        # creates address list with appropriate length
        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        # creates an opcode list by selecting the 11 left most bits
        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            """ # R-type """
            if opcode[i] == 1112:  # ADD OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1624:  # SUB OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1104:  # AND OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1360:  # ORR OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1690:  # LSR OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1691:  # LSL OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1692:  # ASR OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1872:  # EOR OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                """# B-Type"""
            elif 160 <= opcode[i] <= 191:  # B OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & MASKS.bMask), 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
                """# I-Type"""
            elif 1160 <= opcode[i] <= 1161:  # ADDI OPCODE RANGE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.imMask) >> 10)
                self.arg1Str.append("R" + str(self.arg1[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]))
            elif 1672 <= opcode[i] <= 1673:  # SUBI OPCODE RANGE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.imMask) >> 10)
                self.arg1Str.append("R" + str(self.arg1[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]))
                """"# D-Type"""
            elif opcode[i] == 1984:  # STUR OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            elif opcode[i] == 1986:  # LDUR OPCODE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKS.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
                """"# CB Type"""
            elif 1440 <= opcode[i] <= 1447:  # CBZ OPCODE RANGE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ   ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & MASKS.addr2Mask) >> 5,
                        19))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("R" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif 1448 <= opcode[i] <= 1455:  # CBNZ OPCODE RANGE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & MASKS.addr2Mask) >> 5,
                        19))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("R" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
                """"# IM Type"""
            elif 1684 <= opcode[i] <= 1687:  # MOVZ OPCODE RANGE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append(
                    ((int(instructions[i], base=2) & MASKS.imsftMask) >> 21) *
                    16)  # need to send to
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
            elif 1940 <= opcode[i] <= 1943:  # MOVK OPCODE RANGE
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append(
                    ((int(instructions[i], base=2) & MASKS.imsftMask) >> 21) *
                    16)  # need to send to
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKS.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKS.rdMask) >> 0)
                self.arg1Str.append("R" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
                """NOP"""
            elif opcode[i] == 0:  # NOP OPCODE
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                """"# Break"""
            elif opcode[i] == 2038 and (
                    int(instructions[i], base=2)
                    & MASKS.specialMask) == 2031591:  # BREAK OPCODE
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                # print("breaking\n")
                break
            else:
                self.opcodeStr.append("unknown")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("i =: " + str(i))
                print("opcode =: " + str(opcode[i]))
                sys.exit(
                    "You have found an unknown instruction, investigate NOW")

        counter = 0

        for j in range(self.numInstructs, len(instructions)):
            self.rawdata.append(instructions[j])
            self.dataval.append(
                SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    int(self.rawdata[counter], base=2)))
            counter += 1

        return {
            "opcode": opcode,
            "dataval": self.dataval,
            "address": self.address,
            "numInstructs": self.numInstructs,
            "arg1": self.arg1,
            "arg2": self.arg2,
            "arg3": self.arg3,
            "opcodeStr": self.opcodeStr,
            "arg1Str": self.arg1Str,
            "arg2Str": self.arg2Str,
            "arg3Str": self.arg3Str
        }
    def run(self):

        pass

        instructions = []
        instructions = SetUp.import_data_file()

        outputFilename = SetUp.get_output_filename()

        # print(hex(MASKs.bMask))
        # print(bin(MASKs.bMask))
        # print(f'{MASKs.bMask:32b}')

        # create an address list
        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        # create an opcode list
        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        # decode and dissect

        for i in range(len(opcode)):
            self.numInstructions = self.numInstructions + 1
            # NOP type
            if opcode[i] == 0:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")

            # B type
            elif 160 <= opcode[i] <= 191:  # B type
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.bMask) >> 0)
                self.tempArg = self.arg1[i]
                self.tempArg = str(SetUp.decimalToBinary(self.tempArg))
                self.tempArg = SetUp.imm_bit_to_32_bit_converter(self.tempArg)
                self.tempArg = SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    self.tempArg)
                self.arg1[i] = self.tempArg
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("\t#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")

            # AND type
            elif opcode[i] == 1104:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))

            # ADD
            elif opcode[i] == 1112:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))

            # ADDI
            elif 1160 <= opcode[i] <= 1161:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.imMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))

            # ORR
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))

            # CBZ signed
            elif 1440 <= opcode[i] <= 1447:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.addr2Mask) >> 5)
                self.tempArg = self.arg1[i]
                self.tempArg = str(SetUp.decimalToBinary(self.tempArg))
                self.tempArg = SetUp.imm_bit_to_32_bit_converter(self.tempArg)
                self.tempArg = SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    self.tempArg)
                self.arg1[i] = self.tempArg
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")

            # CBNZ signed
            elif 1448 <= opcode[i] <= 1455:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.addr2Mask) >> 5)
                self.tempArg = self.arg1[i]
                self.tempArg = str(SetUp.decimalToBinary(self.tempArg))
                self.tempArg = SetUp.imm_bit_to_32_bit_converter(self.tempArg)
                self.tempArg = SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    self.tempArg)
                self.arg1[i] = self.tempArg
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")

            # SUB
            elif opcode[i] == 1624:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))

            # SUBI
            elif 1672 <= opcode[i] <= 1673:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.imMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))

            # MOVZ
            elif 1684 <= opcode[i] <= 1687:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.imdataMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.imsftMask) >> 17)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg1[i]))
                self.arg3Str.append(", LSL " + str(self.arg2[i]))

            # LSR
            elif opcode[i] == 1690:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))

            # LSL
            elif opcode[i] == 1691:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))

            # ASR
            elif opcode[i] == 1692:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))

            # EOR
            elif opcode[i] == 1872:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))

            # MOVK
            elif 1940 <= opcode[i] <= 1943:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.imdataMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.imsftMask) >> 17)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg1[i]))
                self.arg3Str.append(", LSL " + str(self.arg2[i]))

            # STUR
            elif opcode[i] == 1984:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.addrMask) >> 12)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]) + "]")

            # LDUR
            elif opcode[i] == 1986:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.addrMask) >> 12)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]) + "]")

            # BREAK
            elif opcode[i] == 2038 and (int(instructions[i], base=2)
                                        & self.specialMask) == 2031591:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print
                "breaking"
                break

            # NOT A TYPE
            else:
                self.opcodeStr.append("unkown")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("i=:  " + str(i))
                print("opcode =: " + str(opcode[i]))
                sys.exit(
                    "You have found an unknown instruction, investigate NOW")

        # Now read the memory lines
        #index = 15 #self.numInstructions

        for index in range(self.numInstructions, len(instructions)):
            self.rawData.append(instructions[index])
            self.dataVal.append(
                SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    self.rawData[index - self.numInstructions]))

        return {  #"intructions":instructions
            "opcode": opcode,
            "opcodeStr": self.opcodeStr,
            "arg1": self.arg1,
            "arg1Str": self.arg1Str,
            "arg2": self.arg2,
            "arg2Str": self.arg2Str,
            "arg3": self.arg3,
            "arg3Str": self.arg3Str,
            "dataVal": self.dataVal,
            "address": self.address,
            "numInstructions": self.numInstructions
        }
Exemplo n.º 4
0
    def run(self):

        instructions = []
        instructions = SetUp.import_data_file()

        outputFilename = SetUp.get_output_filename()

        print("raw output filename is ", outputFilename)

        # create an address list with appropriate length
        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        # create an opcode list by selecting the left 11 bits
        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        # decode and dissect :) no bugs in this class

        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            if opcode[i] == 1112:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1104:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1624:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1690:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1691:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1692:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1872:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif 1684 <= opcode[i] <= 1687:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append(
                    ((int(instructions[i], base=2) & self.imsftMask) >> 21) *
                    16)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.addr2Mask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
            elif 1940 <= opcode[i] <= 1943:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append(
                    ((int(instructions[i], base=2) & self.imsftMask) >> 21) *
                    16)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.addr2Mask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
            elif 1440 <= opcode[i] <= 1447:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                self.arg1.append(
                    SetUp.immSignedToTwosConverter(
                        (int(instructions[i], base=2) & self.addr2Mask) >> 5))
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif 1448 <= opcode[i] <= 1455:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append(
                    SetUp.immSignedToTwosConverter(
                        (int(instructions[i], base=2) & self.addr2Mask) >> 5))
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif 1160 <= opcode[i] <= 1161:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.imMask) >> 10)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", R" + str(self.arg1[i]))
            elif 1672 <= opcode[i] <= 1673:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.imMask) >> 10)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", R" + str(self.arg1[i]))
            elif opcode[i] == 1984:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            elif opcode[i] == 1986:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            elif 160 <= opcode[i] <= 191:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & self.bMask), 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("\t#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
            elif opcode[i] == 0:
                self.instrSpaced.append(0)
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
            elif opcode[i] == 2038:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
            else:
                self.instrSpaced.append(instructions[i])
                self.opcodeStr.append(
                    SetUp.immSignedToTwosConverter(int(instructions[i],
                                                       base=2)))
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
Exemplo n.º 5
0
    def run(self):

        instructions = []
        instructions = SetUp.import_data_file()
        for i in instructions:
            print(i)

        outputFilename = SetUp.get_output_filename()

        print("raw output filename is ", outputFilename)

        print(hex(MASKs.bMask))
        print(bin(MASKs.bMask))
        print(f'{MASKs.bMask:032b}')

        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            if opcode[i] == 1112:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1624:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1104:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] >= 160 and opcode[i] <= 191:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                bImm = int(instructions[i], base=2) & MASKs.bMask
                self.arg1.append(SetUp.imm_bit_to_32_bit_converter(bImm, 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("\t#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
            elif opcode[i] >= 1160 and opcode[i] <= 1161:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.imMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] >= 1672 and opcode[i] <= 1673:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.imMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1986:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            elif opcode[i] == 1984:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            elif opcode[i] >= 1440 and opcode[i] <= 1447:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                cbAddr = (int(instructions[i], base=2) & MASKs.addr2Mask) >> 5
                self.arg1.append(SetUp.imm_bit_to_32_bit_converter(cbAddr, 19))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif opcode[i] >= 1448 and opcode[i] <= 1455:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                cbnzAddr = (int(instructions[i], base=2)
                            & MASKs.addr2Mask) >> 5
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(cbnzAddr, 19))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif opcode[i] >= 1684 and opcode[i] <= 1687:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.imsftMask) >> 21)
                self.arg1[i] = self.arg1[i] * 16
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(" ," + str(self.arg2[i]) + ", LSL")
                self.arg3Str.append(" " + str(self.arg1[i]))
            elif opcode[i] >= 1940 and opcode[i] <= 1943:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.imsftMask) >> 21)
                self.arg1[i] = self.arg1[i] * 16
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(" ," + str(self.arg2[i]) + ", LSL")
                self.arg3Str.append(" " + str(self.arg1[i]))
            elif opcode[i] == 1690:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1691:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1692:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1872:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 0:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
            elif opcode[i] == 2038 and (int(instructions[i], base=2)
                                        & MASKs.specialMask) == 2031591:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("breaking")
                break
            else:
                self.opcodeStr.append("unknown")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("i =: " + str(i))
                print("opcode =: " + str(opcode[i]))
                sys.exit(
                    "You have found an unknown instruction, investigate NOW")

        #Reading Raw Data
        k = self.numInstructs

        while k < len(opcode):
            self.rawdata.append(instructions[k])
            k = k + 1

        for i in range(len(self.rawdata)):
            self.dataval.append(
                SetUp.immSignedToTwosConverter(int(self.rawdata[i], base=2)))

        return {
            "opcode": opcode,
            "opcodeStr": self.opcodeStr,
            "arg1": self.arg1,
            "arg1Str": self.arg1Str,
            "arg2": self.arg2,
            "arg2Str": self.arg2Str,
            "arg3": self.arg3,
            "arg3Str": self.arg3Str,
            "dataval": self.dataval,
            "address": self.address,
            "numInstructs": self.numInstructs
        }
Exemplo n.º 6
0
    def run(self):

        instructions = []
        inputfile = SetUp.get_input_filename()
        instructions = SetUp.import_data_file()

        outputFilename = SetUp.get_output_filename()

        print("raw output filename is ", outputFilename)

        # create an address list with appropriate length
        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        # create an opcode list by selecting the left 11 bits
        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        # decode and dissect :) no bugs in this class

        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            if opcode[i] == 1112:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1104:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1624:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1690:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(-1)
            elif opcode[i] == 1691:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(-2)
            elif opcode[i] == 1692:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1872:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(self.arg1[i])
                self.src2Reg.append(self.arg2[i])
            elif 1684 <= opcode[i] <= 1687:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append(
                    ((int(instructions[i], base=2) & self.imsftMask) >> 21) *
                    16)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(-3)
                self.src2Reg.append(-4)
            elif 1940 <= opcode[i] <= 1943:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append(
                    ((int(instructions[i], base=2) & self.imsftMask) >> 21) *
                    16)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg1[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(-5)
                self.src2Reg.append(-6)
            elif 1440 <= opcode[i] <= 1447:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & self.addr2Mask) >> 5,
                        19))
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
                self.destReg.append(-7)
                self.src1Reg.append(-8)
                self.src2Reg.append(-9)
            elif 1448 <= opcode[i] <= 1455:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & self.addr2Mask) >> 5,
                        19))
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
                self.destReg.append(-10)
                self.src1Reg.append(-11)
                self.src2Reg.append(-12)
            elif 1160 <= opcode[i] <= 1161:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.imMask) >> 10)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(-13)
                self.src2Reg.append(self.arg2[i])
            elif 1672 <= opcode[i] <= 1673:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.imMask) >> 10)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]))
                self.destReg.append(self.arg3[i])
                self.src1Reg.append(-14)
                self.src2Reg.append(self.arg2[i])
            elif opcode[i] == 1984:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
                self.destReg.append(-15)
                self.src1Reg.append(-16)
                self.src2Reg.append(-17)
            elif opcode[i] == 1986:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & self.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & self.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & self.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
                self.destReg.append(-18)
                self.src1Reg.append(-18)
                self.src2Reg.append(-19)
            elif 160 <= opcode[i] <= 191:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        int(instructions[i], base=2) & self.bMask, 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("\t#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.destReg.append(-20)
                self.src1Reg.append(-21)
                self.src2Reg.append(-22)
            elif opcode[i] == 0:
                if int(instructions[i], base=2) == 0:
                    self.instrSpaced.append(
                        SetUp.bin2StringSpaced(instructions[i]))
                    self.opcodeStr.append("NOP")
                    self.arg1.append(0)
                    self.arg2.append(0)
                    self.arg3.append(0)
                    self.arg1Str.append("")
                    self.arg2Str.append("")
                    self.arg3Str.append("")
                    self.destReg.append(-23)
                    self.src1Reg.append(-24)
                    self.src2Reg.append(-25)
                else:
                    self.instrSpaced.append(instructions[i])
                    self.opcodeStr.append(
                        SetUp.immSignedToTwosConverter(
                            int(instructions[i], base=2)))
                    self.arg1.append(0)
                    self.arg2.append(0)
                    self.arg3.append(0)
                    self.arg1Str.append("")
                    self.arg2Str.append("")
                    self.arg3Str.append("")
                    self.dataval.append(int(instructions[i], base=2))
                    self.numInstructs -= 1
            elif opcode[i] == 2038:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.destReg.append(-26)
                self.src1Reg.append(-27)
                self.src2Reg.append(-28)
            else:
                self.instrSpaced.append(instructions[i])
                self.opcodeStr.append(
                    SetUp.immSignedToTwosConverter(int(instructions[i],
                                                       base=2)))
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                self.dataval.append(int(instructions[i], base=2))
                self.numInstructs -= 1
        return {
            "instructions": instructions,
            "opcode": opcode,
            "opcodeStr": self.opcodeStr,
            "arg1": self.arg1,
            "arg1Str": self.arg1Str,
            "arg2": self.arg2,
            "arg2Str": self.arg2Str,
            "arg3": self.arg3,
            "arg3Str": self.arg3Str,
            "destReg": self.destReg,
            "src1Reg": self.src1Reg,
            "src2Reg": self.src2Reg,
            "dataval": self.dataval,
            "address": self.address,
            "numInstructs": self.numInstructs
        }
Exemplo n.º 7
0
    def run(self):

        instructions = []
        instructions = SetUp.import_data_file()

        outputFileName = SetUp.get_output_filename()

        print("raw output filename is ", outputFileName)

        # Create an address list with appropriate length
        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        # Create an opcode list by selecting the left 11 bits
        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        # Now the fun begins! Decode and dissect!
        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            if opcode[i] == 1112:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1624:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1160 or opcode[i] == 1161:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.imMask) >> 10)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]))
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1104:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif 1440 <= opcode[i] <= 1447:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.addr2Mask) >> 5),
                                                      19))
                self.arg2.append((int(instructions[i], base=2) & masks.rdMask))
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif 1448 <= opcode[i] <= 1455:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.addr2Mask) >> 5),
                                                      19))
                self.arg2.append(
                    (int(instructions[i], base=2) & masks.rdMask) >>
                    0)  # We Need to treat the conditional as rd for mask
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif 1672 <= opcode[i] <= 1673:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.imMask) >> 10)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]))
            elif 1684 <= opcode[i] <= 1687:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVZ")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.imdataMask) >> 5)
                self.arg3.append(
                    ((int(instructions[i], base=2) & masks.imsftMask) >> 21) *
                    16)
                self.arg1Str.append("\tR" + str(self.arg1[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg3[i]))
            elif 1940 <= opcode[i] <= 1943:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.opcodeStr.append("MOVK")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.imdataMask) >> 5)
                self.arg3.append(
                    ((int(instructions[i], base=2) & masks.imsftMask) >> 21) *
                    16)
                self.arg1Str.append("\tR" + str(self.arg1[i]))
                self.arg2Str.append(", " + str(self.arg2[i]))
                self.arg3Str.append(", LSL " + str(self.arg3[i]))
            elif opcode[i] == 1690:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.shmtMask) >> 10),
                                                      6))
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1691:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.shmtMask) >> 10),
                                                      6))
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1984:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg3.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.addrMask) >> 12),
                                                      9))
                self.arg1Str.append("\tR" + str(self.arg1[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]) + "]")
            elif opcode[i] == 1986:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg3.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.addrMask) >> 12),
                                                      9))
                self.arg1Str.append("\tR" + str(self.arg1[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]) + "]")
            elif opcode[i] == 1692:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & masks.shmtMask) >> 10),
                                                      6))
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1872:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & masks.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & masks.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & masks.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif 160 <= opcode[i] <= 191:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & masks.bMask) >> 0, 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("\t#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
            elif opcode[i] == 2038 and (int(instructions[i], base=2)
                                        & masks.specialMask) == 2031591:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("breaking")
                break
            elif opcode[i] == 0:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
            else:
                self.opcodeStr.append("unknown")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("i=: " + str(opcode[i]))
                sys.exit(
                    "You have found an unknown instruction, investigate NOW")

        # I created parser here because I needed to preserve/reference numInstructs as the beginning of the rawData at a later time
        # Read in the 32 bit strings raw into this vector, will be used for printing and handing data to the converter
        parser = self.numInstructs
        while parser < len(opcode):
            self.rawdata.append(int(instructions[parser]))
            parser = parser + 1

        # Convert the signed 32 bit strings into a signed int and store them in the dataval vector for printing later
        for i in range(len(self.rawdata)):
            self.dataval.append(
                SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    int(self.rawdata[i])))
Exemplo n.º 8
0
    def run(self):
        instructions = []
        instructions = SetUp.import_data_file()
        # for i in instructions:
        # print(i)

        outputFilename = SetUp.get_output_filename()

        print("raw output filename is ", outputFilename)

        # print(hex(MASKs.bMask))
        # print(bin(MASKs.bMask))
        # print(f'{MASKs.bMask:032b}')

        # create an address list with appropriate length
        for i in range(len(instructions)):
            self.address.append(96 + (i * 4))

        opcode = []

        # create an opcode list by selecting the left 11 bits
        for z in instructions:
            opcode.append(int(z, base=2) >> 21)

        for i in range(len(opcode)):
            self.numInstructs = self.numInstructs + 1
            #       REGISTER OPCODES
            if opcode[i] == 1112:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ADD")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1624:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("SUB")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1690:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1691:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("LSL")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1692:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ASR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.shmtMask) >> 10)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg2[i]))
            elif opcode[i] == 1104:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("AND")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1360:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("ORR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))
            elif opcode[i] == 1872:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedR(instructions[i]))
                self.opcodeStr.append("EOR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rmMask) >> 16)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", R" + str(self.arg2[i]))

            #       IMMEDIATE OPCODES

            elif opcode[i] == 1160 or opcode[i] == 1161:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("ADDI")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg3.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & MASKs.imMask) >> 10,
                        12))
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", R" + str(self.arg1[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]))
            elif opcode[i] == 1672 or opcode[i] == 1673:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedI(instructions[i]))
                self.opcodeStr.append("SUBI")
                self.arg1.append((int(instructions[i], base=2) & MASKs.rdMask))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.imMask) >> 10)
                self.arg1Str.append("\tR" + str(self.arg1[i]))
                self.arg2Str.append(", R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg3[i]))
            #       BRANCH OPCODES
            elif 160 <= opcode[i] <= 191:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedB(instructions[i]))
                self.opcodeStr.append("B")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter(
                        (int(instructions[i], base=2) & MASKs.bMask), 26))
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("\t#" + str(self.arg1[i]))
                self.arg2Str.append("")
                self.arg3Str.append("")
            #       MEMORY OPCODES
            elif opcode[i] == 1984:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("STUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            elif opcode[i] == 1986:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedD(instructions[i]))
                self.opcodeStr.append("LDUR")
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.addrMask) >> 12)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rnMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", [R" + str(self.arg2[i]))
                self.arg3Str.append(", #" + str(self.arg1[i]) + "]")
            #       CB INSTRUCTIONS
            elif 1440 <= opcode[i] <= 1447:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & MASKs.addr2Mask) >> 5),
                                                      19))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")
            elif 1448 <= opcode[i] <= 1455:
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedCB(instructions[i]))
                self.opcodeStr.append("CBNZ")
                self.arg1.append(
                    SetUp.imm_bit_to_32_bit_converter((
                        (int(instructions[i], base=2) & MASKs.addr2Mask) >> 5),
                                                      19))
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg3.append(0)
                self.arg1Str.append("\tR" + str(self.arg2[i]))
                self.arg2Str.append(", #" + str(self.arg1[i]))
                self.arg3Str.append("")

            #       IM INSTRUCTIONS
            elif 1684 <= opcode[i] <= 1687:
                self.opcodeStr.append("MOVZ")
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.imsftMask) >> 21)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]) + ", LSL ")
                self.arg3Str.append(str(self.arg1[i] * 16))
            elif 1940 <= opcode[i] <= 1943:
                self.opcodeStr.append("MOVK")
                self.instrSpaced.append(
                    SetUp.bin2StringSpacedIM(instructions[i]))
                self.arg1.append((int(instructions[i], base=2)
                                  & MASKs.imsftMask) >> 21)
                self.arg2.append((int(instructions[i], base=2)
                                  & MASKs.imdataMask) >> 5)
                self.arg3.append((int(instructions[i], base=2)
                                  & MASKs.rdMask) >> 0)
                self.arg1Str.append("\tR" + str(self.arg3[i]))
                self.arg2Str.append(", " + str(self.arg2[i]) + ", LSL ")
                self.arg3Str.append(str(self.arg1[i] * 16))

            # NOP INSTRUCTION

            elif instructions[
                    i] == '00000000000000000000000000000000':  # this might still be wrong need to test more
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("NOP")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")

            # Finding the BREAK instruction

            elif opcode[i] == 2038 and (int(instructions[i], base=2)
                                        & MASKs.specialMask) == 2031591:
                self.instrSpaced.append(SetUp.bin2StringSpaced(
                    instructions[i]))
                self.opcodeStr.append("BREAK")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("breaking")
                break
            else:
                self.opcodeStr.append("unknown")
                self.arg1.append(0)
                self.arg2.append(0)
                self.arg3.append(0)
                self.arg1Str.append("")
                self.arg2Str.append("")
                self.arg3Str.append("")
                print("i =: " + str(i))
                print("opcode =: " + str(opcode[i]))
                sys.exit(
                    "You have found an unknown instruction, investigate NOW")

            # Now read the memory lines .... .... ....
        for addri in range(self.numInstructs, len(opcode)):
            self.rawdata.append(str(instructions[addri]))
            self.dataval.append(
                SetUp.imm_32_bit_unsigned_to_32_bit_signed_converter(
                    int(instructions[addri], base=2)))

        return {  #"instructions": instructions,
            "opcode": opcode,
            "opcodeStr": self.opcodeStr,
            "arg1": self.arg1,
            "arg1Str": self.arg1Str,
            "arg2": self.arg2,
            "arg2Str": self.arg2Str,
            "arg3": self.arg3,
            "arg3Str": self.arg3Str,
            "dataval": self.dataval,
            "address": self.address,
            "numInstructs": self.numInstructs
        }