Make CPU speed configurable when running program

This commit is contained in:
Juhani Krekelä 2023-01-14 22:18:42 +02:00
parent 66e530d225
commit 2089c1cf2c
1 changed files with 17 additions and 7 deletions

View File

@ -705,7 +705,7 @@ def initialize_keyboard():
waiting_for_keypress = False
keypress_arrived = False
def initialize_cpu():
def initialize_cpu(clockspeed):
global cpu_clock, data_registers, ip, stack, i_register
data_registers = [0] * 16
@ -718,10 +718,7 @@ def initialize_cpu():
i_register = 0
# TODO: Don't hardcode the update speed
# Run the interpreter at 500Hz
# This was picked from https://github.com/AfBu/haxe-CHIP-8-emulator/wiki/(Super)CHIP-8-Secrets
cpu_clock = 500
cpu_clock = clockspeed
def load_program(f):
global ram
@ -733,6 +730,19 @@ def load_program(f):
def main():
global window
global cpu_cycles_to_go, frames_to_go
if len(sys.argv) == 2:
program_path = sys.argv[1]
# Run the interpreter at 500Hz by default
# This was picked from https://github.com/AfBu/haxe-CHIP-8-emulator/wiki/(Super)CHIP-8-Secrets
cpu_clock = 500
elif len(sys.argv) == 3:
program_path = sys.argv[1]
cpu_clock = int(sys.argv[2])
else:
print(f'Usage: {sys.argv[0]} program [cpu-speed]', file=sys.stderr)
sys.exit(1)
# TODO: Don't hardcode the size
window = pyglet.window.Window(640, 320, resizable = True)
@ -760,10 +770,10 @@ def main():
initialize_screen()
initialize_timers()
initialize_keyboard()
initialize_cpu()
initialize_cpu(cpu_clock)
# TODO: Deal with missing file gracefully
with open(sys.argv[1], 'rb') as f:
with open(program_path, 'rb') as f:
load_program(f)
cpu_cycles_to_go = 0