# Path: MC6809/components/MC6809data/MC6809_op_data.py
# REG_S = "S"
# 
# REG_U = "U"
# 
# REG_X = "X"
# 
# REG_Y = "Y"
# 
# Path: MC6809/utils/bits.py
# def is_bit_set(value, bit):
#     """
#     Return True/False if the bit at offset >bit< is 1 or 0 in given value.
# 
#     bit 7 |      | bit 0
#        ...10111101
# 
#     e.g.:
# 
#     >>> is_bit_set(0x01, bit=0) # 00000001
#     True
#     >>> is_bit_set(0xfd, bit=1) # 11111101
#     False
#     >>> is_bit_set(int('10000000', 2), bit=7)
#     True
#     >>> is_bit_set(int('01111111', 2), bit=7)
#     False
#     >>> is_bit_set(int('1111000011110000', 2), bit=11)
#     False
#     >>> is_bit_set(int('1111000011110000', 2), bit=12)
#     True
#     """
#     return False if value & 2 ** bit == 0 else True
# 
# Path: MC6809/utils/byte_word_values.py
# def signed5(x):
#     """ convert to signed 5-bit """
#     if x > 0xf:  # 0xf == 2**4-1 == 15
#         x = x - 0x20  # 0x20 == 2**5 == 32
#     return x
# 
# def signed8(x):
#     """ convert to signed 8-bit """
#     if x > 0x7f:  # 0x7f ==  2**7-1 == 127
#         x = x - 0x100  # 0x100 == 2**8 == 256
#     return x
# 
# def signed16(x):
#     """ convert to signed 16-bit """
#     if x > 0x7fff:  # 0x7fff ==  2**15-1 == 32767
#         x = x - 0x10000  # 0x100 == 2**16 == 65536
#     return x


from MC6809.components.MC6809data.MC6809_op_data import REG_S, REG_U, REG_X, REG_Y
from MC6809.utils.bits import is_bit_set
from MC6809.utils.byte_word_values import signed5, signed8, signed16

    def get_ea_direct(self):
        op_addr, m = self.read_pc_byte()
        dp = self.direct_page.value
        ea = dp << 8 | m
#        log.debug("\tget_ea_direct(): ea = dp << 8 | m  =>  $%x=$%x<<8|$%x", ea, dp, m)
        return ea

    def get_ea_m_direct(self):
        ea = self.get_ea_direct()
        m = self.memory.read_byte(ea)
#        log.debug("\tget_ea_m_direct(): ea=$%x m=$%x", ea, m)
        return ea, m

    def get_m_direct(self):
        ea = self.get_ea_direct()
        m = self.memory.read_byte(ea)
#        log.debug("\tget_m_direct(): $%x from $%x", m, ea)
        return m

    def get_m_direct_word(self):
        ea = self.get_ea_direct()
        m = self.memory.read_word(ea)
#        log.debug("\tget_m_direct(): $%x from $%x", m, ea)
        return m

    INDEX_POSTBYTE2STR = {
        0x00: REG_X,  # 16 bit index register
        0x01: REG_Y,  # 16 bit index register
        0x02: REG_U,  # 16 bit user-stack pointer

# Path: MC6809/components/MC6809data/MC6809_op_data.py
# REG_S = "S"
# 
# REG_U = "U"
# 
# REG_X = "X"
# 
# REG_Y = "Y"
# 
# Path: MC6809/utils/bits.py
# def is_bit_set(value, bit):
#     """
#     Return True/False if the bit at offset >bit< is 1 or 0 in given value.
# 
#     bit 7 |      | bit 0
#        ...10111101
# 
#     e.g.:
# 
#     >>> is_bit_set(0x01, bit=0) # 00000001
#     True
#     >>> is_bit_set(0xfd, bit=1) # 11111101
#     False
#     >>> is_bit_set(int('10000000', 2), bit=7)
#     True
#     >>> is_bit_set(int('01111111', 2), bit=7)
#     False
#     >>> is_bit_set(int('1111000011110000', 2), bit=11)
#     False
#     >>> is_bit_set(int('1111000011110000', 2), bit=12)
#     True
#     """
#     return False if value & 2 ** bit == 0 else True
# 
# Path: MC6809/utils/byte_word_values.py
# def signed5(x):
#     """ convert to signed 5-bit """
#     if x > 0xf:  # 0xf == 2**4-1 == 15
#         x = x - 0x20  # 0x20 == 2**5 == 32
#     return x
# 
# def signed8(x):
#     """ convert to signed 8-bit """
#     if x > 0x7f:  # 0x7f ==  2**7-1 == 127
#         x = x - 0x100  # 0x100 == 2**8 == 256
#     return x
# 
# def signed16(x):
#     """ convert to signed 16-bit """
#     if x > 0x7fff:  # 0x7fff ==  2**15-1 == 32767
#         x = x - 0x10000  # 0x100 == 2**16 == 65536
#     return x


# Path: MC6809/components/mc6809_addressing.py
from MC6809.components.MC6809data.MC6809_op_data import REG_S, REG_U, REG_X, REG_Y
from MC6809.utils.bits import is_bit_set
from MC6809.utils.byte_word_values import signed5, signed8, signed16


    def get_ea_direct(self):
        op_addr, m = self.read_pc_byte()
        dp = self.direct_page.value
        ea = dp << 8 | m
#        log.debug("\tget_ea_direct(): ea = dp << 8 | m  =>  $%x=$%x<<8|$%x", ea, dp, m)
        return ea

    def get_ea_m_direct(self):
        ea = self.get_ea_direct()
        m = self.memory.read_byte(ea)
#        log.debug("\tget_ea_m_direct(): ea=$%x m=$%x", ea, m)
        return ea, m

    def get_m_direct(self):
        ea = self.get_ea_direct()
        m = self.memory.read_byte(ea)
#        log.debug("\tget_m_direct(): $%x from $%x", m, ea)
        return m

    def get_m_direct_word(self):
        ea = self.get_ea_direct()
        m = self.memory.read_word(ea)
#        log.debug("\tget_m_direct(): $%x from $%x", m, ea)
        return m

    INDEX_POSTBYTE2STR = {
        0x00: REG_X,  # 16 bit index register
        0x01: REG_Y,  # 16 bit index register
        0x02: REG_U,  # 16 bit user-stack pointer

        0x03: REG_S,  # 16 bit system-stack pointer