Skip to content

Commit

Permalink
Move color_code out of utils and only into test
Browse files Browse the repository at this point in the history
  • Loading branch information
Baekalfen committed Feb 12, 2025
1 parent 9cb27a0 commit 66912b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
5 changes: 0 additions & 5 deletions pyboy/utils.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ cdef class IntIOInterface:
cdef class IntIOWrapper(IntIOInterface):
cdef object buffer

##############################################################
# Misc

cpdef uint8_t color_code(uint8_t, uint8_t, uint8_t) noexcept nogil

##############################################################
# Window Events

Expand Down
19 changes: 0 additions & 19 deletions pyboy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,6 @@ def tell(self):
return self.buffer.tell()


##############################################################
# Misc


# NOTE: Legacy function. Use look-up table in Renderer
def color_code(byte1, byte2, offset):
"""Convert 2 bytes into color code at a given offset.
The colors are 2 bit and are found like this:
Color of the first pixel is 0b10
| Color of the second pixel is 0b01
v v
1 0 0 1 0 0 0 1 <- byte1
0 1 1 1 1 1 0 0 <- byte2
"""
return (((byte2 >> (offset)) & 0b1) << 1) + ((byte1 >> (offset)) & 0b1)


##############################################################
# Window Events
# Temporarily placed here to not be exposed on public API
Expand Down
17 changes: 15 additions & 2 deletions tests/test_pyboy_lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,28 @@ def test_colorcode_example(self):
assert renderer.colorcode_table[(b2_h << 4) | b1_h] == 0x03_02_02_01

def test_colorcode_table(self):
def legacy_color_code(byte1, byte2, offset):
"""Convert 2 bytes into color code at a given offset.
The colors are 2 bit and are found like this:
Color of the first pixel is 0b10
| Color of the second pixel is 0b01
v v
1 0 0 1 0 0 0 1 <- byte1
0 1 1 1 1 1 0 0 <- byte2
"""
return (((byte2 >> (offset)) & 0b1) << 1) + ((byte1 >> (offset)) & 0b1)

renderer = Renderer(False)

for byte1 in range(0x100):
for byte2 in range(0x100):
colorcode_low = renderer.colorcode_table[(byte1 & 0xF) | ((byte2 & 0xF) << 4)]
colorcode_high = renderer.colorcode_table[((byte1 >> 4) & 0xF) | (byte2 & 0xF0)]
for offset in range(4):
assert (colorcode_low >> (3 - offset) * 8) & 0xFF == color_code(byte1, byte2, offset)
assert (colorcode_high >> (3 - offset) * 8) & 0xFF == color_code(byte1, byte2, offset + 4)
assert (colorcode_low >> (3 - offset) * 8) & 0xFF == legacy_color_code(byte1, byte2, offset)
assert (colorcode_high >> (3 - offset) * 8) & 0xFF == legacy_color_code(byte1, byte2, offset + 4)

# def test_tick(self):
# lcd = LCD()
Expand Down

0 comments on commit 66912b6

Please sign in to comment.