git.y1.nz

gbdk-2020

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

commit 010666eb874be47630ffc98dde63437a584b3b83
parent 23ffc3b40bf589ffcd0ebecd98f5665e0b95d66c
Author: Toxa <untoxa@mail.ru>
Date:   Wed,  8 Sep 2021 16:45:53 +0300

meet C standard: initialize variables in the static storage with zeroes; that also saves a few bytes in ROM

Diffstat:
Mgbdk-lib/libc/asm/gbz80/arand.s12+-----------
Mgbdk-lib/libc/asm/gbz80/rand.s2+-
Mgbdk-lib/libc/asm/z80/arand.s13+------------
Mgbdk-lib/libc/asm/z80/rand.s2+-
Mgbdk-lib/libc/targets/gbz80/ap/crt0.s108+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mgbdk-lib/libc/targets/gbz80/color.s2+-
Mgbdk-lib/libc/targets/gbz80/crash_handler.s2+-
Mgbdk-lib/libc/targets/gbz80/drawing.s2+-
Mgbdk-lib/libc/targets/gbz80/far_ptr.s2+-
Mgbdk-lib/libc/targets/gbz80/font.s10+---------
Mgbdk-lib/libc/targets/gbz80/gb/crt0.s104+++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mgbdk-lib/libc/targets/gbz80/input.s2+-
Mgbdk-lib/libc/targets/gbz80/joy.s9+--------
Mgbdk-lib/libc/targets/gbz80/lcd.s9+--------
Mgbdk-lib/libc/targets/gbz80/serial.s11+----------
Mgbdk-lib/libc/targets/gbz80/set_tile_submap.s2+-
Mgbdk-lib/libc/targets/gbz80/tim.s9+--------
Mgbdk-lib/libc/targets/z80/far_ptr.s2+-
Mgbdk-lib/libc/targets/z80/font.s26+++++++++++---------------
Mgbdk-lib/libc/targets/z80/gg/crt0.s73+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mgbdk-lib/libc/targets/z80/set_tile_submap.s2+-
Mgbdk-lib/libc/targets/z80/set_tile_submap_compat.s2+-
Mgbdk-lib/libc/targets/z80/sms/crt0.s73+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
23 files changed, 259 insertions(+), 220 deletions(-)

