Beispiel #1
0
def base_10_to_b3(base_10, length):
    """ base_10_to_b3:
        base_10: integer value
        length:  desired length of resulting string

        convert from base 10 to base -3 w/base_converter.base_convert
        and fill in leading zeros

        return: balanced base 3 string with length of 'length'
    """

    result = base_converter.base_convert(str(base_10), 10, -3)
    if len(result) != length:
        add = length - len(result)
        for i in range(0, add):
            result = '0' + result

    return result
Beispiel #2
0
def decode_code(self, s):
    """ Decode Assembled Code:

        R-Type:
        --------------------------------------------------------
        | type(2) | opcode(13) | rs(3) | rt(3) | rd(3) | sa(3) |
        --------------------------------------------------------
        I-Type:
        --------------------------------------------------------
        | type(2) | opcode(5) | rs(3) | rt(3) | immediate(14)  |
        --------------------------------------------------------
        B-Type:
        --------------------------------------------------------
        | type(2) | opcode(4) | rs(3) | label0(9) | label(9)   |
        --------------------------------------------------------
        J-Type:
        --------------------------------------------------------
        | type(2) | opcode(4) | address(21)                    |
        --------------------------------------------------------
    """

    type = int(base_converter.base_convert(s[0:2], -3, 10))
    # R-Type
    if type == R_TYPE:
        self.alu_op = int(base_converter.base_convert(s[2:15], -3, 10))
        self.src1   = int(base_converter.base_convert(s[15:18], -3, 10))
        self.src2   = int(base_converter.base_convert(s[18:21], -3, 10))
        self.dest   = int(base_converter.base_convert(s[21:24], -3, 10))
        self.imdt1  = int(base_converter.base_convert(s[24:27], -3, 10))

        self.imdt2  = 0

    # I-Type
    elif type == R_TYPE:
        self.alu_op = int(base_converter.base_convert(s[2:7], -3, 10))
        self.src1   = int(base_converter.base_convert(s[7:10], -3, 10))
        self.dest   = int(base_converter.base_convert(s[10:13], -3, 10))
        self.imdt1  = int(base_converter.base_convert(s[13:27], -3, 10))

        self.src2   = 0
        self.imdt2  = 0

    # B-Type
    elif type == R_TYPE:
        self.alu_op = int(base_converter.base_convert(s[2:6], -3, 10))
        self.src1   = int(base_converter.base_convert(s[6:9], -3, 10))
        self.imdt1  = int(base_converter.base_convert(s[9:18], -3, 10))
        self.imdt2  = int(base_converter.base_convert(s[18:27], -3, 10))

        self.src2   = 0
        self.dest   = 0

    # J-Type
    elif type == R_TYPE:
        self.alu_op = int(base_converter.base_convert(s[2:6], -3, 10))
        self.imdt1  = int(base_converter.base_convert(s[6:27], -3, 10))

        self.src1   = 0
        self.src2   = 0
        self.dest   = 0
        self.imdt2  = 0

    # Uknown type
    else:
        raise DecodeError("invalid type code = %s" % (type, ))