git.y1.nz

gbdk-2020

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

commit 1b2e703fe811b251dc964d3f1f9fc8fd80043b75
parent 22d4b68435a234cb10617322234606c882923166
Author: Toxa <untoxa@mail.ru>
Date:   Fri, 16 Jul 2021 19:22:17 +0300

BANKREF / BANKREF_EXTERN macros plus better banks_autobank example

Diffstat:
Mgbdk-lib/examples/gb/banks_autobank/Makefile2+-
Mgbdk-lib/examples/gb/banks_autobank/banks.c36++++++++++++++++++++++--------------
Mgbdk-lib/examples/gb/banks_autobank/srcfile_1.c6+++---
Mgbdk-lib/examples/gb/banks_autobank/srcfile_2.c5+++--
Mgbdk-lib/examples/gb/banks_autobank/srcfile_3.c7+++----
Mgbdk-lib/examples/gb/banks_autobank/srcfile_4_not-autobanked.c7+++----
Mgbdk-lib/include/gb/gb.h36++++++++++++++++++++++++++++++++++++
Mgbdk-lib/include/gb/incbin.h23++---------------------
Mgbdk-lib/include/gb/metasprites.h1+
9 files changed, 74 insertions(+), 49 deletions(-)

diff --git a/gbdk-lib/examples/gb/banks_autobank/Makefile b/gbdk-lib/examples/gb/banks_autobank/Makefile @@ -46,7 +46,7 @@ onepass: clean: - rm -f *.o *.lst *.map *.gb *.ihx *.sym *.cdb *.adb *.asm *.rel + rm -f *.o *.lst *.map *.gb *.ihx *.sym *.cdb *.adb *.asm *.rel *.sav # For lcc linker option: -Wl-ytN where N is one of the numbers below diff --git a/gbdk-lib/examples/gb/banks_autobank/banks.c b/gbdk-lib/examples/gb/banks_autobank/banks.c @@ -2,23 +2,31 @@ #include <stdint.h> #include <stdio.h> -// Bank number references from the other source files -extern const void __bank_srcfile1; -extern const void __bank_srcfile2; -extern const void __bank_srcfile3; -extern const void __bank_srcfile4; - // Banked const vars from the other source files extern const uint8_t some_const_var_1; +BANKREF_EXTERN(some_const_var_1) + extern const uint8_t some_const_var_2; +BANKREF_EXTERN(some_const_var_2) + extern const uint8_t some_const_var_3; +BANKREF_EXTERN(some_const_var_3) + extern const uint8_t some_const_var_4; +BANKREF_EXTERN(some_const_var_4) // Banked functions from the other source files void func_1() BANKED; +BANKREF_EXTERN(func_1) + void func_2() BANKED; +BANKREF_EXTERN(func_2) + void func_3() BANKED; +BANKREF_EXTERN(func_3) + void some_4() BANKED; +BANKREF_EXTERN(some_4) // Non-banked const const uint8_t some_const_var_0 = 0; @@ -47,14 +55,14 @@ void main(void) // Print the const vars, unbanked first then the banked ones printf("Const0= %u nonbanked\n", some_const_var_0); - SWITCH_ROM( (uint8_t)&__bank_srcfile1 ); - printf("Const1= %u in bank %u\n", some_const_var_1, &(__bank_srcfile1)); - SWITCH_ROM( (uint8_t)&__bank_srcfile2 ); - printf("Const2= %u in bank %u\n", some_const_var_2, &(__bank_srcfile2)); - SWITCH_ROM( (uint8_t)&__bank_srcfile3 ); - printf("Const3= %u in bank %u\n", some_const_var_3, &(__bank_srcfile3)); - SWITCH_ROM( (uint8_t)&__bank_srcfile4 ); - printf("Const4= %u in bank %u\n", some_const_var_4, &(__bank_srcfile4)); + SWITCH_ROM(BANK(some_const_var_1)); + printf("Const1= %u in bank %hu\n", some_const_var_1, BANK(some_const_var_1)); + SWITCH_ROM(BANK(some_const_var_2)); + printf("Const2= %u in bank %hu\n", some_const_var_2, BANK(some_const_var_2)); + SWITCH_ROM(BANK(some_const_var_3)); + printf("Const3= %u in bank %hu\n", some_const_var_3, BANK(some_const_var_3)); + SWITCH_ROM(BANK(some_const_var_4)); + printf("Const4= %u in bank %hu\n", some_const_var_4, BANK(some_const_var_4)); printf("\n"); puts("The End..."); diff --git a/gbdk-lib/examples/gb/banks_autobank/srcfile_1.c b/gbdk-lib/examples/gb/banks_autobank/srcfile_1.c @@ -4,18 +4,19 @@ // These two entries are needed to enable auto-banking in the source file #pragma bank 255 -const void __at(255) __bank_srcfile1; const uint8_t some_const_var_1 = 1; +BANKREF(some_const_var_1) void func_1() BANKED { printf("Func1 in ROM bank %u\n", _current_bank); } +BANKREF(func_1) // A big constant array to take up space -const unsigned char big_const_1[] = { +static const unsigned char big_const_1[] = { // 512 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -58,4 +59,3 @@ const unsigned char big_const_1[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, }; - diff --git a/gbdk-lib/examples/gb/banks_autobank/srcfile_2.c b/gbdk-lib/examples/gb/banks_autobank/srcfile_2.c @@ -4,18 +4,19 @@ // These two entries are needed to enable auto-banking in the source file #pragma bank 255 -const void __at(255) __bank_srcfile2; const uint8_t some_const_var_2 = 2; +BANKREF(some_const_var_2) void func_2() BANKED { printf("Func2 in ROM bank %u\n", _current_bank); } +BANKREF(func_2) // A big constant array to take up space -const unsigned char big_const_2[] = { +static const unsigned char big_const_2[] = { // 512 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, diff --git a/gbdk-lib/examples/gb/banks_autobank/srcfile_3.c b/gbdk-lib/examples/gb/banks_autobank/srcfile_3.c @@ -4,18 +4,18 @@ // These two entries are needed to enable auto-banking in the source file #pragma bank 255 -const void __at(255) __bank_srcfile3; - const uint8_t some_const_var_3 = 3; +BANKREF(some_const_var_3) void func_3() BANKED { printf("Func3 in ROM bank %u\n", _current_bank); } +BANKREF(func_3) // A big constant array to take up space -const unsigned char big_const_3[] = { +static const unsigned char big_const_3[] = { // 256 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -23,4 +23,3 @@ const unsigned char big_const_3[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, }; - diff --git a/gbdk-lib/examples/gb/banks_autobank/srcfile_4_not-autobanked.c b/gbdk-lib/examples/gb/banks_autobank/srcfile_4_not-autobanked.c @@ -4,19 +4,20 @@ // In this source file a non-auto bank (2) is used (no auto-banking) #pragma bank 2 -const void __at(2) __bank_srcfile4; const uint8_t some_const_var_4 = 4; +BANKREF(some_const_var_4) void some_4() BANKED { printf("Func4 not autobanked" " is in ROM bank %u\n", _current_bank); } +BANKREF(some_4) // A big constant array to take up space -const unsigned char big_const_4[] = { +static const unsigned char big_const_4[] = { // 512 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -340,5 +341,3 @@ const unsigned char big_const_4[] = { }; - - diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -364,6 +364,42 @@ extern volatile uint8_t _io_out; SWITCH_ROM_MBC5, or call a BANKED function. */ __REG _current_bank; +#define CURRENT_BANK _current_bank + +/** Obtains the __bank number__ of VARNAME + + @param VARNAME Name of the variable which has a __bank_VARNAME companion symbol which is adjusted by bankpack + +*/ +#ifndef BANK +#define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME ) +#endif + +/** Creates the reference for retriving the bank number of the object + + @param VARNAME Variable name to use, may be existiing identifier + + @see BANK() for obtaining the bank number of the included data. + + Use @ref BANKREF_EXTERN() within another source file + to make the variable and it's data accesible there. +*/ +#define BANKREF(VARNAME) void __func_ ## VARNAME() __banked __naked { \ +__asm \ + .local b___func_ ## VARNAME \ + ___bank_ ## VARNAME = b___func_ ## VARNAME \ + .globl ___bank_ ## VARNAME \ +__endasm; \ +} + +/** Creates extern references for accessing a BANKREF() generated variable. + + @param VARNAME Name of the variable used with BANKREF + + @ref BANKREF(), BANK() +*/ +#define BANKREF_EXTERN(VARNAME) extern const void __bank_ ## VARNAME; + /** Makes MBC1 and other compatible MBCs switch the active ROM bank @param b ROM bank to switch to diff --git a/gbdk-lib/include/gb/incbin.h b/gbdk-lib/include/gb/incbin.h @@ -44,7 +44,9 @@ extern const void __bank_ ## VARNAME; @ref INCBIN(), INCBIN_EXTERN() */ +#ifndef BANK #define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME ) +#endif /** Includes binary data into a C source file @@ -77,27 +79,6 @@ _ ## VARNAME:: \ __endasm; \ } -/** Creates the reference for retriving the bank number of the object - - @param VARNAME Variable name to use - - @see BANK() for obtaining the bank number of the included data. - - Use @ref INCBIN_EXTERN() within another source file - to make the variable and it's data accesible there. -*/ -#define BANKREF(VARNAME) void __func_ ## VARNAME() __banked __naked { \ -__asm \ - _ ## VARNAME = 0x4000 \ - .globl _ ## VARNAME \ - ___size_ ## VARNAME = 0x4000 \ - .globl ___size_ ## VARNAME \ - .local b___func_ ## VARNAME \ - ___bank_ ## VARNAME = b___func_ ## VARNAME \ - .globl ___bank_ ## VARNAME \ -__endasm; \ -} - #endif // _INCBIN_H diff --git a/gbdk-lib/include/gb/metasprites.h b/gbdk-lib/include/gb/metasprites.h @@ -60,6 +60,7 @@ #ifndef _METASPRITES_H_INCLUDE #define _METASPRITES_H_INCLUDE +#include <gb/hardware.h> #include <stdint.h> /** Metasprite sub-item structure

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