git.y1.nz

gbdk-2020

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

commit ce87090303366d825ae325fb84782ce0cfa63af9
parent 755c1c8f8a3fc2b0f0b8c263625cbd41422bf318
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Wed, 26 Nov 2025 16:08:13 -0800

Merge pull request #839 from bbbbbr/examples/cart_sram_dataseg_pragma

Examples: new sram bank pragma example
Diffstat:
Mdocs/pages/05_banking_mbcs.md24++++++++++++++++++++++--
Agbdk-lib/examples/cross-platform/sram_banks/Makefile88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/examples/cross-platform/sram_banks/Makefile.targets54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/examples/cross-platform/sram_banks/src/main.c30++++++++++++++++++++++++++++++
Agbdk-lib/examples/cross-platform/sram_banks/src/sram_bank0.c7+++++++
Agbdk-lib/examples/cross-platform/sram_banks/src/sram_bank1.c7+++++++
6 files changed, 208 insertions(+), 2 deletions(-)

diff --git a/docs/pages/05_banking_mbcs.md b/docs/pages/05_banking_mbcs.md @@ -63,6 +63,7 @@ Note: You can use the `NONBANKED` keyword to define a function as non-banked if ## Setting the Cart SRAM bank for a Source file - `#pragma dataseg DATA_<N>` at the start of a source file. Example (Cartridge SRAM bank 3): `#pragma bank 3` + - See the cross-platform `sram_banks` example - Using the lcc switch for Cartridge SRAM bank `-Wf-ba<N>`. Example (Cartridge SRAM bank 3): `-Wf-ba3` @@ -83,8 +84,7 @@ The MBC settings below are available when using the makebin `-Wl-yt<N>` switch. Source: Pandocs. Additional details available at [Pandocs](https://gbdev.io/pandocs/The_Cartridge_Header.html#0147---cartridge-type "Pandocs") -For SMS/GG, the ROM file size must be at least 64K to enable mapper support for Cart SRAM banks in emulators. - - If the generated ROM is too small then `-yo 4` for makebin (or `-Wm-yo4` for LCC) can be used to set the size to 64K. +See the note about @ref gg_sms_cart_sram_info "enabling Cart SRAM and number of banks available for SMS/GG" ## MBC Type Chart @@ -355,6 +355,7 @@ There are several projects in the GBDK 2020 examples folder which demonstrate di @anchor sms_gg_banking "SMS/GG Banking" section. # SMS/Game Gear Banking +## ROM Banks The memory banking setup for SMS and Game Gear in GBDK is different than it is for the Game Boy. Instead of a single switchable bank in the `0x4000 - 0x7FFF` range, there are two switchable frames at different address ranges. The configuration is as follows: - Frame 0: Non-banked, at address `0x0000 - 0x3FFF` @@ -370,6 +371,16 @@ The memory banking setup for SMS and Game Gear in GBDK is different than it is f Banked code and any pointers associated with it will only work correctly when active in Frame 1 (at `0x4000`), so it must use `CODE_<N>`. Graphics and other assets may go in either Frame 1 (at `0x4000`) or, if designed for it then Frame 2 (at `0x8000`). +@anchor gg_sms_cart_sram_info "SMS/GG Cart SRAM info" +## Cart SRAM Banks +The maximum supported number of Cart SRAM banks for SMS/GG is 2. The size is 8K. + - Additional details in the SMS Power docs: https://www.smspower.org/Development/Mappers#RAMMapping + +For SMS/GG, the ROM file size must be at least 64K to enable mapper support for Cart SRAM banks in emulators. + - If the generated ROM is too small then `-yo 4` for makebin (or `-Wm-yo4` for LCC) can be used to set the size to 64K. + - If auto-banking is being used and Cart SRAM banks are specified then @ref makebin will auto-increase the ROM size to 64k to ensure + + ## Auto-Banking CODE and LIT cannot share the same bank number. For example, if `CODE` is assigned to `bank 3` then `LIT` cannot be in `bank 3` as well. @@ -381,3 +392,12 @@ CODE and LIT cannot share the same bank number. For example, if `CODE` is assign The bankpack option `-banktype=` may be used to set a bank to use specific type (`CODE` or `LIT`). This will take effect before bankpack tries to perform any bank assignment. For example: `-banktype=2:LIT` (or `-Wb-banktype=2:LIT` when used with @ref lcc) sets bank 2 to exclusively use type `LIT`. +@anchor nes_banking "NES Banking" section. +# NES Banking + +## ROM Banks +To do + +@anchor nes_cart_sram_info "NES Cart SRAM info" +## Cart SRAM Banks +Cart SRAM is not currently supported for the NES diff --git a/gbdk-lib/examples/cross-platform/sram_banks/Makefile b/gbdk-lib/examples/cross-platform/sram_banks/Makefile @@ -0,0 +1,88 @@ +SHELL := /bin/bash + +# If you move this project you can change the directory +# to match your GBDK root directory (ex: GBDK_HOME = "C:/GBDK/" +ifndef GBDK_HOME + GBDK_HOME = ../../../ +endif + +LCC = $(GBDK_HOME)bin/lcc + +# Set platforms to build here, spaced separated. (These are in the separate Makefile.targets) +# They can also be built/cleaned individually: "make gg" and "make gg-clean" +# Possible are: gb gbc pocket megaduck sms gg +TARGETS=gb sms gg # nes + +# Configure platform specific LCC flags here: +LCCFLAGS_gb = -Wl-yt0x1B # Set an MBC for banking (1B-ROM+MBC5+RAM+BATT) +LCCFLAGS_pocket = -Wl-yt0x1B # Usually the same as required for .gb +LCCFLAGS_duck = -Wl-yt0x1B # Usually the same as required for .gb +LCCFLAGS_gbc = -Wl-yt0x1B -Wm-yc # Same as .gb with: -Wm-yc (gb & gbc) or Wm-yC (gbc exclusive) +LCCFLAGS_sms = +LCCFLAGS_gg = +LCCFLAGS_nes = + +LCCFLAGS += $(LCCFLAGS_$(EXT)) # This adds the current platform specific LCC Flags + +LCCFLAGS += -Wl-j -Wm-yoA -Wm-ya4 -autobank -Wb-ext=.rel -Wb-v # MBC + Autobanking related flags + +# GBDK_DEBUG = ON +ifdef GBDK_DEBUG + LCCFLAGS += -debug -v +endif + + +# You can set the name of the ROM file here +PROJECTNAME = sram_banks + +# EXT?=gb # Only sets extension to default (game boy .gb) if not populated +SRCDIR = src +OBJDIR = obj/$(EXT) +RESDIR = res +BINDIR = build/$(EXT) +MKDIRS = $(OBJDIR) $(BINDIR) # See bottom of Makefile for directory auto-creation + +BINS = $(OBJDIR)/$(PROJECTNAME).$(EXT) +CSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.c))) $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.c))) +ASMSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.s))) +OBJS = $(CSOURCES:%.c=$(OBJDIR)/%.o) $(ASMSOURCES:%.s=$(OBJDIR)/%.o) + +# Builds all targets sequentially +all: $(TARGETS) + +# Compile .c files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< + +# Compile .c files in "res/" to .o object files +$(OBJDIR)/%.o: $(RESDIR)/%.c + $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< + +# Compile .s assembly files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.s + $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< + +# If needed, compile .c files in "src/" to .s assembly files +# (not required if .c is compiled directly to .o) +$(OBJDIR)/%.s: $(SRCDIR)/%.c + $(LCC) $(LCCFLAGS) $(CFLAGS) -S -o $@ $< + +# Link the compiled object files into a .gb ROM file +$(BINS): $(OBJS) + $(LCC) $(LCCFLAGS) $(CFLAGS) -o $(BINDIR)/$(PROJECTNAME).$(EXT) $(OBJS) + +clean: + @echo Cleaning + @for target in $(TARGETS); do \ + $(MAKE) $$target-clean; \ + done + +# Include available build targets +include Makefile.targets + + +# create necessary directories after Makefile is parsed but before build +# info prevents the command from being pasted into the makefile +ifneq ($(strip $(EXT)),) # Only make the directories if EXT has been set by a target +$(info $(shell mkdir -p $(MKDIRS))) +endif diff --git a/gbdk-lib/examples/cross-platform/sram_banks/Makefile.targets b/gbdk-lib/examples/cross-platform/sram_banks/Makefile.targets @@ -0,0 +1,54 @@ + +# Platform specific flags for compiling (only populate if they're both present) +ifneq ($(strip $(PORT)),) +ifneq ($(strip $(PLAT)),) +CFLAGS += -m$(PORT):$(PLAT) +endif +endif + +# Called by the individual targets below to build a ROM +build-target: $(BINS) + +clean-target: + rm -rf $(OBJDIR) + rm -rf $(BINDIR) + +gb-clean: + ${MAKE} clean-target EXT=gb +gb: + ${MAKE} build-target PORT=sm83 PLAT=gb EXT=gb + + +gbc-clean: + ${MAKE} clean-target EXT=gbc +gbc: + ${MAKE} build-target PORT=sm83 PLAT=gb EXT=gbc + + +pocket-clean: + ${MAKE} clean-target EXT=pocket +pocket: + ${MAKE} build-target PORT=sm83 PLAT=ap EXT=pocket + + +megaduck-clean: + ${MAKE} clean-target EXT=duck +megaduck: + ${MAKE} build-target PORT=sm83 PLAT=duck EXT=duck + + +sms-clean: + ${MAKE} clean-target EXT=sms +sms: + ${MAKE} build-target PORT=z80 PLAT=sms EXT=sms + + +gg-clean: + ${MAKE} clean-target EXT=gg +gg: + ${MAKE} build-target PORT=z80 PLAT=gg EXT=gg + +nes-clean: + ${MAKE} clean-target EXT=nes +nes: + ${MAKE} build-target PORT=mos6502 PLAT=nes EXT=nes diff --git a/gbdk-lib/examples/cross-platform/sram_banks/src/main.c b/gbdk-lib/examples/cross-platform/sram_banks/src/main.c @@ -0,0 +1,30 @@ +#include <gbdk/platform.h> +#include <stdint.h> +#include <stdio.h> + +extern int sram_bank_var_0; /* In Cart SRAM bank 0 */ +extern int sram_bank_var_1; /* In Cart SRAM bank 1 */ + +void main(void) +{ + puts("Vars in \n" + "Cart SRAM Banks\n\n" + "Assigned using\n\n" + " #pragma\n" + " dataseg DATA_<n>\n" + ); + + ENABLE_RAM; + + SWITCH_RAM(0); + sram_bank_var_0 = 0; + SWITCH_RAM(1); + sram_bank_var_1 = 1; + SWITCH_RAM(2); + + SWITCH_RAM(0); + printf("sram_bank_var0 is %u\n", sram_bank_var_0); + + SWITCH_RAM(1); + printf("sram_bank_var1 is %u\n", sram_bank_var_1); +} diff --git a/gbdk-lib/examples/cross-platform/sram_banks/src/sram_bank0.c b/gbdk-lib/examples/cross-platform/sram_banks/src/sram_bank0.c @@ -0,0 +1,7 @@ +#include <gbdk/platform.h> +#include <stdint.h> +#include <stdio.h> + +#pragma dataseg DATA_0 // Sets Cart SRAM bank to 0 + +int sram_bank_var_0; /* In Cart SRAM bank 0 */ diff --git a/gbdk-lib/examples/cross-platform/sram_banks/src/sram_bank1.c b/gbdk-lib/examples/cross-platform/sram_banks/src/sram_bank1.c @@ -0,0 +1,7 @@ +#include <gbdk/platform.h> +#include <stdint.h> +#include <stdio.h> + +#pragma dataseg DATA_1 // Sets Cart SRAM bank to 1 + +int sram_bank_var_1; /* In Cart SRAM bank 1 */

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