Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: LCD Controller #4

Open
chrisledet opened this issue Oct 18, 2023 · 1 comment
Open

Feature: LCD Controller #4

chrisledet opened this issue Oct 18, 2023 · 1 comment
Labels
DMG Specific to Original Game Boy

Comments

@chrisledet
Copy link
Owner

chrisledet commented Oct 18, 2023

1. Setting up SDL2:

Ensure you have SDL2 set up and integrated with your development environment. You'll also want the SDL2_image extension if you decide to use image assets in the future.

2. Creating the Display Window:

Use SDL2 to create a window that represents the Game Boy's screen. The Game Boy has a resolution of 160x144 pixels, but you'll probably want to scale this up for modern displays.

3. Memory Map Integration:

Ensure your emulator correctly handles reads/writes to the following important memory locations:

  • 0xFF40: LCDC - LCD Control register
  • 0xFF41: STAT - LCD Status register
  • 0xFF42: SCY - Scroll Y
  • 0xFF43: SCX - Scroll X
  • 0xFF44: LY - LCDC Y-coordinate
  • 0xFF45: LYC - LY compare
  • 0xFF4A: WY - Window Y position
  • 0xFF4B: WX - Window X position minus 7

4. Scanline Rendering:

Implement scanline rendering. For each scanline:

  1. Check if the LCD is enabled.
  2. Render the background.
  3. Render the sprites (taking priority and flags into account).
  4. Render the window if it's enabled.

5. Palettes:

Handle color palettes correctly, including translating Game Boy color values to values that SDL2 can use. The Game Boy is monochrome, but games can assign shades based on various palettes.

6. Buffering and Refresh:

Keep an offscreen buffer where you render the Game Boy's graphics. Once a frame is done (all scanlines rendered), you can present this buffer to your SDL2 window. This ensures smooth graphics and minimizes tearing.

7. Interrupts and STAT Register:

Update the STAT register and fire LCD STAT interrupts when appropriate. The Game Boy fires interrupts based on specific LCD conditions, like when a scanline matches the LYC.

8. Sprite Attributes:

Parse OAM (Object Attribute Memory) to get sprite attributes like position, tile number, priority, and flip flags. Handle 8x8 and 8x16 sprites.

9. Input Handling:

While not directly tied to the LCD, using SDL2 to handle input and translating SDL2 key events to Game Boy button presses would be a logical step during this phase.

10. Debugging Tools (Optional):

Building debugging tools, like a tile viewer or a palette viewer, can be immensely helpful in diagnosing rendering issues. SDL2 can be used to render these tools as separate windows or overlays.

11. Optimizations:

Once your rendering works, consider optimization strategies. SDL2 offers hardware-accelerated rendering, so you might consider using that. On the emulator side, there might be ways to avoid unnecessary recalculations or redundant renderings.

12. Testing:

Use test ROMs and known games to verify the accuracy and performance of your rendering. "Blargg's" test ROMs are especially helpful in this regard.

Remember, the Game Boy's LCD hardware and the associated PPU (Picture Processing Unit) have a lot of nuances. Many games exploit specific behaviors of the hardware, so always be prepared to revisit and refine your implementation.

It might seem overwhelming at first, but breaking the task down into these steps and tackling each one at a time can make the process more manageable. Good luck with your emulator development!

@chrisledet chrisledet added the DMG Specific to Original Game Boy label Oct 18, 2023
@chrisledet
Copy link
Owner Author

Tools to help visual a tile:
https://www.huderlem.com/demos/gameboy2bpp.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DMG Specific to Original Game Boy
Projects
None yet
Development

No branches or pull requests

1 participant