# Path: VM/util.py
# class Instruction(metaclass=InstructionMeta):
#     """
#     This class is here for convenience purposes only since it's much simpler (a.k.a. easier to type) to inherit from a class
#      than to use some weird metaclass. Also, all classes inheriting this one are automagically registered by the metaclass.
#     """
#     ...
# 
# Path: VM/misc.py
# def parity(num: int) -> int:
#     """
#     Calculate the parity of the least significant byte of a number.
#     See: https://graphics.stanford.edu/~seander/bithacks.html#ParityWith64Bits
#     :param num: The byte to calculate the parity of.
#     :return:
#     """
# 
#     return ((((num & 0xFF) * 0x0101010101010101) & 0x8040201008040201) % 0x1FF) & 1
# 
# def sign_extend(num: int, nbytes: int) -> int:
#     # TODO: this is basically converting an unsigned number to signed. Maybe rename the function?
#     """
#     See: https://stackoverflow.com/a/32031543/4354477
#     :param num: The integer to sign-extend
#     :param nbytes: The number of bytes _in that integer_.
#     :return:
#     """
#     if num < 0:
#         return num
#     
#     sign_bit = 1 << (nbytes * 8 - 1)
#     return (num & (sign_bit - 1)) - (num & sign_bit)


import enum
    import logging
from ..util import Instruction
from ..misc import parity, sign_extend
from functools import partialmethod as P
    from ..debug import debug_operand, debug_register_operand

        if _REG != REG:
            return False

        dec = REG
        sz = 1 if _8bit else vm.operand_size

        RM, R = vm.process_ModRM()
        type, loc = RM

        a = (type).get(loc, sz)
        b = 1

        c = a + (b if not dec else MAXVALS[sz] - 1 + b)

        sign_a = (a >> (sz * 8 - 1)) & 1
        sign_b = (b >> (sz * 8 - 1)) & 1
        sign_c = (c >> (sz * 8 - 1)) & 1

        if not dec:
            vm.reg.eflags.OF = (sign_a == sign_b) and (sign_a != sign_c)
            vm.reg.eflags.AF = ((a & 255) + (b & 255)) > MAXVALS[1]
        else:
            vm.reg.eflags.OF = (sign_a != sign_b) and (sign_a != sign_c)
            vm.reg.eflags.AF = (b & 255) > (a & 255)

        c &= MAXVALS[sz]

        vm.reg.eflags.SF = sign_c
        vm.reg.eflags.ZF = c == 0

# Path: VM/util.py
# class Instruction(metaclass=InstructionMeta):
#     """
#     This class is here for convenience purposes only since it's much simpler (a.k.a. easier to type) to inherit from a class
#      than to use some weird metaclass. Also, all classes inheriting this one are automagically registered by the metaclass.
#     """
#     ...
# 
# Path: VM/misc.py
# def parity(num: int) -> int:
#     """
#     Calculate the parity of the least significant byte of a number.
#     See: https://graphics.stanford.edu/~seander/bithacks.html#ParityWith64Bits
#     :param num: The byte to calculate the parity of.
#     :return:
#     """
# 
#     return ((((num & 0xFF) * 0x0101010101010101) & 0x8040201008040201) % 0x1FF) & 1
# 
# def sign_extend(num: int, nbytes: int) -> int:
#     # TODO: this is basically converting an unsigned number to signed. Maybe rename the function?
#     """
#     See: https://stackoverflow.com/a/32031543/4354477
#     :param num: The integer to sign-extend
#     :param nbytes: The number of bytes _in that integer_.
#     :return:
#     """
#     if num < 0:
#         return num
#     
#     sign_bit = 1 << (nbytes * 8 - 1)
#     return (num & (sign_bit - 1)) - (num & sign_bit)


# Path: VM/instructions/math.py
import enum
    import logging
from ..util import Instruction
from ..misc import parity, sign_extend
from functools import partialmethod as P
    from ..debug import debug_operand, debug_register_operand


        if _REG != REG:
            return False

        dec = REG
        sz = 1 if _8bit else vm.operand_size

        RM, R = vm.process_ModRM()
        type, loc = RM

        a = (type).get(loc, sz)
        b = 1

        c = a + (b if not dec else MAXVALS[sz] - 1 + b)

        sign_a = (a >> (sz * 8 - 1)) & 1
        sign_b = (b >> (sz * 8 - 1)) & 1
        sign_c = (c >> (sz * 8 - 1)) & 1

        if not dec:
            vm.reg.eflags.OF = (sign_a == sign_b) and (sign_a != sign_c)
            vm.reg.eflags.AF = ((a & 255) + (b & 255)) > MAXVALS[1]
        else:
            vm.reg.eflags.OF = (sign_a != sign_b) and (sign_a != sign_c)
            vm.reg.eflags.AF = (b & 255) > (a & 255)

        c &= MAXVALS[sz]

        vm.reg.eflags.SF = sign_c
        vm.reg.eflags.ZF = c == 0

        vm.reg.eflags.PF = parity(c)