⚠️ Clean-Room / Educational

This project is educational and Open Source. No code is copied from other emulators. Implementation based solely on technical documentation and permitted tests.

Correction of Log Conditions and Correspondence Verification Framebuffer-Visualization

Date:2025-12-29 StepID:0342 State: VERIFIED

Summary

Correction of the conditions of the diagnostic logs so that they are executed more frequently (changing the condition oflen(frame_indices) == 23040tolen(frame_indices) > 0). New log blocks were added to verify the actual size of the framebuffer, the correspondence between the framebuffer and the display, and the order of reading and drawing pixels. The goal is to identify why the logs were not appearing and verify that the contents of the framebuffer are correctly reflected in the visualization.

Hardware Concept

Framebuffer format

The framebuffer is in 1D format:[y * 160 + x]where:

  • andis the line (0-143)
  • xis the column (0-159)
  • idx = y * 160 + xgives the index in the 1D array
  • Expected size: 160 × 144 = 23,040 pixels

This format allows storing a 160x144 pixel image in a linear array of 23040 elements. Each element contains a color index (0-3).

Pixel Order

The order of pixels in the framebuffer must follow these rules:

  • Horizontally adjacent pixels:Consecutive indices (difference = 1)
  • Vertically adjacent pixels:Indices separated by 160 (difference = 160)
  • Example:Pixel (x=0, y=0) is at index 0, pixel (x=1, y=0) is at index 1, pixel (x=0, y=1) is at index 160

If the order is incorrect, the display will show artifacts such as horizontal or vertical stripes instead of the expected pattern.

Framebuffer-Display Mapping

The correspondence between the framebuffer and the display must be exact:

  • The pixel in the framebuffer at position (x, y) must correspond to the pixel drawn on the surface at position (x, y)
  • The color index in the framebuffer should be correctly converted to RGB using the palette
  • The resulting RGB must match the color drawn on the surface (with tolerance for scaling interpolation)

Fountain:Pan Docs - "LCD Timing", "Frame Rate", "Tile Data", "LCD Control Register"

Implementation

Conditions of existing logs were corrected and new diagnostic log blocks were added insrc/gpu/renderer.py.

Log Conditions Correction

Changed the conditions of the diagnostic logs so that they are executed more frequently:

  • Pixel-Order Logs:Changed fromlen(frame_indices) == 23040tolen(frame_indices) > 0
  • RGB-Conversion Logs:Changed fromlen(frame_indices) == 23040tolen(frame_indices) > 0
  • Scale-Visualization Logs:Added verificationframe_indices is not None and len(frame_indices) > 0

Added warnings when the framebuffer size is not as expected (23040 pixels).

New Log Blocks

Added 3 new diagnostic log blocks:

  1. Verifying Actual Framebuffer Size:Checks framebuffer size when received and warns if it is not 23040 pixels
  2. Pixel Reading and Drawing Order Verification:Verify that the pixel order is correct (horizontally adjacent pixels have consecutive indices, vertically adjacent pixels have indices 160 apart)
  3. Correspondence Verification Between Framebuffer and Display:Verifies that the contents of the framebuffer are correctly reflected in the display by comparing pixels in the framebuffer with pixels on the surface after drawing

Design Decisions

Log Conditions:The condition oflen(frame_indices) == 23040tolen(frame_indices) > 0to allow logs to run as long as there is data in the framebuffer, regardless of size. This allows you to diagnose problems when the framebuffer size is not as expected.

Log Limits:Limits were maintained on the number of logs (10-20 depending on type) to avoid saturating the context and maintain performance.

Correspondence Tolerance:A tolerance of ±5 is used on each RGB component for correspondence checking, as scaling can cause interpolation that changes colors slightly.

Affected Files

  • src/gpu/renderer.py- Correction of log conditions and addition of new diagnostic log blocks

Tests and Verification

Tests were run with the 5 test ROMs (2.5 minutes each) to verify that the logs appear correctly and that the correspondence between framebuffer and visualization is correct.

  • Test ROMs:pkmn.gb, tetris.gb, mario.gbc, pkmn-amarillo.gb, Oro.gbc
  • Logs generated:logs/test_*_step0342.log
  • Analysis commands:commands were usedgrepwith limits to analyze the logs without saturating the context

Compiled C++ module validation:The C++ module was successfully recompiled without errors (only minor formatting warnings).

Sources consulted

  • Pan Docs: "LCD Timing", "Frame Rate", "Tile Data", "LCD Control Register"

Educational Integrity

What I Understand Now

  • Framebuffer format:The framebuffer is in 1D format with organization[y * 160 + x], where horizontally adjacent pixels have consecutive indices and vertically adjacent pixels have indices 160 apart.
  • Log Conditions:Strict conditions (such aslen(frame_indices) == 23040) can prevent logs from being executed when the framebuffer size varies or is not exactly what is expected.
  • Framebuffer-Display Correspondence:The correspondence between the framebuffer and the display should be verified by comparing specific pixels in the framebuffer with pixels drawn on the surface, with allowance for scaling interpolation.

What remains to be confirmed

  • Actual Framebuffer Size:Check if the framebuffer size is always 23040 or varies between frames.
  • Cause of Display Problem:Identify the root cause of the display problem (horizontal stripes instead of the expected checkerboard) based on the generated logs.

Hypotheses and Assumptions

It is assumed that the framebuffer size should always be 23040 pixels (160x144). If the size varies, this could indicate a problem with the PPU or the framebuffer read.

Next Steps

  • [ ] Analyze the generated logs to identify the actual size of the framebuffer and the correspondence between framebuffer and display
  • [ ] If the cause of the display problem is identified, implement the correction in the next step
  • [ ] If the problem persists, perform further analysis and workaround