git.y1.nz

gbdk-2020

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

commit bb9295e714c243a2679d69200ac049febedbc5a2
parent 57c9a8f6cb3a7da8b9d56ffaa36e0d2ab826a483
Author: toxa <untoxa@mail.ru>
Date:   Tue, 27 Oct 2020 12:51:13 +0300

WRAM cleanup removed from CRT0, old behaviour prevented raising "read of uninitialized RAM" exception in BGB, because all WRAM became initialized at startup

Diffstat:
Mgbdk-lib/examples/gb/dscan/dscan.c2++
Mgbdk-lib/examples/gb/galaxy.c10+++++-----
Mgbdk-lib/examples/gb/rand.c4++--
Mgbdk-lib/examples/gb/sgb_border/sgb_border.c2+-
Mgbdk-lib/libc/gb/arand.s11+++++++++++
Mgbdk-lib/libc/gb/crash_handler.s5+++++
Mgbdk-lib/libc/gb/crt0.s118+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mgbdk-lib/libc/gb/font.s14+++++++++++++-
Mgbdk-lib/libc/gb/joy.s7+++++++
Mgbdk-lib/libc/gb/lcd.s9++++++++-
Mgbdk-lib/libc/gb/serial.s10++++++++++
Mgbdk-lib/libc/gb/tim.s7+++++++
12 files changed, 129 insertions(+), 70 deletions(-)

