This project is educational and Open Source. No code is copied from other emulators. Implementation based solely on technical documentation and permitted tests.
Add Debug Timer to Title Bar
Summary
Implemented a debug timer in the emulator title bar that shows the time elapsed since the emulator starts and the time until the first event (frame ready). The timer is initialized at the start of `run()`, detects when the first ready frame occurs, and updates the title bar every 60 frames with the elapsed time and the time until the first event (if it has already occurred).
Hardware Concept
Timing and Events in Emulation:
In an emulator, it is important to be able to measure the time from when the emulation starts until important events occur. The first critical event is when the PPU marks a frame as ready (`frame_ready_ = true`), indicating that the emulator has completed its first rendering cycle.
The time to first event is useful for diagnosing initialization problems, since if the emulator takes too long to generate the first frame, it may indicate problems in component initialization or synchronization between the CPU and PPU.
The total elapsed time allows you to monitor the emulator's performance and verify that it is running at the correct speed (60 FPS).
Implementation
Added three instance variables to the debug timer:
_start_time: Emulator startup time (when callingrun())_first_event_time: Time when the first event occurs (None until it occurs)_first_event_detected: Flag to indicate if the first event has already been detected
Components created/modified
src/viboy.py: Added instance variables in__init__(), timer initialization inrun(), detection of the first event, and updating the title with the timer
Design decisions
Initialization inrun():The timer is initialized at the beginning ofrun()instead of in__init__()because the emulator can be initialized multiple times, but we only want to measure the time since the execution starts.
Detection of the first event:It is detected whenget_frame_ready_and_reset()returnstruefor the first time, indicating that the PPU has completed its first frame.
Title update:The title updates every 60 frames (approximately every second) to avoid overload, and shows both the total elapsed time and the time until the first event (if it has already occurred).
Affected Files
src/viboy.py- Added instance variables for the timer, initialization of the timer inrun(), detection of the first event, and updating the title with the timer
Tests and Verification
The implementation was verified by:
- Visual verification:The timer appears in the title bar and updates correctly
- Logs:The logs
[Viboy-Timer]appear when the emulator starts and when the first event is detected - Title format:The title shows the correct format:
"Viboy Color v0.0.2 - [Game Name] - FPS: XX.X - Time: XX.XXXs | First Event: XX.XXXs"
Test command:
timeout 30 python3 main.py roms/tetris.gb
Expected result:
- The timer appears in the title bar from the beginning
- The time is updated approximately every second
- When the first frame occurs, the time until the first event is also shown
- The logs
[Viboy-Timer]appear on the console
Sources consulted
- Implementation based on general knowledge of timing and events in emulation
- Python documentation
time.time()for time measurement
Educational Integrity
What I Understand Now
- Timing in emulation:It is important to measure the time since the start of the emulation to diagnose performance and synchronization problems
- Event detection:The first important event is when the PPU marks a frame as ready, indicating that the emulator has completed its first rendering cycle.
- UI Update:The title bar updates periodically (every 60 frames) to display debug information without overloading the system
What remains to be confirmed
- Verify that the timer works correctly in different ROMs
- Confirm that the time to first event is useful for diagnosing initialization problems
Hypotheses and Assumptions
Time to first event is assumed to be a good indicator of initialization problems. If the emulator takes more than a few seconds to generate the first frame, it may indicate problems in component initialization or synchronization between the CPU and PPU.
Next Steps
- [ ] Continue with the log analysis of Step 0343
- [ ] Step 0345: Detailed analysis of framebuffer-display correspondence based on the logs of Step 0343
- [ ] Step 0346: Implement correction based on findings