From 2089c1cf2c29d63ed97cf62b183e51bd440b485c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 14 Jan 2023 22:18:42 +0200 Subject: [PATCH] Make CPU speed configurable when running program --- sipsi-8.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/sipsi-8.py b/sipsi-8.py index b951f1a..691f197 100644 --- a/sipsi-8.py +++ b/sipsi-8.py @@ -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