diff --git a/gbdk-lib/examples/gb/dscan/dscan.c b/gbdk-lib/examples/gb/dscan/dscan.c @@ -725,6 +725,8 @@ void main() disable_interrupts(); DISPLAY_OFF; + initarand(((UINT16)DIV_REG << 8)); + init_screen(); init_score(); init_player(); diff --git a/gbdk-lib/examples/gb/galaxy.c b/gbdk-lib/examples/gb/galaxy.c @@ -366,11 +366,11 @@ const unsigned char * const film[] = { #define OPENED 0x02 #define CLOSING 0x03 -static UBYTE time; /* Global "time" value (counter) */ -UBYTE doorstate; /* State of the door (OPENED, CLOSED...) */ -UBYTE doorpos; /* Current position in the door animation */ -static UBYTE color; /* Current color for fading effect */ -UBYTE sframe; /* Current frame of the sprite */ +static UBYTE time = 0; /* Global "time" value (counter) */ +UBYTE doorstate = 0; /* State of the door (OPENED, CLOSED...) */ +UBYTE doorpos = 0; /* Current position in the door animation */ +static UBYTE color = 0; /* Current color for fading effect */ +UBYTE sframe = 0; /* Current frame of the sprite */ fixed bposx, bposy; /* Background position (fixed point) */ fixed bspx, bspy; /* Background speed (fixed point) */ fixed wposx, wposy; /* Window position (fixed point) */ diff --git a/gbdk-lib/examples/gb/rand.c b/gbdk-lib/examples/gb/rand.c @@ -15,8 +15,8 @@ #include <gb/drawing.h> #include <stdio.h> -UBYTE accu[80]; -UBYTE accua[80]; +UBYTE accu[80] = {0}; +UBYTE accua[80] = {0}; void main(void) { diff --git a/gbdk-lib/examples/gb/sgb_border/sgb_border.c b/gbdk-lib/examples/gb/sgb_border/sgb_border.c @@ -9,7 +9,7 @@ #define SGB_SCR_FREEZE 1 #define SGB_SCR_UNFREEZE 0 -unsigned char map_buf[20]; +unsigned char map_buf[20] = {0}; #define SGB_TRANSFER(A,B) map_buf[0]=(A),map_buf[1]=(B),sgb_transfer(map_buf) diff --git a/gbdk-lib/libc/gb/arand.s b/gbdk-lib/libc/gb/arand.s @@ -17,12 +17,23 @@ .globl _rand .area _BSS +.start_arand_vars: + .randarr: .ds 55 .raxj: .ds 0x01 .raxk: .ds 0x01 + +.end_arand_vars: + + .area _GSINIT + + XOR A + LD HL,#.start_arand_vars + LD C,#(.end_arand_vars - .start_arand_vars) + RST 0x28 .area _CODE diff --git a/gbdk-lib/libc/gb/crash_handler.s b/gbdk-lib/libc/gb/crash_handler.s @@ -17,6 +17,11 @@ .area _CRASH_HEADER(ABS) + .org 0x00 + nop + nop + rst 0x38 + .org 0x38 di jp ___HandleCrash diff --git a/gbdk-lib/libc/gb/crt0.s b/gbdk-lib/libc/gb/crt0.s @@ -7,29 +7,38 @@ .module Runtime .area _HEADER (ABS) - ;; Standard header for the GB - .org 0x00 - RET ; Empty function (default for interrupts) - -; .org 0x08 - ; --profile handler utilized by bgb_emu.h - -; .org 0x10 - ; empty -; .org 0x18 - ; crash handler utilized by crash_handler.h - .org 0x20 + ;; RST vectors +; .org 0x00 ; Trap, utilized by crash_handler.h + +; .org 0x08 ; --profile handler utilized by bgb_emu.h + +; .org 0x10 ; empty + +; .org 0x18 ; empty + + .org 0x20 ; RST 0x20 == call HL .call_hl:: - jp (hl) ; RST 0x20 == calling HL + JP (HL) + + .org 0x28 ; zero up to 256 bytes in C pointed by HL +.MemsetSmall:: + LD (HL+),A + DEC C + JR NZ,.MemsetSmall + ret -; .org 0x28 - ; empty -; .org 0x30 - ; empty -; .org 0x38 - ; empty + .org 0x30 ; copy up to 256 bytes in C from DE to HL +.MemcpySmall:: + LD A, (DE) + LD (HL+), A + INC DE + DEC C + JR NZ,.MemcpySmall + RET + +; .org 0x38 ; crash handler utilized by crash_handler.h - ;; Interrupt vectors + ;; Hardware interrupt vectors .org 0x40 ; VBL .int_VBL: PUSH AF @@ -78,7 +87,7 @@ RETI ;; VBlank default interrupt routine -.vbl: +.std_vbl: LD HL,#.sys_time INC (HL) JR NZ,2$ @@ -155,30 +164,24 @@ _reset:: LD A,(__cpu) ;; Initialization code -.code_start: +.code_start:: DI ; Disable interrupts LD D,A ; Store CPU type in D XOR A ;; Initialize the stack LD SP,#.STACK - ;; Clear from 0xC000 to 0xDFFF - LD HL,#0xDFFF - LD C,#0x20 - LD B,#0x00 -1$: - LD (HL-),A - DEC B - JR NZ,1$ - DEC C - JR NZ,1$ - ;; Clear from 0xFF80 to 0xFFFF - LD HL,#0xFFFF - LD B,#0x80 -3$: - LD (HL-),A - DEC B - JR NZ,3$ + + LD HL,#_shadow_OAM + LD C, #(40 << 2) ; 40 entries 4 bytes each + RST 0x28 + + ;; Clear CRT0 global variables + LD HL,#.start_crt_globals + LD C,#(.end_crt_globals - .start_crt_globals) + RST 0x28 + ; LD (.mode),A ; Clearing (.mode) is performed when clearing RAM + ;; Store CPU type LD A,D LD (__cpu),A @@ -187,13 +190,6 @@ _reset:: CALL .display_off XOR A - ;; Clear the OAM (from 0xFE00 to 0xFEFF) - LD HL,#0xFE00 -2$: - LD (HL),A - DEC L - JR NZ,2$ - ;; Initialize the display LDH (.SCY),A LDH (.SCX),A @@ -202,19 +198,17 @@ _reset:: LD A,#0x07 LDH (.WX),A - ;; Copy refresh_OAM routine to HIRAM - LD BC,#.refresh_OAM - LD HL,#.start_refresh_OAM - LD B,#.end_refresh_OAM-.start_refresh_OAM -4$: - LD A,(HL+) - LDH (C),A - INC C - DEC B - JR NZ,4$ + ;; Copy refresh_OAM routine to HRAM + LD DE,#.start_refresh_OAM ; source + LD HL,#.refresh_OAM ; dest + LD C,#(.end_refresh_OAM - .start_refresh_OAM) ; size + RST 0x30 ; call .MemcpySmall + + ;; Clear the OAM by calling refresh_OAM + CALL .refresh_OAM ;; Install interrupt routines - LD BC,#.vbl + LD BC,#.std_vbl CALL .add_VBL ;; Standard color palettes @@ -243,7 +237,7 @@ _reset:: ; Serial I/O = Off ; Timer Ovfl = Off ; LCDC = Off - ; V-Blank = On + ; V-Blank = On LDH (.IE),A LDH (__current_bank),A ; current bank is 1 at startup @@ -298,6 +292,8 @@ _exit:: .area _HEAP .area _BSS +.start_crt_globals: + __cpu:: .ds 0x01 ; GB type (GB, PGB, CGB) .mode:: @@ -306,11 +302,13 @@ __cpu:: _sys_time:: .ds 0x02 ; System time in VBL units .int_0x40:: - .blkw 0x08 + .blkw 0x0A ; 4 interrupt handlers (built-in + user-defined) + +.end_crt_globals: .area _HRAM (ABS) - .org 0xFF90 + .org 0xFF90 __current_bank:: ; Current bank .ds 0x01 .vbl_done: diff --git a/gbdk-lib/libc/gb/font.s b/gbdk-lib/libc/gb/font.s @@ -44,6 +44,8 @@ .area _BSS ; The current font +.start_font_vars: + font_current:: .ds sfont_handle_sizeof ; Cached copy of the first free tile @@ -52,7 +54,17 @@ font_first_free_tile:: ; Table containing descriptors for all of the fonts font_table:: .ds sfont_handle_sizeof*.MAX_FONTS - +.end_font_vars: + + .area _GSINIT + + XOR A + LD (#.curx),A + LD (#.cury),A + LD HL,#.start_font_vars + LD C,#(.end_font_vars - .start_font_vars) + RST 0x28 + .area _BASE ; Copy uncompressed 16 byte tiles from (BC) to (HL), length = DE*2 ; Note: HL must be aligned on a UWORD boundry diff --git a/gbdk-lib/libc/gb/joy.s b/gbdk-lib/libc/gb/joy.s @@ -11,6 +11,13 @@ LD HL,#.int_0x60 JP .int + .area _GSINIT + + XOR A + LD HL,#.int_0x60 + LD C,#0x08 + RST 0x28 + .area _BASE _add_JOY:: diff --git a/gbdk-lib/libc/gb/lcd.s b/gbdk-lib/libc/gb/lcd.s @@ -6,7 +6,14 @@ .org 0x48 ; LCD .int_LCD: JP .int_lcd_handler - + + .area _GSINIT + + XOR A + LD HL,#.int_0x48 + LD C,#0x08 + RST 0x28 + .area _BASE .int_lcd_handler: diff --git a/gbdk-lib/libc/gb/serial.s b/gbdk-lib/libc/gb/serial.s @@ -17,6 +17,13 @@ .area _GSINIT + ;; Clear SIO global variables + XOR A + LD HL,#.start_sio_globals + LD C,#(.end_sio_globals - .start_sio_globals) + RST 0x28 + + ;; initialize SIO LD BC,#.serial_IO CALL .add_SIO @@ -100,6 +107,7 @@ _remove_SIO:: JR 3$ .area _BSS +.start_sio_globals: __io_out:: .ds 0x01 ; Byte to send @@ -110,6 +118,8 @@ __io_status:: .int_0x58:: .blkw 0x08 +.end_sio_globals: + .area _CODE ;; Send byte in __io_out to the serial port diff --git a/gbdk-lib/libc/gb/tim.s b/gbdk-lib/libc/gb/tim.s @@ -11,6 +11,13 @@ LD HL,#.int_0x50 JP .int + .area _GSINIT + + XOR A + LD HL,#.int_0x50 + LD C,#0x08 + RST 0x28 + .area _BASE _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.