Skip to content

Commit 376fc98

Browse files
committed
Centralize use of constants
1 parent 351c442 commit 376fc98

16 files changed

+37
-35
lines changed

pyboy/core/cpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import pyboy
66

77
from . import opcodes
8+
from pyboy.utils import INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW
89

910
FLAGC, FLAGH, FLAGN, FLAGZ = range(4, 8)
10-
INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW = [1 << x for x in range(5)]
1111

1212

1313
logger = pyboy.logging.get_logger(__name__)

pyboy/core/lcd.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from pyboy.logging.logging cimport Logger
1414
from pyboy.utils cimport IntIOInterface
1515

1616

17-
cdef uint8_t INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW
17+
cdef uint8_t INTR_VBLANK, INTR_LCDC
1818
cdef uint16_t LCDC, STAT, SCY, SCX, LY, LYC, DMA, BGP, OBP0, OBP1, WY, WX
1919
cdef int ROWS, COLS, TILES, FRAME_CYCLES, VIDEO_RAM, OBJECT_ATTRIBUTE_MEMORY
2020
cdef uint32_t COL0_FLAG, BG_PRIORITY_FLAG

pyboy/core/lcd.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
from random import getrandbits
99

1010
import pyboy
11-
from pyboy.utils import PyBoyException
11+
from pyboy.utils import PyBoyException, INTR_VBLANK, INTR_LCDC, FRAME_CYCLES
12+
from pyboy.api.constants import ROWS, COLS, TILES
1213

1314
logger = pyboy.logging.get_logger(__name__)
1415

1516
VIDEO_RAM = 8 * 1024 # 8KB
1617
OBJECT_ATTRIBUTE_MEMORY = 0xA0
17-
INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW = [1 << x for x in range(5)]
18-
ROWS, COLS = 144, 160
19-
TILES = 384
20-
21-
FRAME_CYCLES = 70224
2218

2319

2420
def rgb_to_bgr(color):
@@ -622,7 +618,7 @@ def scanline_sprites(self, lcd, ly, buffer, buffer_attributes, ignore_priority):
622618
# The lowest X-coordinate has priority, when overlapping
623619
spriteheight = 16 if lcd._LCDC.sprite_height else 8
624620
sprite_count = 0
625-
for n in range(0x00, 0xA0, 4):
621+
for n in range(0x00, OBJECT_ATTRIBUTE_MEMORY, 4):
626622
y = lcd.OAM[n] - 16 # Documentation states the y coordinate needs to be subtracted by 16
627623
x = lcd.OAM[n + 1] - 8 # Documentation states the x coordinate needs to be subtracted by 8
628624

pyboy/core/mb.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ cdef Logger logger
2323

2424
cdef int64_t MAX_CYCLES
2525
cdef uint16_t STAT, LY, LYC
26-
cdef short VBLANK, LCDC, TIMER, SERIAL, HIGHTOLOW
27-
cdef int INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW
26+
cdef int INTR_TIMER, INTR_HIGHTOLOW
27+
cdef uint16_t OPCODE_BRK
2828
cdef int STATE_VERSION
2929

3030

pyboy/core/mb.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
#
55

66
import pyboy
7-
from pyboy.utils import STATE_VERSION, PyBoyException, PyBoyOutOfBoundsException
7+
from pyboy.utils import (
8+
STATE_VERSION,
9+
PyBoyException,
10+
PyBoyOutOfBoundsException,
11+
INTR_TIMER,
12+
INTR_HIGHTOLOW,
13+
OPCODE_BRK,
14+
)
815

916
from . import bootrom, cartridge, cpu, interaction, lcd, ram, sound, timer
1017

11-
INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW = [1 << x for x in range(5)]
12-
OPCODE_BRK = 0xDB
13-
14-
1518
logger = pyboy.logging.get_logger(__name__)
16-
1719
MAX_CYCLES = 1 << 31
1820

