2

I don't see it listed anywhere in the 0000-ffff address space. Is it controled by the CPU? Or something else entirely?

dav
  • 1,059
  • 1
  • 10
  • 16

2 Answers2

3

The LCD control block is located at FF40h and the LCDC is the first thereof, see as well the Pandocs.

Raffzahn
  • 222,541
  • 22
  • 631
  • 918
3

It's controlled by the programmer, not the CPU. The LCDC is located at memory address &FF40 and can be read from/written to just like any other piece of memory. This memory location is one byte long and each bit represents a specific hardware feature related to the screen. In the list below, bit 7 is the "leftmost" bit, bit 0 is the "rightmost."

  • Bit 7: If this equals 1, the screen is on. If it's 0, the screen is off.
  • Bit 6: If this equals 1, the "window" gets its tile data from addresses &9C00-&9FFF. If this equals 0, the "window" gets its tile data from addresses &9800-&9BFF.
  • Bit 5: Turns the "window" on or off. The window is a separate layer from the background and sprites, which uses its own tilemap. (You have two choices for where that tilemap can be stored, Bit 6 decides which one is active.)
  • Bit 4: The Game Boy will pull its sprite graphics data from memory area &8000-&8FFF if this bit equals 1. If 0, the sprite graphics come from &8800-&97FF.
  • Bit 3: The Game Boy will pull background graphics from memory area &9C00-&9FFF if this bit equals 1. If this bit equals 0, they come from &9800-&9BFF instead.
  • Bit 2: If this bit equals 1, sprites are 8x16 pixels in size. If 0, they are 8x8.
  • Bit 1: Sprites are only visible if this bit equals 1.
  • Bit 0: The background is only visible if this bit equals 1. (This is only true for the original Game Boy - on the Game Boy Color, the background is always visible.)

There are a few ways to write to this register, I'll quickly list some.

LD HL,&FF40
SET 7,(HL)   ;turn on the screen. This method can only alter one setting at a time,
             ;but it won't affect any other settings.
LD A,%11100111
LD (&FF40),A
;turn on screen, window tiles come from &9C00+, window on,
;sprites from &8800, background from &9800, 8x16 sprites, sprites on, background on

;This method quickly changes a bunch of settings at once, but any old settings ;are replaced with the new ones.

Just as a warning: If you clear bit 7 of LCDC outside of VBlank, you run the risk of damaging the screen. Emulators aren't affected by this of course, but a real Game Boy is. If you're going to shut off the screen during the game (which you will need to do to load graphics) you need to call a procedure that waits for VBlank first, then as soon as you return clear bit 7 of LCDC.

There's more explained in the Pan Docs.

user3840170
  • 23,072
  • 4
  • 91
  • 150
puppydrum64
  • 1,638
  • 5
  • 18
  • Just out of curiosity: Why do you think will the screen be damaged? – the busybee Oct 25 '21 at 06:46
  • The PanDocs says it will happen. I haven't tested it personally but I'd rather not find out. Supposedly Nintendo knew this was a problem and wouldn't publish a game that didn't wait for vBlank to clear bit 7 of &FF40. – puppydrum64 Oct 27 '21 at 14:32