leuat / TRSE

Turbo Rascal Syntax Error full repo
GNU General Public License v3.0
243 stars 44 forks source link

Optimization prevents use of CGB palette autoincrement #859

Open nummacway opened 4 months ago

nummacway commented 4 months ago

The CGB has 8 BG and OBJ palettes each. Each palette has 4 colors. Each color has two bytes (RGB1555) LE. So they're 64 bytes. One does not simply write the CGB palette. You ask the CGB to make $FF69 (BG) or $FF6B (OBJ) the desired byte, by writing the index in $FF68 (BG) or $FF6A (OBJ). Now you can read or write $FF69 or $FF6B like it was that byte of the palette. Because you'll always want to write at least 2 bytes, the CGB thankfully has an autoincrement feature, so it increments $FF68 after every write to $FF69 (BG, OBJ is comparable). You simply or $80 when writing the index and then repeatedly write to $FF69 or $FF6B.

var
  BCPS: Byte absolute $FF68;
  BCPD: Byte absolute $FF69;

// ...

begin
  // ...

  // request palette 0, color 0, byte 0 and enable autoincrement
  BCPS := $80;
  // write black to that color
  BCPD := 0;
  BCPD := 0;
  // write white to the next color
  BCPD := $ff; 
  BCPD := $7f;

  // ...
end;

This doesn't work because OrgAsm simply ignores the second write to BCPD. If you write more colors after that, the consecutive colors begin to shift. So the color first color isn't black but #ff3f00 orange (the last 3 bits of the green component aren't actually defined, but it will still be orange) and the second color isn't white either. Is there a way to make this work without inline Asm?