# Path: dragonpy/tests/test_base.py
# class Test6809_Dragon32_Base(BaseCPUTestCase):
#     """
#     Run tests with the Dragon32 ROM.
#     """
#     TEMP_FILE = os.path.join(
#         tempfile.gettempdir(),
#         f"DragonPy_Dragon32_unittests_Py{sys.version_info[0]:d}.dat"
#     )
# 
#     @classmethod
#     def setUpClass(cls, cmd_args=None):
#         """
#         prerun ROM to complete initiate and ready for user input.
#         save the CPU state to speedup unittest
#         """
#         super().setUpClass()
# 
#         log.info(f"CPU state pickle file: {cls.TEMP_FILE!r}")
# #         os.remove(cls.TEMP_FILE);print "Delete CPU date file!"
# 
#         cfg = Dragon32Cfg(cls.UNITTEST_CFG_DICT)
# 
#         cls.user_input_queue = queue.Queue()
# 
#         cls.machine = Machine(
#             cfg,
#             periphery_class=Dragon32PeripheryUnittest,
#             display_callback=None,
#             user_input_queue=cls.user_input_queue,
#         )
#         cls.cpu = cls.machine.cpu
#         cls.periphery = cls.machine.periphery
#         cls.periphery.setUp()
# 
# #        os.remove(cls.TEMP_FILE)
#         try:
#             temp_file = open(cls.TEMP_FILE, "rb")
#         except OSError:
#             log.info("init machine...")
#             init_start = time.time()
#             cls.cpu.test_run(
#                 start=cls.cpu.program_counter.value,
#                 end=cfg.STARTUP_END_ADDR,
#             )
#             duration = time.time() - init_start
#             log.info("done in %iSec. it's %.2f cycles/sec. (current cycle: %i)" % (
#                 duration, float(cls.cpu.cycles / duration), cls.cpu.cycles
#             ))
# 
#             # Check if machine is ready
#             output = cls.periphery.striped_output()[:5]
#             assert output == [
#                 '(C) 1982 DRAGON DATA LTD',
#                 '16K BASIC INTERPRETER 1.0',
#                 '(C) 1982 BY MICROSOFT',
#                 '', 'OK'
#             ]
#             # Save CPU state
#             init_state = cls.cpu.get_state()
#             with open(cls.TEMP_FILE, "wb") as f:
#                 pickle.dump(init_state, f)
#                 log.info(f"Save CPU init state to: {cls.TEMP_FILE!r}")
#             cls.__init_state = init_state
#         else:
#             log.info(f"Load CPU init state from: {cls.TEMP_FILE!r}")
#             cls.__init_state = pickle.load(temp_file)
#             temp_file.close()
# 
# #        print "cls.__init_state:", ;print_cpu_state_data(cls.__init_state)
# 
#     def setUp(self):
#         """ restore CPU/Periphery state to a fresh startup. """
#         self.periphery.setUp()
# #        print "self.__init_state:", ;print_cpu_state_data(self.__init_state)
#         self.cpu.set_state(self.__init_state)
# #        print "self.cpu.get_state():", ;print_cpu_state_data(self.cpu.get_state())
# 
#     def _run_until_OK(self, OK_count=1, max_ops=5000):
#         old_cycles = self.cpu.cycles
#         existing_OK_count = 0
#         for op_call_count in range(max_ops):
#             try:
#                 self.cpu.get_and_call_next_op()
#             except Exception as err:
#                 log.critical("Execute Error: %s", err)
#                 cycles = self.cpu.cycles - old_cycles
#                 return op_call_count, cycles, self.periphery.striped_output()
# 
#             output_lines = self.periphery.output_lines
#             if output_lines[-1] == "OK":
#                 existing_OK_count += 1
#             if existing_OK_count >= OK_count:
#                 cycles = self.cpu.cycles - old_cycles
#                 return op_call_count, cycles, self.periphery.striped_output()
# 
#         msg = "ERROR: Abort after %i op calls (%i cycles)" % (
#             op_call_count, (self.cpu.cycles - old_cycles)
#         )
#         raise self.failureException(msg)
# 
#     def _run_until_response(self, max_ops=10000):
#         old_cycles = self.cpu.cycles
#         for op_call_count in range(max_ops):
#             self.cpu.get_and_call_next_op()
#             try:
#                 result = self.response_queue.get(block=False)
#             except queue.Empty:
#                 continue
#             else:
#                 cycles = self.cpu.cycles - old_cycles
#                 return op_call_count, cycles, result
# 
#         msg = "ERROR: Abort after %i op calls (%i cycles)" % (
#             op_call_count, (self.cpu.cycles - old_cycles)
#         )
#         raise self.failureException(msg)


import logging
import unittest
from dragonpy.tests.test_base import Test6809_Dragon32_Base
#!/usr/bin/env python

