git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit f1e1764fa2b5118e428d8f8739b938f5c65619fa
parent df36d615cf34fb2c225beef8e95b6e5859ad9534
Author: Toxa <untoxa@mail.ru>
Date:   Fri, 17 Sep 2021 14:01:56 +0300

rework 1bpp tile loading and font loading routines; unify behavior with SMS/GG

Diffstat:
Mgbdk-lib/include/gb/drawing.h16++++------------
Mgbdk-lib/include/gb/gb.h9+++++++--
Mgbdk-lib/libc/targets/gbz80/color.s50+++++++++++++++++++++++++++++---------------------
Mgbdk-lib/libc/targets/gbz80/font.s199++++++++++++++-----------------------------------------------------------------
Mgbdk-lib/libc/targets/gbz80/set_1bit_data.s161+++++++++++++++++++++++++++++++++++++++++--------------------------------------
5 files changed, 158 insertions(+), 277 deletions(-)

diff --git a/gbdk-lib/include/gb/drawing.h b/gbdk-lib/include/gb/drawing.h @@ -37,18 +37,10 @@ #define GRAPHICS_WIDTH 160 #define GRAPHICS_HEIGHT 144 -/** Possible drawing modes */ -#if ORIGINAL - #define SOLID 0x10 /* Overwrites the existing pixels */ - #define OR 0x20 /* Performs a logical OR */ - #define XOR 0x40 /* Performs a logical XOR */ - #define AND 0x80 /* Performs a logical AND */ -#else - #define SOLID 0x00 /* Overwrites the existing pixels */ - #define OR 0x01 /* Performs a logical OR */ - #define XOR 0x02 /* Performs a logical XOR */ - #define AND 0x03 /* Performs a logical AND */ -#endif +#define SOLID 0x00 /* Overwrites the existing pixels */ +#define OR 0x01 /* Performs a logical OR */ +#define XOR 0x02 /* Performs a logical XOR */ +#define AND 0x03 /* Performs a logical AND */ /** Possible drawing colours */ #define WHITE 0 diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -793,6 +793,12 @@ inline void set_2bpp_palette(uint16_t palette) { palette; } +extern uint16_t _current_1bpp_colors; +void set_1bpp_colors_ex(uint8_t fgcolor, uint8_t bgcolor, uint8_t mode) OLDCALL; +inline void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor) { + set_1bpp_colors_ex(fgcolor, bgcolor, 0); +} + /** Sets VRAM Tile Pattern data for the Background / Window @param first_tile Index of the first tile to write @@ -818,7 +824,6 @@ void set_bkg_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLD @param first_tile Index of the first Tile to write @param nb_tiles Number of Tiles to write @param data Pointer to (1bpp) source Tile Pattern data - @param color Color Similar to @ref set_bkg_data, except source data is 1 bit-per-pixel which gets expanded into 2 bits-per-pixel. @@ -829,7 +834,7 @@ void set_bkg_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLD @see SHOW_BKG, HIDE_BKG, set_bkg_tiles */ -void set_bkg_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data, uint8_t color) OLDCALL __preserves_regs(b, c); +void set_bkg_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL __preserves_regs(b, c); /** Copies from Background / Window VRAM Tile Pattern data into a buffer diff --git a/gbdk-lib/libc/targets/gbz80/color.s b/gbdk-lib/libc/targets/gbz80/color.s @@ -1,24 +1,32 @@ - .include "global.s" + .include "global.s" - ;; Data - .area _DATA - ;; Foreground drawing colour -.fg_colour:: - .ds 1 - ;; Background drawing colour -.bg_colour:: - .ds 1 - ;; Drawing mode (.SOILD etc) + ;; Data + .area _INITIALIZED +__current_1bpp_colors:: + ;; Foreground drawing colour +.fg_colour:: + .ds 1 + ;; Background drawing colour +.bg_colour:: + .ds 1 + ;; Drawing mode (.SOILD etc) .draw_mode:: - .ds 1 + .ds 1 - .area _HOME -_color:: - LDA HL,2(SP) ; Skip return address and registers - LD A,(HL+) ; A = Foreground - LD (.fg_colour),a - LD A,(HL+) - LD (.bg_colour),a - LD A,(HL) - LD (.draw_mode),a - RET + .area _INITIALIZER + + .db 0x03 ; .fg_colour + .db 0x00 ; .bg_color + .db 0x00 ; .draw_mode + + .area _HOME +_set_1bpp_colors_ex:: +_color:: + LDA HL,2(SP) ; Skip return address and registers + LD A,(HL+) ; A = Foreground + LD (.fg_colour),a + LD A,(HL+) + LD (.bg_colour),a + LD A,(HL) + LD (.draw_mode),a + RET diff --git a/gbdk-lib/libc/targets/gbz80/font.s b/gbdk-lib/libc/targets/gbz80/font.s @@ -42,6 +42,7 @@ .globl .drawing_vbl, .drawing_lcd .globl .int_0x40, .int_0x48 .globl .remove_int + .globl _set_bkg_1bpp_data, _set_bkg_data .area _INITIALIZED .curx:: ; Cursor position @@ -72,122 +73,6 @@ _font_load_ibm:: call font_load ret - ; Copy uncompressed 16 byte tiles from (BC) to (HL), length = DE*2 - ; Note: HL must be aligned on a uint16_t boundry -font_copy_uncompressed:: - ld a,d - or e - ret z - - ld a,h - cp #0x98 - jr c,4$ - sub #0x98-0x88 - ld h,a -4$: - xor a - cp e ; Special for when e=0 you will get another loop - jr nz,1$ - dec d -1$: - WAIT_STAT - ld a,(bc) - ld (hl+),a - inc bc - - WAIT_STAT - ld a,(bc) - ld (hl),a - inc bc - - inc l - jr nz,2$ - inc h - ld a,h ; Special wrap-around - cp #0x98 - jr nz,2$ - ld h,#0x88 -2$: - dec e - jr nz,1$ - dec d - bit 7,d ; -1? - jr z,1$ - ret - - ; Copy a set of compressed (8 bytes/cell) tiles to VRAM - ; Sets the foreground and background colours based on the current - ; font colours - ; Entry: - ; From (BC) to (HL), length (DE) where DE = #cells * 8 - ; Uses the current fg_colour and bg_colour fields -font_copy_compressed:: - ld a,d - or e - ret z ; Do nothing - - ld a,h - cp #0x98 ; Take care of the 97FF -> 8800 wrap around - jr c,font_copy_compressed_loop - sub #0x98-0x88 - ld h,a -font_copy_compressed_loop: - push de - ld a,(bc) - ld e,a - inc bc - push bc - - ld bc,#0 - ; Do the background colour first - ld a,(.bg_colour) - bit 0,a - jr z,font_copy_compressed_bg_grey1 - ld b,#0xFF -font_copy_compressed_bg_grey1: - bit 1,a - jr z,font_copy_compressed_bg_grey2 - ld c,#0xFF -font_copy_compressed_bg_grey2: - ; BC contains the background colour - ; Compute what xoring we need to do to get the correct fg colour - ld d,a - ld a,(.fg_colour) - xor d - ld d,a - - bit 0,d - jr z,font_copy_compressed_grey1 - ld a,e - xor b - ld b,a -font_copy_compressed_grey1: - bit 1,d - jr z,font_copy_compressed_grey2 - ld a,e - xor c - ld c,a -font_copy_compressed_grey2: - WAIT_STAT - - ld (hl),b - inc l ; can't overflow here - ld (hl),c - inc hl - - ld a,h ; Take care of the 97FFF -> 8800 wrap around - cp #0x98 - jr nz,1$ - ld h,#0x88 -1$: - pop bc - pop de - dec de - ld a,d - or e - jr nz,font_copy_compressed_loop - ret - ; Load the font HL font_load:: call .display_off @@ -259,56 +144,42 @@ font_copy_current:: ld h,(hl) ld l,a - inc hl ; Points to the 'tiles required' entry - ld a,(hl-) - ld d,#0 - rla ; Multiple DE by 8 - rl d - rla - rl d - rla - rl d - ld e, a ; DE has the length of the tile data - - ld a,(hl) ; Get the flags - push af - and #3 ; Only lower 2 bits set encoding table size - - ld bc,#128 - cp #FONT_128ENCODING ; 0 for 256 char encoding table, 1 for 128 char - jr z,font_copy_current_copy - - ld bc,#0 - cp #FONT_NOENCODING - jr z,font_copy_current_copy + ld a, (hl+) + ld e, a + ld a, (hl+) + ld d, a - ld bc,#256 ; Must be 256 element encoding -font_copy_current_copy: - inc hl - inc hl ; Points to the start of the encoding table - add hl,bc - ld c,l - ld b,h ; BC points to the start of the tile data - - ; Find the offset in VRAM for this font - ld a,(font_current+sfont_handle_first_tile) ; First tile used for this font - swap a - ld l,a - and #0x0f - ld h,a - ld a, l - and #0xf0 + ld a, e + ld c, #128 + and #3 + cp #FONT_128ENCODING + jr z, 1$ + cp #FONT_NOENCODING + jr z, 2$ + inc h + jr 2$ +1$: + ld a, c + add l ld l, a - - ld a,#0x90 ; Tile 0 is at 9000h - add a,h - ld h,a - ; Is this font compressed? - pop af ; Recover flags - bit FONT_BCOMPRESSED,a - ; Do the jump in a mildly different way - jp z,font_copy_uncompressed - jp font_copy_compressed + adc h + sub l + ld h, a +2$: + push hl + ld c, e + ld a, (font_current+sfont_handle_first_tile) + ld e, a + push de + bit FONT_BCOMPRESSED, c + jr nz, 3$ + call _set_bkg_data + jr 4$ +3$: + call _set_bkg_1bpp_data +4$: + add sp, #4 + ret ; Set the current font to HL font_set:: diff --git a/gbdk-lib/libc/targets/gbz80/set_1bit_data.s b/gbdk-lib/libc/targets/gbz80/set_1bit_data.s @@ -1,93 +1,98 @@ - .include "global.s" + .include "global.s" - .area _HOME + .globl __current_1bpp_colors + + .area _HOME _set_bkg_1bpp_data:: _set_win_1bpp_data:: - ld d, #0x90 - ldh a, (.LCDC) - and #LCDCF_BG8000 - jr z, .copy_1bpp_tiles + ld d, #0x90 + ldh a, (.LCDC) + and #LCDCF_BG8000 + jr z, .copy_1bpp_tiles _set_sprite_1bpp_data:: - ld d, #0x80 + ld d, #0x80 .copy_1bpp_tiles: - push bc + push bc + + lda hl, 4(sp) + ld a, (hl+) ; ID of 1st tile + ld e, a + ld a, (hl+) ; Nb of tiles + ld c, a + ld a, (hl+) ; Src ptr + ld h, (hl) + ld l, a + + ; Compute dest ptr + swap e ; *16 (size of a tile) + ld a, e + and #0x0F ; Get high bits + add d ; Add base offset of target tile "block" + ld d, a + ld a, e + and #0xF0 ; Get low bits only + ld e, a - lda hl, 8(sp) - ld a, (hl) - push af + push de + ld d, h + ld e, l + pop hl - lda hl, 6(sp) - ld a, (hl+) ; ID of 1st tile - ld e, a - ld a, (hl+) ; Nb of tiles - ld c, a - ld a, (hl+) ; Src ptr - ld h, (hl) - ld l, a - - ; Compute dest ptr - swap e ; *16 (size of a tile) - ld a, e - and #0x0F ; Get high bits - add d ; Add base offset of target tile "block" - ld d, a - ld a, e - and #0xF0 ; Get low bits only - ld e, a 1$: - ; Wrap from past $97FF to $8800 onwards - ; This can be reduced to "bit 4 must be clear if bit 3 is set" - bit 3, d - jr z, 2$ - res 4, d + ; Wrap from past $97FF to $8800 onwards + ; This can be reduced to "bit 4 must be clear if bit 3 is set" + bit 3, h + jr z, 2$ + res 4, h 2$: - ld b, #8 + ld b, #8 3$: - pop af - push af + push bc + ld a, (de) ; a == bits + inc de + push de + push hl + + ld hl, #__current_1bpp_colors + ld c, (hl) + inc hl + ld h, (hl) + ld l, c ; L ==.fg_colour, H == .bg_colour + + ld e, #8 +8$: + rra + jr c, 7$ + ld d, h + jr 9$ +7$: + ld d, l +9$: + srl d + rr c + srl d + rr b + + dec e + jr nz, 8$ + + pop hl - dec a - jr z, 4$ - dec a - jr z, 5$ + WAIT_STAT + ld (hl), c + inc l + ld (hl), b + inc hl - WAIT_STAT - ld a, (hl) - ld (de), a - inc de - WAIT_STAT - ld a, (hl+) - ld (de), a - inc de - jr 6$ -4$: - WAIT_STAT - ld a, (hl+) - ld (de), a - inc de - WAIT_STAT - xor a - ld (de), a - inc de - jr 6$ -5$: - WAIT_STAT - xor a - ld (de), a - inc de - WAIT_STAT - ld a, (hl+) - ld (de), a - inc de -6$: - dec b - jr nz, 3$ + pop de + pop bc - dec c - jr nz, 1$ + dec b + jr nz, 3$ - pop af + dec c + jr nz, 1$ - pop bc - ret - + pop bc + ret +

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.