# Path: dragonpy/constants.py
# DRAGON32 = "Dragon32"
# DRAGON64 = "Dragon64"
# COCO2B = "CoCo2b"
# SBC09 = "sbc09"
# SIMPLE6809 = "Simple6809"
# MULTICOMP6809 = "Multicomp6809"
# VECTREX = "Vectrex"
# 
# Path: dragonpy/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
# 
# def set_bit(value, bit):
#     """
#     returns an integer with the bit at offset >bit< set to 1.
# 
#     >>> set_bit(0x00, 2)
#     4
#     >>> set_bit(0x00, 7)
#     128
#     >>> '{0:08b}'.format(set_bit(int('00000000', 2), bit=5))
#     '00100000'
#     >>> '{0:08b}'.format(set_bit(int('10101010', 2), bit=0))
#     '10101011'
#     >>> '{0:08b}'.format(set_bit(int('1111000011110000', 2), bit=10))
#     '1111010011110000'
#     >>> '{0:08b}'.format(set_bit(int('11111111', 2), bit=3))
#     '11111111'
#     """
#     return value | 1 << bit
# 
# Path: dragonpy/utils/humanize.py
# def byte2bit_string(data):
#     """
#     >>> byte2bit_string(0x1b)
#     '00011011'
#     """
#     return f'{data:08b}'


import logging
import queue
from dragonpy import constants
from dragonpy.utils.bits import is_bit_set, set_bit
from dragonpy.utils.humanize import byte2bit_string

    # --------------------------------------------------------------------------

    def read_serial_interface(self, cpu_cycles, op_address, address):
        log.error("TODO: read from $%04x (D64 serial interface", address)
        return 0x00

    def write_serial_interface(self, cpu_cycles, op_address, address, value):
        log.error(
            "TODO: write $%02x to $%04x (D64 serial interface", value, address)

    # --------------------------------------------------------------------------
    # Keyboard matrix on PIA0

    def read_PIA0_A_data(self, cpu_cycles, op_address, address):
        """
        read from 0xff00 -> PIA 0 A side Data reg.

        bit 7 | PA7 | joystick comparison input
        bit 6 | PA6 | keyboard matrix row 7
        bit 5 | PA5 | keyboard matrix row 6
        bit 4 | PA4 | keyboard matrix row 5
        bit 3 | PA3 | keyboard matrix row 4 & left  joystick switch 2
        bit 2 | PA2 | keyboard matrix row 3 & right joystick switch 2
        bit 1 | PA1 | keyboard matrix row 2 & left  joystick switch 1
        bit 0 | PA0 | keyboard matrix row 1 & right joystick switch 1
        """
        pia0b = self.pia_0_B_data.value  # $ff02

        # FIXME: Find a way to handle CoCo and Dragon in the same way!

# Path: dragonpy/constants.py
# DRAGON32 = "Dragon32"
# DRAGON64 = "Dragon64"
# COCO2B = "CoCo2b"
# SBC09 = "sbc09"
# SIMPLE6809 = "Simple6809"
# MULTICOMP6809 = "Multicomp6809"
# VECTREX = "Vectrex"
# 
# Path: dragonpy/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
# 
# def set_bit(value, bit):
#     """
#     returns an integer with the bit at offset >bit< set to 1.
# 
#     >>> set_bit(0x00, 2)
#     4
#     >>> set_bit(0x00, 7)
#     128
#     >>> '{0:08b}'.format(set_bit(int('00000000', 2), bit=5))
#     '00100000'
#     >>> '{0:08b}'.format(set_bit(int('10101010', 2), bit=0))
#     '10101011'
#     >>> '{0:08b}'.format(set_bit(int('1111000011110000', 2), bit=10))
#     '1111010011110000'
#     >>> '{0:08b}'.format(set_bit(int('11111111', 2), bit=3))
#     '11111111'
#     """
#     return value | 1 << bit
# 
# Path: dragonpy/utils/humanize.py
# def byte2bit_string(data):
#     """
#     >>> byte2bit_string(0x1b)
#     '00011011'
#     """
#     return f'{data:08b}'


# Path: dragonpy/Dragon32/MC6821_PIA.py
import logging
import queue
from dragonpy import constants
from dragonpy.utils.bits import is_bit_set, set_bit
from dragonpy.utils.humanize import byte2bit_string


    # --------------------------------------------------------------------------

    def read_serial_interface(self, cpu_cycles, op_address, address):
        log.error("TODO: read from $%04x (D64 serial interface", address)
        return 0x00

    def write_serial_interface(self, cpu_cycles, op_address, address, value):
        log.error(
            "TODO: write $%02x to $%04x (D64 serial interface", value, address)

    # --------------------------------------------------------------------------
    # Keyboard matrix on PIA0

    def read_PIA0_A_data(self, cpu_cycles, op_address, address):
        """
        read from 0xff00 -> PIA 0 A side Data reg.

        bit 7 | PA7 | joystick comparison input
        bit 6 | PA6 | keyboard matrix row 7
        bit 5 | PA5 | keyboard matrix row 6
        bit 4 | PA4 | keyboard matrix row 5
        bit 3 | PA3 | keyboard matrix row 4 & left  joystick switch 2
        bit 2 | PA2 | keyboard matrix row 3 & right joystick switch 2
        bit 1 | PA1 | keyboard matrix row 2 & left  joystick switch 1
        bit 0 | PA0 | keyboard matrix row 1 & right joystick switch 1
        """
        pia0b = self.pia_0_B_data.value  # $ff02

        # FIXME: Find a way to handle CoCo and Dragon in the same way!

        if self.cfg.CONFIG_NAME == constants.COCO2B: