Skip to content

Commit

Permalink
Skip lookup in DMG color palette
Browse files Browse the repository at this point in the history
  • Loading branch information
Baekalfen committed Jul 13, 2024
1 parent ff168ef commit 813ce2d
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions pyboy/core/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ def __init__(self, cgb, cartridge_cgb, color_palette, cgb_color_palette, randomi
self.LY = 0x00
self.LYC = 0x00
# self.DMA = 0x00
self.BGP = PaletteRegister(0xFC)
self.OBP0 = PaletteRegister(0xFF)
self.OBP1 = PaletteRegister(0xFF)
self.WY = 0x00
self.WX = 0x00
self.clock = 0
Expand All @@ -62,22 +59,23 @@ def __init__(self, cgb, cartridge_cgb, color_palette, cgb_color_palette, randomi
self.cgb = cgb

if self.cgb:
# Setting for both modes, even though CGB is ignoring them. BGP[0] used in scanline_blank.
bg_pal, obj0_pal, obj1_pal = cgb_color_palette
self.BGP = PaletteRegister(0xFC, [(rgb_to_bgr(c)) for c in bg_pal])
self.OBP0 = PaletteRegister(0xFF, [(rgb_to_bgr(c)) for c in obj0_pal])
self.OBP1 = PaletteRegister(0xFF, [(rgb_to_bgr(c)) for c in obj1_pal])
if cartridge_cgb:
logger.debug("Starting CGB renderer")
self.renderer = CGBRenderer()
else:
logger.debug("Starting CGB renderer in DMG-mode")
# Running DMG ROM on CGB hardware use the default palettes
bg_pal, obj0_pal, obj1_pal = cgb_color_palette
self.BGP.palette_mem_rgb = [(rgb_to_bgr(c)) for c in bg_pal]
self.OBP0.palette_mem_rgb = [(rgb_to_bgr(c)) for c in obj0_pal]
self.OBP1.palette_mem_rgb = [(rgb_to_bgr(c)) for c in obj1_pal]
# Running DMG ROM on CGB hardware uses the palettes above
self.renderer = Renderer(False)
else:
logger.debug("Starting DMG renderer")
self.BGP.palette_mem_rgb = [(rgb_to_bgr(c)) for c in color_palette]
self.OBP0.palette_mem_rgb = [(rgb_to_bgr(c)) for c in color_palette]
self.OBP1.palette_mem_rgb = [(rgb_to_bgr(c)) for c in color_palette]
self.BGP = PaletteRegister(0xFC, [(rgb_to_bgr(c)) for c in color_palette])
self.OBP0 = PaletteRegister(0xFF, [(rgb_to_bgr(c)) for c in color_palette])
self.OBP1 = PaletteRegister(0xFF, [(rgb_to_bgr(c)) for c in color_palette])
self.renderer = Renderer(False)

def get_lcdc(self):
Expand Down Expand Up @@ -288,11 +286,11 @@ def getviewport(self):


class PaletteRegister:
def __init__(self, value):
def __init__(self, value, palette):
self.value = 0
self.lookup = [0] * 4
self.palette_mem_rgb = palette
self.set(value)
self.palette_mem_rgb = [0] * 4

def set(self, value):
# Pokemon Blue continuously sets this without changing the value
Expand All @@ -301,14 +299,14 @@ def set(self, value):

self.value = value
for x in range(4):
self.lookup[x] = (value >> x * 2) & 0b11
self.lookup[x] = self.palette_mem_rgb[(value >> x * 2) & 0b11]
return True

def get(self):
return self.value

def getcolor(self, i):
return self.palette_mem_rgb[self.lookup[i]]
return self.lookup[i]


class STATRegister:
Expand Down Expand Up @@ -543,7 +541,7 @@ def scanline_window(self, y, _x, wx, wy, cols, lcd):
self.update_tilecache0(lcd, wt, 0)

# TODO: Dynamic direct mapping?
pixel = lcd.BGP.palette_mem_rgb[lcd.BGP.lookup[self._tilecache0[yy, xx]]]
pixel = lcd.BGP.lookup[self._tilecache0[yy, xx]]
self._pixel(self._tilecache0, pixel, x, y, xx, yy, 0)
return cols

Expand Down Expand Up @@ -582,7 +580,7 @@ def scanline_background(self, y, _x, bx, by, cols, lcd):
yy = b_yy

# TODO: Dynamic direct mapping?
pixel = lcd.BGP.palette_mem_rgb[lcd.BGP.lookup[self._tilecache0[yy, xx]]]
pixel = lcd.BGP.lookup[self._tilecache0[yy, xx]]
self._pixel(self._tilecache0, pixel, x, y, xx, yy, 0)
return cols

Expand Down

0 comments on commit 813ce2d

Please sign in to comment.