git.y1.nz

gbdk-2020

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

commit d96ef1e82951300f3f5b1821c1458bd00aa8e7f6
parent b8fbff77f8a789b395df562d3eb53eab06f75766
Author: Toxa <untoxa@mail.ru>
Date:   Sat, 30 Mar 2024 14:40:42 +0300

SMS/GG/MSX refactor the VDP access because of this: https://sourceforge.net/p/sdcc/bugs/3721/ the side effect of the refactor is that almost any VDP manipulation within the user ISR will allow interrupt nesting which is potentially quite dangerous

Diffstat:
Mgbdk-lib/include/msx/msx.h31++++++++++++++++++++++++++++++-
Mgbdk-lib/include/sms/sms.h30+++++++++++++++++++++++++++++-
Mgbdk-lib/libc/targets/z80/gg/global.s12+++---------
Mgbdk-lib/libc/targets/z80/gg/palette.s21+++++++--------------
Mgbdk-lib/libc/targets/z80/msxdos/crt0.s10----------
Mgbdk-lib/libc/targets/z80/msxdos/global.s8+-------
Mgbdk-lib/libc/targets/z80/msxdos/msx_int.s3---
Mgbdk-lib/libc/targets/z80/set_interrupts.s5-----
Mgbdk-lib/libc/targets/z80/sms/global.s8+-------
Mgbdk-lib/libc/targets/z80/sms/palette.s19++++++-------------
10 files changed, 77 insertions(+), 70 deletions(-)

