Skip to content

Commit c4da8f7

Browse files
committed
Fix failing HALT tests
1 parent 8ee165b commit c4da8f7

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

k0emu/tests/test_processor_interrupts.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,26 @@ def _make_processor_with_timer():
277277
class HaltTests(unittest.TestCase):
278278
"""Tests for the HALT instruction (0x71 0x10)."""
279279

280+
def _run_until_halt_wakes(self, proc, max_steps=5000):
281+
"""Step until HALT wakes (PC leaves address 0)."""
282+
for _ in range(max_steps):
283+
proc.step()
284+
if proc.pc != 0:
285+
return
286+
self.fail("HALT did not wake within %d steps" % max_steps)
287+
280288
def test_halt_wakes_on_pending_interrupt(self):
281289
proc, mem, intc, wt = _make_processor_with_timer()
282-
# HALT at address 0
283290
mem.write(0, 0x71)
284291
mem.write(1, 0x10)
285-
# ISR at vector 0x0024 (INTWTNI0) points to 0x2000
286292
mem.write(0x0024, 0x00)
287293
mem.write(0x0025, 0x20)
288-
# Enable watch timer: WTNM00=1, n=0, fw=128 -> interval=2048
289294
wt.write(0, 0x01)
290-
# Unmask INTWTNI0
291295
intc.write(intc.MK1L, intc.read(intc.MK1L) & 0xFE)
292296
proc.write_psw(Flags.IE | Flags.ISP)
293297
proc.write_sp(0xFE00)
294298
proc.pc = 0
295-
proc.step()
299+
self._run_until_halt_wakes(proc)
296300
self.assertEqual(proc.pc, 0x2000)
297301

298302
def test_halt_advances_cycles(self):
@@ -307,7 +311,7 @@ def test_halt_advances_cycles(self):
307311
proc.write_sp(0xFE00)
308312
proc.pc = 0
309313
cycles_before = proc.total_cycles
310-
proc.step()
314+
self._run_until_halt_wakes(proc)
311315
elapsed = proc.total_cycles - cycles_before
312316
self.assertGreaterEqual(elapsed, 2048)
313317

@@ -322,7 +326,7 @@ def test_halt_does_not_double_tick(self):
322326
proc.write_psw(Flags.IE | Flags.ISP)
323327
proc.write_sp(0xFE00)
324328
proc.pc = 0
325-
proc.step()
329+
self._run_until_halt_wakes(proc)
326330
self.assertLess(wt._prescaler_counter, 100)
327331

328332
def test_halt_with_interrupt_already_pending(self):
@@ -338,10 +342,8 @@ def test_halt_with_interrupt_already_pending(self):
338342
proc.pc = 0
339343
cycles_before = proc.total_cycles
340344
proc.step()
341-
# Should wake almost immediately
342345
self.assertEqual(proc.pc, 0x2000)
343346
elapsed = proc.total_cycles - cycles_before
344-
# Only the HALT fetch cycles + 1 tick to detect the pending interrupt
345347
self.assertLess(elapsed, 20)
346348

347349
def test_halt_resumes_at_next_instruction_when_ie_disabled(self):

0 commit comments

Comments
 (0)