This project is educational and Open Source. No code is copied from other emulators. Implementation based solely on technical documentation and permitted tests.
Fix: Cython Properties for Interrupt Tests
Summary
Fixed three interrupt tests that were failing due to trying to access propertiesimeandhalteddirectly in the instance ofPyCPU, but the Cython wrapper only exposed methodsget_ime()andget_halted(). Added Python properties using decorator@propertyin the Cython wrapper to allow direct access to these values, while maintaining compatibility with existing tests.
Hardware Concept
The Cython wrapper acts as a bridge between Python and C++, allowing Python code to access functionality implemented in C++ efficiently. In Python, it is common to access object properties using attribute syntax (ex:cpu.ime) instead of methods (ex:cpu.get_ime()), especially in tests where a more natural and readable API is sought.
The decorator@propertyPython allows you to convert methods into properties, allowing code to access values as if they were attributes, but keeping the access logic encapsulated. This is especially useful in Cython wrappers where we want to expose C++ values naturally from Python.
Implementation
Two properties added to the Cython wrapperPyCPUinsrc/core/cython/cpu.pyx:
Modified components
src/core/cython/cpu.pyx: Properties addedimeandhaltedusing the decorator@property.tests/test_core_cpu_interrupts.py: The test was correctedtest_halt_wakeup_on_interruptto reflect correct hardware behavior when the CPU wakes up from HALT without processing the interrupt.
Design decisions
It was decided to maintain both the methodsget_ime()andget_halted()like the propertiesimeandhaltedto maintain compatibility with existing code and allow a more natural API in tests. Properties simply call the underlying getter methods, keeping the logic centralized.
In addition, the test was correctedtest_halt_wakeup_on_interruptto reflect correct hardware behavior: when the CPU wakes up from HALT without processing the interrupt (because IME is disabled), the CPU executes the next instruction, so the PC should advance. This matches the behavior documented in Pan Docs and the old Python test.
Affected Files
src/core/cython/cpu.pyx- Added propertiesimeandhaltedtests/test_core_cpu_interrupts.py- Corrected testtest_halt_wakeup_on_interrupt
Tests and Verification
All interruption tests were run to verify that the fix works correctly:
pytest tests/test_core_cpu_interrupts.py -v
Result:7 tests passed correctly:
- ✅
test_di_disables_ime- PASSED - ✅
test_ei_delayed_activation- PASSED - ✅
test_halt_stops_execution- PASSED - ✅
test_halt_wakeup_on_interrupt- PASSED - ✅
test_interrupt_dispatch_vblank- PASSED - ✅
test_interrupt_priority- PASSED - ✅
test_all_interrupt_vectors- PASSED
Compiled C++ module validation:The module was successfully recompiled after adding the properties, confirming that the Cython syntax is correct and that the properties are correctly exposed from C++.
Sources consulted
- Cython Documentation:https://cython.readthedocs.io/- @property decorator in Cython classes
- Python Documentation:https://docs.python.org/3/library/functions.html#property- Decorator @property
- Pan Docs: HALT behavior when interrupts are pending but IME is disabled
Educational Integrity
What I Understand Now
- Properties in Cython:The decorator
@propertyIt works the same in Cython as in pure Python, allowing you to create properties that access C++ values naturally. - HALT Behavior:When the CPU is in HALT and there is an interrupt pending but IME is disabled, the CPU wakes up and executes the next instruction, so the PC proceeds normally.
- API Support:It is important to maintain both methods and properties when exposing C++ code to Python, to maintain compatibility with different code styles.
What remains to be confirmed
- Nothing pending - all tests pass and the behavior matches the documentation.
Hypotheses and Assumptions
There are no assumptions - behavior is documented and validated by tests.
Next Steps
- [ ] Continue analyzing the triggered trace to identify unimplemented opcodes
- [ ] Implement missing opcodes that block graphics rendering