diff --git a/gbdk-lib/libc/asm/gbz80/arand.s b/gbdk-lib/libc/asm/gbz80/arand.s @@ -13,8 +13,7 @@ .globl .initrand .globl _rand - .area _BSS -.start_arand_vars: + .area _DATA .randarr: .ds 55 @@ -22,15 +21,6 @@ .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/asm/gbz80/rand.s b/gbdk-lib/libc/asm/gbz80/rand.s @@ -36,7 +36,7 @@ ;; It's your choice - but if you're doing Monte Carlo physics simulations on the ;; GameBoy, this is a safe bet! - .area _BSS + .area _DATA .randhi:: ; Storage for last random number (or seed) .ds 0x01 .randlo:: diff --git a/gbdk-lib/libc/asm/z80/arand.s b/gbdk-lib/libc/asm/z80/arand.s @@ -3,8 +3,7 @@ .globl .initrand .globl _rand - .area _BSS -.start_arand_vars: + .area _DATA .randarr: .ds 55 @@ -12,16 +11,6 @@ .ds 0x01 .raxk: .ds 0x01 - -.end_arand_vars: - - .area _GSINIT - - ld hl, #.start_arand_vars - ld de, #(.start_arand_vars + 1) - ld (hl), #0 - ld bc, #(.end_arand_vars - .start_arand_vars) - ldir .area _CODE diff --git a/gbdk-lib/libc/asm/z80/rand.s b/gbdk-lib/libc/asm/z80/rand.s @@ -1,6 +1,6 @@ .module Random - .area _BSS + .area _DATA .randval: .ds 0x02 diff --git a/gbdk-lib/libc/targets/gbz80/ap/crt0.s b/gbdk-lib/libc/targets/gbz80/ap/crt0.s @@ -107,6 +107,23 @@ _refresh_OAM:: LD A, #>_shadow_OAM JP (HL) +.clear_WRAM: + PUSH DE + XOR A + LD BC, #l__DATA + LD HL, #s__DATA + CALL .memset_simple + + LD A, #>_shadow_OAM + LDH (__shadow_OAM_base), A + LD H, A + XOR A + LD L, A + LD C, #(40 << 2) ; 40 entries 4 bytes each + RST 0x28 + POP DE + RET + ;; GameBoy Header ;; DO NOT CHANGE... @@ -176,18 +193,7 @@ _reset:: ;; Initialize the stack LD SP, #.STACK - LD A, #>_shadow_OAM - LDH (__shadow_OAM_base), A - LD H, A - XOR A - LD L, A - 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 + CALL .clear_WRAM ; LD (.mode),A ; Clearing (.mode) is performed when clearing RAM @@ -324,9 +330,7 @@ _set_interrupts:: ;; For malloc .area _HEAP - .area _BSS -.start_crt_globals: - + .area _DATA __cpu:: .ds 0x01 ; GB type (GB, PGB, CGB) __is_GBA:: @@ -339,8 +343,6 @@ _sys_time:: .int_0x40:: .blkw 0x0A ; 4 interrupt handlers (built-in + user-defined) -.end_crt_globals: - .area _HRAM (ABS) .org 0xFF90 @@ -354,41 +356,59 @@ __shadow_OAM_base:: ;; Runtime library .area _GSINIT gsinit:: - LD DE, #l__INITIALIZER - LD A, D - OR E - JR Z, 4$ - LD HL, #s__INITIALIZED - LD BC, #s__INITIALIZER - - SRL D - RR E + ;; initialize static storage variables + LD BC, #l__INITIALIZER + LD HL, #s__INITIALIZER + LD DE, #s__INITIALIZED + call .memcpy_simple + + .area _GSFINAL + ret + + .area _HOME + + ;; fills memory at HL of length BC with A, clobbers DE +.memset_simple:: + LD E, A + LD A, B + OR C + RET Z + LD (HL), E + DEC BC + LD D, H + LD E, L + INC DE + + ;; copies BC bytes from HL into DE +.memcpy_simple:: + LD A, B + OR C + RET Z + + SRL B + RR C JR NC,3$ - LD A,(BC) - LD (HL+),A - INC BC + LD A, (HL+) + LD (DE), A + INC DE 3$: - INC D - INC E + INC B + INC C JR 2$ 1$: - LD A,(BC) - LD (HL+),A - INC BC - LD A,(BC) - LD (HL+),A - INC BC + LD A, (HL+) + LD (DE), A + INC DE + LD A, (HL+) + LD (DE), A + INC DE 2$: - DEC E + DEC C JR NZ,1$ - DEC D + DEC B JR NZ,1$ 4$: - - .area _GSFINAL - ret - - .area _HOME + RET ;; Remove interrupt routine in BC from the VBL interrupt list ;; falldown to .remove_int diff --git a/gbdk-lib/libc/targets/gbz80/color.s b/gbdk-lib/libc/targets/gbz80/color.s @@ -1,7 +1,7 @@ .include "global.s" ;; Data - .area _BSS + .area _DATA ;; Foreground drawing colour .fg_colour:: .ds 1 diff --git a/gbdk-lib/libc/targets/gbz80/crash_handler.s b/gbdk-lib/libc/targets/gbz80/crash_handler.s @@ -411,7 +411,7 @@ loadfont: .ascii " " - .area _BSS + .area _DATA wCrashA: .ds 1 ; We need at least one working register, and A allows accessing memory diff --git a/gbdk-lib/libc/targets/gbz80/drawing.s b/gbdk-lib/libc/targets/gbz80/drawing.s @@ -35,7 +35,7 @@ .module Drawing1 ;; Data - .area _BSS + .area _DATA ;; Fill style .style: .ds 0x01 diff --git a/gbdk-lib/libc/targets/gbz80/far_ptr.s b/gbdk-lib/libc/targets/gbz80/far_ptr.s @@ -62,7 +62,7 @@ _to_far_ptr:: ld L, A ret - .area _BSS + .area _DATA ___call_banked_ptr:: ___call_banked_addr:: diff --git a/gbdk-lib/libc/targets/gbz80/font.s b/gbdk-lib/libc/targets/gbz80/font.s @@ -42,9 +42,8 @@ .globl .fg_colour .globl .bg_colour - .area _BSS + .area _DATA ; The current font -.start_font_vars: font_current:: .ds sfont_handle_sizeof @@ -54,13 +53,6 @@ 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 - - ld hl,#.start_font_vars - ld c,#(.end_font_vars - .start_font_vars) - rst 0x28 .area _BASE diff --git a/gbdk-lib/libc/targets/gbz80/gb/crt0.s b/gbdk-lib/libc/targets/gbz80/gb/crt0.s @@ -107,6 +107,23 @@ _refresh_OAM:: LD A, #>_shadow_OAM JP (HL) +.clear_WRAM: + PUSH DE + XOR A + LD BC, #l__DATA + LD HL, #s__DATA + CALL .memset_simple + + LD A, #>_shadow_OAM + LDH (__shadow_OAM_base), A + LD H, A + XOR A + LD L, A + LD C, #(40 << 2) ; 40 entries 4 bytes each + RST 0x28 + POP DE + RET + ;; GameBoy Header ;; DO NOT CHANGE... @@ -182,18 +199,7 @@ _reset:: ;; Initialize the stack LD SP, #.STACK - LD A, #>_shadow_OAM - LDH (__shadow_OAM_base), A - LD H, A - XOR A - LD L, A - 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 + CALL .clear_WRAM ; LD (.mode),A ; Clearing (.mode) is performed when clearing RAM @@ -329,7 +335,7 @@ _set_interrupts:: ;; For malloc .area _HEAP - .area _BSS + .area _DATA .start_crt_globals: __cpu:: @@ -359,41 +365,59 @@ __shadow_OAM_base:: ;; Runtime library .area _GSINIT gsinit:: - LD DE, #l__INITIALIZER - LD A, D - OR E - JR Z, 4$ - LD HL, #s__INITIALIZED - LD BC, #s__INITIALIZER - - SRL D - RR E + ;; initialize static storage variables + LD BC, #l__INITIALIZER + LD HL, #s__INITIALIZER + LD DE, #s__INITIALIZED + call .memcpy_simple + + .area _GSFINAL + ret + + .area _HOME + + ;; fills memory at HL of length BC with A, clobbers DE +.memset_simple:: + LD E, A + LD A, B + OR C + RET Z + LD (HL), E + DEC BC + LD D, H + LD E, L + INC DE + + ;; copies BC bytes from HL into DE +.memcpy_simple:: + LD A, B + OR C + RET Z + + SRL B + RR C JR NC,3$ - LD A,(BC) - LD (HL+),A - INC BC + LD A, (HL+) + LD (DE), A + INC DE 3$: - INC D - INC E + INC B + INC C JR 2$ 1$: - LD A,(BC) - LD (HL+),A - INC BC - LD A,(BC) - LD (HL+),A - INC BC + LD A, (HL+) + LD (DE), A + INC DE + LD A, (HL+) + LD (DE), A + INC DE 2$: - DEC E + DEC C JR NZ,1$ - DEC D + DEC B JR NZ,1$ 4$: - - .area _GSFINAL - ret - - .area _HOME + RET ;; Remove interrupt routine in BC from the VBL interrupt list ;; falldown to .remove_int diff --git a/gbdk-lib/libc/targets/gbz80/input.s b/gbdk-lib/libc/targets/gbz80/input.s @@ -43,7 +43,7 @@ .module Terminal - .area _BSS + .area _DATA .msx: ; Mouse position .ds 0x01 diff --git a/gbdk-lib/libc/targets/gbz80/joy.s b/gbdk-lib/libc/targets/gbz80/joy.s @@ -11,13 +11,6 @@ LD HL,#.int_0x60 JP .int - .area _GSINIT - - XOR A - LD HL,#.int_0x60 - LD C,#0x08 - RST 0x28 - .area _BASE _add_JOY:: @@ -48,7 +41,7 @@ _remove_JOY:: LD HL,#.int_0x60 JP .remove_int - .area _BSS + .area _DATA .int_0x60:: .blkw 0x08 diff --git a/gbdk-lib/libc/targets/gbz80/lcd.s b/gbdk-lib/libc/targets/gbz80/lcd.s @@ -7,13 +7,6 @@ .int_LCD: JP .int_lcd_handler - .area _GSINIT - - XOR A - LD HL,#.int_0x48 - LD C,#0x08 - RST 0x28 - .area _BASE .int_lcd_handler: @@ -82,7 +75,7 @@ _remove_LCD:: LD HL,#.int_0x48 JP .remove_int - .area _BSS + .area _DATA .int_0x48:: .blkw 0x08 diff --git a/gbdk-lib/libc/targets/gbz80/serial.s b/gbdk-lib/libc/targets/gbz80/serial.s @@ -17,12 +17,6 @@ .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 @@ -106,8 +100,7 @@ _remove_SIO:: LD A,#.IO_ERROR JR 3$ - .area _BSS -.start_sio_globals: + .area _DATA __io_out:: .ds 0x01 ; Byte to send @@ -118,8 +111,6 @@ __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/targets/gbz80/set_tile_submap.s b/gbdk-lib/libc/targets/gbz80/set_tile_submap.s @@ -1,6 +1,6 @@ .include "global.s" - .area _BSS + .area _DATA .image_tile_width:: .ds 0x01 diff --git a/gbdk-lib/libc/targets/gbz80/tim.s b/gbdk-lib/libc/targets/gbz80/tim.s @@ -11,13 +11,6 @@ LD HL,#.int_0x50 JP .int - .area _GSINIT - - XOR A - LD HL,#.int_0x50 - LD C,#0x08 - RST 0x28 - .area _BASE _add_TIM:: @@ -48,7 +41,7 @@ _remove_TIM:: LD HL,#.int_0x50 JP .remove_int - .area _BSS + .area _DATA .int_0x50:: .blkw 0x08 diff --git a/gbdk-lib/libc/targets/z80/far_ptr.s b/gbdk-lib/libc/targets/z80/far_ptr.s @@ -55,7 +55,7 @@ _to_far_ptr:: push bc ret - .area _BSS + .area _DATA ___call_banked_ptr:: ___call_banked_addr:: diff --git a/gbdk-lib/libc/targets/z80/font.s b/gbdk-lib/libc/targets/z80/font.s @@ -22,14 +22,8 @@ ; Maximum number of fonts .MAX_FONTS = 6 - .area _BSS + .area _DATA ; The current font -.start_font_vars: -.fg_colour:: - .ds 1 -.bg_colour:: - .ds 1 - font_current:: .ds sfont_handle_sizeof ; Cached copy of the first free tile @@ -46,16 +40,18 @@ plane2: .ds 1 plane3: .ds 1 -.end_font_vars: - .area _GSINIT + .area _INITIALIZED + ; The current font +.fg_colour:: + .ds 1 +.bg_colour:: + .ds 1 - xor a - ld hl,#.start_font_vars - ld c,#(.end_font_vars-.start_font_vars) - call .memset_small - ld a, #3 - ld (.fg_colour), a + .area _INITIALIZER + + .db 0x03 ; .fg_colour + .db 0x00 ; .bg_color .area _HOME diff --git a/gbdk-lib/libc/targets/z80/gg/crt0.s b/gbdk-lib/libc/targets/z80/gg/crt0.s @@ -45,6 +45,14 @@ _WRITE_VDP_DATA:: ld sp, #.STACK ; set stack pointer at end of RAM ld a, (#.BIOS) + push af + + xor a + ld bc, #l__DATA + ld hl, #s__DATA + call .memset_simple ; initialize veriables in RAM with zero + + pop af ld (#__BIOS), a ; save BIOS value ld hl, #_shadow_OAM @@ -90,21 +98,7 @@ _WRITE_VDP_DATA:: 3$: ld (#__SYSTEM), a - ;; clear VRAM - ld a, #<.VDP_VRAM - out (#.VDP_CMD), a - ld a, #>.VDP_VRAM - out (#.VDP_CMD), a - xor a - ld bc, #0x4101 - jr 6$ -5$: - out (.VDP_DATA), a -6$: - dec c - jr nz, 5$ - dec b - jr nz, 5$ + call .clear_VRAM call _set_default_palette @@ -135,19 +129,54 @@ _WRITE_VDP_DATA:: .area _CODE .area _GSINIT .gsinit:: + ;; initialize static storage variables ld bc, #l__INITIALIZER - ld a, b - or a, c - jr Z, 1$ - ld de, #s__INITIALIZED ld hl, #s__INITIALIZER - ldir -1$: + ld de, #s__INITIALIZED + call .memcpy_simple .area _GSFINAL ret .area _HOME + +.clear_VRAM: + ld a, #<.VDP_VRAM + out (#.VDP_CMD), a + ld a, #>.VDP_VRAM + out (#.VDP_CMD), a + xor a + ld bc, #0x4101 + jr 6$ +5$: + out (.VDP_DATA), a +6$: + dec c + jr nz, 5$ + dec b + jr nz, 5$ + ret + + ;; fills memory at HL of length BC with A, clobbers DE +.memset_simple:: + ld e, a + ld a, c + or b + ret z + ld (hl), e + dec bc + ld d, h + ld e, l + inc de + + ;; copies BC bytes from HL into DE +.memcpy_simple:: + ld a, c + or b + ret z + ldir + ret + ;; Wait for VBL interrupt to be finished .wait_vbl_done:: _wait_vbl_done:: @@ -164,7 +193,7 @@ _wait_vbl_done:: jr z, 1$ ret - .area _BSS + .area _DATA .start_crt_globals: __BIOS:: diff --git a/gbdk-lib/libc/targets/z80/set_tile_submap.s b/gbdk-lib/libc/targets/z80/set_tile_submap.s @@ -5,7 +5,7 @@ .ez80 - .area _BSS + .area _DATA .image_tile_width:: .ds 0x01 diff --git a/gbdk-lib/libc/targets/z80/set_tile_submap_compat.s b/gbdk-lib/libc/targets/z80/set_tile_submap_compat.s @@ -5,7 +5,7 @@ .ez80 - .area _BSS + .area _DATA .image_tile_width_compat:: .ds 0x01 diff --git a/gbdk-lib/libc/targets/z80/sms/crt0.s b/gbdk-lib/libc/targets/z80/sms/crt0.s @@ -45,6 +45,14 @@ _WRITE_VDP_DATA:: ld sp, #.STACK ; set stack pointer at end of RAM ld a, (#.BIOS) + push af + + xor a + ld bc, #l__DATA + ld hl, #s__DATA + call .memset_simple ; initialize veriables in RAM with zero + + pop af ld (#__BIOS), a ; save BIOS value ld hl, #_shadow_OAM @@ -97,21 +105,7 @@ _WRITE_VDP_DATA:: 4$: ld (#__SYSTEM), a - ;; clear VRAM - ld a, #<.VDP_VRAM - out (#.VDP_CMD), a - ld a, #>.VDP_VRAM - out (#.VDP_CMD), a - xor a - ld bc, #0x4101 - jr 6$ -5$: - out (.VDP_DATA), a -6$: - dec c - jr nz, 5$ - dec b - jr nz, 5$ + call .clear_VRAM call _set_default_palette @@ -142,19 +136,54 @@ _WRITE_VDP_DATA:: .area _CODE .area _GSINIT .gsinit:: + ;; initialize static storage variables ld bc, #l__INITIALIZER - ld a, b - or a, c - jr Z, 1$ - ld de, #s__INITIALIZED ld hl, #s__INITIALIZER - ldir -1$: + ld de, #s__INITIALIZED + call .memcpy_simple .area _GSFINAL ret .area _HOME + +.clear_VRAM: + ld a, #<.VDP_VRAM + out (#.VDP_CMD), a + ld a, #>.VDP_VRAM + out (#.VDP_CMD), a + xor a + ld bc, #0x4101 + jr 6$ +5$: + out (.VDP_DATA), a +6$: + dec c + jr nz, 5$ + dec b + jr nz, 5$ + ret + + ;; fills memory at HL of length BC with A, clobbers DE +.memset_simple:: + ld e, a + ld a, c + or b + ret z + ld (hl), e + dec bc + ld d, h + ld e, l + inc de + + ;; copies BC bytes from HL into DE +.memcpy_simple:: + ld a, c + or b + ret z + ldir + ret + ;; Wait for VBL interrupt to be finished .wait_vbl_done:: _wait_vbl_done:: @@ -171,7 +200,7 @@ _wait_vbl_done:: jr z, 1$ ret - .area _BSS + .area _DATA .start_crt_globals: __BIOS::

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