diff --git a/gbdk-lib/include/msx/msx.h b/gbdk-lib/include/msx/msx.h @@ -91,7 +91,7 @@ #define S_PAL(n) (((n) & 0x01U) << 3) // VDP helper macros -#define __WRITE_VDP_REG(REG, v) shadow_##REG=(v);__critical{VDP_CMD=(shadow_##REG),VDP_CMD=REG;} +#define __WRITE_VDP_REG(REG, v) shadow_##REG=(v);__asm__("di");VDP_CMD=(shadow_##REG);VDP_CMD=REG;__asm__("ei") #define __READ_VDP_REG(REG) shadow_##REG void WRITE_VDP_CMD(uint16_t cmd) Z88DK_FASTCALL PRESERVES_REGS(b, c, d, e, iyh, iyl); @@ -467,6 +467,35 @@ uint8_t joypad_init(uint8_t npads, joypads_t * joypads) Z88DK_CALLEE; void joypad_ex(joypads_t * joypads) Z88DK_FASTCALL PRESERVES_REGS(iyh, iyl); +/** Enables unmasked interrupts + + @note Use @ref CRITICAL {...} instead for creating a block of + of code which should execute with interrupts temporarily + turned off. + + @see disable_interrupts, set_interrupts, CRITICAL +*/ +inline void enable_interrupts(void) PRESERVES_REGS(a, b, c, d, e, h, l, iyh, iyl) { + __asm__("ei"); +} + +/** Disables interrupts + + @note Use @ref CRITICAL {...} instead for creating a block of + of code which should execute with interrupts temporarily + turned off. + + This function may be called as many times as you like; + however the first call to @ref enable_interrupts will re-enable + them. + + @see enable_interrupts, set_interrupts, CRITICAL +*/ +inline void disable_interrupts(void) PRESERVES_REGS(a, b, c, d, e, h, l, iyh, iyl) { + __asm__("di"); +} + + #if defined(__TARGET_msxdos) #define RGB(r,g,b) ((r) | ((g) << 2) | ((b) << 4)) diff --git a/gbdk-lib/include/sms/sms.h b/gbdk-lib/include/sms/sms.h @@ -98,7 +98,7 @@ #define S_PAL(n) (((n) & 0x01U) << 3) // VDP helper macros -#define __WRITE_VDP_REG(REG, v) shadow_##REG=(v);CRITICAL{VDP_CMD=(shadow_##REG),VDP_CMD=REG;} +#define __WRITE_VDP_REG(REG, v) shadow_##REG=(v);__asm__("di");VDP_CMD=(shadow_##REG);VDP_CMD=REG;__asm__("ei") #define __READ_VDP_REG(REG) shadow_##REG void WRITE_VDP_CMD(uint16_t cmd) Z88DK_FASTCALL PRESERVES_REGS(b, c, d, e, iyh, iyl); @@ -474,6 +474,34 @@ uint8_t joypad_init(uint8_t npads, joypads_t * joypads) Z88DK_CALLEE; */ void joypad_ex(joypads_t * joypads) Z88DK_FASTCALL PRESERVES_REGS(iyh, iyl); +/** Enables unmasked interrupts + + @note Use @ref CRITICAL {...} instead for creating a block of + of code which should execute with interrupts temporarily + turned off. + + @see disable_interrupts, set_interrupts, CRITICAL +*/ +inline void enable_interrupts(void) PRESERVES_REGS(a, b, c, d, e, h, l, iyh, iyl) { + __asm__("ei"); +} + +/** Disables interrupts + + @note Use @ref CRITICAL {...} instead for creating a block of + of code which should execute with interrupts temporarily + turned off. + + This function may be called as many times as you like; + however the first call to @ref enable_interrupts will re-enable + them. + + @see enable_interrupts, set_interrupts, CRITICAL +*/ +inline void disable_interrupts(void) PRESERVES_REGS(a, b, c, d, e, h, l, iyh, iyl) { + __asm__("di"); +} + #if defined(__TARGET_sms) diff --git a/gbdk-lib/libc/targets/z80/gg/global.s b/gbdk-lib/libc/targets/z80/gg/global.s @@ -249,26 +249,20 @@ ;; Macro definitions .macro VDP_WRITE_DATA regH regL ?lbl - ld a, i ld a, regL di - out (#.VDP_DATA), a ; 11 - ld a, regH ; 4 - jp po, lbl ; 7/12 - ei ; 4 (total: 26/27) -lbl: + out (#.VDP_DATA), a + ld a, regH + ei out (#.VDP_DATA), a .endm .macro VDP_WRITE_CMD regH regL ?lbl - ld a, i ld a, regL di out (#.VDP_CMD), a ld a, regH - jp po, lbl ei -lbl: out (#.VDP_CMD), a .endm diff --git a/gbdk-lib/libc/targets/z80/gg/palette.s b/gbdk-lib/libc/targets/z80/gg/palette.s @@ -22,9 +22,8 @@ _set_palette_entry:: add hl, bc pop bc - ld a, i - di ld a, l + di out (#.VDP_CMD), a ld a, h out (#.VDP_CMD), a @@ -33,13 +32,10 @@ _set_palette_entry:: jr 3$ 3$: ld a, b - out (#.VDP_DATA), a - jp po, 2$ ei -2$: + out (#.VDP_DATA), a - ld h, d - ld l, e + ex de, hl jp (hl) ; void set_palette(uint8_t first_palette, uint8_t nb_palettes, uint16_t *rgb_data) __z88dk_callee; @@ -60,15 +56,13 @@ _set_palette:: ld c, a - ld a, i - di ld a, l + di out (#.VDP_CMD), a ld a, h - out (#.VDP_CMD), a - jp po, 2$ ei -2$: + out (#.VDP_CMD), a + ld a, c or a jr z, 3$ @@ -86,8 +80,7 @@ _set_palette:: 3$: ENABLE_VBLANK_COPY ; switch ON copy shadow SAT - ld h, d - ld l, e + ex de, hl jp (hl) .CRT_DEFAULT_PALETTE:: diff --git a/gbdk-lib/libc/targets/z80/msxdos/crt0.s b/gbdk-lib/libc/targets/z80/msxdos/crt0.s @@ -432,19 +432,12 @@ _shadow_OAM:: ld de, #__old_int_vector .restore_ldir: ld bc, #0x0005 - ld a, i - push af di ldir - pop af - jp po, 1$ ei -1$: ret .setup_video_mode: - ld a, i - push af di ;; Initialize VDP ld c, #.VDP_CMD @@ -462,10 +455,7 @@ _shadow_OAM:: or a jr nz, 1$ - pop af - jp po, 2$ ei -2$: ret .shadow_VDP: diff --git a/gbdk-lib/libc/targets/z80/msxdos/global.s b/gbdk-lib/libc/targets/z80/msxdos/global.s @@ -388,26 +388,20 @@ .endm .macro VDP_WRITE_DATA regH regL ?lbl - ld a, i ld a, regL di out (#.VDP_DATA), a ; 11 ld a, regH ; 4 - jp po, lbl ; 7/12 - ei ; 4 (total: 26/27) -lbl: + ei out (#.VDP_DATA), a .endm .macro VDP_WRITE_CMD regH regL ?lbl - ld a, i ld a, regL di out (#.VDP_CMD), a ld a, regH - jp po, lbl ei -lbl: out (#.VDP_CMD), a .endm diff --git a/gbdk-lib/libc/targets/z80/msxdos/msx_int.s b/gbdk-lib/libc/targets/z80/msxdos/msx_int.s @@ -8,14 +8,11 @@ .area _GSINIT - ld a, i ld a, #0xc3 di ld (.LS_INT_VECTOR), a ld hl, #_INT_ISR - jp po, 1$ ei -1$: ld (.LS_INT_VECTOR + 1), hl .area _HOME diff --git a/gbdk-lib/libc/targets/z80/set_interrupts.s b/gbdk-lib/libc/targets/z80/set_interrupts.s @@ -9,9 +9,7 @@ .area _HOME _set_interrupts:: - ld a, i di - push af ld a, (_shadow_VDP_R1) bit 0, l @@ -39,8 +37,5 @@ _set_interrupts:: ld a, #.VDP_R0 out (.VDP_CMD), a - pop af - jp po, 5$ ei -5$: ret diff --git a/gbdk-lib/libc/targets/z80/sms/global.s b/gbdk-lib/libc/targets/z80/sms/global.s @@ -213,26 +213,20 @@ ;; Macro definitions .macro VDP_WRITE_DATA regH regL ?lbl - ld a, i ld a, regL di out (#.VDP_DATA), a ; 11 ld a, regH ; 4 - jp po, lbl ; 7/12 - ei ; 4 (total: 26/27) -lbl: + ei out (#.VDP_DATA), a .endm .macro VDP_WRITE_CMD regH regL ?lbl - ld a, i ld a, regL di out (#.VDP_CMD), a ld a, regH - jp po, lbl ei -lbl: out (#.VDP_CMD), a .endm diff --git a/gbdk-lib/libc/targets/z80/sms/palette.s b/gbdk-lib/libc/targets/z80/sms/palette.s @@ -10,6 +10,7 @@ _set_palette_entry:: pop de pop bc + ld hl, #.VDP_CRAM bit 0, c jr z, 1$ @@ -20,20 +21,16 @@ _set_palette_entry:: add hl, bc pop bc - ld a, i - di ld a, l + di out (#.VDP_CMD), a ld a, h out (#.VDP_CMD), a ld a, c - jp po, 2$ ei -2$: out (#.VDP_DATA), a - ld h, d - ld l, e + ex de, hl jp (hl) ; void set_palette(uint8_t first_palette, uint8_t nb_palettes, uint16_t *rgb_data) __z88dk_callee; @@ -54,15 +51,12 @@ _set_palette:: ld c, a - ld a, i - di ld a, l + di out (#.VDP_CMD), a ld a, h - out (#.VDP_CMD), a - jp po, 2$ ei -2$: + out (#.VDP_CMD), a ld a, c or a jr z, 3$ @@ -80,8 +74,7 @@ _set_palette:: 3$: ENABLE_VBLANK_COPY ; switch ON copy shadow SAT - ld h, d - ld l, e + ex de, hl jp (hl) .CRT_DEFAULT_PALETTE::

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