# 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
#                 self.program_counter, value_signed, ea,
#             )
        elif addr_mode == 0xd:
            #             log.debug("\t1101 0xd | n, PCR | 16 bit offset from program counter")
            __, value = self.read_pc_word()
            value_signed = signed16(value)
            ea = self.program_counter.value + value_signed
            self.cycles += 1
#             log.debug("\tea = pc($%x) + $%x = $%x (dez.: %i + %i = %i)",
#                 self.program_counter, value_signed, ea,
#                 self.program_counter, value_signed, ea,
#             )
        elif addr_mode == 0xe:
            #             log.error("\tget_ea_indexed(): illegal address mode, use 0xffff")
            ea = 0xffff  # illegal
        elif addr_mode == 0xf:
            #             log.debug("\t1111 0xf | [n] | 16 bit address - extended indirect")
            __, ea = self.read_pc_word()
        else:
            raise RuntimeError(f"Illegal indexed addressing mode: ${addr_mode:x}")

        if offset is not None:
            ea = register_value + offset
#             log.debug("\t$%x + $%x = $%x (dez: %i + %i = %i)",
#                 register_value, offset, ea,
#                 register_value, offset, ea
#             )

        ea = ea & 0xffff


# 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

#                 self.program_counter, value_signed, ea,
#             )
        elif addr_mode == 0xd:
            #             log.debug("\t1101 0xd | n, PCR | 16 bit offset from program counter")
            __, value = self.read_pc_word()
            value_signed = signed16(value)
            ea = self.program_counter.value + value_signed
            self.cycles += 1
#             log.debug("\tea = pc($%x) + $%x = $%x (dez.: %i + %i = %i)",
#                 self.program_counter, value_signed, ea,
#                 self.program_counter, value_signed, ea,
#             )
        elif addr_mode == 0xe:
            #             log.error("\tget_ea_indexed(): illegal address mode, use 0xffff")
            ea = 0xffff  # illegal
        elif addr_mode == 0xf:
            #             log.debug("\t1111 0xf | [n] | 16 bit address - extended indirect")
            __, ea = self.read_pc_word()
        else:
            raise RuntimeError(f"Illegal indexed addressing mode: ${addr_mode:x}")

        if offset is not None:
            ea = register_value + offset
#             log.debug("\t$%x + $%x = $%x (dez: %i + %i = %i)",
#                 register_value, offset, ea,
#                 register_value, offset, ea
#             )

        ea = ea & 0xffff


        if is_bit_set(postbyte, bit=4):  # bit 4 is 1 -> Indirect