Coverage for MC6809/components/mc6809_speedlimited.py: 21%
24 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-06 19:50 +0100
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-06 19:50 +0100
1#!/usr/bin/env python
3"""
4 MC6809 - 6809 CPU emulator in Python
5 =======================================
7 6809 is Big-Endian
9 Links:
10 http://dragondata.worldofdragon.org/Publications/inside-dragon.htm
11 http://www.burgins.com/m6809.html
12 http://koti.mbnet.fi/~atjs/mc6809/
14 :copyleft: 2013-2015 by the MC6809 team, see AUTHORS for more details.
15 :license: GNU GPL v3 or above, see LICENSE for more details.
17 Based on:
18 * ApplyPy by James Tauber (MIT license)
19 * XRoar emulator by Ciaran Anscomb (GPL license)
20 more info, see README
21"""
24import time
27class CPUSpeedLimitMixin:
28 max_delay = 0.01 # maximum time.sleep() value per burst run
29 delay = 0 # the current time.sleep() value per burst run
31 def delayed_burst_run(self, target_cycles_per_sec):
32 """ Run CPU not faster than given speedlimit """
33 old_cycles = self.cycles
34 start_time = time.time()
36 self.burst_run()
38 is_duration = time.time() - start_time
39 new_cycles = self.cycles - old_cycles
40 try:
41 is_cycles_per_sec = new_cycles / is_duration
42 except ZeroDivisionError:
43 pass
44 else:
45 should_burst_duration = is_cycles_per_sec / target_cycles_per_sec
46 target_duration = should_burst_duration * is_duration
47 delay = target_duration - is_duration
48 if delay > 0:
49 if delay > self.max_delay:
50 self.delay = self.max_delay
51 else:
52 self.delay = delay
53 time.sleep(self.delay)
55 self.call_sync_callbacks()