"""
    6809 unittests
    ~~~~~~~~~~~~~~

    Test CPU with BASIC Interpreter from Dragon32 ROM.

    :created: 2014 by Jens Diemer - www.jensdiemer.de
    :copyleft: 2014 by the DragonPy team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""





log = logging.getLogger(__name__)



# Path: dragonpy/tests/test_base.py
# class Test6809_Dragon32_Base(BaseCPUTestCase):
#     """
#     Run tests with the Dragon32 ROM.
#     """
#     TEMP_FILE = os.path.join(
#         tempfile.gettempdir(),
#         f"DragonPy_Dragon32_unittests_Py{sys.version_info[0]:d}.dat"
#     )
# 
#     @classmethod
#     def setUpClass(cls, cmd_args=None):
#         """
#         prerun ROM to complete initiate and ready for user input.
#         save the CPU state to speedup unittest
#         """
#         super().setUpClass()
# 
#         log.info(f"CPU state pickle file: {cls.TEMP_FILE!r}")
# #         os.remove(cls.TEMP_FILE);print "Delete CPU date file!"
# 
#         cfg = Dragon32Cfg(cls.UNITTEST_CFG_DICT)
# 
#         cls.user_input_queue = queue.Queue()
# 
#         cls.machine = Machine(
#             cfg,
#             periphery_class=Dragon32PeripheryUnittest,
#             display_callback=None,
#             user_input_queue=cls.user_input_queue,
#         )
#         cls.cpu = cls.machine.cpu
#         cls.periphery = cls.machine.periphery
#         cls.periphery.setUp()
# 
# #        os.remove(cls.TEMP_FILE)
#         try:
#             temp_file = open(cls.TEMP_FILE, "rb")
#         except OSError:
#             log.info("init machine...")
#             init_start = time.time()
#             cls.cpu.test_run(
#                 start=cls.cpu.program_counter.value,
#                 end=cfg.STARTUP_END_ADDR,
#             )
#             duration = time.time() - init_start
#             log.info("done in %iSec. it's %.2f cycles/sec. (current cycle: %i)" % (
#                 duration, float(cls.cpu.cycles / duration), cls.cpu.cycles
#             ))
# 
#             # Check if machine is ready
#             output = cls.periphery.striped_output()[:5]
#             assert output == [
#                 '(C) 1982 DRAGON DATA LTD',
#                 '16K BASIC INTERPRETER 1.0',
#                 '(C) 1982 BY MICROSOFT',
#                 '', 'OK'
#             ]
#             # Save CPU state
#             init_state = cls.cpu.get_state()
#             with open(cls.TEMP_FILE, "wb") as f:
#                 pickle.dump(init_state, f)
#                 log.info(f"Save CPU init state to: {cls.TEMP_FILE!r}")
#             cls.__init_state = init_state
#         else:
#             log.info(f"Load CPU init state from: {cls.TEMP_FILE!r}")
#             cls.__init_state = pickle.load(temp_file)
#             temp_file.close()
# 
# #        print "cls.__init_state:", ;print_cpu_state_data(cls.__init_state)
# 
#     def setUp(self):
#         """ restore CPU/Periphery state to a fresh startup. """
#         self.periphery.setUp()
# #        print "self.__init_state:", ;print_cpu_state_data(self.__init_state)
#         self.cpu.set_state(self.__init_state)
# #        print "self.cpu.get_state():", ;print_cpu_state_data(self.cpu.get_state())
# 
#     def _run_until_OK(self, OK_count=1, max_ops=5000):
#         old_cycles = self.cpu.cycles
#         existing_OK_count = 0
#         for op_call_count in range(max_ops):
#             try:
#                 self.cpu.get_and_call_next_op()
#             except Exception as err:
#                 log.critical("Execute Error: %s", err)
#                 cycles = self.cpu.cycles - old_cycles
#                 return op_call_count, cycles, self.periphery.striped_output()
# 
#             output_lines = self.periphery.output_lines
#             if output_lines[-1] == "OK":
#                 existing_OK_count += 1
#             if existing_OK_count >= OK_count:
#                 cycles = self.cpu.cycles - old_cycles
#                 return op_call_count, cycles, self.periphery.striped_output()
# 
#         msg = "ERROR: Abort after %i op calls (%i cycles)" % (
#             op_call_count, (self.cpu.cycles - old_cycles)
#         )
#         raise self.failureException(msg)
# 
#     def _run_until_response(self, max_ops=10000):
#         old_cycles = self.cpu.cycles
#         for op_call_count in range(max_ops):
#             self.cpu.get_and_call_next_op()
#             try:
#                 result = self.response_queue.get(block=False)
#             except queue.Empty:
#                 continue
#             else:
#                 cycles = self.cpu.cycles - old_cycles
#                 return op_call_count, cycles, result
# 
#         msg = "ERROR: Abort after %i op calls (%i cycles)" % (
#             op_call_count, (self.cpu.cycles - old_cycles)
#         )
#         raise self.failureException(msg)


# Path: dragonpy/tests/test_BASIC_Dragon32.py
import logging
import unittest
from dragonpy.tests.test_base import Test6809_Dragon32_Base

#!/usr/bin/env python

"""
    6809 unittests
    ~~~~~~~~~~~~~~

    Test CPU with BASIC Interpreter from Dragon32 ROM.

    :created: 2014 by Jens Diemer - www.jensdiemer.de
    :copyleft: 2014 by the DragonPy team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""





log = logging.getLogger(__name__)



class Test_Dragon32_BASIC(Test6809_Dragon32_Base):