mirror of
https://github.com/pret/pokegold.git
synced 2026-10-02 11:56:40 -05:00
Clean up makefile, submodules, root dir; Python 3 compatibility.
This commit is contained in:
34
.gitattributes
vendored
34
.gitattributes
vendored
@@ -3,24 +3,32 @@
|
||||
|
||||
# Explicitly declare text files you want to always be normalized and converted
|
||||
# to native line endings on checkout.
|
||||
|
||||
# files part of the build
|
||||
*.asm text
|
||||
*.pal text
|
||||
|
||||
# Non-source files
|
||||
*.sh text
|
||||
*.py text
|
||||
*.link text
|
||||
*.txt text
|
||||
|
||||
# extra files
|
||||
*.awk text
|
||||
*.c text
|
||||
*.h text
|
||||
*.awk text
|
||||
*.md text
|
||||
*.py text
|
||||
*.sh text
|
||||
*.sha1 text
|
||||
|
||||
# Denote all files that are truly binary and should not be modified.
|
||||
*.png binary
|
||||
*.lz binary
|
||||
*.2bpp binary
|
||||
*.1bpp binary
|
||||
*.blk binary
|
||||
*.bin binary
|
||||
*.tilemap binary
|
||||
*.rle binary
|
||||
*.png binary diff=hex
|
||||
*.lz.* binary diff=hex
|
||||
*.bin binary diff=hex
|
||||
*.blk binary diff=hex
|
||||
*.rle binary diff=hex
|
||||
*.attrmap binary diff=hex
|
||||
*.tilemap binary diff=hex
|
||||
|
||||
# these are generated but just in case
|
||||
*.lz binary diff=hex
|
||||
*.2bpp binary diff=hex
|
||||
*.1bpp binary diff=hex
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,4 +44,3 @@ pokesilver.txt
|
||||
*.2bpp
|
||||
*.lz
|
||||
*.1bpp
|
||||
|
||||
|
||||
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,6 +0,0 @@
|
||||
[submodule "extras"]
|
||||
path = extras
|
||||
url = https://github.com/pret/pokemon-reverse-engineering-tools
|
||||
[submodule "crowdmap"]
|
||||
path = crowdmap
|
||||
url = https://github.com/yenatch/crowdmap
|
||||
92
Makefile
92
Makefile
@@ -1,28 +1,43 @@
|
||||
PYTHON := python
|
||||
MD5 := md5sum -c
|
||||
|
||||
.SUFFIXES:
|
||||
.PHONY: all clean gold silver pngs compare
|
||||
.SECONDEXPANSION:
|
||||
.PRECIOUS:
|
||||
.SECONDARY:
|
||||
|
||||
gfx := $(PYTHON) gfx.py
|
||||
includes := $(PYTHON) scan_includes.py
|
||||
|
||||
roms := pokegold.gbc pokesilver.gbc
|
||||
|
||||
rom_obj := \
|
||||
audio.o \
|
||||
home.o \
|
||||
main.o \
|
||||
wram.o \
|
||||
data/text/common.o \
|
||||
data/pokemon/dex_entries.o \
|
||||
wram.o \
|
||||
main.o \
|
||||
home.o
|
||||
|
||||
|
||||
gold_obj := $(rom_obj:.o=_gold.o)
|
||||
silver_obj := $(rom_obj:.o=_silver.o)
|
||||
|
||||
roms := pokegold.gbc pokesilver.gbc
|
||||
|
||||
### Build tools
|
||||
|
||||
ifeq (,$(shell which sha1sum))
|
||||
SHA1 := shasum
|
||||
else
|
||||
SHA1 := sha1sum
|
||||
endif
|
||||
|
||||
RGBDS ?=
|
||||
RGBASM ?= $(RGBDS)rgbasm
|
||||
RGBFIX ?= $(RGBDS)rgbfix
|
||||
RGBGFX ?= $(RGBDS)rgbgfx
|
||||
RGBLINK ?= $(RGBDS)rgblink
|
||||
|
||||
PYTHON := python
|
||||
gfx := $(PYTHON) tools/gfx.py
|
||||
|
||||
|
||||
### Build targets
|
||||
|
||||
.SUFFIXES:
|
||||
.PHONY: all gold silver clean pngs compare tools
|
||||
.SECONDEXPANSION:
|
||||
.PRECIOUS:
|
||||
.SECONDARY:
|
||||
|
||||
all: $(roms)
|
||||
gold: pokegold.gbc
|
||||
@@ -30,31 +45,48 @@ silver: pokesilver.gbc
|
||||
|
||||
clean:
|
||||
rm -f $(roms) $(gold_obj) $(silver_obj) $(roms:.gbc=.map) $(roms:.gbc=.sym)
|
||||
find gfx/pics gfx/trainers -name "*.png" -delete
|
||||
$(MAKE) clean -C tools/
|
||||
|
||||
compare: pokegold.gbc pokesilver.gbc
|
||||
@$(MD5) roms.md5
|
||||
compare: $(roms)
|
||||
@$(SHA1) -c roms.sha1
|
||||
|
||||
%.asm: ;
|
||||
tools:
|
||||
$(MAKE) -C tools/
|
||||
|
||||
|
||||
$(gold_obj): RGBASMFLAGS = -D _GOLD
|
||||
$(silver_obj): RGBASMFLAGS = -D _SILVER
|
||||
|
||||
# The dep rules have to be explicit or else missing files won't be reported.
|
||||
# As a side effect, they're evaluated immediately instead of when the rule is invoked.
|
||||
# It doesn't look like $(shell) can be deferred so there might not be a better way.
|
||||
define DEP
|
||||
$1: $$(shell $$(includes) $2)
|
||||
$1: $2 $$(shell tools/scan_includes $2)
|
||||
$$(RGBASM) $$(RGBASMFLAGS) -L -o $$@ $$<
|
||||
endef
|
||||
|
||||
# Build tools when building the rom.
|
||||
# This has to happen before the rules are processed, since that's when scan_includes is run.
|
||||
ifeq (,$(filter clean tools,$(MAKECMDGOALS)))
|
||||
|
||||
$(info $(shell $(MAKE) -C tools))
|
||||
|
||||
$(foreach obj, $(gold_obj), $(eval $(call DEP,$(obj),$(obj:_gold.o=.asm))))
|
||||
$(foreach obj, $(silver_obj), $(eval $(call DEP,$(obj),$(obj:_silver.o=.asm))))
|
||||
|
||||
$(gold_obj): %_gold.o: %.asm $$(dep)
|
||||
rgbasm -D GOLD -L -o $@ $<
|
||||
endif
|
||||
|
||||
$(silver_obj): %_silver.o: %.asm $$(dep)
|
||||
rgbasm -D SILVER -L -o $@ $<
|
||||
|
||||
pokegold.gbc: $(gold_obj)
|
||||
rgblink -n pokegold.sym -m pokegold.map -l pokegold.link -o $@ $^
|
||||
rgbfix -cjsv -i AAUE -k 01 -l 0x33 -m 0x10 -p 0 -r 3 -t "POKEMON_GLD" $@
|
||||
pokegold.gbc: $(gold_obj) pokegold.link
|
||||
$(RGBLINK) -n pokegold.sym -m pokegold.map -l pokegold.link -o $@ $(gold_obj)
|
||||
$(RGBFIX) -cjsv -i AAUE -k 01 -l 0x33 -m 0x10 -p 0 -r 3 -t "POKEMON_GLD" $@
|
||||
tools/sort_symfile.sh pokegold.sym
|
||||
|
||||
pokesilver.gbc: $(silver_obj)
|
||||
rgblink -n pokesilver.sym -m pokesilver.map -l pokesilver.link -o $@ $^
|
||||
rgbfix -cjsv -i AAXE -k 01 -l 0x33 -m 0x10 -p 0 -r 3 -t "POKEMON_SLV" $@
|
||||
pokesilver.gbc: $(silver_obj) pokesilver.link
|
||||
$(RGBLINK) -n pokesilver.sym -m pokesilver.map -l pokesilver.link -o $@ $(silver_obj)
|
||||
$(RGBFIX) -cjsv -i AAXE -k 01 -l 0x33 -m 0x10 -p 0 -r 3 -t "POKEMON_SLV" $@
|
||||
tools/sort_symfile.sh pokesilver.sym
|
||||
|
||||
pngs:
|
||||
find . -iname "*.lz" -exec $(gfx) unlz {} +
|
||||
|
||||
@@ -286,4 +286,4 @@ ITEM_FROM_MEM EQU $ff
|
||||
; leftovers from red
|
||||
SAFARI_BALL EQU $08 ; MOON_STONE
|
||||
MOON_STONE_RED EQU $0a ; BURN_HEAL
|
||||
FULL_HEAL_RED EQU $34 ; X_SPEED
|
||||
FULL_HEAL_RED EQU $34 ; X_SPEED
|
||||
|
||||
@@ -278,4 +278,4 @@ NUM_UNOWN_PUZZLES EQU const_value
|
||||
const_def 1
|
||||
const MOVETUTOR_FLAMETHROWER ; 1
|
||||
const MOVETUTOR_THUNDERBOLT ; 2
|
||||
const MOVETUTOR_ICE_BEAM ; 3
|
||||
const MOVETUTOR_ICE_BEAM ; 3
|
||||
|
||||
1
crowdmap
1
crowdmap
Submodule crowdmap deleted from 16eb22c54e
@@ -10,4 +10,4 @@ TileCollisionStdScripts:
|
||||
dbw COLL_TV, tv
|
||||
dbw COLL_WINDOW, window
|
||||
dbw COLL_INCENSE_BURNER, incenseburner
|
||||
db -1 ; end
|
||||
db -1 ; end
|
||||
|
||||
@@ -9,4 +9,4 @@ HappinessData_OlderHaircutBrother:
|
||||
db $ff, 4, HAPPINESS_OLDERCUT3 ; 30% chance
|
||||
|
||||
HappinessData_DaisysGrooming:
|
||||
db $ff, 2, HAPPINESS_GROOMING ; 99.6% chance
|
||||
db $ff, 2, HAPPINESS_GROOMING ; 99.6% chance
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
INCLUDE "constants.asm"
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
SECTION "Pokedex Entries 001-064", ROMX
|
||||
|
||||
BulbasaurPokedexEntry:: INCLUDE "data/pokemon/dex_entries/gold/bulbasaur.asm"
|
||||
@@ -268,7 +268,7 @@ HoOhPokedexEntry:: INCLUDE "data/pokemon/dex_entries/gold/ho_oh.asm"
|
||||
CelebiPokedexEntry:: INCLUDE "data/pokemon/dex_entries/gold/celebi.asm"
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
SECTION "Pokedex Entries 001-064", ROMX
|
||||
|
||||
BulbasaurPokedexEntry:: INCLUDE "data/pokemon/dex_entries/silver/bulbasaur.asm"
|
||||
|
||||
@@ -215,11 +215,11 @@ NameRival: ; c3f7
|
||||
ret
|
||||
|
||||
.DefaultName:
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
db "SILVER@"
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
db "GOLD@"
|
||||
ENDC
|
||||
|
||||
|
||||
@@ -1343,20 +1343,20 @@ PalPacket_a235: db $79, $26, $08, $00, $0b, $39, $cd, $48, $0c, $d0, $34, $a5, $
|
||||
PalPacket_a245: db $79, $1b, $08, $00, $0b, $ea, $ea, $ea, $ea, $ea, $a9, $01, $cd, $4f, $0c, $d0
|
||||
PalPacket_a255: db $79, $10, $08, $00, $0b, $4c, $20, $08, $ea, $ea, $ea, $ea, $ea, $60, $ea, $ea
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
Palettes_a265: INCLUDE "gfx/pals/gold_a265.pal"
|
||||
ENDC
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
Palettes_a265: INCLUDE "gfx/pals/silver_a265.pal"
|
||||
ENDC
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
SGBBorderMap: INCBIN "gfx/sgb_border/gold.map"
|
||||
SGBBorderPalettes: INCLUDE "gfx/sgb_border/pals/gold.pal"
|
||||
SGBBorder: INCBIN "gfx/sgb_border/gold.2bpp"
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
SGBBorderMap: INCBIN "gfx/sgb_border/silver.map"
|
||||
SGBBorderPalettes: INCLUDE "gfx/sgb_border/pals/silver.pal"
|
||||
SGBBorder: INCBIN "gfx/sgb_border/silver.2bpp"
|
||||
@@ -1620,7 +1620,7 @@ Palettes_bac6:
|
||||
RGB 07, 07, 07
|
||||
|
||||
Palettes_bb36:
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
RGB 31, 31, 31
|
||||
RGB 18, 23, 31
|
||||
RGB 15, 20, 31
|
||||
@@ -1647,7 +1647,7 @@ IF DEF(GOLD)
|
||||
RGB 0, 0, 0
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
RGB 31, 31, 31
|
||||
RGB 0, 12, 15
|
||||
RGB 4, 8, 21
|
||||
@@ -1738,7 +1738,7 @@ Palettes_bb9e:
|
||||
RGB 00, 00, 00
|
||||
|
||||
Palettes_bbbe:
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
RGB 31, 31, 31
|
||||
RGB 24, 25, 28
|
||||
RGB 24, 24, 09
|
||||
@@ -1820,7 +1820,7 @@ IF DEF(GOLD)
|
||||
RGB 00, 00, 00
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
RGB 31, 31, 31
|
||||
RGB 25, 26, 14
|
||||
RGB 20, 17, 31
|
||||
|
||||
@@ -890,13 +890,13 @@ NamePlayer: ; 6085 (1:6085)
|
||||
db 5
|
||||
db "NEW NAME@"
|
||||
.GoldSilver:
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
db "GOLD@"
|
||||
db "HIRO@"
|
||||
db "TAYLOR@"
|
||||
db "KARL@"
|
||||
ENDC
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
db "SILVER@"
|
||||
db "KAMON@"
|
||||
db "OSCAR@"
|
||||
@@ -1221,12 +1221,12 @@ Function6341: ; 6341 (1:6341)
|
||||
jr nz, .asm_6358
|
||||
ld a, $d8
|
||||
ldh [rBGP], a
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
ld a, $ff
|
||||
ldh [rOBP0], a
|
||||
ld a, $f8
|
||||
ENDC
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
ld a, $f0
|
||||
ldh [rOBP0], a
|
||||
ld a, $f0
|
||||
@@ -1237,12 +1237,12 @@ ENDC
|
||||
.asm_6358
|
||||
ld a, $e4
|
||||
ldh [rBGP], a
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
ld a, $ff
|
||||
ldh [rOBP0], a
|
||||
ld a, $e4
|
||||
ENDC
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
ld a, $f0
|
||||
ldh [rOBP0], a
|
||||
ld a, $e0
|
||||
@@ -1253,7 +1253,7 @@ ENDC
|
||||
.asm_6365
|
||||
ld a, $e4
|
||||
call DmgToCgbBGPals
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
ld a, $e0
|
||||
ENDC
|
||||
call DmgToCgbObjPal0
|
||||
@@ -1344,7 +1344,7 @@ TitleScreenFrame: ; 63da (1:63da)
|
||||
ret
|
||||
|
||||
Function63fe: ; 63fe (1:63fe)
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
ldh a, [hVBlankCounter]
|
||||
and $7
|
||||
ret nz
|
||||
@@ -1381,10 +1381,10 @@ Function6426:
|
||||
ld hl, wce63
|
||||
inc [hl]
|
||||
ld hl, wce65
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
ld de, $13c0
|
||||
ENDC
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
ld de, $1140
|
||||
ENDC
|
||||
ld [hl], e
|
||||
@@ -1474,7 +1474,7 @@ Function64b1: ; 64b1 (1:64b1)
|
||||
ld a, [wce65]
|
||||
and $3
|
||||
ret nz
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
ld bc, wAnimatedObjectStruct10Index
|
||||
ld hl, $a
|
||||
add hl, bc
|
||||
@@ -1499,14 +1499,14 @@ IF DEF(GOLD)
|
||||
ld d, [hl]
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
ld de, $7c58
|
||||
ENDC
|
||||
ld a, $f
|
||||
call InitSpriteAnimStruct
|
||||
ret
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
.Data_64e0:
|
||||
db $5c, $50, $00, $00
|
||||
db $5c, $68, $5c, $58
|
||||
|
||||
@@ -50,4 +50,4 @@ CorrectNickErrors:: ; 677e (1:677e)
|
||||
db "<ENEMY>", "<ENEMY>" + 1
|
||||
db $49, "<TM>" + 1
|
||||
db "<ROCKET>", "┘" + 1
|
||||
db -1 ; end
|
||||
db -1 ; end
|
||||
|
||||
1
extras
1
extras
Submodule extras deleted from 0e1798937a
130
gbhw.asm
130
gbhw.asm
@@ -1,130 +0,0 @@
|
||||
; Graciously aped from http://nocash.emubase.de/pandocs.htm .
|
||||
|
||||
; MBC3
|
||||
MBC3SRamEnable EQU $0000
|
||||
MBC3RomBank EQU $2000
|
||||
MBC3SRamBank EQU $4000
|
||||
MBC3LatchClock EQU $6000
|
||||
MBC3RTC EQU $a000
|
||||
|
||||
SRAM_DISABLE EQU $00
|
||||
SRAM_ENABLE EQU $0a
|
||||
|
||||
NUM_SRAM_BANKS EQU 4
|
||||
|
||||
RTC_S EQU $08 ; Seconds 0-59 (0-3Bh)
|
||||
RTC_M EQU $09 ; Minutes 0-59 (0-3Bh)
|
||||
RTC_H EQU $0a ; Hours 0-23 (0-17h)
|
||||
RTC_DL EQU $0b ; Lower 8 bits of Day Counter (0-FFh)
|
||||
RTC_DH EQU $0c ; Upper 1 bit of Day Counter, Carry Bit, Halt Flag
|
||||
; Bit 0 Most significant bit of Day Counter (Bit 8)
|
||||
; Bit 6 Halt (0=Active, 1=Stop Timer)
|
||||
; Bit 7 Day Counter Carry Bit (1=Counter Overflow)
|
||||
|
||||
; interrupt flags
|
||||
VBLANK EQU 0
|
||||
LCD_STAT EQU 1
|
||||
TIMER EQU 2
|
||||
SERIAL EQU 3
|
||||
JOYPAD EQU 4
|
||||
|
||||
; OAM attribute flags
|
||||
OAM_PALETTE EQU %111
|
||||
OAM_TILE_BANK EQU 3
|
||||
OAM_OBP_NUM EQU 4 ; Non CGB Mode Only
|
||||
OAM_X_FLIP EQU 5
|
||||
OAM_Y_FLIP EQU 6
|
||||
OAM_PRIORITY EQU 7 ; 0: OBJ above BG, 1: OBJ behind BG (colors 1-3)
|
||||
|
||||
|
||||
; Hardware registers
|
||||
rJOYP EQU $ff00 ; Joypad (R/W)
|
||||
rSB EQU $ff01 ; Serial transfer data (R/W)
|
||||
rSC EQU $ff02 ; Serial Transfer Control (R/W)
|
||||
rSC_ON EQU 7
|
||||
rSC_CGB EQU 1
|
||||
rSC_CLOCK EQU 0
|
||||
rDIV EQU $ff04 ; Divider Register (R/W)
|
||||
rTIMA EQU $ff05 ; Timer counter (R/W)
|
||||
rTMA EQU $ff06 ; Timer Modulo (R/W)
|
||||
rTAC EQU $ff07 ; Timer Control (R/W)
|
||||
rTAC_ON EQU 2
|
||||
rTAC_4096_HZ EQU 0
|
||||
rTAC_262144_HZ EQU 1
|
||||
rTAC_65536_HZ EQU 2
|
||||
rTAC_16384_HZ EQU 3
|
||||
rIF EQU $ff0f ; Interrupt Flag (R/W)
|
||||
rNR10 EQU $ff10 ; Channel 1 Sweep register (R/W)
|
||||
rNR11 EQU $ff11 ; Channel 1 Sound length/Wave pattern duty (R/W)
|
||||
rNR12 EQU $ff12 ; Channel 1 Volume Envelope (R/W)
|
||||
rNR13 EQU $ff13 ; Channel 1 Frequency lo (Write Only)
|
||||
rNR14 EQU $ff14 ; Channel 1 Frequency hi (R/W)
|
||||
rNR20 EQU $ff15 ; Channel 2 Sweep register (R/W)
|
||||
rNR21 EQU $ff16 ; Channel 2 Sound Length/Wave Pattern Duty (R/W)
|
||||
rNR22 EQU $ff17 ; Channel 2 Volume Envelope (R/W)
|
||||
rNR23 EQU $ff18 ; Channel 2 Frequency lo data (W)
|
||||
rNR24 EQU $ff19 ; Channel 2 Frequency hi data (R/W)
|
||||
rNR30 EQU $ff1a ; Channel 3 Sound on/off (R/W)
|
||||
rNR31 EQU $ff1b ; Channel 3 Sound Length
|
||||
rNR32 EQU $ff1c ; Channel 3 Select output level (R/W)
|
||||
rNR33 EQU $ff1d ; Channel 3 Frequency's lower data (W)
|
||||
rNR34 EQU $ff1e ; Channel 3 Frequency's higher data (R/W)
|
||||
rNR40 EQU $ff1f ; Channel 4 Sweep register (R/W)
|
||||
rNR41 EQU $ff20 ; Channel 4 Sound Length (R/W)
|
||||
rNR42 EQU $ff21 ; Channel 4 Volume Envelope (R/W)
|
||||
rNR43 EQU $ff22 ; Channel 4 Polynomial Counter (R/W)
|
||||
rNR44 EQU $ff23 ; Channel 4 Counter/consecutive; Inital (R/W)
|
||||
rNR50 EQU $ff24 ; Channel control / ON-OFF / Volume (R/W)
|
||||
rNR51 EQU $ff25 ; Selection of Sound output terminal (R/W)
|
||||
rNR52 EQU $ff26 ; Sound on/off
|
||||
rWave_0 EQU $ff30
|
||||
rWave_1 EQU $ff31
|
||||
rWave_2 EQU $ff32
|
||||
rWave_3 EQU $ff33
|
||||
rWave_4 EQU $ff34
|
||||
rWave_5 EQU $ff35
|
||||
rWave_6 EQU $ff36
|
||||
rWave_7 EQU $ff37
|
||||
rWave_8 EQU $ff38
|
||||
rWave_9 EQU $ff39
|
||||
rWave_a EQU $ff3a
|
||||
rWave_b EQU $ff3b
|
||||
rWave_c EQU $ff3c
|
||||
rWave_d EQU $ff3d
|
||||
rWave_e EQU $ff3e
|
||||
rWave_f EQU $ff3f
|
||||
rLCDC EQU $ff40 ; LCD Control (R/W)
|
||||
rSTAT EQU $ff41 ; LCDC Status (R/W)
|
||||
rSCY EQU $ff42 ; Scroll Y (R/W)
|
||||
rSCX EQU $ff43 ; Scroll X (R/W)
|
||||
rLY EQU $ff44 ; LCDC Y-Coordinate (R)
|
||||
rLYC EQU $ff45 ; LY Compare (R/W)
|
||||
rDMA EQU $ff46 ; DMA Transfer and Start Address (W)
|
||||
rBGP EQU $ff47 ; BG Palette Data (R/W) - Non CGB Mode Only
|
||||
rOBP0 EQU $ff48 ; Object Palette 0 Data (R/W) - Non CGB Mode Only
|
||||
rOBP1 EQU $ff49 ; Object Palette 1 Data (R/W) - Non CGB Mode Only
|
||||
rWY EQU $ff4a ; Window Y Position (R/W)
|
||||
rWX EQU $ff4b ; Window X Position minus 7 (R/W)
|
||||
rLCDMODE EQU $ff4c
|
||||
rKEY1 EQU $ff4d ; CGB Mode Only - Prepare Speed Switch
|
||||
rVBK EQU $ff4f ; CGB Mode Only - VRAM Bank
|
||||
rBLCK EQU $ff50
|
||||
rHDMA1 EQU $ff51 ; CGB Mode Only - New DMA Source, High
|
||||
rHDMA2 EQU $ff52 ; CGB Mode Only - New DMA Source, Low
|
||||
rHDMA3 EQU $ff53 ; CGB Mode Only - New DMA Destination, High
|
||||
rHDMA4 EQU $ff54 ; CGB Mode Only - New DMA Destination, Low
|
||||
rHDMA5 EQU $ff55 ; CGB Mode Only - New DMA Length/Mode/Start
|
||||
rRP EQU $ff56 ; CGB Mode Only - Infrared Communications Port
|
||||
rBGPI EQU $ff68 ; CGB Mode Only - Background Palette Index
|
||||
rBGPD EQU $ff69 ; CGB Mode Only - Background Palette Data
|
||||
rOBPI EQU $ff6a ; CGB Mode Only - Sprite Palette Index
|
||||
rOBPD EQU $ff6b ; CGB Mode Only - Sprite Palette Data
|
||||
rUNKNOWN1 EQU $ff6c ; (FEh) Bit 0 (Read/Write) - CGB Mode Only
|
||||
rSVBK EQU $ff70 ; CGB Mode Only - WRAM Bank
|
||||
rUNKNOWN2 EQU $ff72 ; (00h) - Bit 0-7 (Read/Write)
|
||||
rUNKNOWN3 EQU $ff73 ; (00h) - Bit 0-7 (Read/Write)
|
||||
rUNKNOWN4 EQU $ff74 ; (00h) - Bit 0-7 (Read/Write) - CGB Mode Only
|
||||
rUNKNOWN5 EQU $ff75 ; (8Fh) - Bit 4-6 (Read/Write)
|
||||
rUNKNOWN6 EQU $ff76 ; (00h) - Always 00h (Read Only)
|
||||
rUNKNOWN7 EQU $ff77 ; (00h) - Always 00h (Read Only)
|
||||
rIE EQU $ffff ; Interrupt Enable (R/W)
|
||||
@@ -22,4 +22,4 @@ IsHMMove::
|
||||
db FLASH
|
||||
db WATERFALL
|
||||
db WHIRLPOOL
|
||||
db -1 ; end
|
||||
db -1 ; end
|
||||
|
||||
@@ -23,10 +23,10 @@ INCLUDE "macros/flag.asm"
|
||||
INCLUDE "macros/legacy.asm"
|
||||
|
||||
dr: macro
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
INCBIN "baserom-gold.gbc", \1, \2 +- \1
|
||||
ELSE
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
INCBIN "baserom-silver.gbc", \1, \2 +- \1
|
||||
ENDC
|
||||
ENDC
|
||||
|
||||
16
main.asm
16
main.asm
@@ -663,7 +663,7 @@ _InitSpriteAnimStruct:: ; 8d1f7
|
||||
|
||||
_ReinitSpriteAnimFrame:: ; 8d332
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
dr $8d332, $8e774
|
||||
|
||||
ClearSpriteAnims2::
|
||||
@@ -676,7 +676,7 @@ HoldSwitchmonIcon::
|
||||
dr $8e93d, $90000
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
dr $8d332, $8e75a
|
||||
|
||||
ClearSpriteAnims2::
|
||||
@@ -743,7 +743,7 @@ Function97c2a:: ; 97c2a
|
||||
dr $97c2a, $98000
|
||||
|
||||
SECTION "bank26", ROMX, BANK[$26]
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
TitleScreenGFX1:
|
||||
dr $98000, $98476
|
||||
TitleScreenGFX2:
|
||||
@@ -752,7 +752,7 @@ GSIntroTilemap:
|
||||
dr $98616, $9c000
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
TitleScreenGFX1:
|
||||
dr $98000, $98498
|
||||
TitleScreenGFX2:
|
||||
@@ -881,7 +881,7 @@ SECTION "bank39", ROMX, BANK[$39]
|
||||
CopyrightGFX:
|
||||
INCBIN "gfx/intro/copyright.2bpp"
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
TitleScreenGFX3:
|
||||
dr $e41e0, $e4260
|
||||
TitleScreenGFX4:
|
||||
@@ -890,7 +890,7 @@ OptionsMenu:
|
||||
dr $e4608, $e49a8
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
TitleScreenGFX3:
|
||||
dr $e41e0, $e4220
|
||||
TitleScreenGFX4:
|
||||
@@ -901,12 +901,12 @@ ENDC
|
||||
|
||||
INCLUDE "engine/movie/gamefreak_presents.asm"
|
||||
|
||||
IF DEF(GOLD)
|
||||
IF DEF(_GOLD)
|
||||
GoldSilverIntro:
|
||||
dr $e4cb1, $e8000
|
||||
ENDC
|
||||
|
||||
IF DEF(SILVER)
|
||||
IF DEF(_SILVER)
|
||||
GoldSilverIntro:
|
||||
dr $e4af9, $e8000
|
||||
ENDC
|
||||
|
||||
2
roms.md5
2
roms.md5
@@ -1,2 +0,0 @@
|
||||
a6924ce1f9ad2228e1c6580779b23878 *pokegold.gbc
|
||||
2ac166169354e84d0e2d7cf4cb40b312 *pokesilver.gbc
|
||||
2
roms.sha1
Normal file
2
roms.sha1
Normal file
@@ -0,0 +1,2 @@
|
||||
d8b8a3600a465308c9953dfa04f0081c05bdcb94 *pokegold.gbc
|
||||
49b163f7e57702bc939d642a18f591de55d92dae *pokesilver.gbc
|
||||
@@ -1,35 +0,0 @@
|
||||
#!/bin/python
|
||||
# coding: utf-8
|
||||
|
||||
"""
|
||||
Recursively scan an asm file for dependencies.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
includes = set()
|
||||
|
||||
def scan_file(filename):
|
||||
for line in open(filename):
|
||||
if 'INC' not in line:
|
||||
continue
|
||||
line = line.split(';')[0]
|
||||
if 'INCLUDE' in line:
|
||||
include = line.split('"')[1]
|
||||
includes.add(include)
|
||||
scan_file(include)
|
||||
elif 'INCBIN' in line:
|
||||
include = line.split('"')[1]
|
||||
includes.add(include)
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('filenames', nargs='*')
|
||||
args = ap.parse_args()
|
||||
for filename in set(args.filenames):
|
||||
scan_file(filename)
|
||||
sys.stdout.write(' '.join(includes))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
8
tools/.gitignore
vendored
Normal file
8
tools/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
gfx
|
||||
lzcomp
|
||||
md5
|
||||
palette
|
||||
png_dimensions
|
||||
pokemon_animation
|
||||
pokemon_animation_graphics
|
||||
scan_includes
|
||||
18
tools/Makefile
Normal file
18
tools/Makefile
Normal file
@@ -0,0 +1,18 @@
|
||||
.PHONY: all clean
|
||||
|
||||
CC := gcc
|
||||
CFLAGS := -O3 -std=c99 -Wall -Wextra -Wno-missing-field-initializers
|
||||
|
||||
tools := \
|
||||
scan_includes \
|
||||
gfx
|
||||
all: $(tools)
|
||||
@:
|
||||
|
||||
clean:
|
||||
rm -f $(tools)
|
||||
|
||||
gfx: common.h
|
||||
|
||||
%: %.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
40
tools/common.h
Normal file
40
tools/common.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef GUARD_COMMON_H
|
||||
#define GUARD_COMMON_H
|
||||
|
||||
int __getopt_long_i__;
|
||||
#define getopt_long(c, v, s, l) getopt_long(c, v, s, l, &__getopt_long_i__)
|
||||
|
||||
FILE *fopen_verbose(char *filename, char *mode) {
|
||||
FILE *f = fopen(filename, mode);
|
||||
if (!f) {
|
||||
fprintf(stderr, "Could not open file: \"%s\"\n", filename);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
uint8_t *read_u8(char *filename, int *size) {
|
||||
FILE *f = fopen_verbose(filename, "rb");
|
||||
if (!f) {
|
||||
exit(1);
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
*size = ftell(f);
|
||||
rewind(f);
|
||||
uint8_t *data = malloc(*size);
|
||||
if (*size != (int)fread(data, 1, *size, f)) {
|
||||
fprintf(stderr, "Could not read file: \"%s\"\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
fclose(f);
|
||||
return data;
|
||||
}
|
||||
|
||||
void write_u8(char *filename, uint8_t *data, int size) {
|
||||
FILE *f = fopen_verbose(filename, "wb");
|
||||
if (f) {
|
||||
fwrite(data, 1, size, f);
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // GUARD_COMMON_H
|
||||
296
tools/gfx.c
Normal file
296
tools/gfx.c
Normal file
@@ -0,0 +1,296 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <getopt.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
static void usage(void) {
|
||||
fprintf(stderr, "Usage: gfx [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--png filename] [-d depth] [-h] [-o outfile] infile\n");
|
||||
}
|
||||
|
||||
static void error(char *message) {
|
||||
fputs(message, stderr);
|
||||
fputs("\n", stderr);
|
||||
}
|
||||
|
||||
struct Options {
|
||||
int trim_whitespace;
|
||||
int remove_whitespace;
|
||||
int help;
|
||||
char *outfile;
|
||||
int depth;
|
||||
int interleave;
|
||||
int remove_duplicates;
|
||||
int keep_whitespace;
|
||||
int remove_xflip;
|
||||
int remove_yflip;
|
||||
char *png_file;
|
||||
};
|
||||
|
||||
struct Options Options = {
|
||||
.depth = 2,
|
||||
};
|
||||
|
||||
void get_args(int argc, char *argv[]) {
|
||||
struct option long_options[] = {
|
||||
{"remove-whitespace", no_argument, &Options.remove_whitespace, 1},
|
||||
{"trim-whitespace", no_argument, &Options.trim_whitespace, 1},
|
||||
{"interleave", no_argument, &Options.interleave, 1},
|
||||
{"remove-duplicates", no_argument, &Options.remove_duplicates, 1},
|
||||
{"keep-whitespace", no_argument, &Options.keep_whitespace, 1},
|
||||
{"remove-xflip", no_argument, &Options.remove_xflip, 1},
|
||||
{"remove-yflip", no_argument, &Options.remove_yflip, 1},
|
||||
{"png", required_argument, 0, 'p'},
|
||||
{"depth", required_argument, 0, 'd'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0}
|
||||
};
|
||||
for (int opt = 0; opt != -1;) {
|
||||
switch (opt = getopt_long(argc, argv, "ho:d:p:", long_options)) {
|
||||
case 'h':
|
||||
Options.help = true;
|
||||
break;
|
||||
case 'o':
|
||||
Options.outfile = optarg;
|
||||
break;
|
||||
case 'd':
|
||||
Options.depth = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
case 'p':
|
||||
Options.png_file = optarg;
|
||||
break;
|
||||
case 0:
|
||||
case -1:
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Graphic {
|
||||
int size;
|
||||
uint8_t *data;
|
||||
};
|
||||
|
||||
bool is_whitespace(uint8_t *tile, int tile_size) {
|
||||
uint8_t WHITESPACE = 0;
|
||||
for (int i = 0; i < tile_size; i++) {
|
||||
if (tile[i] != WHITESPACE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void trim_whitespace(struct Graphic *graphic) {
|
||||
int tile_size = Options.depth * 8;
|
||||
for (int i = graphic->size - tile_size; i > 0; i -= tile_size) {
|
||||
if (is_whitespace(&graphic->data[i], tile_size)) {
|
||||
graphic->size = i;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void remove_whitespace(struct Graphic *graphic) {
|
||||
int tile_size = Options.depth * 8;
|
||||
if (Options.interleave) tile_size *= 2;
|
||||
int i = 0;
|
||||
for (int j = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
|
||||
while (is_whitespace(&graphic->data[j], tile_size)) {
|
||||
j += tile_size;
|
||||
}
|
||||
if (j >= graphic->size) {
|
||||
break;
|
||||
}
|
||||
if (j > i) {
|
||||
memcpy(&graphic->data[i], &graphic->data[j], tile_size);
|
||||
}
|
||||
}
|
||||
graphic->size = i;
|
||||
}
|
||||
|
||||
bool tile_exists(uint8_t *tile, uint8_t *tiles, int tile_size, int num_tiles) {
|
||||
for (int i = 0; i < num_tiles; i++) {
|
||||
bool match = true;
|
||||
for (int j = 0; j < tile_size; j++) {
|
||||
if (tile[j] != tiles[i * tile_size + j]) {
|
||||
match = false;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void remove_duplicates(struct Graphic *graphic) {
|
||||
int tile_size = Options.depth * 8;
|
||||
if (Options.interleave) tile_size *= 2;
|
||||
int num_tiles = 0;
|
||||
for (int i = 0, j = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
|
||||
while (tile_exists(&graphic->data[j], graphic->data, tile_size, num_tiles)) {
|
||||
if (Options.keep_whitespace && is_whitespace(&graphic->data[j], tile_size)) {
|
||||
break;
|
||||
}
|
||||
j += tile_size;
|
||||
}
|
||||
if (j >= graphic->size) {
|
||||
break;
|
||||
}
|
||||
if (j > i) {
|
||||
memcpy(&graphic->data[i], &graphic->data[j], tile_size);
|
||||
}
|
||||
num_tiles++;
|
||||
}
|
||||
graphic->size = num_tiles * tile_size;
|
||||
}
|
||||
|
||||
bool flip_exists(uint8_t *tile, uint8_t *tiles, int tile_size, int num_tiles, bool xflip, bool yflip) {
|
||||
uint8_t *flip = calloc(tile_size, 1);
|
||||
int half_size = tile_size / 2;
|
||||
for (int i = 0; i < tile_size; i++) {
|
||||
int byte = i;
|
||||
if (yflip) {
|
||||
byte = tile_size - 1 - (i ^ 1);
|
||||
if (Options.interleave && i < half_size) {
|
||||
byte = half_size - 1 - (i ^ 1);
|
||||
}
|
||||
}
|
||||
if (xflip) {
|
||||
for (int bit = 0; bit < 8; bit++) {
|
||||
flip[byte] |= ((tile[i] >> bit) & 1) << (7 - bit);
|
||||
}
|
||||
} else {
|
||||
flip[byte] = tile[i];
|
||||
}
|
||||
}
|
||||
if (tile_exists(flip, tiles, tile_size, num_tiles)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void remove_flip(struct Graphic *graphic, bool xflip, bool yflip) {
|
||||
int tile_size = Options.depth * 8;
|
||||
if (Options.interleave) tile_size *= 2;
|
||||
int num_tiles = 0;
|
||||
for (int i = 0, j = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
|
||||
while (flip_exists(&graphic->data[j], graphic->data, tile_size, num_tiles, xflip, yflip)) {
|
||||
if (Options.keep_whitespace && is_whitespace(&graphic->data[j], tile_size)) {
|
||||
break;
|
||||
}
|
||||
j += tile_size;
|
||||
}
|
||||
if (j >= graphic->size) {
|
||||
break;
|
||||
}
|
||||
if (j > i) {
|
||||
memcpy(&graphic->data[i], &graphic->data[j], tile_size);
|
||||
}
|
||||
num_tiles++;
|
||||
}
|
||||
graphic->size = num_tiles * tile_size;
|
||||
}
|
||||
|
||||
void interleave(struct Graphic *graphic, int width) {
|
||||
int tile_size = Options.depth * 8;
|
||||
int width_tiles = width / 8;
|
||||
int num_tiles = graphic->size / tile_size;
|
||||
uint8_t *interleaved = malloc(graphic->size);
|
||||
for (int i = 0; i < num_tiles; i++) {
|
||||
int tile = i * 2;
|
||||
int row = i / width_tiles;
|
||||
tile -= width_tiles * row;
|
||||
if (row % 2) {
|
||||
tile -= width_tiles;
|
||||
tile += 1;
|
||||
}
|
||||
memcpy(&interleaved[tile * tile_size], &graphic->data[i * tile_size], tile_size);
|
||||
}
|
||||
graphic->size = num_tiles * tile_size;
|
||||
memcpy(graphic->data, interleaved, graphic->size);
|
||||
free(interleaved);
|
||||
}
|
||||
|
||||
int png_get_width(char *filename) {
|
||||
FILE *f = fopen_verbose(filename, "rb");
|
||||
if (!f) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const int OFFSET_WIDTH = 16;
|
||||
uint8_t bytes[4];
|
||||
fseek(f, OFFSET_WIDTH, SEEK_SET);
|
||||
size_t size = 4;
|
||||
size_t result = fread(bytes, 1, size, f);
|
||||
fclose(f);
|
||||
if (result != size) {
|
||||
fprintf(stderr, "Could not read file at offset 0x%x: \"%s\"\n", OFFSET_WIDTH, filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int width = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
width |= bytes[i] << (8 * (3 - i));
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
get_args(argc, argv);
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
if (Options.help) {
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
if (argc < 1) {
|
||||
usage();
|
||||
exit(1);
|
||||
}
|
||||
char *infile = argv[0];
|
||||
struct Graphic graphic;
|
||||
graphic.data = read_u8(infile, &graphic.size);
|
||||
if (Options.trim_whitespace) {
|
||||
trim_whitespace(&graphic);
|
||||
}
|
||||
if (Options.interleave) {
|
||||
if (!Options.png_file) {
|
||||
error("interleave: need --png to infer dimensions");
|
||||
usage();
|
||||
exit(1);
|
||||
}
|
||||
int width = png_get_width(Options.png_file);
|
||||
interleave(&graphic, width);
|
||||
}
|
||||
if (Options.remove_duplicates) {
|
||||
remove_duplicates(&graphic);
|
||||
}
|
||||
if (Options.remove_xflip) {
|
||||
remove_flip(&graphic, true, false);
|
||||
}
|
||||
if (Options.remove_yflip) {
|
||||
remove_flip(&graphic, false, true);
|
||||
}
|
||||
if (Options.remove_xflip && Options.remove_yflip) {
|
||||
remove_flip(&graphic, true, true);
|
||||
}
|
||||
if (Options.remove_whitespace) {
|
||||
remove_whitespace(&graphic);
|
||||
}
|
||||
if (Options.outfile) {
|
||||
write_u8(Options.outfile, graphic.data, graphic.size);
|
||||
}
|
||||
free(graphic.data);
|
||||
return 0;
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
import os
|
||||
import argparse
|
||||
|
||||
from extras.pokemontools import gfx, lz
|
||||
from pokemontools import gfx, lz
|
||||
|
||||
|
||||
# Graphics with inverted tilemaps that aren't covered by filepath_rules.
|
||||
1
tools/pokemontools/__init__.py
Normal file
1
tools/pokemontools/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# A subset of https://github.com/pret/pokemon-reverse-engineering-tools
|
||||
938
tools/pokemontools/gfx.py
Normal file
938
tools/pokemontools/gfx.py
Normal file
@@ -0,0 +1,938 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
from . import png
|
||||
from math import sqrt, floor, ceil
|
||||
import argparse
|
||||
import operator
|
||||
|
||||
from .lz import Compressed, Decompressed
|
||||
|
||||
|
||||
def split(list_, interval):
|
||||
"""
|
||||
Split a list by length.
|
||||
"""
|
||||
for i in range(0, len(list_), interval):
|
||||
j = min(i + interval, len(list_))
|
||||
yield list_[i:j]
|
||||
|
||||
|
||||
def hex_dump(data, length=0x10):
|
||||
"""
|
||||
just use hexdump -C
|
||||
"""
|
||||
margin = len('%x' % len(data))
|
||||
output = []
|
||||
address = 0
|
||||
for line in split(data, length):
|
||||
output += [
|
||||
hex(address)[2:].zfill(margin) +
|
||||
' | ' +
|
||||
' '.join('%.2x' % byte for byte in line)
|
||||
]
|
||||
address += length
|
||||
return '\n'.join(output)
|
||||
|
||||
|
||||
def get_tiles(image):
|
||||
"""
|
||||
Split a 2bpp image into 8x8 tiles.
|
||||
"""
|
||||
return list(split(image, 0x10))
|
||||
|
||||
def connect(tiles):
|
||||
"""
|
||||
Combine 8x8 tiles into a 2bpp image.
|
||||
"""
|
||||
return [byte for tile in tiles for byte in tile]
|
||||
|
||||
def transpose(tiles, width=None):
|
||||
"""
|
||||
Transpose a tile arrangement along line y=-x.
|
||||
|
||||
00 01 02 03 04 05 00 06 0c 12 18 1e
|
||||
06 07 08 09 0a 0b 01 07 0d 13 19 1f
|
||||
0c 0d 0e 0f 10 11 <-> 02 08 0e 14 1a 20
|
||||
12 13 14 15 16 17 03 09 0f 15 1b 21
|
||||
18 19 1a 1b 1c 1d 04 0a 10 16 1c 22
|
||||
1e 1f 20 21 22 23 05 0b 11 17 1d 23
|
||||
|
||||
00 01 02 03 00 04 08
|
||||
04 05 06 07 <-> 01 05 09
|
||||
08 09 0a 0b 02 06 0a
|
||||
03 07 0b
|
||||
"""
|
||||
if width == None:
|
||||
width = int(sqrt(len(tiles))) # assume square image
|
||||
tiles = sorted(enumerate(tiles), key= lambda i_tile: i_tile[0] % width)
|
||||
return [tile for i, tile in tiles]
|
||||
|
||||
def transpose_tiles(image, width=None):
|
||||
return connect(transpose(get_tiles(image), width))
|
||||
|
||||
def interleave(tiles, width):
|
||||
"""
|
||||
00 01 02 03 04 05 00 02 04 06 08 0a
|
||||
06 07 08 09 0a 0b 01 03 05 07 09 0b
|
||||
0c 0d 0e 0f 10 11 --> 0c 0e 10 12 14 16
|
||||
12 13 14 15 16 17 0d 0f 11 13 15 17
|
||||
18 19 1a 1b 1c 1d 18 1a 1c 1e 20 22
|
||||
1e 1f 20 21 22 23 19 1b 1d 1f 21 23
|
||||
"""
|
||||
interleaved = []
|
||||
left, right = split(tiles[::2], width), split(tiles[1::2], width)
|
||||
for l, r in zip(left, right):
|
||||
interleaved += l + r
|
||||
return interleaved
|
||||
|
||||
def deinterleave(tiles, width):
|
||||
"""
|
||||
00 02 04 06 08 0a 00 01 02 03 04 05
|
||||
01 03 05 07 09 0b 06 07 08 09 0a 0b
|
||||
0c 0e 10 12 14 16 --> 0c 0d 0e 0f 10 11
|
||||
0d 0f 11 13 15 17 12 13 14 15 16 17
|
||||
18 1a 1c 1e 20 22 18 19 1a 1b 1c 1d
|
||||
19 1b 1d 1f 21 23 1e 1f 20 21 22 23
|
||||
"""
|
||||
deinterleaved = []
|
||||
rows = list(split(tiles, width))
|
||||
for left, right in zip(rows[::2], rows[1::2]):
|
||||
for l, r in zip(left, right):
|
||||
deinterleaved += [l, r]
|
||||
return deinterleaved
|
||||
|
||||
def interleave_tiles(image, width):
|
||||
return connect(interleave(get_tiles(image), width))
|
||||
|
||||
def deinterleave_tiles(image, width):
|
||||
return connect(deinterleave(get_tiles(image), width))
|
||||
|
||||
|
||||
def condense_image_to_map(image, pic=0):
|
||||
"""
|
||||
Reduce an image of adjacent frames to an image containing a base frame and any unrepeated tiles.
|
||||
Returns the new image and the corresponding tilemap used to reconstruct the input image.
|
||||
|
||||
If <pic> is 0, ignore the concept of frames. This behavior might be better off as another function.
|
||||
"""
|
||||
tiles = get_tiles(image)
|
||||
new_tiles, tilemap = condense_tiles_to_map(tiles, pic)
|
||||
new_image = connect(new_tiles)
|
||||
return new_image, tilemap
|
||||
|
||||
def condense_tiles_to_map(tiles, pic=0):
|
||||
"""
|
||||
Reduce a sequence of tiles representing adjacent frames to a base frame and any unrepeated tiles.
|
||||
Returns the new tiles and the corresponding tilemap used to reconstruct the input tile sequence.
|
||||
|
||||
If <pic> is 0, ignore the concept of frames. This behavior might be better off as another function.
|
||||
"""
|
||||
|
||||
# Leave the first frame intact for pics.
|
||||
new_tiles = tiles[:pic]
|
||||
tilemap = range(pic)
|
||||
|
||||
for i, tile in enumerate(tiles[pic:]):
|
||||
if tile not in new_tiles:
|
||||
new_tiles.append(tile)
|
||||
|
||||
if pic:
|
||||
# Match the first frame exactly where possible.
|
||||
# This reduces the space needed to replace tiles in pic animations.
|
||||
# For example, if a tile is repeated twice in the first frame,
|
||||
# but at the same relative index as the second tile, use the second index.
|
||||
# When creating a bitmask later, the second index would not require a replacement, but the first index would have.
|
||||
pic_i = i % pic
|
||||
if tile == new_tiles[pic_i]:
|
||||
tilemap.append(pic_i)
|
||||
else:
|
||||
tilemap.append(new_tiles.index(tile))
|
||||
else:
|
||||
tilemap.append(new_tiles.index(tile))
|
||||
return new_tiles, tilemap
|
||||
|
||||
def test_condense_tiles_to_map():
|
||||
test = condense_tiles_to_map(list('abcadbae'))
|
||||
if test != (list('abcde'), [0, 1, 2, 0, 3, 1, 0, 4]):
|
||||
raise Exception(test)
|
||||
test = condense_tiles_to_map(list('abcadbae'), 2)
|
||||
if test != (list('abcde'), [0, 1, 2, 0, 3, 1, 0, 4]):
|
||||
raise Exception(test)
|
||||
test = condense_tiles_to_map(list('abcadbae'), 4)
|
||||
if test != (list('abcade'), [0, 1, 2, 3, 4, 1, 0, 5]):
|
||||
raise Exception(test)
|
||||
test = condense_tiles_to_map(list('abcadbea'), 4)
|
||||
if test != (list('abcade'), [0, 1, 2, 3, 4, 1, 5, 3]):
|
||||
raise Exception(test)
|
||||
|
||||
|
||||
def to_file(filename, data):
|
||||
"""
|
||||
Apparently open(filename, 'wb').write(bytearray(data)) won't work.
|
||||
"""
|
||||
file = open(filename, 'wb')
|
||||
for byte in data:
|
||||
file.write('%c' % byte)
|
||||
file.close()
|
||||
|
||||
|
||||
def decompress_file(filein, fileout=None):
|
||||
image = bytearray(open(filein).read())
|
||||
de = Decompressed(image)
|
||||
|
||||
if fileout == None:
|
||||
fileout = os.path.splitext(filein)[0]
|
||||
to_file(fileout, de.output)
|
||||
|
||||
|
||||
def compress_file(filein, fileout=None):
|
||||
image = bytearray(open(filein).read())
|
||||
lz = Compressed(image)
|
||||
|
||||
if fileout == None:
|
||||
fileout = filein + '.lz'
|
||||
to_file(fileout, lz.output)
|
||||
|
||||
|
||||
def bin_to_rgb(word):
|
||||
red = word & 0b11111
|
||||
word >>= 5
|
||||
green = word & 0b11111
|
||||
word >>= 5
|
||||
blue = word & 0b11111
|
||||
return (red, green, blue)
|
||||
|
||||
def convert_binary_pal_to_text_by_filename(filename):
|
||||
pal = bytearray(open(filename).read())
|
||||
return convert_binary_pal_to_text(pal)
|
||||
|
||||
def convert_binary_pal_to_text(pal):
|
||||
output = ''
|
||||
words = [hi * 0x100 + lo for lo, hi in zip(pal[::2], pal[1::2])]
|
||||
for word in words:
|
||||
red, green, blue = ['%.2d' % c for c in bin_to_rgb(word)]
|
||||
output += '\tRGB ' + ', '.join((red, green, blue))
|
||||
output += '\n'
|
||||
return output
|
||||
|
||||
def read_rgb_macros(lines):
|
||||
colors = []
|
||||
for line in lines:
|
||||
macro = line.split(" ")[0].strip()
|
||||
if macro == 'RGB':
|
||||
params = ' '.join(line.split(" ")[1:]).split(',')
|
||||
red, green, blue = [int(v) for v in params]
|
||||
colors += [[red, green, blue]]
|
||||
return colors
|
||||
|
||||
|
||||
def rewrite_binary_pals_to_text(filenames):
|
||||
for filename in filenames:
|
||||
pal_text = convert_binary_pal_to_text_by_filename(filename)
|
||||
with open(filename, 'w') as out:
|
||||
out.write(pal_text)
|
||||
|
||||
|
||||
def flatten(planar):
|
||||
"""
|
||||
Flatten planar 2bpp image data into a quaternary pixel map.
|
||||
"""
|
||||
strips = []
|
||||
for bottom, top in split(planar, 2):
|
||||
bottom = bottom
|
||||
top = top
|
||||
strip = []
|
||||
for i in range(7,-1,-1):
|
||||
color = (
|
||||
(bottom >> i & 1) +
|
||||
(top *2 >> i & 2)
|
||||
)
|
||||
strip += [color]
|
||||
strips += strip
|
||||
return strips
|
||||
|
||||
def to_lines(image, width):
|
||||
"""
|
||||
Convert a tiled quaternary pixel map to lines of quaternary pixels.
|
||||
"""
|
||||
tile_width = 8
|
||||
tile_height = 8
|
||||
num_columns = width // tile_width
|
||||
height = len(image) // width
|
||||
|
||||
lines = []
|
||||
for cur_line in range(height):
|
||||
tile_row = cur_line // tile_height
|
||||
line = []
|
||||
for column in range(num_columns):
|
||||
anchor = (
|
||||
num_columns * tile_row * tile_width * tile_height +
|
||||
column * tile_width * tile_height +
|
||||
cur_line % tile_height * tile_width
|
||||
)
|
||||
line += image[anchor : anchor + tile_width]
|
||||
lines += [line]
|
||||
return lines
|
||||
|
||||
|
||||
def dmg2rgb(word):
|
||||
"""
|
||||
For PNGs.
|
||||
"""
|
||||
def shift(value):
|
||||
while True:
|
||||
yield value & (2**5 - 1)
|
||||
value >>= 5
|
||||
word = shift(word)
|
||||
# distribution is less even w/ << 3
|
||||
red, green, blue = [int(color * 8.25) for color in [word.next() for _ in range(3)]]
|
||||
alpha = 255
|
||||
return (red, green, blue, alpha)
|
||||
|
||||
|
||||
def rgb_to_dmg(color):
|
||||
"""
|
||||
For PNGs.
|
||||
"""
|
||||
word = (color['r'] / 8)
|
||||
word += (color['g'] / 8) << 5
|
||||
word += (color['b'] / 8) << 10
|
||||
return word
|
||||
|
||||
|
||||
def pal_to_png(filename):
|
||||
"""
|
||||
Interpret a .pal file as a png palette.
|
||||
"""
|
||||
with open(filename) as rgbs:
|
||||
colors = read_rgb_macros(rgbs.readlines())
|
||||
a = 255
|
||||
palette = []
|
||||
for color in colors:
|
||||
# even distribution over 000-255
|
||||
r, g, b = [int(hue * 8.25) for hue in color]
|
||||
palette += [(r, g, b, a)]
|
||||
white = (255,255,255,255)
|
||||
black = (000,000,000,255)
|
||||
if white not in palette and len(palette) < 4:
|
||||
palette = [white] + palette
|
||||
if black not in palette and len(palette) < 4:
|
||||
palette = palette + [black]
|
||||
return palette
|
||||
|
||||
|
||||
def png_to_rgb(palette):
|
||||
"""
|
||||
Convert a png palette to rgb macros.
|
||||
"""
|
||||
output = ''
|
||||
for color in palette:
|
||||
r, g, b = [color[c] / 8 for c in 'rgb']
|
||||
output += '\tRGB ' + ', '.join(['%.2d' % hue for hue in (r, g, b)])
|
||||
output += '\n'
|
||||
return output
|
||||
|
||||
|
||||
def read_filename_arguments(filename):
|
||||
"""
|
||||
Infer graphics conversion arguments given a filename.
|
||||
|
||||
Arguments are separated with '.'.
|
||||
"""
|
||||
parsed_arguments = {}
|
||||
|
||||
int_arguments = {
|
||||
'w': 'width',
|
||||
'h': 'height',
|
||||
't': 'tile_padding',
|
||||
}
|
||||
arguments = os.path.splitext(filename)[0].lstrip('.').split('.')[1:]
|
||||
for argument in arguments:
|
||||
|
||||
# Check for integer arguments first (i.e. "w128").
|
||||
arg = argument[0]
|
||||
param = argument[1:]
|
||||
if param.isdigit():
|
||||
arg = int_arguments.get(arg, False)
|
||||
if arg:
|
||||
parsed_arguments[arg] = int(param)
|
||||
|
||||
elif argument == 'arrange':
|
||||
parsed_arguments['norepeat'] = True
|
||||
parsed_arguments['tilemap'] = True
|
||||
|
||||
# Pic dimensions (i.e. "6x6").
|
||||
elif 'x' in argument and any(map(str.isdigit, argument)):
|
||||
w, h = argument.split('x')
|
||||
if w.isdigit() and h.isdigit():
|
||||
parsed_arguments['pic_dimensions'] = (int(w), int(h))
|
||||
|
||||
else:
|
||||
parsed_arguments[argument] = True
|
||||
|
||||
return parsed_arguments
|
||||
|
||||
|
||||
def export_2bpp_to_png(filein, fileout=None, pal_file=None, height=0, width=0, tile_padding=0, pic_dimensions=None, **kwargs):
|
||||
|
||||
if fileout == None:
|
||||
fileout = os.path.splitext(filein)[0] + '.png'
|
||||
|
||||
image = open(filein, 'rb').read()
|
||||
|
||||
arguments = {
|
||||
'width': width,
|
||||
'height': height,
|
||||
'pal_file': pal_file,
|
||||
'tile_padding': tile_padding,
|
||||
'pic_dimensions': pic_dimensions,
|
||||
}
|
||||
arguments.update(read_filename_arguments(filein))
|
||||
|
||||
if pal_file == None:
|
||||
if os.path.exists(os.path.splitext(fileout)[0]+'.pal'):
|
||||
arguments['pal_file'] = os.path.splitext(fileout)[0]+'.pal'
|
||||
|
||||
result = convert_2bpp_to_png(image, **arguments)
|
||||
width, height, palette, greyscale, bitdepth, px_map = result
|
||||
|
||||
w = png.Writer(
|
||||
width,
|
||||
height,
|
||||
palette=palette,
|
||||
compression=9,
|
||||
greyscale=greyscale,
|
||||
bitdepth=bitdepth
|
||||
)
|
||||
with open(fileout, 'wb') as f:
|
||||
w.write(f, px_map)
|
||||
|
||||
|
||||
def convert_2bpp_to_png(image, **kwargs):
|
||||
"""
|
||||
Convert a planar 2bpp graphic to png.
|
||||
"""
|
||||
|
||||
image = bytearray(image)
|
||||
|
||||
pad_color = bytearray([0])
|
||||
|
||||
width = kwargs.get('width', 0)
|
||||
height = kwargs.get('height', 0)
|
||||
tile_padding = kwargs.get('tile_padding', 0)
|
||||
pic_dimensions = kwargs.get('pic_dimensions', None)
|
||||
pal_file = kwargs.get('pal_file', None)
|
||||
interleave = kwargs.get('interleave', False)
|
||||
|
||||
# Width must be specified to interleave.
|
||||
if interleave and width:
|
||||
image = interleave_tiles(image, width / 8)
|
||||
|
||||
# Pad the image by a given number of tiles if asked.
|
||||
image += pad_color * 0x10 * tile_padding
|
||||
|
||||
# Some images are transposed in blocks.
|
||||
if pic_dimensions:
|
||||
w, h = pic_dimensions
|
||||
if not width: width = w * 8
|
||||
|
||||
pic_length = w * h * 0x10
|
||||
|
||||
trailing = len(image) % pic_length
|
||||
|
||||
pic = []
|
||||
for i in range(0, len(image) - trailing, pic_length):
|
||||
pic += transpose_tiles(image[i:i+pic_length], h)
|
||||
image = bytearray(pic) + image[len(image) - trailing:]
|
||||
|
||||
# Pad out trailing lines.
|
||||
image += pad_color * 0x10 * ((w - (len(image) // 0x10) % h) % w)
|
||||
|
||||
def px_length(img):
|
||||
return len(img) * 4
|
||||
def tile_length(img):
|
||||
return len(img) * 4 / (8*8)
|
||||
|
||||
if width and height:
|
||||
tile_width = width / 8
|
||||
more_tile_padding = (tile_width - (tile_length(image) % tile_width or tile_width))
|
||||
image += pad_color * 0x10 * more_tile_padding
|
||||
|
||||
elif width and not height:
|
||||
tile_width = width // 8
|
||||
more_tile_padding = (tile_width - (tile_length(image) % tile_width or tile_width))
|
||||
image += pad_color * 0x10 * more_tile_padding
|
||||
height = px_length(image) / width
|
||||
|
||||
elif height and not width:
|
||||
tile_height = height / 8
|
||||
more_tile_padding = (tile_height - (tile_length(image) % tile_height or tile_height))
|
||||
image += pad_color * 0x10 * more_tile_padding
|
||||
width = px_length(image) / height
|
||||
|
||||
# at least one dimension should be given
|
||||
if width * height != px_length(image):
|
||||
# look for possible combos of width/height that would form a rectangle
|
||||
matches = []
|
||||
# Height need not be divisible by 8, but width must.
|
||||
# See pokered gfx/minimize_pic.1bpp.
|
||||
for w in range(8, px_length(image) // 2 + 1, 8):
|
||||
h = px_length(image) / w
|
||||
if w * h == px_length(image):
|
||||
matches += [(w, h)]
|
||||
# go for the most square image
|
||||
if len(matches):
|
||||
width, height = sorted(matches, key= lambda w_h: (w_h[1] % 8 != 0, w_h[1] + w_h[0]))[0] # favor height
|
||||
else:
|
||||
raise Exception('Image can\'t be divided into tiles (%d px)!' % (px_length(image)))
|
||||
|
||||
# convert tiles to lines
|
||||
lines = to_lines(flatten(image), width)
|
||||
|
||||
if pal_file == None:
|
||||
palette = None
|
||||
greyscale = True
|
||||
bitdepth = 2
|
||||
px_map = [[3 - pixel for pixel in line] for line in lines]
|
||||
|
||||
else: # gbc color
|
||||
palette = pal_to_png(pal_file)
|
||||
greyscale = False
|
||||
bitdepth = 8
|
||||
px_map = [[pixel for pixel in line] for line in lines]
|
||||
|
||||
return width, height, palette, greyscale, bitdepth, px_map
|
||||
|
||||
|
||||
def get_pic_animation(tmap, w, h):
|
||||
"""
|
||||
Generate pic animation data from a combined tilemap of each frame.
|
||||
"""
|
||||
frame_text = ''
|
||||
bitmask_text = ''
|
||||
|
||||
frames = list(split(tmap, w * h))
|
||||
base = frames.pop(0)
|
||||
bitmasks = []
|
||||
|
||||
for i in range(len(frames)):
|
||||
frame_text += '\tdw .frame{}\n'.format(i + 1)
|
||||
|
||||
for i, frame in enumerate(frames):
|
||||
bitmask = map(operator.ne, frame, base)
|
||||
if bitmask not in bitmasks:
|
||||
bitmasks.append(bitmask)
|
||||
which_bitmask = bitmasks.index(bitmask)
|
||||
|
||||
mask = iter(bitmask)
|
||||
masked_frame = filter(lambda _: mask.next(), frame)
|
||||
|
||||
frame_text += '.frame{}\n'.format(i + 1)
|
||||
frame_text += '\tdb ${:02x} ; bitmask\n'.format(which_bitmask)
|
||||
if masked_frame:
|
||||
frame_text += '\tdb {}\n'.format(', '.join(
|
||||
map('${:02x}'.format, masked_frame)
|
||||
))
|
||||
|
||||
for i, bitmask in enumerate(bitmasks):
|
||||
bitmask_text += '; {}\n'.format(i)
|
||||
for byte in split(bitmask, 8):
|
||||
byte = int(''.join(map(int.__repr__, reversed(byte))), 2)
|
||||
bitmask_text += '\tdb %{:08b}\n'.format(byte)
|
||||
|
||||
return frame_text, bitmask_text
|
||||
|
||||
|
||||
def export_png_to_2bpp(filein, fileout=None, palout=None, **kwargs):
|
||||
|
||||
arguments = {
|
||||
'tile_padding': 0,
|
||||
'pic_dimensions': None,
|
||||
'animate': False,
|
||||
'stupid_bitmask_hack': [],
|
||||
}
|
||||
arguments.update(kwargs)
|
||||
arguments.update(read_filename_arguments(filein))
|
||||
|
||||
image, arguments = png_to_2bpp(filein, **arguments)
|
||||
|
||||
if fileout == None:
|
||||
fileout = os.path.splitext(filein)[0] + '.2bpp'
|
||||
to_file(fileout, image)
|
||||
|
||||
tmap = arguments.get('tmap')
|
||||
|
||||
if tmap != None and arguments['animate'] and arguments['pic_dimensions']:
|
||||
# Generate pic animation data.
|
||||
frame_text, bitmask_text = get_pic_animation(tmap, *arguments['pic_dimensions'])
|
||||
|
||||
frames_path = os.path.join(os.path.split(fileout)[0], 'frames.asm')
|
||||
with open(frames_path, 'w') as out:
|
||||
out.write(frame_text)
|
||||
|
||||
bitmask_path = os.path.join(os.path.split(fileout)[0], 'bitmask.asm')
|
||||
|
||||
# The following Pokemon have a bitmask dummied out.
|
||||
for exception in arguments['stupid_bitmask_hack']:
|
||||
if exception in bitmask_path:
|
||||
bitmasks = bitmask_text.split(';')
|
||||
bitmasks[-1] = bitmasks[-1].replace('1', '0')
|
||||
bitmask_text = ';'.join(bitmasks)
|
||||
|
||||
with open(bitmask_path, 'w') as out:
|
||||
out.write(bitmask_text)
|
||||
|
||||
elif tmap != None and arguments.get('tilemap', False):
|
||||
tilemap_path = os.path.splitext(fileout)[0] + '.tilemap'
|
||||
to_file(tilemap_path, tmap)
|
||||
|
||||
palette = arguments.get('palette')
|
||||
if palout == None:
|
||||
palout = os.path.splitext(fileout)[0] + '.pal'
|
||||
export_palette(palette, palout)
|
||||
|
||||
|
||||
def get_image_padding(width, height, wstep=8, hstep=8):
|
||||
|
||||
padding = {
|
||||
'left': 0,
|
||||
'right': 0,
|
||||
'top': 0,
|
||||
'bottom': 0,
|
||||
}
|
||||
|
||||
if width % wstep and width >= wstep:
|
||||
pad = float(width % wstep) / 2
|
||||
padding['left'] = int(ceil(pad))
|
||||
padding['right'] = int(floor(pad))
|
||||
|
||||
if height % hstep and height >= hstep:
|
||||
pad = float(height % hstep) / 2
|
||||
padding['top'] = int(ceil(pad))
|
||||
padding['bottom'] = int(floor(pad))
|
||||
|
||||
return padding
|
||||
|
||||
|
||||
def png_to_2bpp(filein, **kwargs):
|
||||
"""
|
||||
Convert a png image to planar 2bpp.
|
||||
"""
|
||||
|
||||
arguments = {
|
||||
'tile_padding': 0,
|
||||
'pic_dimensions': False,
|
||||
'interleave': False,
|
||||
'norepeat': False,
|
||||
'tilemap': False,
|
||||
}
|
||||
arguments.update(kwargs)
|
||||
|
||||
if type(filein) is str:
|
||||
filein = open(filein)
|
||||
|
||||
assert type(filein) is file
|
||||
|
||||
width, height, rgba, info = png.Reader(filein).asRGBA8()
|
||||
|
||||
# png.Reader returns flat pixel data. Nested is easier to work with
|
||||
len_px = len('rgba')
|
||||
image = []
|
||||
palette = []
|
||||
for line in rgba:
|
||||
newline = []
|
||||
for px in range(0, len(line), len_px):
|
||||
color = dict(zip('rgba', line[px:px+len_px]))
|
||||
if color not in palette:
|
||||
if len(palette) < 4:
|
||||
palette += [color]
|
||||
else:
|
||||
# TODO Find the nearest match
|
||||
print('WARNING: %s: Color %s truncated to' % (filein, color))
|
||||
color = sorted(palette, key=lambda x: sum(x.values()))[0]
|
||||
print(color)
|
||||
newline += [color]
|
||||
image += [newline]
|
||||
|
||||
assert len(palette) <= 4, '%s: palette should be 4 colors, is really %d (%s)' % (filein, len(palette), palette)
|
||||
|
||||
# Pad out smaller palettes with greyscale colors
|
||||
greyscale = {
|
||||
'black': { 'r': 0x00, 'g': 0x00, 'b': 0x00, 'a': 0xff },
|
||||
'grey': { 'r': 0x55, 'g': 0x55, 'b': 0x55, 'a': 0xff },
|
||||
'gray': { 'r': 0xaa, 'g': 0xaa, 'b': 0xaa, 'a': 0xff },
|
||||
'white': { 'r': 0xff, 'g': 0xff, 'b': 0xff, 'a': 0xff },
|
||||
}
|
||||
preference = 'white', 'black', 'grey', 'gray'
|
||||
for hue in map(greyscale.get, preference):
|
||||
if len(palette) >= 4:
|
||||
break
|
||||
if hue not in palette:
|
||||
palette += [hue]
|
||||
|
||||
palette.sort(key=lambda x: sum(x.values()))
|
||||
|
||||
# Game Boy palette order
|
||||
palette.reverse()
|
||||
|
||||
# Map pixels to quaternary color ids
|
||||
padding = get_image_padding(width, height)
|
||||
width += padding['left'] + padding['right']
|
||||
height += padding['top'] + padding['bottom']
|
||||
pad = bytearray([0])
|
||||
|
||||
qmap = []
|
||||
qmap += pad * width * padding['top']
|
||||
for line in image:
|
||||
qmap += pad * padding['left']
|
||||
for color in line:
|
||||
qmap += [palette.index(color)]
|
||||
qmap += pad * padding['right']
|
||||
qmap += pad * width * padding['bottom']
|
||||
|
||||
# Graphics are stored in tiles instead of lines
|
||||
tile_width = 8
|
||||
tile_height = 8
|
||||
num_columns = max(width, tile_width) / tile_width
|
||||
num_rows = max(height, tile_height) / tile_height
|
||||
image = []
|
||||
|
||||
for row in range(num_rows):
|
||||
for column in range(num_columns):
|
||||
|
||||
# Split it up into strips to convert to planar data
|
||||
for strip in range(min(tile_height, height)):
|
||||
anchor = (
|
||||
row * num_columns * tile_width * tile_height +
|
||||
column * tile_width +
|
||||
strip * width
|
||||
)
|
||||
line = qmap[anchor : anchor + tile_width]
|
||||
bottom, top = 0, 0
|
||||
for bit, quad in enumerate(line):
|
||||
bottom += (quad & 1) << (7 - bit)
|
||||
top += (quad /2 & 1) << (7 - bit)
|
||||
image += [bottom, top]
|
||||
|
||||
dim = arguments['pic_dimensions']
|
||||
if dim:
|
||||
if type(dim) in (tuple, list):
|
||||
w, h = dim
|
||||
else:
|
||||
# infer dimensions based on width.
|
||||
w = width / tile_width
|
||||
h = height / tile_height
|
||||
if h % w == 0:
|
||||
h = w
|
||||
|
||||
tiles = get_tiles(image)
|
||||
pic_length = w * h
|
||||
tile_width = width / 8
|
||||
trailing = len(tiles) % pic_length
|
||||
new_image = []
|
||||
for block in range(len(tiles) / pic_length):
|
||||
offset = (h * tile_width) * ((block * w) / tile_width) + ((block * w) % tile_width)
|
||||
pic = []
|
||||
for row in range(h):
|
||||
index = offset + (row * tile_width)
|
||||
pic += tiles[index:index + w]
|
||||
new_image += transpose(pic, w)
|
||||
new_image += tiles[len(tiles) - trailing:]
|
||||
image = connect(new_image)
|
||||
|
||||
# Remove any tile padding used to make the png rectangular.
|
||||
image = image[:len(image) - arguments['tile_padding'] * 0x10]
|
||||
|
||||
tmap = None
|
||||
|
||||
if arguments['interleave']:
|
||||
image = deinterleave_tiles(image, num_columns)
|
||||
|
||||
if arguments['pic_dimensions']:
|
||||
image, tmap = condense_image_to_map(image, w * h)
|
||||
elif arguments['norepeat']:
|
||||
image, tmap = condense_image_to_map(image)
|
||||
if not arguments['tilemap']:
|
||||
tmap = None
|
||||
|
||||
arguments.update({ 'palette': palette, 'tmap': tmap, })
|
||||
|
||||
return image, arguments
|
||||
|
||||
|
||||
def export_palette(palette, filename):
|
||||
"""
|
||||
Export a palette from png to rgb macros in a .pal file.
|
||||
"""
|
||||
|
||||
if os.path.exists(filename):
|
||||
|
||||
# Pic palettes are 2 colors (black/white are added later).
|
||||
with open(filename) as rgbs:
|
||||
colors = read_rgb_macros(rgbs.readlines())
|
||||
|
||||
if len(colors) == 2:
|
||||
palette = palette[1:3]
|
||||
|
||||
text = png_to_rgb(palette)
|
||||
with open(filename, 'w') as out:
|
||||
out.write(text)
|
||||
|
||||
|
||||
def png_to_lz(filein):
|
||||
|
||||
name = os.path.splitext(filein)[0]
|
||||
|
||||
export_png_to_2bpp(filein)
|
||||
image = open(name+'.2bpp', 'rb').read()
|
||||
to_file(name+'.2bpp'+'.lz', Compressed(image).output)
|
||||
|
||||
|
||||
def convert_2bpp_to_1bpp(data):
|
||||
"""
|
||||
Convert planar 2bpp image data to 1bpp. Assume images are two colors.
|
||||
"""
|
||||
return data[::2]
|
||||
|
||||
def convert_1bpp_to_2bpp(data):
|
||||
"""
|
||||
Convert 1bpp image data to planar 2bpp (black/white).
|
||||
"""
|
||||
output = []
|
||||
for i in data:
|
||||
output += [i, i]
|
||||
return output
|
||||
|
||||
|
||||
def export_2bpp_to_1bpp(filename):
|
||||
name, extension = os.path.splitext(filename)
|
||||
image = open(filename, 'rb').read()
|
||||
image = convert_2bpp_to_1bpp(image)
|
||||
to_file(name + '.1bpp', image)
|
||||
|
||||
def export_1bpp_to_2bpp(filename):
|
||||
name, extension = os.path.splitext(filename)
|
||||
image = open(filename, 'rb').read()
|
||||
image = convert_1bpp_to_2bpp(image)
|
||||
to_file(name + '.2bpp', image)
|
||||
|
||||
|
||||
def export_1bpp_to_png(filename, fileout=None):
|
||||
|
||||
if fileout == None:
|
||||
fileout = os.path.splitext(filename)[0] + '.png'
|
||||
|
||||
arguments = read_filename_arguments(filename)
|
||||
|
||||
image = open(filename, 'rb').read()
|
||||
image = convert_1bpp_to_2bpp(image)
|
||||
|
||||
result = convert_2bpp_to_png(image, **arguments)
|
||||
width, height, palette, greyscale, bitdepth, px_map = result
|
||||
|
||||
w = png.Writer(width, height, palette=palette, compression=9, greyscale=greyscale, bitdepth=bitdepth)
|
||||
with open(fileout, 'wb') as f:
|
||||
w.write(f, px_map)
|
||||
|
||||
|
||||
def export_png_to_1bpp(filename, fileout=None):
|
||||
|
||||
if fileout == None:
|
||||
fileout = os.path.splitext(filename)[0] + '.1bpp'
|
||||
|
||||
arguments = read_filename_arguments(filename)
|
||||
image = png_to_1bpp(filename, **arguments)
|
||||
|
||||
to_file(fileout, image)
|
||||
|
||||
def png_to_1bpp(filename, **kwargs):
|
||||
image, kwargs = png_to_2bpp(filename, **kwargs)
|
||||
return convert_2bpp_to_1bpp(image)
|
||||
|
||||
|
||||
def convert_to_2bpp(filenames=[]):
|
||||
for filename in filenames:
|
||||
filename, name, extension = try_decompress(filename)
|
||||
if extension == '.1bpp':
|
||||
export_1bpp_to_2bpp(filename)
|
||||
elif extension == '.2bpp':
|
||||
pass
|
||||
elif extension == '.png':
|
||||
export_png_to_2bpp(filename)
|
||||
else:
|
||||
raise Exception("Don't know how to convert {} to 2bpp!".format(filename))
|
||||
|
||||
def convert_to_1bpp(filenames=[]):
|
||||
for filename in filenames:
|
||||
filename, name, extension = try_decompress(filename)
|
||||
if extension == '.1bpp':
|
||||
pass
|
||||
elif extension == '.2bpp':
|
||||
export_2bpp_to_1bpp(filename)
|
||||
elif extension == '.png':
|
||||
export_png_to_1bpp(filename)
|
||||
else:
|
||||
raise Exception("Don't know how to convert {} to 1bpp!".format(filename))
|
||||
|
||||
def convert_to_png(filenames=[]):
|
||||
for filename in filenames:
|
||||
filename, name, extension = try_decompress(filename)
|
||||
if extension == '.1bpp':
|
||||
export_1bpp_to_png(filename)
|
||||
elif extension == '.2bpp':
|
||||
export_2bpp_to_png(filename)
|
||||
elif extension == '.png':
|
||||
pass
|
||||
else:
|
||||
raise Exception("Don't know how to convert {} to png!".format(filename))
|
||||
|
||||
def compress(filenames=[]):
|
||||
for filename in filenames:
|
||||
data = open(filename, 'rb').read()
|
||||
lz_data = Compressed(data).output
|
||||
to_file(filename + '.lz', lz_data)
|
||||
|
||||
def decompress(filenames=[]):
|
||||
for filename in filenames:
|
||||
name, extension = os.path.splitext(filename)
|
||||
lz_data = open(filename, 'rb').read()
|
||||
data = Decompressed(lz_data).output
|
||||
to_file(name, data)
|
||||
|
||||
def try_decompress(filename):
|
||||
"""
|
||||
Try to decompress a graphic when determining the filetype.
|
||||
This skips the manual unlz step when attempting
|
||||
to convert lz-compressed graphics to png.
|
||||
"""
|
||||
name, extension = os.path.splitext(filename)
|
||||
if extension == '.lz':
|
||||
decompress([filename])
|
||||
filename = name
|
||||
name, extension = os.path.splitext(filename)
|
||||
return filename, name, extension
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('mode')
|
||||
ap.add_argument('filenames', nargs='*')
|
||||
args = ap.parse_args()
|
||||
|
||||
method = {
|
||||
'2bpp': convert_to_2bpp,
|
||||
'1bpp': convert_to_1bpp,
|
||||
'png': convert_to_png,
|
||||
'lz': compress,
|
||||
'unlz': decompress,
|
||||
}.get(args.mode, None)
|
||||
|
||||
if method == None:
|
||||
raise Exception("Unknown conversion method!")
|
||||
|
||||
method(args.filenames)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
580
tools/pokemontools/lz.py
Normal file
580
tools/pokemontools/lz.py
Normal file
@@ -0,0 +1,580 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Pokemon Crystal data de/compression.
|
||||
"""
|
||||
|
||||
"""
|
||||
A rundown of Pokemon Crystal's compression scheme:
|
||||
|
||||
Control commands occupy bits 5-7.
|
||||
Bits 0-4 serve as the first parameter <n> for each command.
|
||||
"""
|
||||
lz_commands = {
|
||||
'literal': 0, # n values for n bytes
|
||||
'iterate': 1, # one value for n bytes
|
||||
'alternate': 2, # alternate two values for n bytes
|
||||
'blank': 3, # zero for n bytes
|
||||
}
|
||||
|
||||
"""
|
||||
Repeater commands repeat any data that was just decompressed.
|
||||
They take an additional signed parameter <s> to mark a relative starting point.
|
||||
These wrap around (positive from the start, negative from the current position).
|
||||
"""
|
||||
lz_commands.update({
|
||||
'repeat': 4, # n bytes starting from s
|
||||
'flip': 5, # n bytes in reverse bit order starting from s
|
||||
'reverse': 6, # n bytes backwards starting from s
|
||||
})
|
||||
|
||||
"""
|
||||
The long command is used when 5 bits aren't enough. Bits 2-4 contain a new control code.
|
||||
Bits 0-1 are appended to a new byte as 8-9, allowing a 10-bit parameter.
|
||||
"""
|
||||
lz_commands.update({
|
||||
'long': 7, # n is now 10 bits for a new control code
|
||||
})
|
||||
max_length = 1 << 10 # can't go higher than 10 bits
|
||||
lowmax = 1 << 5 # standard 5-bit param
|
||||
|
||||
"""
|
||||
If 0xff is encountered instead of a command, decompression ends.
|
||||
"""
|
||||
lz_end = 0xff
|
||||
|
||||
|
||||
bit_flipped = [
|
||||
sum(((byte >> i) & 1) << (7 - i) for i in range(8))
|
||||
for byte in range(0x100)
|
||||
]
|
||||
|
||||
|
||||
class Compressed:
|
||||
|
||||
"""
|
||||
Usage:
|
||||
lz = Compressed(data).output
|
||||
or
|
||||
lz = Compressed().compress(data)
|
||||
or
|
||||
c = Compressed()
|
||||
c.data = data
|
||||
lz = c.compress()
|
||||
|
||||
There are some issues with reproducing the target compressor.
|
||||
Some notes are listed here:
|
||||
- the criteria for detecting a lookback is inconsistent
|
||||
- sometimes lookbacks that are mostly 0s are pruned, sometimes not
|
||||
- target appears to skip ahead if it can use a lookback soon, stopping the current command short or in some cases truncating it with literals.
|
||||
- this has been implemented, but the specifics are unknown
|
||||
- self.min_scores: It's unknown if blank's minimum score should be 1 or 2. Most likely it's 1, with some other hack to account for edge cases.
|
||||
- may be related to the above
|
||||
- target does not appear to compress backwards
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
self.min_scores = {
|
||||
'blank': 1,
|
||||
'iterate': 2,
|
||||
'alternate': 3,
|
||||
'repeat': 3,
|
||||
'reverse': 3,
|
||||
'flip': 3,
|
||||
}
|
||||
|
||||
self.preference = [
|
||||
'repeat',
|
||||
'blank',
|
||||
'flip',
|
||||
'reverse',
|
||||
'iterate',
|
||||
'alternate',
|
||||
#'literal',
|
||||
]
|
||||
|
||||
self.lookback_methods = 'repeat', 'reverse', 'flip'
|
||||
|
||||
self.__dict__.update({
|
||||
'data': None,
|
||||
'commands': lz_commands,
|
||||
'debug': False,
|
||||
'literal_only': False,
|
||||
})
|
||||
|
||||
self.arg_names = 'data', 'commands', 'debug', 'literal_only'
|
||||
|
||||
self.__dict__.update(kwargs)
|
||||
self.__dict__.update(dict(zip(self.arg_names, args)))
|
||||
|
||||
if self.data is not None:
|
||||
self.compress()
|
||||
|
||||
def compress(self, data=None):
|
||||
if data is not None:
|
||||
self.data = data
|
||||
|
||||
self.data = list(bytearray(self.data))
|
||||
|
||||
self.indexes = {}
|
||||
self.lookbacks = {}
|
||||
for method in self.lookback_methods:
|
||||
self.lookbacks[method] = {}
|
||||
|
||||
self.address = 0
|
||||
self.end = len(self.data)
|
||||
self.output = []
|
||||
self.literal = None
|
||||
|
||||
while self.address < self.end:
|
||||
|
||||
if self.score():
|
||||
self.do_literal()
|
||||
self.do_winner()
|
||||
|
||||
else:
|
||||
if self.literal == None:
|
||||
self.literal = self.address
|
||||
self.address += 1
|
||||
|
||||
self.do_literal()
|
||||
|
||||
self.output += [lz_end]
|
||||
return self.output
|
||||
|
||||
def reset_scores(self):
|
||||
self.scores = {}
|
||||
self.offsets = {}
|
||||
self.helpers = {}
|
||||
for method in self.min_scores.iterkeys():
|
||||
self.scores[method] = 0
|
||||
|
||||
def bit_flip(self, byte):
|
||||
return bit_flipped[byte]
|
||||
|
||||
def do_literal(self):
|
||||
if self.literal != None:
|
||||
length = abs(self.address - self.literal)
|
||||
start = min(self.literal, self.address + 1)
|
||||
self.helpers['literal'] = self.data[start:start+length]
|
||||
self.do_cmd('literal', length)
|
||||
self.literal = None
|
||||
|
||||
def score(self):
|
||||
self.reset_scores()
|
||||
|
||||
map(self.score_literal, ['iterate', 'alternate', 'blank'])
|
||||
|
||||
for method in self.lookback_methods:
|
||||
self.scores[method], self.offsets[method] = self.find_lookback(method, self.address)
|
||||
|
||||
self.stop_short()
|
||||
|
||||
return any(
|
||||
score
|
||||
> self.min_scores[method] + int(score > lowmax)
|
||||
for method, score in self.scores.iteritems()
|
||||
)
|
||||
|
||||
def stop_short(self):
|
||||
"""
|
||||
If a lookback is close, reduce the scores of other commands.
|
||||
"""
|
||||
best_method, best_score = max(
|
||||
self.scores.items(),
|
||||
key = lambda x: (
|
||||
x[1],
|
||||
-self.preference.index(x[0])
|
||||
)
|
||||
)
|
||||
for method in self.lookback_methods:
|
||||
min_score = self.min_scores[method]
|
||||
for address in range(self.address+1, self.address+best_score):
|
||||
length, index = self.find_lookback(method, address)
|
||||
if length > max(min_score, best_score):
|
||||
# BUG: lookbacks can reduce themselves. This appears to be a bug in the target also.
|
||||
for m, score in self.scores.items():
|
||||
self.scores[m] = min(score, address - self.address)
|
||||
|
||||
|
||||
def read(self, address=None):
|
||||
if address is None:
|
||||
address = self.address
|
||||
if 0 <= address < len(self.data):
|
||||
return self.data[address]
|
||||
return None
|
||||
|
||||
def find_all_lookbacks(self):
|
||||
for method in self.lookback_methods:
|
||||
for address, byte in enumerate(self.data):
|
||||
self.find_lookback(method, address)
|
||||
|
||||
def find_lookback(self, method, address=None):
|
||||
"""Temporarily stubbed, because the real function doesn't run in polynomial time."""
|
||||
return 0, None
|
||||
|
||||
def broken_find_lookback(self, method, address=None):
|
||||
if address is None:
|
||||
address = self.address
|
||||
|
||||
existing = self.lookbacks.get(method, {}).get(address)
|
||||
if existing != None:
|
||||
return existing
|
||||
|
||||
lookback = 0, None
|
||||
|
||||
# Better to not carelessly optimize at the moment.
|
||||
"""
|
||||
if address < 2:
|
||||
return lookback
|
||||
"""
|
||||
|
||||
byte = self.read(address)
|
||||
if byte is None:
|
||||
return lookback
|
||||
|
||||
direction, mutate = {
|
||||
'repeat': ( 1, int),
|
||||
'reverse': (-1, int),
|
||||
'flip': ( 1, self.bit_flip),
|
||||
}[method]
|
||||
|
||||
# Doesn't seem to help
|
||||
"""
|
||||
if mutate == self.bit_flip:
|
||||
if byte == 0:
|
||||
self.lookbacks[method][address] = lookback
|
||||
return lookback
|
||||
"""
|
||||
|
||||
data_len = len(self.data)
|
||||
is_two_byte_index = lambda index: int(index < address - 0x7f)
|
||||
|
||||
for index in self.get_indexes(mutate(byte)):
|
||||
|
||||
if index >= address:
|
||||
break
|
||||
|
||||
old_length, old_index = lookback
|
||||
if direction == 1:
|
||||
if old_length > data_len - index: break
|
||||
else:
|
||||
if old_length > index: continue
|
||||
|
||||
if self.read(index) in [None]: continue
|
||||
|
||||
length = 1 # we know there's at least one match, or we wouldn't be checking this index
|
||||
while 1:
|
||||
this_byte = self.read(address + length)
|
||||
that_byte = self.read(index + length * direction)
|
||||
if that_byte == None or this_byte != mutate(that_byte):
|
||||
break
|
||||
length += 1
|
||||
|
||||
score = length - is_two_byte_index(index)
|
||||
old_score = old_length - is_two_byte_index(old_index)
|
||||
if score >= old_score or (score == old_score and length > old_length):
|
||||
# XXX maybe avoid two-byte indexes when possible
|
||||
if score >= lookback[0] - is_two_byte_index(lookback[1]):
|
||||
lookback = length, index
|
||||
|
||||
self.lookbacks[method][address] = lookback
|
||||
return lookback
|
||||
|
||||
def get_indexes(self, byte):
|
||||
if not self.indexes.has_key(byte):
|
||||
self.indexes[byte] = []
|
||||
index = -1
|
||||
while 1:
|
||||
try:
|
||||
index = self.data.index(byte, index + 1)
|
||||
except ValueError:
|
||||
break
|
||||
self.indexes[byte].append(index)
|
||||
return self.indexes[byte]
|
||||
|
||||
def score_literal(self, method):
|
||||
address = self.address
|
||||
|
||||
compare = {
|
||||
'blank': [0],
|
||||
'iterate': [self.read(address)],
|
||||
'alternate': [self.read(address), self.read(address + 1)],
|
||||
}[method]
|
||||
|
||||
# XXX may or may not be correct
|
||||
if method == 'alternate' and compare[0] == 0:
|
||||
return
|
||||
|
||||
length = 0
|
||||
while self.read(address + length) == compare[length % len(compare)]:
|
||||
length += 1
|
||||
|
||||
self.scores[method] = length
|
||||
self.helpers[method] = compare
|
||||
|
||||
def do_winner(self):
|
||||
winners = filter(
|
||||
lambda method_score:
|
||||
method_score[1]
|
||||
> self.min_scores[method_score[0]] + int(method_score[1] > lowmax),
|
||||
self.scores.iteritems()
|
||||
)
|
||||
winners.sort(
|
||||
key = lambda method_score: (
|
||||
-(method_score[1] - self.min_scores[method_score[0]] - int(method_score[1] > lowmax)),
|
||||
self.preference.index(method_score[0])
|
||||
)
|
||||
)
|
||||
winner, score = winners[0]
|
||||
|
||||
length = min(score, max_length)
|
||||
self.do_cmd(winner, length)
|
||||
self.address += length
|
||||
|
||||
def do_cmd(self, cmd, length):
|
||||
start_address = self.address
|
||||
|
||||
cmd_length = length - 1
|
||||
|
||||
output = []
|
||||
|
||||
if length > lowmax:
|
||||
output.append(
|
||||
(self.commands['long'] << 5)
|
||||
+ (self.commands[cmd] << 2)
|
||||
+ (cmd_length >> 8)
|
||||
)
|
||||
output.append(
|
||||
cmd_length & 0xff
|
||||
)
|
||||
else:
|
||||
output.append(
|
||||
(self.commands[cmd] << 5)
|
||||
+ cmd_length
|
||||
)
|
||||
|
||||
self.helpers['blank'] = [] # quick hack
|
||||
output += self.helpers.get(cmd, [])
|
||||
|
||||
if cmd in self.lookback_methods:
|
||||
offset = self.offsets[cmd]
|
||||
# Negative offsets are one byte.
|
||||
# Positive offsets are two.
|
||||
if 0 < start_address - offset - 1 <= 0x7f:
|
||||
offset = (start_address - offset - 1) | 0x80
|
||||
output += [offset]
|
||||
else:
|
||||
output += [offset / 0x100, offset % 0x100] # big endian
|
||||
|
||||
if self.debug:
|
||||
print(' '.join(map(str, [
|
||||
cmd, length, '\t',
|
||||
' '.join(map('{:02x}'.format, output)),
|
||||
self.data[start_address:start_address+length] if cmd in self.lookback_methods else '',
|
||||
])))
|
||||
|
||||
self.output += output
|
||||
|
||||
|
||||
|
||||
class Decompressed:
|
||||
"""
|
||||
Interpret and decompress lz-compressed data, usually 2bpp.
|
||||
"""
|
||||
|
||||
"""
|
||||
Usage:
|
||||
data = Decompressed(lz).output
|
||||
or
|
||||
data = Decompressed().decompress(lz)
|
||||
or
|
||||
d = Decompressed()
|
||||
d.lz = lz
|
||||
data = d.decompress()
|
||||
|
||||
To decompress from offset 0x80000 in a rom:
|
||||
data = Decompressed(rom, start=0x80000).output
|
||||
"""
|
||||
|
||||
lz = None
|
||||
start = 0
|
||||
commands = lz_commands
|
||||
debug = False
|
||||
|
||||
arg_names = 'lz', 'start', 'commands', 'debug'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.__dict__.update(dict(zip(self.arg_names, args)))
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
self.command_names = dict(map(reversed, self.commands.items()))
|
||||
self.address = self.start
|
||||
|
||||
if self.lz is not None:
|
||||
self.decompress()
|
||||
|
||||
if self.debug: print(self.command_list())
|
||||
|
||||
|
||||
def command_list(self):
|
||||
"""
|
||||
Print a list of commands that were used. Useful for debugging.
|
||||
"""
|
||||
|
||||
text = ''
|
||||
|
||||
output_address = 0
|
||||
for name, attrs in self.used_commands:
|
||||
length = attrs['length']
|
||||
address = attrs['address']
|
||||
offset = attrs['offset']
|
||||
direction = attrs['direction']
|
||||
|
||||
text += '{2:03x} {0}: {1}'.format(name, length, output_address)
|
||||
text += '\t' + ' '.join(
|
||||
'{:02x}'.format(int(byte))
|
||||
for byte in self.lz[ address : address + attrs['cmd_length'] ]
|
||||
)
|
||||
|
||||
if offset is not None:
|
||||
repeated_data = self.output[ offset : offset + length * direction : direction ]
|
||||
if name == 'flip':
|
||||
repeated_data = map(bit_flipped.__getitem__, repeated_data)
|
||||
text += ' [' + ' '.join(map('{:02x}'.format, repeated_data)) + ']'
|
||||
|
||||
text += '\n'
|
||||
output_address += length
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def decompress(self, lz=None):
|
||||
|
||||
if lz is not None:
|
||||
self.lz = lz
|
||||
|
||||
self.lz = bytearray(self.lz)
|
||||
|
||||
self.used_commands = []
|
||||
self.output = []
|
||||
|
||||
while 1:
|
||||
|
||||
cmd_address = self.address
|
||||
self.offset = None
|
||||
self.direction = None
|
||||
|
||||
if (self.byte == lz_end):
|
||||
self.next()
|
||||
break
|
||||
|
||||
self.cmd = (self.byte & 0b11100000) >> 5
|
||||
|
||||
if self.cmd_name == 'long':
|
||||
# 10-bit length
|
||||
self.cmd = (self.byte & 0b00011100) >> 2
|
||||
self.length = (self.next() & 0b00000011) * 0x100
|
||||
self.length += self.next() + 1
|
||||
else:
|
||||
# 5-bit length
|
||||
self.length = (self.next() & 0b00011111) + 1
|
||||
|
||||
self.__class__.__dict__[self.cmd_name](self)
|
||||
|
||||
self.used_commands += [(
|
||||
self.cmd_name,
|
||||
{
|
||||
'length': self.length,
|
||||
'address': cmd_address,
|
||||
'offset': self.offset,
|
||||
'cmd_length': self.address - cmd_address,
|
||||
'direction': self.direction,
|
||||
}
|
||||
)]
|
||||
|
||||
# Keep track of the data we just decompressed.
|
||||
self.compressed_data = self.lz[self.start : self.address]
|
||||
|
||||
|
||||
@property
|
||||
def byte(self):
|
||||
return self.lz[ self.address ]
|
||||
|
||||
def next(self):
|
||||
byte = self.byte
|
||||
self.address += 1
|
||||
return byte
|
||||
|
||||
@property
|
||||
def cmd_name(self):
|
||||
return self.command_names.get(self.cmd)
|
||||
|
||||
|
||||
def get_offset(self):
|
||||
|
||||
if self.byte >= 0x80: # negative
|
||||
# negative
|
||||
offset = self.next() & 0x7f
|
||||
offset = len(self.output) - offset - 1
|
||||
else:
|
||||
# positive
|
||||
offset = self.next() * 0x100
|
||||
offset += self.next()
|
||||
|
||||
self.offset = offset
|
||||
|
||||
|
||||
def literal(self):
|
||||
"""
|
||||
Copy data directly.
|
||||
"""
|
||||
self.output += self.lz[ self.address : self.address + self.length ]
|
||||
self.address += self.length
|
||||
|
||||
def iterate(self):
|
||||
"""
|
||||
Write one byte repeatedly.
|
||||
"""
|
||||
self.output += [self.next()] * self.length
|
||||
|
||||
def alternate(self):
|
||||
"""
|
||||
Write alternating bytes.
|
||||
"""
|
||||
alts = [self.next(), self.next()]
|
||||
self.output += [ alts[x & 1] for x in range(self.length) ]
|
||||
|
||||
def blank(self):
|
||||
"""
|
||||
Write zeros.
|
||||
"""
|
||||
self.output += [0] * self.length
|
||||
|
||||
def flip(self):
|
||||
"""
|
||||
Repeat flipped bytes from output.
|
||||
|
||||
Example: 11100100 -> 00100111
|
||||
"""
|
||||
self._repeat(table=bit_flipped)
|
||||
|
||||
def reverse(self):
|
||||
"""
|
||||
Repeat reversed bytes from output.
|
||||
"""
|
||||
self._repeat(direction=-1)
|
||||
|
||||
def repeat(self):
|
||||
"""
|
||||
Repeat bytes from output.
|
||||
"""
|
||||
self._repeat()
|
||||
|
||||
def _repeat(self, direction=1, table=None):
|
||||
self.get_offset()
|
||||
self.direction = direction
|
||||
# Note: appends must be one at a time (this way, repeats can draw from themselves if required)
|
||||
for i in range(self.length):
|
||||
byte = self.output[ self.offset + i * direction ]
|
||||
self.output.append( table[byte] if table else byte )
|
||||
2650
tools/pokemontools/png.py
Normal file
2650
tools/pokemontools/png.py
Normal file
File diff suppressed because it is too large
Load Diff
135
tools/scan_includes.c
Normal file
135
tools/scan_includes.c
Normal file
@@ -0,0 +1,135 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <getopt.h>
|
||||
|
||||
void usage(void) {
|
||||
printf("Usage: scan_includes [-h] [-s] filename\n"
|
||||
"-h, --help\n"
|
||||
" Print usage and exit\n"
|
||||
"-s, --strict\n"
|
||||
" Fail if a file cannot be read\n");
|
||||
}
|
||||
|
||||
struct Options {
|
||||
bool help;
|
||||
bool strict;
|
||||
};
|
||||
|
||||
struct Options Options = {0};
|
||||
|
||||
void scan_file(char* filename) {
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
if (Options.strict) {
|
||||
fprintf(stderr, "Could not open file: '%s'\n", filename);
|
||||
exit(1);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
rewind(f);
|
||||
|
||||
char *buffer = malloc(size + 1);
|
||||
char *orig = buffer;
|
||||
size = fread(buffer, 1, size, f);
|
||||
buffer[size] = '\0';
|
||||
fclose(f);
|
||||
|
||||
for (; buffer && (buffer - orig < size); buffer++) {
|
||||
bool is_include = false;
|
||||
bool is_incbin = false;
|
||||
switch (*buffer) {
|
||||
case ';':
|
||||
buffer = strchr(buffer, '\n');
|
||||
if (!buffer) {
|
||||
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case '"':
|
||||
buffer++;
|
||||
buffer = strchr(buffer, '"');
|
||||
if (!buffer) {
|
||||
fprintf(stderr, "%s: unterminated string\n", filename);
|
||||
break;
|
||||
}
|
||||
buffer++;
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
case 'I':
|
||||
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
|
||||
is_incbin = true;
|
||||
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
|
||||
is_include = true;
|
||||
}
|
||||
if (is_incbin || is_include) {
|
||||
buffer = strchr(buffer, '"');
|
||||
if (!buffer) {
|
||||
break;
|
||||
}
|
||||
buffer++;
|
||||
int length = strcspn(buffer, "\"");
|
||||
char *include = malloc(length + 1);
|
||||
strncpy(include, buffer, length);
|
||||
include[length] = '\0';
|
||||
printf("%s ", include);
|
||||
if (is_include) {
|
||||
scan_file(include);
|
||||
}
|
||||
free(include);
|
||||
buffer = strchr(buffer, '"');
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
if (!buffer) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
free(orig);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int i = 0;
|
||||
struct option long_options[] = {
|
||||
{"strict", no_argument, 0, 's'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0}
|
||||
};
|
||||
int opt = -1;
|
||||
while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
|
||||
switch (opt) {
|
||||
case 's':
|
||||
Options.strict = true;
|
||||
break;
|
||||
case 'h':
|
||||
Options.help = true;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
if (Options.help) {
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
if (argc < 1) {
|
||||
usage();
|
||||
exit(1);
|
||||
}
|
||||
scan_file(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
14
tools/sort_symfile.sh
Executable file
14
tools/sort_symfile.sh
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
TEMP_FILE=$(mktemp)
|
||||
sed \
|
||||
-e "s/^..:[0-3]/0_ROM0@&/g" \
|
||||
-e "s/^..:[4-7]/1_ROMX@&/g" \
|
||||
-e "s/^..:[8-9]/2_VRAM@&/g" \
|
||||
-e "s/^..:[A-Ba-b]/3_SRAM@&/g" \
|
||||
-e "s/^00:[C-Dc-d]/4_WRAM0@&/g" \
|
||||
-e "s/^..:[D-Dd-d]/5_WRAMX@&/g" \
|
||||
-e "s/^..:[F-Ff-f]/6_HRAM@&/g" \
|
||||
$1 \
|
||||
| sort \
|
||||
| sed -e "s/^.*@//g" > $TEMP_FILE
|
||||
mv $TEMP_FILE $1
|
||||
Reference in New Issue
Block a user