From 1fc142964e4fa4e5dad42f2dcced3458a08f3069 Mon Sep 17 00:00:00 2001 From: Toxa Date: Sat, 9 Sep 2023 12:02:04 +0300 Subject: [PATCH] SMS/GG: ability to move VDP SAT and name table to other locations by writing to VDP R2 and VDP R5; set name table to 0x1800 and SAT to 0x1F00 by default to free up some sprite tile space --- gbdk-lib/include/sms/sms.h | 12 ++--- gbdk-lib/libc/targets/z80/cls.s | 12 ++++- gbdk-lib/libc/targets/z80/coords_to_address.s | 16 ++++-- gbdk-lib/libc/targets/z80/gg/crt0.s | 30 ++++++------ gbdk-lib/libc/targets/z80/gg/global.s | 3 +- gbdk-lib/libc/targets/z80/putchar.s | 13 +++-- gbdk-lib/libc/targets/z80/scroll.s | 22 +++++++-- gbdk-lib/libc/targets/z80/set_tile.s | 9 ---- gbdk-lib/libc/targets/z80/set_tile_map_xy.s | 26 ++++++---- .../libc/targets/z80/set_tile_map_xy_compat.s | 28 +++++++---- gbdk-lib/libc/targets/z80/set_tile_submap.s | 26 ++++++---- .../libc/targets/z80/set_tile_submap_compat.s | 26 ++++++---- gbdk-lib/libc/targets/z80/sms/crt0.s | 30 ++++++------ gbdk-lib/libc/targets/z80/sms/global.s | 3 +- gbdk-lib/libc/targets/z80/sms_fill_rect_xy.s | 26 ++++++---- .../targets/z80/sms_fill_rect_xy_compat.s | 28 +++++++---- gbdk-lib/libc/targets/z80/sms_int.s | 49 ++++++++++--------- gbdk-lib/libc/targets/z80/sms_refresh_oam.s | 9 ++-- 18 files changed, 222 insertions(+), 146 deletions(-) diff --git a/gbdk-lib/include/sms/sms.h b/gbdk-lib/include/sms/sms.h index 85b27e97..9d769ec7 100644 --- a/gbdk-lib/include/sms/sms.h +++ b/gbdk-lib/include/sms/sms.h @@ -593,8 +593,8 @@ inline void set_sprite_1bpp_data(uint16_t start, uint16_t ntiles, const void *sr void set_data(uint16_t dst, const void *src, uint16_t size) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); void vmemcpy(uint16_t dst, const void *src, uint16_t size) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); -void set_tile_map(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); -void set_tile_map_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); +void set_tile_map(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) Z88DK_CALLEE; +void set_tile_map_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) Z88DK_CALLEE; #define set_bkg_tiles set_tile_map_compat #define set_win_tiles set_tile_map_compat @@ -617,8 +617,8 @@ inline void set_bkg_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const VBK_REG = VBK_TILES; } -void set_tile_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); -void set_tile_submap_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); +void set_tile_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE; +void set_tile_submap_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE; inline void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) { set_tile_submap_compat(x, y, w, h, map_w, map); } @@ -645,8 +645,8 @@ inline void set_bkg_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h VBK_REG = VBK_TILES; } -void fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); -void fill_rect_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); +void fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE; +void fill_rect_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE; #define fill_bkg_rect fill_rect_compat #define fill_win_rect fill_rect_compat diff --git a/gbdk-lib/libc/targets/z80/cls.s b/gbdk-lib/libc/targets/z80/cls.s index 9ae8e7fa..b561fd5d 100644 --- a/gbdk-lib/libc/targets/z80/cls.s +++ b/gbdk-lib/libc/targets/z80/cls.s @@ -15,7 +15,15 @@ _cls:: DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT - ld hl, #(.VDP_TILEMAP + ((.SCREEN_Y_OFS * .VDP_MAP_WIDTH) * 2)) + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld d, a + ld e, #0 + ld hl, #((.SCREEN_Y_OFS * .VDP_MAP_WIDTH) * 2) + add hl, de + WRITE_VDP_CMD_HL ld hl, #.SPACE @@ -28,7 +36,7 @@ _cls:: 1$: dec c jr nz, 2$ - djnz 2$ + djnz 2$ ENABLE_VBLANK_COPY ; switch ON copy shadow SAT ret \ No newline at end of file diff --git a/gbdk-lib/libc/targets/z80/coords_to_address.s b/gbdk-lib/libc/targets/z80/coords_to_address.s index 7c30c835..06d845fd 100644 --- a/gbdk-lib/libc/targets/z80/coords_to_address.s +++ b/gbdk-lib/libc/targets/z80/coords_to_address.s @@ -5,23 +5,30 @@ .area _HOME -; translate coords in DE and given base in BC into address in DE +; translate coords in DE into address in DE .coords_to_address:: + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld b, a + ld a, d add #.SCREEN_Y_OFS ld d, a xor a + ld c, a FAST_MOD8 d #28 ld d, a ld a, e add #.SCREEN_X_OFS and #0x1f - ld e, a + ld e, a ld a, d rrca ; rrca(2) == rlca(6) - rrca + rrca ld d, a and #0x07 add b @@ -41,7 +48,6 @@ _get_bkg_xy_addr:: pop hl ex (sp), hl ex de, hl - ld bc, #.VDP_TILEMAP - call .coords_to_address + call .coords_to_address ex de, hl ret \ No newline at end of file diff --git a/gbdk-lib/libc/targets/z80/gg/crt0.s b/gbdk-lib/libc/targets/z80/gg/crt0.s index d7e4ec08..802e33f4 100644 --- a/gbdk-lib/libc/targets/z80/gg/crt0.s +++ b/gbdk-lib/libc/targets/z80/gg/crt0.s @@ -3,7 +3,7 @@ .title "Runtime" .module Runtime .area _HEADER (ABS) - + .globl _set_default_palette .org 0x00 ; Reset 00h @@ -11,11 +11,11 @@ im 1 ; interrupt mode 1 (this won't change) jp .init -; .org 0x08 ; --profile handler +; .org 0x08 ; --profile handler .org 0x10 ; RST 0x10: VDP_WRITE_CMD -_WRITE_VDP_CMD:: +_WRITE_VDP_CMD:: VDP_WRITE_CMD h, l ret @@ -23,7 +23,7 @@ _WRITE_VDP_CMD:: .org 0x20 ; RST 0x20: VDP_WRITE_DATA -_WRITE_VDP_DATA:: +_WRITE_VDP_DATA:: VDP_WRITE_DATA h, l ret @@ -89,12 +89,12 @@ _WRITE_VDP_DATA:: ld a, b or #.VDP_REG_MASK out (c), a - + ld a, b or a jr nz, 1$ - ;; detect PAL/NTSC + ;; detect PAL/NTSC in a, (.GG_STATE) and #.GGSTATE_NNTS jr nz, 2$ @@ -163,7 +163,7 @@ _WRITE_VDP_DATA:: ;; fills memory at HL of length BC with A, clobbers DE .memset_simple:: - ld e, a + ld e, a ld a, c or b ret z @@ -188,7 +188,7 @@ _vsync:: ld a, (_shadow_VDP_R1) and #.R1_DISP_ON ret z - + xor a ld (.vbl_done), a 1$: @@ -199,7 +199,7 @@ _vsync:: ret .area _DATA - + .start_crt_globals: __BIOS:: .ds 0x01 ; GB type (GB, PGB, CGB) @@ -234,7 +234,7 @@ _shadow_VDP_RSCY:: .ds 0x01 _shadow_VDP_R10:: .ds 0x01 -.shadow_VDP_end:: +.shadow_VDP_end:: .sys_time:: _sys_time:: @@ -250,15 +250,15 @@ __shadow_OAM_OFF:: .ds 0x01 .mode:: .ds 0x01 ; Current mode - + .area _INITIALIZER .db .R0_DEFAULT - .db #(.R1_DEFAULT | .R1_DISP_ON | .R1_IE) ; VBLANK - .db .R2_MAP_0x3800 - .db 0xFF + .db #(.R1_DEFAULT | .R1_DISP_ON | .R1_IE) ; VBLANK + .db .R2_MAP_0x1800 ; .R2_MAP_0x3800 .db 0xFF - .db .R5_SAT_0x3F00 + .db 0xFF + .db .R5_SAT_0x1F00 ; .R5_SAT_0x3F00 .db .R6_DATA_0x2000 .db #(0 | .R7_COLOR_MASK) .db 0 ; SCX diff --git a/gbdk-lib/libc/targets/z80/gg/global.s b/gbdk-lib/libc/targets/z80/gg/global.s index 7a899354..d2a9bec0 100644 --- a/gbdk-lib/libc/targets/z80/gg/global.s +++ b/gbdk-lib/libc/targets/z80/gg/global.s @@ -55,9 +55,7 @@ .JOY_P2_LATCH = 0b00001000 .VDP_VRAM = 0x4000 - .VDP_TILEMAP = 0x7800 .VDP_CRAM = 0xC000 - .VDP_SAT = 0x7F00 .VDP_SAT_TERM = 0xD0 @@ -116,6 +114,7 @@ .VDP_R5 = 0b10000101 .R5_SAT_0x3F00 = 0xFF + .R5_SAT_0x1F00 = 0xBF .R5_SAT_MASK = 0b10000001 .VDP_R6 = 0b10000110 diff --git a/gbdk-lib/libc/targets/z80/putchar.s b/gbdk-lib/libc/targets/z80/putchar.s index 22c5ce98..fee89425 100644 --- a/gbdk-lib/libc/targets/z80/putchar.s +++ b/gbdk-lib/libc/targets/z80/putchar.s @@ -46,14 +46,14 @@ _putchar:: 0$: ld hl, (font_current+sfont_handle_font) ld a, h - or l + or l jr nz, 6$ - + push de call font_load_ibm ld a, h or l - ret z + ret z call font_set pop de ld hl, (font_current+sfont_handle_font) @@ -89,7 +89,12 @@ _putchar:: sub l ld h, a - ld bc, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld b, a + ld c, #0 add hl, bc DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT diff --git a/gbdk-lib/libc/targets/z80/scroll.s b/gbdk-lib/libc/targets/z80/scroll.s index 47306d6b..a6e702e1 100644 --- a/gbdk-lib/libc/targets/z80/scroll.s +++ b/gbdk-lib/libc/targets/z80/scroll.s @@ -9,7 +9,15 @@ .scroll_viewport:: DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT - ld hl, #(.VDP_TILEMAP + ((.SCREEN_Y_OFS + 1) * .VDP_MAP_WIDTH * 2)) + + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld d, a + ld e, #0 + ld hl, #((.SCREEN_Y_OFS + 1) * .VDP_MAP_WIDTH * 2) + add hl, de ld bc, #((.SCREEN_HEIGHT - 1) * .VDP_MAP_WIDTH * 2) inc b @@ -46,9 +54,17 @@ 1$: dec c jr nz, 2$ - djnz 2$ + djnz 2$ + + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld d, a + ld e, #0 + ld hl, #((.SCREEN_Y_OFS + .SCREEN_HEIGHT - 1) * .VDP_MAP_WIDTH * 2) + add hl, de - ld hl, #(.VDP_TILEMAP + ((.SCREEN_Y_OFS + .SCREEN_HEIGHT - 1) * .VDP_MAP_WIDTH * 2)) WRITE_VDP_CMD_HL ld hl, #.SPACE diff --git a/gbdk-lib/libc/targets/z80/set_tile.s b/gbdk-lib/libc/targets/z80/set_tile.s index 3c4ab0e1..79f4d85c 100644 --- a/gbdk-lib/libc/targets/z80/set_tile.s +++ b/gbdk-lib/libc/targets/z80/set_tile.s @@ -27,9 +27,6 @@ _set_attributed_tile_xy:: pop de ; DE = YX ex (sp), hl ; HL = data - ld a, d - - ld bc, #.VDP_TILEMAP call .coords_to_address ex de, hl @@ -51,9 +48,6 @@ _set_tile_xy:: dec sp ex (sp), hl ; HL = data - ld a, d - - ld bc, #.VDP_TILEMAP call .coords_to_address ex de, hl @@ -76,9 +70,6 @@ _set_attribute_xy:: dec sp ex (sp), hl ; HL = data - ld a, d - - ld bc, #.VDP_TILEMAP call .coords_to_address ex de, hl diff --git a/gbdk-lib/libc/targets/z80/set_tile_map_xy.s b/gbdk-lib/libc/targets/z80/set_tile_map_xy.s index 7e028e2c..1a0fb54e 100644 --- a/gbdk-lib/libc/targets/z80/set_tile_map_xy.s +++ b/gbdk-lib/libc/targets/z80/set_tile_map_xy.s @@ -10,10 +10,17 @@ ;; Set background tile table from (BC) at XY = DE of size WH = HL .set_tile_map_xy:: push hl - ld hl, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld h, a + ld l, #0 ;; Set background tile from (BC) at YX = DE, size WH on stack, to VRAM from address (HL) .set_tile_map_xy_tt:: + ld a, h + ld iyh, a push bc ; Store source ld a, d @@ -30,10 +37,10 @@ ld c, a ; dest BC = HL + ((0x20 * Y) * 2) + (X * 2) ld a, b - cp #>(.VDP_TILEMAP+0x0700) - jr c, 5$ - ld b, #>.VDP_TILEMAP -5$: + and #0b00000111 + or iyh + ld b, a + pop hl ; HL = source pop de ; DE = HW push ix ; save IX @@ -85,11 +92,12 @@ ld bc, #0x40 add ix, bc + ld a, ixh - cp #>(.VDP_TILEMAP+0x0700) - jp c, 4$ - ld ixh, #>.VDP_TILEMAP -4$: + and #0b00000111 + or iyh + ld ixh, a + push ix jp 1$ 6$: diff --git a/gbdk-lib/libc/targets/z80/set_tile_map_xy_compat.s b/gbdk-lib/libc/targets/z80/set_tile_map_xy_compat.s index eb630dc2..d6ae9111 100644 --- a/gbdk-lib/libc/targets/z80/set_tile_map_xy_compat.s +++ b/gbdk-lib/libc/targets/z80/set_tile_map_xy_compat.s @@ -21,15 +21,22 @@ __map_tile_offset:: ;; Set background tile table from (BC) at XY = DE of size WH = HL .set_tile_map_xy_compat:: push hl - ld hl, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld h, a + ld l, #0 ;; Set background tile from (BC) at YX = DE, size WH on stack, to VRAM from address (HL) .set_tile_map_xy_tt_compat:: + ld a, h + ld iyh, a push bc ; Store source ld a, d rrca ; rrca(2) == rlca(6) - rrca + rrca ld d, a and #0x07 add h @@ -43,10 +50,10 @@ __map_tile_offset:: ld c, a ; dest BC = HL + ((0x20 * Y) * 2) + (X * 2) ld a, b - cp #>(.VDP_TILEMAP+0x0700) - jr c, 5$ - ld b, #>.VDP_TILEMAP -5$: + and #0b00000111 + or iyh + ld b, a + pop hl ; HL = source pop de ; DE = HW push ix ; save IX @@ -102,11 +109,12 @@ __map_tile_offset:: ld bc, #0x40 add ix, bc + ld a, ixh - cp #>(.VDP_TILEMAP+0x0700) - jp c, 4$ - ld ixh, #>.VDP_TILEMAP -4$: + and #0b00000111 + or iyh + ld ixh, a + push ix jp 1$ 6$: diff --git a/gbdk-lib/libc/targets/z80/set_tile_submap.s b/gbdk-lib/libc/targets/z80/set_tile_submap.s index 198bf0d0..92a969d7 100644 --- a/gbdk-lib/libc/targets/z80/set_tile_submap.s +++ b/gbdk-lib/libc/targets/z80/set_tile_submap.s @@ -62,10 +62,17 @@ _set_tile_submap:: ;; Set background tile table from (BC) at XY = DE of size WH = HL .set_tile_submap_xy:: push hl - ld hl, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld h, a + ld l, #0 ;; Set background tile from (BC) at YX = DE, size WH on stack, to VRAM from address (HL) .set_tile_submap_xy_tt:: + ld a, h + ld iyh, a push bc ; Store source ld a, d @@ -82,10 +89,10 @@ _set_tile_submap:: ld c, a ; dest BC = HL + ((0x20 * Y) * 2) + (X * 2) ld a, b - cp #>(.VDP_TILEMAP+0x0700) - jr c, 5$ - ld b, #>.VDP_TILEMAP -5$: + and #0b00000111 + or iyh + ld b, a + pop hl ; HL = source pop de ; DE = HW push ix ; save IX @@ -143,11 +150,12 @@ _set_tile_submap:: ld bc, #0x40 add ix, bc + ld a, ixh - cp #>(.VDP_TILEMAP+0x0700) - jp c, 4$ - ld ixh, #>.VDP_TILEMAP -4$: + and #0b00000111 + or iyh + ld ixh, a + push ix jp 1$ 6$: diff --git a/gbdk-lib/libc/targets/z80/set_tile_submap_compat.s b/gbdk-lib/libc/targets/z80/set_tile_submap_compat.s index 477dde97..34ab05d2 100644 --- a/gbdk-lib/libc/targets/z80/set_tile_submap_compat.s +++ b/gbdk-lib/libc/targets/z80/set_tile_submap_compat.s @@ -70,10 +70,17 @@ _set_tile_submap_compat:: ;; Set background tile table from (BC) at XY = DE of size WH = HL .set_tile_submap_xy_compat:: push hl - ld hl, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld h, a + ld l, #0 ;; Set background tile from (BC) at YX = DE, size WH on stack, to VRAM from address (HL) .set_tile_submap_xy_tt_compat:: + ld a, h + ld iyh, a push bc ; Store source ld a, d @@ -92,10 +99,10 @@ _set_tile_submap_compat:: ld c, a ; dest BC = HL + ((0x20 * Y) * 2) + (X * 2) ld a, b - cp #>(.VDP_TILEMAP+0x0700) - jr c, 5$ - ld b, #>.VDP_TILEMAP -5$: + and #0b00000111 + or iyh + ld b, a + pop hl ; HL = source pop de ; DE = HW push ix ; save IX @@ -155,11 +162,12 @@ _set_tile_submap_compat:: ld bc, #0x40 add ix, bc + ld a, ixh - cp #>(.VDP_TILEMAP+0x0700) - jp c, 4$ - ld ixh, #>.VDP_TILEMAP -4$: + and #0b00000111 + or iyh + ld ixh, a + push ix jp 1$ 6$: diff --git a/gbdk-lib/libc/targets/z80/sms/crt0.s b/gbdk-lib/libc/targets/z80/sms/crt0.s index b3c41417..b5bb33d3 100644 --- a/gbdk-lib/libc/targets/z80/sms/crt0.s +++ b/gbdk-lib/libc/targets/z80/sms/crt0.s @@ -3,7 +3,7 @@ .title "Runtime" .module Runtime .area _HEADER (ABS) - + .globl _set_default_palette .org 0x00 ; Reset 00h @@ -11,11 +11,11 @@ im 1 ; interrupt mode 1 (this won't change) jp .init -; .org 0x08 ; --profile handler +; .org 0x08 ; --profile handler .org 0x10 ; RST 0x10: VDP_WRITE_CMD -_WRITE_VDP_CMD:: +_WRITE_VDP_CMD:: VDP_WRITE_CMD h, l ret @@ -23,7 +23,7 @@ _WRITE_VDP_CMD:: .org 0x20 ; RST 0x20: VDP_WRITE_DATA -_WRITE_VDP_DATA:: +_WRITE_VDP_DATA:: VDP_WRITE_DATA h, l ret @@ -89,7 +89,7 @@ _WRITE_VDP_DATA:: ld a, b or #.VDP_REG_MASK out (c), a - + ld a, b or a jr nz, 1$ @@ -170,7 +170,7 @@ _WRITE_VDP_DATA:: ;; fills memory at HL of length BC with A, clobbers DE .memset_simple:: - ld e, a + ld e, a ld a, c or b ret z @@ -179,7 +179,7 @@ _WRITE_VDP_DATA:: ld d, h ld e, l inc de - + ;; copies BC bytes from HL into DE .memcpy_simple:: ld a, c @@ -195,7 +195,7 @@ _vsync:: ld a, (_shadow_VDP_R1) and #.R1_DISP_ON ret z - + xor a ld (.vbl_done), a 1$: @@ -206,7 +206,7 @@ _vsync:: ret .area _DATA - + .start_crt_globals: __BIOS:: .ds 0x01 ; GB type (GB, PGB, CGB) @@ -241,7 +241,7 @@ _shadow_VDP_RSCY:: .ds 0x01 _shadow_VDP_R10:: .ds 0x01 -.shadow_VDP_end:: +.shadow_VDP_end:: .sys_time:: _sys_time:: @@ -257,15 +257,15 @@ __shadow_OAM_OFF:: .ds 0x01 .mode:: .ds 0x01 ; Current mode - + .area _INITIALIZER .db .R0_DEFAULT - .db #(.R1_DEFAULT | .R1_DISP_ON | .R1_IE) ; VBLANK - .db .R2_MAP_0x3800 - .db 0xFF + .db #(.R1_DEFAULT | .R1_DISP_ON | .R1_IE) ; VBLANK + .db .R2_MAP_0x1800 ; .R2_MAP_0x3800 .db 0xFF - .db .R5_SAT_0x3F00 + .db 0xFF + .db .R5_SAT_0x1F00 ; .R5_SAT_0x3F00 .db .R6_DATA_0x2000 .db #(0 | .R7_COLOR_MASK) .db 0 ; SCX diff --git a/gbdk-lib/libc/targets/z80/sms/global.s b/gbdk-lib/libc/targets/z80/sms/global.s index 0147b569..1eaeb7b8 100644 --- a/gbdk-lib/libc/targets/z80/sms/global.s +++ b/gbdk-lib/libc/targets/z80/sms/global.s @@ -19,9 +19,7 @@ .JOY_P2_LATCH = 0b00001000 .VDP_VRAM = 0x4000 - .VDP_TILEMAP = 0x7800 .VDP_CRAM = 0xC000 - .VDP_SAT = 0x7F00 .VDP_SAT_TERM = 0xD0 @@ -80,6 +78,7 @@ .VDP_R5 = 0b10000101 .R5_SAT_0x3F00 = 0xFF + .R5_SAT_0x1F00 = 0xBF .R5_SAT_MASK = 0b10000001 .VDP_R6 = 0b10000110 diff --git a/gbdk-lib/libc/targets/z80/sms_fill_rect_xy.s b/gbdk-lib/libc/targets/z80/sms_fill_rect_xy.s index 02325cbe..4ca1c2e7 100644 --- a/gbdk-lib/libc/targets/z80/sms_fill_rect_xy.s +++ b/gbdk-lib/libc/targets/z80/sms_fill_rect_xy.s @@ -10,10 +10,17 @@ ;; Set background tile table from (BC) at XY = DE of size WH = HL .fill_rect_xy:: push hl - ld hl, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld h, a + ld l, #0 ;; Set background tile from (BC) at YX = DE, size WH on stack, to VRAM from address (HL) .fill_rect_xy_tt:: + ld a, h + ld iyh, a push bc ; Store source ld a, d @@ -30,10 +37,10 @@ ld c, a ; dest BC = HL + ((0x20 * Y) * 2) + (X * 2) ld a, b - cp #>(.VDP_TILEMAP+0x0700) - jr c, 5$ - ld b, #>.VDP_TILEMAP -5$: + and #0b00000111 + or iyh + ld b, a + pop hl ; HL = source pop de ; DE = HW push ix ; save IX @@ -85,11 +92,12 @@ ld bc, #0x40 add ix, bc + ld a, ixh - cp #>(.VDP_TILEMAP+0x0700) - jp c, 4$ - ld ixh, #>.VDP_TILEMAP -4$: + and #0b00000111 + or iyh + ld ixh, a + push ix jp 1$ 6$: diff --git a/gbdk-lib/libc/targets/z80/sms_fill_rect_xy_compat.s b/gbdk-lib/libc/targets/z80/sms_fill_rect_xy_compat.s index 7183ff9d..adcec129 100644 --- a/gbdk-lib/libc/targets/z80/sms_fill_rect_xy_compat.s +++ b/gbdk-lib/libc/targets/z80/sms_fill_rect_xy_compat.s @@ -12,15 +12,22 @@ ;; Set background tile table from (BC) at XY = DE of size WH = HL .fill_rect_xy_compat:: push hl - ld hl, #.VDP_TILEMAP + ld a, (_shadow_VDP_R2) + rlca + rlca + and #0b01111000 + ld h, a + ld l, #0 ;; Set background tile from (BC) at YX = DE, size WH on stack, to VRAM from address (HL) .fill_rect_xy_tt_compat:: + ld a, h + ld iyh, a push bc ; Store source ld a, d rrca ; rrca(2) == rlca(6) - rrca + rrca ld d, a and #0x07 add h @@ -34,10 +41,10 @@ ld c, a ; dest BC = HL + ((0x20 * Y) * 2) + (X * 2) ld a, b - cp #>(.VDP_TILEMAP+0x0700) - jr c, 5$ - ld b, #>.VDP_TILEMAP -5$: + and #0b00000111 + or iyh + ld b, a + pop hl ; HL = source pop de ; DE = HW push ix ; save IX @@ -88,11 +95,12 @@ ld bc, #0x40 add ix, bc + ld a, ixh - cp #>(.VDP_TILEMAP+0x0700) - jp c, 4$ - ld ixh, #>.VDP_TILEMAP -4$: + and #0b00000111 + or iyh + ld ixh, a + push ix jp 1$ 6$: diff --git a/gbdk-lib/libc/targets/z80/sms_int.s b/gbdk-lib/libc/targets/z80/sms_int.s index 3de09db0..c31debf5 100644 --- a/gbdk-lib/libc/targets/z80/sms_int.s +++ b/gbdk-lib/libc/targets/z80/sms_int.s @@ -3,7 +3,7 @@ .title "INT Handler" .module INTHandler - .globl .sys_time, .vbl_done + .globl .sys_time, .vbl_done, _shadow_VDP_R5 .globl .OUTI128, .OUTI64, __shadow_OAM_base .area _HOME @@ -20,7 +20,7 @@ _INT_ISR:: and #.STATF_INT_VBL jp z, 2$ ;; handle VBlank - + ld hl, (.sys_time) inc hl ld (.sys_time), hl @@ -38,18 +38,19 @@ _INT_ISR:: or h jp z, 1$ + ld a, (_shadow_VDP_R5) + srl a + ld d, a ld c, #.VDP_CMD - ld a, #<.VDP_SAT - out (c), a - ld a, #>.VDP_SAT + xor a out (c), a + out (c), d dec c ; c == .VDP_DATA call .OUTI64 inc c ; c == .VDP_CMD - ld a, #<(.VDP_SAT + 0x80) - out (c), a - ld a, #>(.VDP_SAT + 0x80) + ld a, #0x80 out (c), a + out (c), d dec c ; c == .VDP_DATA call .OUTI128 1$: @@ -75,7 +76,7 @@ _INT_ISR:: jp 3$ ;; handle HBlank -2$: +2$: ld hl, (.HBLANK_HANDLER0) CALL_HL @@ -87,26 +88,26 @@ _INT_ISR:: pop bc pop af ei - reti - + reti + ; void remove_LCD (int_handler h) __z88dk_fastcall __preserves_regs(b, c, iyh, iyl); _remove_LCD:: .remove_LCD:: - ld hl, #.empty_function + ld hl, #.empty_function ; void add_LCD (int_handler h) __z88dk_fastcall __preserves_regs(b, c, iyh, iyl); _add_LCD:: .add_LCD:: ld (.HBLANK_HANDLER0), hl ret - + ; void add_VBL(int_handler h) __z88dk_fastcall __preserves_regs(d, e, iyh, iyl); _add_VBL:: ld b, h ld c, l .add_VBL:: - ld hl, #.VBLANK_HANDLER0 + ld hl, #.VBLANK_HANDLER0 ;; Add interrupt routine in BC to the interrupt list in HL .add_int:: @@ -120,7 +121,7 @@ _add_VBL:: 2$: ld (hl), b dec hl - ld (hl), c + ld (hl), c ret ; void remove_VBL(int_handler h) __z88dk_fastcall __preserves_regs(iyh, iyl); @@ -148,7 +149,7 @@ _remove_VBL:: ld a, e cp c jr nz, 1$ - + ld a, d cp b jr nz, 1$ @@ -173,12 +174,12 @@ _remove_JOY:: _add_TIM:: _add_SIO:: _add_JOY:: -.empty_function: +.empty_function: ret - + .area _INITIALIZED - -.HBLANK_HANDLER0: + +.HBLANK_HANDLER0: .ds 0x02 .VBLANK_HANDLER0: .ds 0x02 @@ -187,11 +188,11 @@ _add_JOY:: .VBLANK_HANDLER2: .ds 0x02 .ds 0x02 - + .area _INITIALIZER - + .dw .empty_function - .dw 0x0000 - .dw 0x0000 + .dw 0x0000 + .dw 0x0000 .dw 0x0000 .dw 0x0000 \ No newline at end of file diff --git a/gbdk-lib/libc/targets/z80/sms_refresh_oam.s b/gbdk-lib/libc/targets/z80/sms_refresh_oam.s index e107563b..06e1e102 100644 --- a/gbdk-lib/libc/targets/z80/sms_refresh_oam.s +++ b/gbdk-lib/libc/targets/z80/sms_refresh_oam.s @@ -9,10 +9,13 @@ .area _HOME -; void refresh_OAM(); +; void refresh_OAM(); _refresh_OAM:: DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT - ld de, #.VDP_SAT + ld a, (_shadow_VDP_R5) + srl a + ld d, a + ld e, #0 VDP_WRITE_CMD d, e ld h, #>_shadow_OAM @@ -23,7 +26,7 @@ _refresh_OAM:: outi jp nz, 1$ - ld de, #(.VDP_SAT + 0x80) + ld e, #0x80 VDP_WRITE_CMD d, e ld b, #128 2$: