git.y1.nz

gbdk-2020

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

commit cf03f162d8767b34992b82edaf0b55d4f41eb008
parent 61bdc20295cec4814ed0eb41f8a8406fcda9a952
Author: Toxa <untoxa@mail.ru>
Date:   Thu, 24 Jun 2021 12:02:43 +0300

MBC-agnostic bank switching macros + examples

Diffstat:
Mgbdk-lib/examples/gb/banks/Makefile4++--
Mgbdk-lib/examples/gb/banks/banks.c21++++++++++-----------
Mgbdk-lib/examples/gb/banks_autobank/Makefile91+++++++++++++++++++++++++++++++++++++-------------------------------------------
Mgbdk-lib/examples/gb/banks_autobank/banks.c18+++++++++---------
Mgbdk-lib/include/gb/gb.h8++++++++
5 files changed, 71 insertions(+), 71 deletions(-)

diff --git a/gbdk-lib/examples/gb/banks/Makefile b/gbdk-lib/examples/gb/banks/Makefile @@ -54,10 +54,10 @@ bank_3.o: bank_3.c $(CC) -Wf-bo3 -Wf-ba3 -c -o $@ $< # Link banks -# ROM+MBC1+RAM : -Wl-yt2 +# MBC5+ROM+RAM : -Wl-yt0x19 # 4 ROM banks : -Wl-yo4 # 4 RAM banks : -Wl-ya4 # banks.gb: banks.o bank_0.o bank_1.o bank_2.o bank_3.o - $(CC) -Wl-yt2 -Wl-yo4 -Wl-ya4 -o $@ banks.o bank_0.o bank_1.o bank_2.o bank_3.o + $(CC) -Wl-yt0x19 -Wl-yo4 -Wl-ya4 -o $@ banks.o bank_0.o bank_1.o bank_2.o bank_3.o diff --git a/gbdk-lib/examples/gb/banks/banks.c b/gbdk-lib/examples/gb/banks/banks.c @@ -22,7 +22,7 @@ void bank_fixed(void) NONBANKED void print_var(uint8_t bank) { - SWITCH_RAM_MBC1(bank); + SWITCH_RAM(bank); printf("Var_%u is %u\n"); } @@ -30,17 +30,16 @@ void main(void) { puts("Program Start..."); - ENABLE_RAM_MBC1; - SWITCH_4_32_MODE_MBC1; + ENABLE_RAM; var_internal = 1; - SWITCH_RAM_MBC1(0); + SWITCH_RAM(0); var_0 = 2; - SWITCH_RAM_MBC1(1); + SWITCH_RAM(1); var_1 = 3; - SWITCH_RAM_MBC1(2); + SWITCH_RAM(2); var_2 = 4; - SWITCH_RAM_MBC1(3); + SWITCH_RAM(3); var_3 = 5; bank_fixed(); @@ -49,13 +48,13 @@ void main(void) bank_3(); printf("Var is %u\n", var_internal); - SWITCH_RAM_MBC1(0); + SWITCH_RAM(0); printf("Var_0 is %u\n", var_0); - SWITCH_RAM_MBC1(1); + SWITCH_RAM(1); printf("Var_1 is %u\n", var_1); - SWITCH_RAM_MBC1(2); + SWITCH_RAM(2); printf("Var_2 is %u\n", var_2); - SWITCH_RAM_MBC1(3); + SWITCH_RAM(3); printf("Var_3 is %u\n", var_3); puts("The End..."); diff --git a/gbdk-lib/examples/gb/banks_autobank/Makefile b/gbdk-lib/examples/gb/banks_autobank/Makefile @@ -1,15 +1,6 @@ -LCC = ../../../bin/lcc -Wa-l -Wl-m -BANKPACK = ../../../bin/bankpack +CC = ../../../bin/lcc -Wa-l -Wl-m -Wl-j -# uncomment for lcc verbose output -# CFLAGS = -v - -BINS = autobanks.gb -CSOURCES := $(wildcard *.c) -ASMSOURCES := $(wildcard *.s) - -# Compiling will produce .o files -OBJS = $(CSOURCES:%.c=%.o) $(ASMSOURCES:%.c=%.o) +BINS = banks.gb \ all: $(BINS) @@ -18,53 +9,55 @@ make.bat: Makefile @make -sn | sed y/\\//\\\\/ | grep -v make >> make.bat %.o: %.c - $(LCC) $(CFLAGS) -c -o $@ $< - -%.o: %.s - $(LCC) $(CFLAGS) -c -o $@ $< + $(CC) -c -o $@ $< %.s: %.c - $(LCC) $(CFLAGS) -S -o $@ $< + $(CC) -S -o $@ $< -# Process: .c and .s files -> .o -> (bankpack) -> .rel -> (linker) ... -> .gb -# -# -autobank : Tells lcc to call bankpack for the compiled object (.o) files before linking -# -Wb-ext=.rel : Set output extension for autobanked files to .rel -# -Wb-v : Prints out assigned autobank info (optional) -# -Wl-yoA : Have makebin automatically calculate required number of ROM banks (otherwise use -Wl-yo4 in this example) -# -Wl-ya4 : Use 4 RAM banks -# -Wl-yt0x1B : Use MBC5+RAM+BATTERY cartridge type -$(BINS): $(OBJS) - $(LCC) $(CFLAGS) -autobank -Wb-ext=.rel -Wb-v -Wl-yt0x1B -Wl-yoA -Wl-ya4 -o $(BINS) $(OBJS) +%.o: %.s + $(CC) -c -o $@ $< +%.gb: %.o + $(CC) -o $@ $< -# It's also possible to do all the compiling, autobanking and linking in a single call to lcc -# with none of the other makefile "targets" above. You can try this target out by using "make onepass". -# Notice that the input files are the .c and .asm sources instead of the compiled object files. -onepass: - $(LCC) $(CFLAGS) -autobank -Wb-ext=.rel -Wb-v -Wl-yt0x1B -Wl-yoA -Wl-ya4 -o $(BINS) $(CSOURCES) $(ASMSOURCES) +clean: + rm -f *.o *.lst *.map *.gb *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi +############################################################ +# Multiple bank example -clean: - rm -f *.o *.lst *.map *.gb *.ihx *.sym *.cdb *.adb *.asm *.rel +# Compile bank 0 (no ROM) +# RAM bank 0 : -Wf-ba0 +# +bank_0.o: bank_0.c + $(CC) -Wf-ba0 -c -o $@ $< + +# Compile bank 1 (ROM+RAM) +# ROM bank 1 : -Wf-bo1 +# RAM bank 1 : -Wf-ba1 +# +bank_1.o: bank_1.c + $(CC) -Wf-bo1 -Wf-ba1 -c -o $@ $< +# Compile bank 2 (ROM+RAM) +# ROM bank 2 : -Wf-bo2 +# RAM bank 2 : -Wf-ba2 +# +bank_2.o: bank_2.c + $(CC) -Wf-bo2 -Wf-ba2 -c -o $@ $< -# For lcc linker option: -Wl-ytN where N is one of the numbers below +# Compile bank 3 (ROM+RAM) +# ROM bank 3 : -Wf-bo3 +# RAM bank 3 : -Wf-ba3 # -# ROM Byte 0147: Cartridge type +bank_3.o: bank_3.c + $(CC) -Wf-bo3 -Wf-ba3 -c -o $@ $< + +# Link banks +# MBC5+ROM+RAM : -Wl-yt0x19 +# 4 ROM banks : -Wl-yo4 +# 4 RAM banks : -Wl-ya4 # -# 0-ROM ONLY 12-ROM+MBC3+RAM -# 1-ROM+MBC1 13-ROM+MBC3+RAM+BATT -# 2-ROM+MBC1+RAM 19-ROM+MBC5 -# 3-ROM+MBC1+RAM+BATT 1A-ROM+MBC5+RAM -# 5-ROM+MBC2 1B-ROM+MBC5+RAM+BATT -# 6-ROM+MBC2+BATTERY 1C-ROM+MBC5+RUMBLE -# 8-ROM+RAM 1D-ROM+MBC5+RUMBLE+SRAM -# 9-ROM+RAM+BATTERY 1E-ROM+MBC5+RUMBLE+SRAM+BATT -# B-ROM+MMM01 1F-Pocket Camera -# C-ROM+MMM01+SRAM FD-Bandai TAMA5 -# D-ROM+MMM01+SRAM+BATT FE - Hudson HuC-3 -# F-ROM+MBC3+TIMER+BATT FF - Hudson HuC-1 -# 10-ROM+MBC3+TIMER+RAM+BATT -# 11-ROM+MBC3 +banks.gb: banks.o bank_0.o bank_1.o bank_2.o bank_3.o + $(CC) -Wm-yt0x1A -Wl-yo4 -Wl-ya4 -o $@ banks.o bank_0.o bank_1.o bank_2.o bank_3.o diff --git a/gbdk-lib/examples/gb/banks_autobank/banks.c b/gbdk-lib/examples/gb/banks_autobank/banks.c @@ -45,16 +45,16 @@ void main(void) printf("\n"); // Print the const vars, unbanked first then the banked ones - printf("Var0 = %u is unbanked", some_const_var_0); + printf("Const0= %u nonbanked\n", some_const_var_0); - SWITCH_ROM_MBC1( (uint8_t)&__bank_srcfile1 ); - printf("Var1 = %u in bank %u\n", some_const_var_1, &(__bank_srcfile1)); - SWITCH_ROM_MBC1( (uint8_t)&__bank_srcfile2 ); - printf("Var2 = %u in bank %u\n", some_const_var_2, &(__bank_srcfile2)); - SWITCH_ROM_MBC1( (uint8_t)&__bank_srcfile3 ); - printf("Var3 = %u in bank %u\n", some_const_var_3, &(__bank_srcfile3)); - SWITCH_ROM_MBC1( (uint8_t)&__bank_srcfile4 ); - printf("Var4 = %u in bank %u\n", some_const_var_4, &(__bank_srcfile4)); + 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)); printf("\n"); puts("The End..."); diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -358,22 +358,30 @@ __REG _current_bank; #define SWITCH_ROM_MBC1(b) \ _current_bank = (b), *(uint8_t *)0x2000 = (b) +#define SWITCH_ROM SWITCH_ROM_MBC1 + /** Switches SRAM bank on MBC1 and other compaticle MBCs @param b SRAM bank to switch to */ #define SWITCH_RAM_MBC1(b) \ *(uint8_t *)0x4000 = (b) +#define SWITCH_RAM SWITCH_RAM_MBC1 + /** Enables SRAM on MBC1 */ #define ENABLE_RAM_MBC1 \ *(uint8_t *)0x0000 = 0x0A +#define ENABLE_RAM ENABLE_RAM_MBC1 + /** Disables SRAM on MBC1 */ #define DISABLE_RAM_MBC1 \ *(uint8_t *)0x0000 = 0x00 +#define DISABLE_RAM DISABLE_RAM_MBC1 + #define SWITCH_16_8_MODE_MBC1 \ *(uint8_t *)0x6000 = 0x00

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