1921

pyboy/core/sound.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ from libc.stdint cimport int8_t, int64_t, uint8_t, uint16_t, uint64_t
88

99
from pyboy.logging.logging cimport Logger
1010
from pyboy.utils cimport IntIOInterface, double_to_uint64_ceil
11-
from pyboy.core.lcd cimport FRAME_CYCLES
11+
12+
cdef int FRAME_CYCLES
1213

1314

1415
cdef Logger logger

pyboy/core/sound.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
import pyboy
1414
from pyboy.utils import PyBoyAssertException, cython_compiled
15+
from pyboy.utils import FRAME_CYCLES
1516

1617
if not cython_compiled:
17-
from pyboy.core.lcd import FRAME_CYCLES
18-
1918
# Hide it from Cython in 'exec' statement
2019
exec("from pyboy.utils import double_to_uint64_ceil")
2120

pyboy/plugins/game_wrapper_super_mario_land.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from pyboy.plugins.base_plugin cimport PyBoyGameWrapper
1111

1212
cdef Logger logger
1313

14-
cdef int ROWS, COLS
14+
cdef int TILES
1515

1616

1717
cdef class GameWrapperSuperMarioLand(PyBoyGameWrapper):

pyboy/plugins/game_wrapper_super_mario_land.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pyboy
1313
from pyboy.utils import PyBoyException, PyBoyInvalidInputException, _bcd_to_dec, bcd_to_dec, dec_to_bcd
14+
from pyboy.api.constants import TILES
1415

1516
from .base_plugin import PyBoyGameWrapper
1617

@@ -86,7 +87,6 @@
8687
explosion = [157, 158]
8788
spike = [237]
8889

89-
TILES = 384
9090
mapping_minimal = np.zeros(TILES, dtype=np.uint8)
9191
minimal_list = [
9292
base_scripts + plane + submarine,

pyboy/plugins/game_wrapper_tetris.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from pyboy.plugins.base_plugin cimport PyBoyGameWrapper
1111

1212
cdef Logger logger
1313

14-
cdef int ROWS, COLS
14+
cdef int TILES
1515
cdef int NEXT_TETROMINO_ADDR
1616

1717
cdef class GameWrapperTetris(PyBoyGameWrapper):

pyboy/plugins/game_wrapper_tetris.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pyboy
1414

1515
from .base_plugin import PyBoyGameWrapper
16+
from pyboy.api.constants import TILES
1617

1718
logger = pyboy.logging.get_logger(__name__)
1819

@@ -31,8 +32,6 @@
3132
NEXT_TETROMINO_ADDR = 0xC213
3233

3334
# Construct a translation table for tile ID's to a minimal/compressed id system
34-
TILES = 384
35-
3635
# Compressed assigns an ID to each Tetromino type
3736
mapping_compressed = np.zeros(TILES, dtype=np.uint8)
3837
# BLANK, J, Z, O, L, T, S, I, BLACK

pyboy/pyboy.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ from pyboy.logging.logging cimport Logger
1919
from pyboy.plugins.manager cimport PluginManager
2020
from pyboy.utils cimport IntIOInterface, IntIOWrapper
2121

22+
cdef int TILES, TILES_CGB, OPCODE_BRK
2223

2324
cdef Logger logger
2425

pyboy/pyboy.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import numpy as np
1515

16+
from pyboy.api.constants import TILES, TILES_CGB
1617
from pyboy.api.gameshark import GameShark
1718
from pyboy.api.memory_scanner import MemoryScanner
1819
from pyboy.api.screen import Screen
@@ -28,6 +29,7 @@
2829
PyBoyOutOfBoundsException,
2930
WindowEvent,
3031
cython_compiled,
32+
OPCODE_BRK,
3133
)
3234

