# 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
        bit 6 | IRQ 2 flag(not used)
        bit 5 | Control line 2 (CB2) is an output = 1
        bit 4 | Control line 2 (CB2) set by bit 3 = 1
        bit 3 | select line MSB of analog multiplexor (MUX): 0 = control line 2 LO / 1 = control line 2 HI
        bit 2 | set data direction: 0 = $FF02 is DDR / 1 = $FF02 is normal data lines
        bit 1 | control line 1 (CB1): IRQ polarity 0 = IRQ on HI to LO / 1 = IRQ on LO to HI
        bit 0 | VSYNC IRQ: 0 = disable IRQ / 1 = enable IRQ
        """
        log.critical(
            "%04x| write $%02x (%s) to $%04x -> PIA 0 B side Control reg.\t|%s",
            op_address, value, byte2bit_string(value),
            address, self.cfg.mem_info.get_shortest(op_address)
        )

        if is_bit_set(value, bit=0):
            log.critical(
                "%04x| write $%02x (%s) to $%04x -> VSYNC IRQ: enable\t|%s",
                op_address, value, byte2bit_string(value),
                address, self.cfg.mem_info.get_shortest(op_address)
            )
            self.cpu.irq_enabled = True
            value = set_bit(value, bit=7)
        else:
            log.critical(
                "%04x| write $%02x (%s) to $%04x -> VSYNC IRQ: disable\t|%s",
                op_address, value, byte2bit_string(value),
                address, self.cfg.mem_info.get_shortest(op_address)
            )
            self.cpu.irq_enabled = False


# 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

        bit 6 | IRQ 2 flag(not used)
        bit 5 | Control line 2 (CB2) is an output = 1
        bit 4 | Control line 2 (CB2) set by bit 3 = 1
        bit 3 | select line MSB of analog multiplexor (MUX): 0 = control line 2 LO / 1 = control line 2 HI
        bit 2 | set data direction: 0 = $FF02 is DDR / 1 = $FF02 is normal data lines
        bit 1 | control line 1 (CB1): IRQ polarity 0 = IRQ on HI to LO / 1 = IRQ on LO to HI
        bit 0 | VSYNC IRQ: 0 = disable IRQ / 1 = enable IRQ
        """
        log.critical(
            "%04x| write $%02x (%s) to $%04x -> PIA 0 B side Control reg.\t|%s",
            op_address, value, byte2bit_string(value),
            address, self.cfg.mem_info.get_shortest(op_address)
        )

        if is_bit_set(value, bit=0):
            log.critical(
                "%04x| write $%02x (%s) to $%04x -> VSYNC IRQ: enable\t|%s",
                op_address, value, byte2bit_string(value),
                address, self.cfg.mem_info.get_shortest(op_address)
            )
            self.cpu.irq_enabled = True
            value = set_bit(value, bit=7)
        else:
            log.critical(
                "%04x| write $%02x (%s) to $%04x -> VSYNC IRQ: disable\t|%s",
                op_address, value, byte2bit_string(value),
                address, self.cfg.mem_info.get_shortest(op_address)
            )
            self.cpu.irq_enabled = False


        if not is_bit_set(value, bit=2):