# Path: avatar2/targets/target.py
# class TargetStates(IntEnum):
#     """
#     A simple Enum for the different states a target can be in.
#     """
#     CREATED = 0x1
#     INITIALIZED = 0x2
#     STOPPED = 0x4
#     RUNNING = 0x8
#     SYNCING = 0x10
#     EXITED = 0x20
#     NOT_RUNNING = INITIALIZED | STOPPED
#     BREAKPOINT = SYNCING | STOPPED
# 
# Path: avatar2/message.py
# class AvatarMessage(object):
#     def __init__(self, origin):
#         self.origin = origin
# 
#     def __str__(self):
#         if self.origin:
#             return "%s from %s" % (self.__class__.__name__, self.origin.name)
#         else:
#             return "%s from unkown origin" % self.__class__.__name__
# 
# class UpdateStateMessage(AvatarMessage):
#     def __init__(self, origin, new_state):
#         super(UpdateStateMessage, self).__init__(origin)
#         self.state = new_state
# 
# class BreakpointHitMessage(UpdateStateMessage):
#     def __init__(self, origin, breakpoint_number, address):
#         super(BreakpointHitMessage, self).__init__(origin, TargetStates.BREAKPOINT)
#         self.breakpoint_number = breakpoint_number
#         self.address = address


import sys
import logging
import struct
import ctypes
    import Queue as queue
    import queue
import usb.core
import usb.util
from math import ceil
from collections import OrderedDict
from os.path import abspath
from avatar2.targets import TargetStates
from avatar2.message import AvatarMessage, UpdateStateMessage, BreakpointHitMessage

        # Execute a step by setting the C_STEP bit to 1 in DHCSR register
        self.write_memory(0xE000EDF0, 4, (0xA05F << 16) | 0b101)

        avatar_msg = UpdateStateMessage(self._origin, TargetStates.RUNNING)
        self._fast_queue.put(avatar_msg)

        # Check the core is halted 
        DHCSR = self.read_memory(0xE000EDF0, 4)
        if not ((DHCSR >> 1) & 1):
            self.log.warning("Core is not halted after single step")

        avatar_msg = UpdateStateMessage(self._origin, TargetStates.STOPPED)
        self._fast_queue.put(avatar_msg)

        return True

    def wait(self, state):
        """
        Wait until CPU reach provided state.

        :state:  state of the CPU
        """

        halt = True if state == TargetStates.STOPPED else False
        
        self.log.debug("Wait until target reach %s state" % state)
        while (self.check_halt() != halt):
            pass


# Path: avatar2/targets/target.py
# class TargetStates(IntEnum):
#     """
#     A simple Enum for the different states a target can be in.
#     """
#     CREATED = 0x1
#     INITIALIZED = 0x2
#     STOPPED = 0x4
#     RUNNING = 0x8
#     SYNCING = 0x10
#     EXITED = 0x20
#     NOT_RUNNING = INITIALIZED | STOPPED
#     BREAKPOINT = SYNCING | STOPPED
# 
# Path: avatar2/message.py
# class AvatarMessage(object):
#     def __init__(self, origin):
#         self.origin = origin
# 
#     def __str__(self):
#         if self.origin:
#             return "%s from %s" % (self.__class__.__name__, self.origin.name)
#         else:
#             return "%s from unkown origin" % self.__class__.__name__
# 
# class UpdateStateMessage(AvatarMessage):
#     def __init__(self, origin, new_state):
#         super(UpdateStateMessage, self).__init__(origin)
#         self.state = new_state
# 
# class BreakpointHitMessage(UpdateStateMessage):
#     def __init__(self, origin, breakpoint_number, address):
#         super(BreakpointHitMessage, self).__init__(origin, TargetStates.BREAKPOINT)
#         self.breakpoint_number = breakpoint_number
#         self.address = address


# Path: avatar2/protocols/inception.py
import sys
import logging
import struct
import ctypes
    import Queue as queue
    import queue
import usb.core
import usb.util
from math import ceil
from collections import OrderedDict
from os.path import abspath
from avatar2.targets import TargetStates
from avatar2.message import AvatarMessage, UpdateStateMessage, BreakpointHitMessage


        # Execute a step by setting the C_STEP bit to 1 in DHCSR register
        self.write_memory(0xE000EDF0, 4, (0xA05F << 16) | 0b101)

        avatar_msg = UpdateStateMessage(self._origin, TargetStates.RUNNING)
        self._fast_queue.put(avatar_msg)

        # Check the core is halted 
        DHCSR = self.read_memory(0xE000EDF0, 4)
        if not ((DHCSR >> 1) & 1):
            self.log.warning("Core is not halted after single step")

        avatar_msg = UpdateStateMessage(self._origin, TargetStates.STOPPED)
        self._fast_queue.put(avatar_msg)

        return True

    def wait(self, state):
        """
        Wait until CPU reach provided state.

        :state:  state of the CPU
        """

        halt = True if state == TargetStates.STOPPED else False
        
        self.log.debug("Wait until target reach %s state" % state)
        while (self.check_halt() != halt):
            pass


        avatar_msg = UpdateStateMessage(self._origin, state)