@@ -84,7 +84,8 @@ def __init__(
84
84
jump_quirks = False ,
85
85
clip_quirks = False ,
86
86
logic_quirks = False ,
87
- mem_size = "64K"
87
+ mem_size = "64K" ,
88
+ max_ticks = 1000 ,
88
89
):
89
90
"""
90
91
Initialize the Chip8 CPU. The only required parameter is a screen
@@ -98,6 +99,7 @@ def __init__(
98
99
:param clip_quirks: enables screen clipping quirks
99
100
:param logic_quirks: enables logic quirks
100
101
:param mem_size: sets the maximum memory available "4K" or "64K"
102
+ :param max_ticks: sets the maximum allowable operations per second
101
103
"""
102
104
self .last_pc = 0x0000
103
105
self .last_op = "None"
@@ -109,6 +111,11 @@ def __init__(
109
111
self .index = 0
110
112
self .rpl = [0 ] * NUM_REGISTERS
111
113
114
+ self .tick_counter = 0
115
+ self .max_ticks = max_ticks
116
+ if self .max_ticks < 200 :
117
+ self .max_ticks = 200
118
+
112
119
self .pitch = 64
113
120
self .playback_rate = 4000
114
121
self .audio_pattern_buffer = [0 ] * 16
@@ -226,6 +233,9 @@ def execute_instruction(self, operand=None):
226
233
:param operand: the operand to execute
227
234
:return: returns the operand executed
228
235
"""
236
+ if self .tick_counter > self .max_ticks :
237
+ return None
238
+
229
239
self .last_pc = self .pc
230
240
if operand :
231
241
self .operand = operand
@@ -236,6 +246,7 @@ def execute_instruction(self, operand=None):
236
246
self .pc += 2
237
247
operation = (self .operand & 0xF000 ) >> 12
238
248
self .operation_lookup [operation ]()
249
+ self .tick_counter += 1
239
250
return self .operand
240
251
241
252
def execute_logical_instruction (self ):
@@ -1249,6 +1260,7 @@ def reset(self):
1249
1260
self .sound_playing = False
1250
1261
self .sound_waveform = None
1251
1262
self .bitplane = 1
1263
+ self .tick_counter = 0
1252
1264
1253
1265
def load_rom (self , filename , offset = PROGRAM_COUNTER_START ):
1254
1266
"""
@@ -1269,6 +1281,8 @@ def decrement_timers(self):
1269
1281
"""
1270
1282
Decrement both the sound and delay timer.
1271
1283
"""
1284
+ self .tick_counter = 0
1285
+
1272
1286
self .delay -= 1 if self .delay > 0 else 0
1273
1287
self .sound -= 1 if self .delay > 0 else 0
1274
1288
if self .sound > 0 and not self .sound_playing :
0 commit comments