From 8386aaf12f47fcffd4170777689f09c3f113787a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Sun, 9 Jun 2019 15:15:08 +0200 Subject: [PATCH 1/2] Save 20 bytes in the CGB boot ROM --- BootROMs/cgb_boot.asm | 73 ++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 4c073ed..ac9b054 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -103,10 +103,11 @@ Start: ; Load Tilemap ld hl, $98C2 ld b, 3 - ld a, 8 IF DEF(FAST) xor a ldh [$4F], a +ELSE + ld a, 8 ENDC .tilemapLoop @@ -120,8 +121,7 @@ IF !DEF(FAST) ; Switch to second VRAM Bank ld a, 1 ldh [$4F], a - ld a, 8 - ld [hl], a + ld [hl], 8 ; Switch to back first VRAM Bank xor a ldh [$4F], a @@ -628,6 +628,8 @@ ClearMemoryPage: jr z, ClearMemoryPage ret +ReadTwoTileLines: + call ReadTileLine ; c = $f0 for even lines, $f for odd lines. ReadTileLine: ld a, [de] @@ -647,34 +649,28 @@ ReadTileLine: .dontSwap inc hl ldi [hl], a + swap c ret ReadCGBLogoHalfTile: - ld c, $f0 - call ReadTileLine - ld c, $f - call ReadTileLine - inc e - ld c, $f0 - call ReadTileLine - ld c, $f - call ReadTileLine + call .do_twice +.do_twice + call ReadTwoTileLines inc e + ld a, e ret ReadCGBLogoTile: + ld c, $f0 call ReadCGBLogoHalfTile - ld a, e add a, 22 ld e, a call ReadCGBLogoHalfTile - ld a, e sub a, 22 ld e, a ret - ReadTrademarkSymbol: ld de, TrademarkSymbol ld c,$08 @@ -706,19 +702,23 @@ LoadPalettes: jr nz, .loop ret - -AdvanceIntroAnimation: +DoIntroAnimation: + ; Animate the intro + ld a, 1 + ldh [$4F], a + ld d, 26 +.animationLoop + ld b, 2 + call WaitBFrames ld hl, $98C0 ld c, 3 ; Row count .loop ld a, [hl] cp $F ; Already blue jr z, .nextTile - inc a - ld [hl], a + inc [hl] and $7 - cp $1 ; Changed a white tile, go to next line - jr z, .nextLine + jr z, .nextLine ; Changed a white tile, go to next line .nextTile inc hl jr .loop @@ -728,18 +728,7 @@ AdvanceIntroAnimation: ld l, a inc hl dec c - ret z - jr .loop - -DoIntroAnimation: - ; Animate the intro - ld a, 1 - ldh [$4F], a - ld d, 26 -.animationLoop - ld b, 2 - call WaitBFrames - call AdvanceIntroAnimation + jr nz, .loop dec d jr nz, .animationLoop ret @@ -796,7 +785,7 @@ ENDC .emulateDMGForCGBGame call EmulateDMG ldh [$4C], a - ld a, $1; + ld a, $1 ret EmulateDMG: @@ -833,7 +822,7 @@ GetPaletteIndex: ld a, [hl] ; Old Licensee cp $33 jr z, .newLicensee - cp 1 ; Nintendo + dec a ; 1 = Nintendo jr nz, .notNintendo jr .doChecksum .newLicensee @@ -848,22 +837,22 @@ GetPaletteIndex: .doChecksum ld l, $34 ld c, $10 - ld b, 0 + xor a .checksumLoop - ld a, [hli] - add b - ld b, a + add [hl] + inc l dec c jr nz, .checksumLoop + ld b, a ; c = 0 ld hl, TitleChecksums .searchLoop ld a, l - cp ChecksumsEnd & $FF - jr z, .notNintendo + sub LOW(ChecksumsEnd) ; use sub to zero out a + ret z ld a, [hli] cp b jr nz, .searchLoop @@ -1189,4 +1178,4 @@ BgPalettes: InputPalette: ds 1 WaitLoopCounter: - ds 1 \ No newline at end of file + ds 1 From c678407d1eae9fec625a644506b3cd66654d9b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Mon, 10 Jun 2019 17:45:14 +0200 Subject: [PATCH 2/2] Compress the Sameboy logo. 117 bytes are now free --- BootROMs/SameboyLogo.1bpp | Bin 384 -> 0 bytes BootROMs/cgb_boot.asm | 175 ++++++++++++++++++++------------------ BootROMs/logo-compress.c | 48 +++++++++++ Makefile | 14 ++- 4 files changed, 151 insertions(+), 86 deletions(-) delete mode 100644 BootROMs/SameboyLogo.1bpp create mode 100644 BootROMs/logo-compress.c diff --git a/BootROMs/SameboyLogo.1bpp b/BootROMs/SameboyLogo.1bpp deleted file mode 100644 index b219f7d2a198da14e871fc1559abfda928c026ed..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 384 zcmXYtJ#GRq5QS$&;nEjC_7&1+WM7Fci{!{e6qI{|y5T9yQwX0cF;Mj>ZX4>YBeBtshI-bXqK=bkE%wyuL>49$o&`iZ`_ zyRIXPI1L-xU2fCgr+fPROypf?^O&5sS~X%YjCl?$CXPeQZD3|sv|z1St=e|Ie)k{u z`}O*LI`#b?^ar8>h>XEjo>}RFOSIaH4Kws7&Hxzy#^}8>1}kk|(p#+2GKXopU3Rh7 zF{NQBrCJ${E@Sv`fVua^Eagl|!kc}JQ9NV=gaKhxcxNs1cs%ZL*aB*^#Ruk)aV*~O K*b2q3+4K*8j+-0+ diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index ac9b054..5d492f9 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -57,11 +57,10 @@ Start: .loadLogoLoop ld a, [de] ; Read 2 rows ld b, a - call DoubleBitsAndWriteRow - call DoubleBitsAndWriteRow + call DoubleBitsAndWriteRowTwice inc de ld a, e - xor $34 ; End of logo + cp $34 ; End of logo jr nz, .loadLogoLoop call ReadTrademarkSymbol @@ -71,53 +70,24 @@ Start: xor a ld hl, $8000 call ClearMemoryPage + call LoadTileset -; Copy SameBoy Logo - ld de, SameboyLogo - ld hl, $8080 - ld c, (SameboyLogoEnd - SameboyLogo) / 2 -.sameboyLogoLoop - ld a, [de] - ldi [hl], a - inc hl - inc de - ld a, [de] - ldi [hl], a - inc hl - inc de - dec c - jr nz, .sameboyLogoLoop - -; Copy (unresized) ROM logo - ld de, $104 - ld c, 6 -.CGBROMLogoLoop - push bc - call ReadCGBLogoTile - pop bc - dec c - jr nz, .CGBROMLogoLoop - inc hl - call ReadTrademarkSymbol - -; Load Tilemap - ld hl, $98C2 ld b, 3 IF DEF(FAST) xor a ldh [$4F], a ELSE +; Load Tilemap + ld hl, $98C2 + ld d, 3 ld a, 8 -ENDC .tilemapLoop ld c, $10 .tilemapRowLoop - ld [hl], a push af -IF !DEF(FAST) ; Switch to second VRAM Bank ld a, 1 ldh [$4F], a @@ -125,25 +95,29 @@ IF !DEF(FAST) ; Switch to back first VRAM Bank xor a ldh [$4F], a -ENDC pop af ldi [hl], a - inc a + add d dec c jr nz, .tilemapRowLoop + sub 47 + push de ld de, $10 add hl, de + pop de dec b jr nz, .tilemapLoop - cp $38 - jr nz, .doneTilemap + dec d + jr z, .endTilemap + dec d - ld hl, $99a7 - ld b, 1 - ld c, 7 + ld a, $38 + ld l, $a7 + ld bc, $0107 jr .tilemapRowLoop -.doneTilemap +.endTilemap +ENDC ; Expand Palettes ld de, AnimationColors @@ -187,9 +161,7 @@ ENDC jr nz, .expandPalettesLoop ld hl, BgPalettes - ld d, 64 ; Length of write - ld e, c ; Index of write (C=0) - call LoadBGPalettes + call LoadBGPalettes64 ; Turn on LCD ld a, $91 @@ -560,8 +532,7 @@ TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c SameboyLogo: - incbin "SameboyLogo.1bpp" -SameboyLogoEnd: + incbin "SameboyLogo.rle" AnimationColors: dw $7FFF ; White @@ -578,7 +549,9 @@ DMGPalettes: dw $7FFF, $32BF, $00D0, $0000 ; Helper Functions -DoubleBitsAndWriteRow: +DoubleBitsAndWriteRowTwice: + call .twice +.twice ; Double the most significant 4 bits, b is shifted by 4 ld a, 4 ld c, 0 @@ -661,7 +634,46 @@ ReadCGBLogoHalfTile: ld a, e ret -ReadCGBLogoTile: +LoadTileset: +; Copy SameBoy Logo + ld de, SameboyLogo + ld hl, $8080 +.sameboyLogoLoop + ld a, [de] + inc de + + ld b, a + and $0f + jr z, .skipLiteral + ld c, a + +.literalLoop + ld a, [de] + ldi [hl], a + inc hl + inc de + dec c + jr nz, .literalLoop +.skipLiteral + swap b + ld a, b + and $0f + jr z, .sameboyLogoEnd + ld c, a + ld a, [de] + inc de + +.repeatLoop + ldi [hl], a + inc hl + dec c + jr nz, .repeatLoop + jr .sameboyLogoLoop + +.sameboyLogoEnd +; Copy (unresized) ROM logo + ld de, $104 +.CGBROMLogoLoop ld c, $f0 call ReadCGBLogoHalfTile add a, 22 @@ -669,8 +681,10 @@ ReadCGBLogoTile: call ReadCGBLogoHalfTile sub a, 22 ld e, a - ret - + cp $1c + jr nz, .CGBROMLogoLoop + inc hl + ; fallthrough ReadTrademarkSymbol: ld de, TrademarkSymbol ld c,$08 @@ -687,7 +701,11 @@ LoadObjPalettes: ld c, $6A jr LoadPalettes +LoadBGPalettes64: + ld d, 64 + LoadBGPalettes: + ld e, 0 ld c, $68 LoadPalettes: @@ -735,7 +753,23 @@ DoIntroAnimation: Preboot: IF !DEF(FAST) - call FadeOut + ld b, 32 ; 32 times to fade +.fadeLoop + ld c, 32 ; 32 colors to fade + ld hl, BgPalettes +.frameLoop + push bc + call BrightenColor + pop bc + dec c + jr nz, .frameLoop + + call WaitFrame + call WaitFrame + ld hl, BgPalettes + call LoadBGPalettes64 + dec b + jr nz, .fadeLoop ENDC call ClearVRAMViaHDMA ; Select the first bank @@ -921,9 +955,7 @@ LoadPalettesFromIndex: ; a = index of combination ld c, a add hl, bc ld d, 8 - ld e, 0 - call LoadBGPalettes - ret + jp LoadBGPalettes BrightenColor: ld a, [hli] @@ -976,28 +1008,6 @@ BrightenColor: ld [hli], a ret -FadeOut: - ld b, 32 ; 32 times to fade -.fadeLoop - ld c, 32 ; 32 colors to fade - ld hl, BgPalettes -.frameLoop - push bc - call BrightenColor - pop bc - dec c - jr nz, .frameLoop - - call WaitFrame - call WaitFrame - ld hl, BgPalettes - ld d, 64 ; Length of write - ld e, 0 ; Index of write - call LoadBGPalettes - dec b - ret z - jr .fadeLoop - ClearVRAMViaHDMA: ld hl, $FF51 @@ -1014,8 +1024,7 @@ ClearVRAMViaHDMA: ld [hli], a ; Do it - ld a, $12 - ld [hl], a + ld [hl], $12 ret GetInputPaletteIndex: @@ -1113,9 +1122,7 @@ ChangeAnimationPalette: call WaitFrame ld hl, BgPalettes - ld d, 64 ; Length of write - ld e, 0 ; Index of write - call LoadBGPalettes + call LoadBGPalettes64 ; Delay the wait loop while the user is selecting a palette ld a, 30 ldh [WaitLoopCounter], a diff --git a/BootROMs/logo-compress.c b/BootROMs/logo-compress.c new file mode 100644 index 0000000..e4d2677 --- /dev/null +++ b/BootROMs/logo-compress.c @@ -0,0 +1,48 @@ +#include +#include +#include + +void pair(size_t count, uint8_t byte) { + static size_t unique_count = 0; + static uint8_t unique_data[15]; + if (count == 1) { + unique_data[unique_count++] = byte; + assert(unique_count <= 15); + } else { + assert(count <= 15); + uint8_t control = (count << 4) | unique_count; + putchar(control); + + for (size_t i = 0; i < unique_count; i++) { + putchar(unique_data[i]); + } + + if (count != 0) { + putchar(byte); + } else { + assert(control == 0); + } + + unique_count = 0; + } +} + +int main(int argc, char *argv[]) { + size_t count = 1; + uint8_t byte = getchar(); + int new; + size_t position = 0; + + while ((new = getchar()) != EOF) { + if (byte == new) { + count++; + } else { + pair(count, byte); + byte = new; + count = 1; + } + } + + pair(count, byte); + pair(0, 0); +} diff --git a/Makefile b/Makefile index e052287..b4be355 100644 --- a/Makefile +++ b/Makefile @@ -322,13 +322,23 @@ $(BIN)/SDL/Shaders: Shaders # Boot ROMs +$(OBJ)/%.1bpp: %.png + -@$(MKDIR) -p $(dir $@) + rgbgfx -d 1 -h -o $@ $< + +$(OBJ)/BootROMs/SameboyLogo.rle: $(OBJ)/BootROMs/SameboyLogo.1bpp build/logo-compress + build/logo-compress < $< > $@ + +build/logo-compress: BootROMs/logo-compress.c + $(CC) $< -o $@ + $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm -$(BIN)/BootROMs/%.bin: BootROMs/%.asm +$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameboyLogo.rle -@$(MKDIR) -p $(dir $@) - cd BootROMs && rgbasm -o ../$@.tmp ../$< + rgbasm -i $(OBJ)/BootROMs/ -i BootROMs/ -o $@.tmp $< rgblink -o $@.tmp2 $@.tmp dd if=$@.tmp2 of=$@ count=1 bs=$(if $(findstring dmg,$@)$(findstring sgb,$@),256,2304) @rm $@.tmp $@.tmp2