gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 1b5f9f1255849e36b52b3d60787b171507d77f39 parent de539e1c9adcaa8acd22ece7a99478b6d7a9a991 Author: Toxa <untoxa@mail.ru> Date: Thu, 26 Aug 2021 14:02:52 +0300 SMS/GG: allow to set arbitrary palette of 4 colors from SMS/GG palette of 16 colors when loading 2bpp tiles Diffstat:
| M | gbdk-lib/examples/gg/large_map/large_map.c | 3 | +++ |
| M | gbdk-lib/include/sms/sms.h | 16 | +++++++++++++--- |
| M | gbdk-lib/libc/targets/z80/set_2bpp_data.s | 136 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- |
3 files changed, 120 insertions(+), 35 deletions(-)
diff --git a/gbdk-lib/examples/gg/large_map/large_map.c b/gbdk-lib/examples/gg/large_map/large_map.c @@ -47,6 +47,8 @@ void set_camera() { } void main(){ + DISPLAY_OFF; + set_2bpp_palette(COMPAT_PALETTE(11,5,4,3)); set_bkg_2bpp_data(0, 241u, bigmap_tiles); map_pos_x = map_pos_y = 0; @@ -59,6 +61,7 @@ void main(){ redraw = FALSE; move_bkg(camera_x, camera_y % 224); + DISPLAY_ON; while (TRUE) { joy = joypad(); // up or down diff --git a/gbdk-lib/include/sms/sms.h b/gbdk-lib/include/sms/sms.h @@ -348,9 +348,19 @@ void set_palette(uint8_t first_palette, uint8_t nb_palettes, uint16_t *rgb_data) #define set_bkg_data set_tile_data #define set_sprite_data(start,ntiles,src) set_tile_data((uint8_t)(start)+0x100,(uint8_t)(ntiles),src) void set_tile_data(uint16_t start, uint16_t ntiles, const void *src) __z88dk_callee __preserves_regs(iyh,iyl); -#define set_bkg_2bpp_data set_tile_2bpp_data -#define set_sprite_2bpp_data(start,ntiles,src) set_tile_2bpp_data((uint8_t)(start)+0x100,(uint8_t)(ntiles),src) -void set_tile_2bpp_data(uint16_t start, uint16_t ntiles, const void *src) __z88dk_callee __preserves_regs(iyh,iyl); + +#define COMPAT_PALETTE(C0,C1,C2,C3) (((C3) << 12) | ((C2) << 8) | ((C1) << 4) | (C0)) +extern uint16_t _current_2bpp_palette; +inline void set_2bpp_palette(uint16_t palette) { + _current_2bpp_palette = palette; +} +void set_tile_2bpp_data(uint16_t start, uint16_t ntiles, const void *src, uint16_t palette) __z88dk_callee __preserves_regs(iyh,iyl); +inline void set_bkg_2bpp_data(uint16_t start, uint16_t ntiles, const void *src) { + set_tile_2bpp_data(start, ntiles, src, _current_2bpp_palette); +} +inline void set_sprite_2bpp_data(uint16_t start, uint16_t ntiles, const void *src) { + set_tile_2bpp_data((uint8_t)(start) + 0x100u, ntiles, src, _current_2bpp_palette); +} /** Copies arbitrary data to an address in VRAM diff --git a/gbdk-lib/libc/targets/z80/set_2bpp_data.s b/gbdk-lib/libc/targets/z80/set_2bpp_data.s @@ -2,11 +2,21 @@ .title "VRAM utilities" .module VRAMUtils + + .ez80 + + .area _INITIALIZED +__current_2bpp_palette:: + .ds 0x02 + + .area _INITIALIZER + .dw 0b0011001000010000 + .area _HOME -; void set_tile_2bpp_data(uint16_t start, uint16_t ntiles, const void *src) __z88dk_callee __preserves_regs(iyh,iyl); +; void set_tile_2bpp_data(uint16_t start, uint16_t ntiles, const void *src, uint16_t palette) __z88dk_callee __preserves_regs(iyh,iyl); _set_tile_2bpp_data:: - pop de ; pop ret address + pop de ; pop ret address pop hl add hl, hl @@ -18,48 +28,110 @@ _set_tile_2bpp_data:: ld bc, #.VDP_VRAM add hl, bc - DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT + DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT SMS_WRITE_VDP_CMD h, l - pop bc - pop hl - push de - - ld e, c - ld d, b - - ld c, #.VDP_DATA - inc d - inc e - xor a + ex de, hl ; hl = ret + + pop bc ; bc = ntiles + pop de ; de = src + ex (sp), hl ; hl = palette + + ex de, hl + + inc b + inc c + push ix + push iy + ld iy, #-4 + add iy, sp + ld sp, iy + push bc jr 2$ - + 1$: - ld b, #16 -3$: - outi - jr 4$ -4$: - outi - jr 5$ -5$: - nop - out (c), a - jr 6$ + ex (sp), hl + + ld ixh, #8 6$: - nop - nop - out (c), a - nop + ld c, (hl) + inc hl + ld b, (hl) + inc hl + + ld ixl, #8 +5$: + xor a + srl b + rla + srl c + rla + + push de ; save palette + + or a + jr z, 4$ + +3$: + srl d + rr e + srl d + rr e + srl d + rr e + srl d + rr e + dec a jr nz, 3$ +4$: + ld a, e + and #0x0F + + rra + rr 0 (iy) + rra + rr 1 (iy) + rra + rr 2 (iy) + rra + rr 3 (iy) + + pop de ; restore palette + + dec ixl + jr nz, 5$ + + ld a, 0 (iy) + out (.VDP_DATA), a + VDP_DELAY + ld a, 1 (iy) + out (.VDP_DATA), a + VDP_DELAY + ld a, 2 (iy) + out (.VDP_DATA), a + VDP_DELAY + ld a, 3 (iy) + out (.VDP_DATA), a + + dec ixh + jr nz, 6$ + 2$: - dec e + ex (sp), hl + + dec l jr nz, 1$ - dec d + dec h jr nz, 1$ + ld iy, #6 + add iy, sp + ld sp, iy + pop iy + pop ix + ENABLE_VBLANK_COPY ; switch ON copy shadow SAT 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.