git.y1.nz

gbdk-2020

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

commit 114d11fa4752178d5bb1a39b59000a8034f39b6f
parent 2d05e1914be9147d643277a332c1ed00f7da6049
Author: Toxa <untoxa@mail.ru>
Date:   Sat, 28 Jan 2023 02:42:28 +0300

refactor interrupts

Diffstat:
Mgbdk-lib/include/gb/gb.h26+++++++++++++-------------
Mgbdk-lib/libc/targets/sm83/ap/crt0.s67+++++++++++++++++++++++--------------------------------------------
Mgbdk-lib/libc/targets/sm83/drawing.s4++--
Mgbdk-lib/libc/targets/sm83/duck/crt0.s67+++++++++++++++++++++++--------------------------------------------
Mgbdk-lib/libc/targets/sm83/font.s14+++++---------
Mgbdk-lib/libc/targets/sm83/gb/crt0.s75+++++++++++++++++++++++++++------------------------------------------------
Mgbdk-lib/libc/targets/sm83/joy.s18------------------
Mgbdk-lib/libc/targets/sm83/lcd.s20+-------------------
Mgbdk-lib/libc/targets/sm83/serial.s22++--------------------
Mgbdk-lib/libc/targets/sm83/tim.s9+--------
Mgbdk-lib/libc/targets/sm83/tim_common.s9---------
Mgbdk-lib/libc/targets/sm83/tim_nested.s16+++-------------
12 files changed, 100 insertions(+), 247 deletions(-)

diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -171,17 +171,17 @@ typedef void (*int_handler)(void) NONBANKED; Removes the VBL interrupt handler. @see add_VBL() */ -void remove_VBL(int_handler h) OLDCALL; +void remove_VBL(int_handler h); /** Removes the LCD interrupt handler. @see add_LCD(), remove_VBL() */ -void remove_LCD(int_handler h) OLDCALL; +void remove_LCD(int_handler h); /** Removes the TIM interrupt handler. @see add_TIM(), remove_VBL() */ -void remove_TIM(int_handler h) OLDCALL; +void remove_TIM(int_handler h); /** Removes the Serial Link / SIO interrupt handler. @see add_SIO(), @see remove_VBL() @@ -195,12 +195,12 @@ void remove_TIM(int_handler h) OLDCALL; Only secondary chained SIO ISRs (added with @ref add_SIO() ) can be removed. */ -void remove_SIO(int_handler h) OLDCALL; +void remove_SIO(int_handler h); /** Removes the JOY interrupt handler. @see add_JOY(), remove_VBL() */ -void remove_JOY(int_handler h) OLDCALL; +void remove_JOY(int_handler h); /** Adds a Vertical Blanking interrupt handler. @@ -225,7 +225,7 @@ void remove_JOY(int_handler h) OLDCALL; @see ISR_VECTOR() */ -void add_VBL(int_handler h) OLDCALL; +void add_VBL(int_handler h); /** Adds a LCD interrupt handler. @@ -242,7 +242,7 @@ void add_VBL(int_handler h) OLDCALL; This can be useful for dynamically controlling the @ref SCX_REG / @ref SCY_REG registers ($FF43/$FF42) to perform special video effects. - + __Do not__ use the function definition attributes @ref CRITICAL and @ref INTERRUPT when declaring ISR functions added via add_VBL() (or LCD, etc). @@ -260,7 +260,7 @@ void add_VBL(int_handler h) OLDCALL; @see add_VBL, nowait_int_handler, ISR_VECTOR() */ -void add_LCD(int_handler h) OLDCALL; +void add_LCD(int_handler h); /** Adds a timer interrupt handler. @@ -275,7 +275,7 @@ void add_LCD(int_handler h) OLDCALL; @see add_VBL @see set_interrupts() with TIM_IFLAG, ISR_VECTOR() */ -void add_TIM(int_handler h) OLDCALL; +void add_TIM(int_handler h); /** Adds a timer interrupt handler, that could be interrupted by the other interrupts, @@ -292,7 +292,7 @@ void add_TIM(int_handler h) OLDCALL; @see add_VBL @see set_interrupts() with TIM_IFLAG, ISR_VECTOR() */ -void add_low_priority_TIM(int_handler h) OLDCALL; +void add_low_priority_TIM(int_handler h); /** Adds a Serial Link transmit complete interrupt handler. @@ -305,7 +305,7 @@ void add_low_priority_TIM(int_handler h) OLDCALL; @see send_byte, receive_byte(), add_VBL() @see set_interrupts() with SIO_IFLAG */ -void add_SIO(int_handler h) OLDCALL; +void add_SIO(int_handler h); /** Adds a joypad button change interrupt handler. @@ -322,7 +322,7 @@ void add_SIO(int_handler h) OLDCALL; @see joypad(), add_VBL() */ -void add_JOY(int_handler h) OLDCALL; +void add_JOY(int_handler h); /** Interrupt handler chain terminator that does __not__ wait for .STAT @@ -757,7 +757,7 @@ inline void disable_interrupts() PRESERVES_REGS(a, b, c, d, e, h, l) { @see enable_interrupts(), disable_interrupts() @see VBL_IFLAG, LCD_IFLAG, TIM_IFLAG, SIO_IFLAG, JOY_IFLAG */ -void set_interrupts(uint8_t flags) OLDCALL PRESERVES_REGS(b, c, d, e); +void set_interrupts(uint8_t flags) PRESERVES_REGS(b, c, d, e, h, l); /** Performs a warm reset by reloading the CPU value then jumping to the start of crt0 (0x0150) diff --git a/gbdk-lib/libc/targets/sm83/ap/crt0.s b/gbdk-lib/libc/targets/sm83/ap/crt0.s @@ -228,7 +228,7 @@ _reset:: CALL .refresh_OAM ;; Install interrupt routines - LD BC,#.std_vbl + LD DE,#.std_vbl CALL .add_VBL ;; Standard color palettes @@ -273,13 +273,10 @@ _exit:: _set_interrupts:: DI - LDA HL,2(SP) ; Skip return address + LDH (.IE),A ; interrupts are still disabled here XOR A LDH (.IF),A ; Clear pending interrupts - LD A,(HL) - EI ; Enable interrupts - LDH (.IE),A ; interrupts are still disabled here - RET + RETI ;; Copy OAM data to OAM RAM .start_refresh_OAM: @@ -409,20 +406,21 @@ gsinit:: 4$: RET - ;; Remove interrupt routine in BC from the VBL interrupt list + ;; Remove interrupt routine in DE from the VBL interrupt list ;; falldown to .remove_int +_remove_VBL:: .remove_VBL:: LD HL,#.int_0x40 - ;; Remove interrupt BC from interrupt list HL if it exists + ;; Remove interrupt DE from interrupt list HL if it exists ;; Abort if a 0000 is found (end of list) .remove_int:: 1$: LD A,(HL+) - LD E,A - LD D,(HL) - INC HL - OR D + LD C,A + LD A,(HL+) + LD B,A + OR C RET Z ; No interrupt found LD A,E @@ -432,26 +430,27 @@ gsinit:: CP B JR NZ,1$ - LD D,H - LD E,L - DEC DE - DEC DE + LD B,H + LD C,L + DEC BC + DEC BC ;; Now do a memcpy from here until the end of the list 2$: LD A,(HL+) - LD (DE),A - LD B,A - INC DE + LD (BC),A + INC BC + LD D,A LD A,(HL+) - LD (DE),A - INC DE - OR B - RET Z - JR 2$ + LD (BC),A + INC BC + OR D + JR NZ, 2$ + RET ;; Add interrupt routine in BC to the VBL interrupt list ;; falldown to .add_int +_add_VBL:: .add_VBL:: LD HL,#.int_0x40 @@ -507,23 +506,3 @@ _display_off:: AND #~LCDCF_ON LDH (.LCDC),A ; Turn off screen RET - -_remove_VBL:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD A,(HL+) - LD C,A - LD B,(HL) - CALL .remove_VBL - POP BC - RET - -_add_VBL:: - PUSH BC - LDA HL, 4(SP) ; Skip return address and registers - LD A,(HL+) - LD C,A - LD B,(HL) - CALL .add_VBL - POP BC - RET diff --git a/gbdk-lib/libc/targets/sm83/drawing.s b/gbdk-lib/libc/targets/sm83/drawing.s @@ -81,9 +81,9 @@ CALL .init_vram ; Init the charset at 0x8000 ;; Install interrupt routines - LD BC,#.drawing_vbl + LD DE,#.drawing_vbl CALL .add_VBL - LD BC,#.drawing_lcd + LD DE,#.drawing_lcd CALL .add_LCD LD A,#72 ; Set line at which LCD interrupt occurs diff --git a/gbdk-lib/libc/targets/sm83/duck/crt0.s b/gbdk-lib/libc/targets/sm83/duck/crt0.s @@ -168,7 +168,7 @@ _reset:: CALL .refresh_OAM ;; Install interrupt routines - LD BC,#.std_vbl + LD DE,#.std_vbl CALL .add_VBL ;; Standard color palettes @@ -213,13 +213,10 @@ _exit:: _set_interrupts:: DI - LDA HL,2(SP) ; Skip return address + LDH (.IE),A ; interrupts are still disabled here XOR A LDH (.IF),A ; Clear pending interrupts - LD A,(HL) - EI ; Enable interrupts - LDH (.IE),A ; interrupts are still disabled here - RET + RETI ;; Copy OAM data to OAM RAM .start_refresh_OAM: @@ -349,20 +346,21 @@ gsinit:: 4$: RET - ;; Remove interrupt routine in BC from the VBL interrupt list + ;; Remove interrupt routine in DE from the VBL interrupt list ;; falldown to .remove_int +_remove_VBL:: .remove_VBL:: LD HL,#.int_0x40 - ;; Remove interrupt BC from interrupt list HL if it exists + ;; Remove interrupt DE from interrupt list HL if it exists ;; Abort if a 0000 is found (end of list) .remove_int:: 1$: LD A,(HL+) - LD E,A - LD D,(HL) - INC HL - OR D + LD C,A + LD A,(HL+) + LD B,A + OR C RET Z ; No interrupt found LD A,E @@ -372,26 +370,27 @@ gsinit:: CP B JR NZ,1$ - LD D,H - LD E,L - DEC DE - DEC DE + LD B,H + LD C,L + DEC BC + DEC BC ;; Now do a memcpy from here until the end of the list 2$: LD A,(HL+) - LD (DE),A - LD B,A - INC DE + LD (BC),A + INC BC + LD D,A LD A,(HL+) - LD (DE),A - INC DE - OR B - RET Z - JR 2$ + LD (BC),A + INC BC + OR D + JR NZ, 2$ + RET ;; Add interrupt routine in BC to the VBL interrupt list ;; falldown to .add_int +_add_VBL:: .add_VBL:: LD HL,#.int_0x40 @@ -447,23 +446,3 @@ _display_off:: AND #~LCDCF_ON LDH (.LCDC),A ; Turn off screen RET - -_remove_VBL:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD A,(HL+) - LD C,A - LD B,(HL) - CALL .remove_VBL - POP BC - RET - -_add_VBL:: - PUSH BC - LDA HL, 4(SP) ; Skip return address and registers - LD A,(HL+) - LD C,A - LD B,(HL) - CALL .add_VBL - POP BC - RET diff --git a/gbdk-lib/libc/targets/sm83/font.s b/gbdk-lib/libc/targets/sm83/font.s @@ -39,9 +39,7 @@ .globl .fg_colour, .bg_colour - .globl .drawing_vbl, .drawing_lcd - .globl .int_0x40, .int_0x48 - .globl .remove_int + .globl .remove_VBL, .remove_LCD .globl _set_bkg_1bpp_data, _set_bkg_data .area _INITIALIZED @@ -527,12 +525,10 @@ _posy:: CALL .display_off ;; Remove any interrupts setup by the drawing routine - LD BC,#.drawing_vbl - LD HL,#.int_0x40 - CALL .remove_int - LD BC,#.drawing_lcd - LD HL,#.int_0x48 - CALL .remove_int + LD DE,#.drawing_vbl + CALL .remove_VBL + LD DE,#.drawing_lcd + CALL .remove_LCD 1$: CALL .tmode_out diff --git a/gbdk-lib/libc/targets/sm83/gb/crt0.s b/gbdk-lib/libc/targets/sm83/gb/crt0.s @@ -234,7 +234,7 @@ _reset:: CALL .refresh_OAM ;; Install interrupt routines - LD BC,#.std_vbl + LD DE,#.std_vbl CALL .add_VBL ;; Standard color palettes @@ -279,13 +279,10 @@ _exit:: _set_interrupts:: DI - LDA HL,2(SP) ; Skip return address + LDH (.IE),A ; interrupts are still disabled here XOR A LDH (.IF),A ; Clear pending interrupts - LD A,(HL) - EI ; Enable interrupts - LDH (.IE),A ; interrupts are still disabled here - RET + RETI ;; Copy OAM data to OAM RAM .start_refresh_OAM: @@ -419,20 +416,21 @@ gsinit:: 4$: RET - ;; Remove interrupt routine in BC from the VBL interrupt list + ;; Remove interrupt routine in DE from the VBL interrupt list ;; falldown to .remove_int +_remove_VBL:: .remove_VBL:: LD HL,#.int_0x40 - ;; Remove interrupt BC from interrupt list HL if it exists + ;; Remove interrupt DE from interrupt list HL if it exists ;; Abort if a 0000 is found (end of list) .remove_int:: 1$: LD A,(HL+) - LD E,A - LD D,(HL) - INC HL - OR D + LD C,A + LD A,(HL+) + LD B,A + OR C RET Z ; No interrupt found LD A,E @@ -442,30 +440,31 @@ gsinit:: CP B JR NZ,1$ - LD D,H - LD E,L - DEC DE - DEC DE + LD B,H + LD C,L + DEC BC + DEC BC ;; Now do a memcpy from here until the end of the list 2$: LD A,(HL+) - LD (DE),A - LD B,A - INC DE + LD (BC),A + INC BC + LD D,A LD A,(HL+) - LD (DE),A - INC DE - OR B - RET Z - JR 2$ + LD (BC),A + INC BC + OR D + JR NZ, 2$ + RET - ;; Add interrupt routine in BC to the VBL interrupt list + ;; Add interrupt routine in DE to the VBL interrupt list ;; falldown to .add_int +_add_VBL:: .add_VBL:: LD HL,#.int_0x40 - ;; Add interrupt routine in BC to the interrupt list in HL + ;; Add interrupt routine in DE to the interrupt list in HL .add_int:: 1$: LD A,(HL+) @@ -474,9 +473,9 @@ gsinit:: INC HL JR 1$ 2$: - LD A,B + LD A,D LD (HL-),A - LD (HL),C + LD (HL),E RET ;; Wait for VBL interrupt to be finished @@ -517,23 +516,3 @@ _display_off:: AND #~LCDCF_ON LDH (.LCDC),A ; Turn off screen RET - -_remove_VBL:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD A,(HL+) - LD C,A - LD B,(HL) - CALL .remove_VBL - POP BC - RET - -_add_VBL:: - PUSH BC - LDA HL, 4(SP) ; Skip return address and registers - LD A,(HL+) - LD C,A - LD B,(HL) - CALL .add_VBL - POP BC - RET diff --git a/gbdk-lib/libc/targets/sm83/joy.s b/gbdk-lib/libc/targets/sm83/joy.s @@ -14,29 +14,11 @@ .area _HOME _add_JOY:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .add_JOY - POP BC - RET - .add_JOY:: LD HL,#.int_0x60 JP .add_int _remove_JOY:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .remove_JOY - POP BC - RET - .remove_JOY:: LD HL,#.int_0x60 JP .remove_int diff --git a/gbdk-lib/libc/targets/sm83/lcd.s b/gbdk-lib/libc/targets/sm83/lcd.s @@ -35,7 +35,7 @@ LD L,A OR H CALL NZ, .call_hl -1$: +1$: POP HL POP DE POP BC @@ -48,29 +48,11 @@ RETI _add_LCD:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .add_LCD - POP BC - RET - .add_LCD:: LD HL,#.int_0x48 JP .add_int _remove_LCD:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .remove_LCD - POP BC - RET - .remove_LCD:: LD HL,#.int_0x48 JP .remove_int diff --git a/gbdk-lib/libc/targets/sm83/serial.s b/gbdk-lib/libc/targets/sm83/serial.s @@ -20,10 +20,10 @@ ;; initialize SIO LD BC,#.serial_IO CALL .add_SIO - + XOR A LDH (.IF),A - + LDH A,(.IE) OR A,#0b00001000 ; Serial I/O = On LDH (.IE),A @@ -37,29 +37,11 @@ .area _HOME _add_SIO:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .add_SIO - POP BC - RET - .add_SIO:: LD HL,#.int_0x58 JP .add_int _remove_SIO:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .remove_SIO - POP BC - RET - .remove_SIO:: LD HL,#.int_0x58 JP .remove_int diff --git a/gbdk-lib/libc/targets/sm83/tim.s b/gbdk-lib/libc/targets/sm83/tim.s @@ -15,11 +15,4 @@ .area _HOME _add_TIM:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .add_TIM - POP BC - RET + JP .add_TIM diff --git a/gbdk-lib/libc/targets/sm83/tim_common.s b/gbdk-lib/libc/targets/sm83/tim_common.s @@ -9,15 +9,6 @@ JP .add_int _remove_TIM:: - PUSH BC - LDA HL,4(SP) ; Skip return address and registers - LD C,(HL) - INC HL - LD B,(HL) - CALL .remove_TIM - POP BC - RET - .remove_TIM:: LD HL,#.int_0x50 JP .remove_int diff --git a/gbdk-lib/libc/targets/sm83/tim_nested.s b/gbdk-lib/libc/targets/sm83/tim_nested.s @@ -10,20 +10,10 @@ EI PUSH AF PUSH HL - JP .tim_isr_jump - - .area _HOME - -.tim_isr_jump: LD HL, #.int_0x50 JP .int + .area _HOME + _add_low_priority_TIM:: - PUSH BC - LDA HL, 4(SP) ; Skip return address and registers - LD C, (HL) - INC HL - LD B,(HL) - CALL .add_TIM - POP BC - RET + JP .add_TIM

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