3335
try:
@@ -997,7 +999,8 @@ def game_area_mapping(self, mapping, sprite_offset=0):
997999
9981000
Example of custom mapping:
9991001
```python
1000-
>>> mapping = [x for x in range(384)] # 1:1 mapping
1002+
>>> from pyboy.api.constants import TILES
1003+
>>> mapping = [x for x in range(TILES)] # 1:1 mapping of 384 tiles
10011004
>>> mapping[0] = 0 # Map tile identifier 0 -> 0
10021005
>>> mapping[1] = 0 # Map tile identifier 1 -> 0
10031006
>>> mapping[2] = 0 # Map tile identifier 2 -> 0
@@ -1019,11 +1022,11 @@ def game_area_mapping(self, mapping, sprite_offset=0):
10191022
"""
10201023

10211024
if mapping is None:
1022-
mapping = [x for x in range(768)]
1025+
mapping = [x for x in range(TILES_CGB)]
10231026

10241027
assert isinstance(sprite_offset, int)
10251028
assert isinstance(mapping, (np.ndarray, list))
1026-
assert len(mapping) == 384 or len(mapping) == 768
1029+
assert len(mapping) == TILES or len(mapping) == TILES_CGB
10271030

10281031
self.game_wrapper.game_area_mapping(mapping, sprite_offset)
10291032

@@ -1231,7 +1234,7 @@ def hook_register(self, bank, addr, callback, context):
12311234
bank, addr = self._lookup_symbol(addr)
12321235

12331236
opcode = self.memory[bank, addr]
1234-
if opcode == 0xDB:
1237+
if opcode == OPCODE_BRK:
12351238
raise ValueError("Hook already registered for this bank and address.")
12361239
self.mb.breakpoint_add(bank, addr)
12371240
bank_addr_opcode = (bank & 0xFF) << 24 | (addr & 0xFFFF) << 8 | (opcode & 0xFF)

pyboy/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
STATE_VERSION = 14
2424

25+
INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW = [1 << x for x in range(5)]
26+
OPCODE_BRK = 0xDB
27+
FRAME_CYCLES = 70224
28+
2529

2630
class PyBoyException(Exception):
2731
"""

tests/test_game_wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77

88
from pyboy import PyBoy
9-
from pyboy.api.constants import LCDC_OFFSET, OAM_OFFSET, VRAM_OFFSET
9+
from pyboy.api.constants import LCDC_OFFSET, OAM_OFFSET, VRAM_OFFSET, TILES
1010

1111

1212
def test_game_wrapper_basics(default_rom):
@@ -74,7 +74,7 @@ def test_game_wrapper_mapping(default_rom):
7474
)
7575

7676
# List-based mapping, don't call tick
77-
mapping = [x for x in range(384)] # 1:1 mapping
77+
mapping = [x for x in range(TILES)] # 1:1 mapping
7878
mapping[0] = 10
7979
mapping[1] = 10
8080
mapping[2] = 10

tests/test_lcd.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010
import numpy as np
1111
from pyboy import PyBoy
1212
from pyboy.core.lcd import LCD, Renderer
13-
from pyboy.utils import cython_compiled
14-
15-
INTR_VBLANK, INTR_LCDC, INTR_TIMER, INTR_SERIAL, INTR_HIGHTOLOW = [1 << x for x in range(5)]
13+
from pyboy.utils import cython_compiled, INTR_LCDC, FRAME_CYCLES
1614

1715
color_palette = (0xFFFFFF, 0x999999, 0x555555, 0x000000)
1816
cgb_color_palette = (
1917
(0xFFFFFF, 0x7BFF31, 0x0063C5, 0x000000),
2018
(0xFFFFFF, 0xFF8484, 0xFF8484, 0x000000),
2119
(0xFFFFFF, 0xFF8484, 0xFF8484, 0x000000),
2220
)
23-
FRAME_CYCLES = 70224
2421

2522

2623
@pytest.mark.skipif(cython_compiled, reason="This test requires access to internal registers not available in Cython")

0 commit comments

Comments
 (0)