Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ libagbsyscall/*.s
*.exe
*.dll
*.sdl
*.iso

# PSP build outputs
EBOOT.PBP
PARAM.SFO
sa2_debug.log

# third party deps
/ext
166 changes: 141 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ else ifeq ($(CPU_ARCH),i386)
TOOLCHAIN := /usr/x86_64-w64-mingw32/
PREFIX := x86_64-w64-mingw32-
endif
# PSP
else ifeq ($(PLATFORM),psp)
PSPDEV ?= $(HOME)/pspdev
PSPSDK := $(PSPDEV)/psp/sdk
export PATH := $(PSPDEV)/bin:$(PATH)
PREFIX := psp-
else ifeq ($(PLATFORM),sdl_ps2)
PREFIX := mips64r5900el-ps2-elf-
else ifeq ($(PLATFORM),ps2)
PREFIX := mips64r5900el-ps2-elf-
else
# Native
ifneq ($(PLATFORM),sdl)
Expand All @@ -74,6 +84,7 @@ CC1 := tools/agbcc/bin/agbcc$(EXE)
CC1_OLD := tools/agbcc/bin/old_agbcc$(EXE)
else
CC1 := $(PREFIX)gcc$(EXE)
CXX := $(PREFIX)g++$(EXE)
CC1_OLD := $(CC1)
endif

Expand Down Expand Up @@ -120,6 +131,18 @@ else ifeq ($(PLATFORM),sdl)
ROM := $(BUILD_NAME).sdl
ELF := $(ROM).elf
MAP := $(ROM).map
else ifeq ($(PLATFORM),psp)
ROM := EBOOT.PBP
ELF := $(BUILD_NAME).psp.elf
MAP := $(BUILD_NAME).psp.map
else ifeq ($(PLATFORM),sdl_ps2)
ROM := $(BUILD_NAME).$(PLATFORM).iso
ELF := $(ROM:.iso=.elf)
MAP := $(ROM:.iso=.map)
else ifeq ($(PLATFORM),ps2)
ROM := $(BUILD_NAME).$(PLATFORM).iso
ELF := $(ROM:.iso=.elf)
MAP := $(ROM:.iso=.map)
else
ROM := $(BUILD_NAME).$(PLATFORM).exe
ELF := $(ROM:.exe=.elf)
Expand Down Expand Up @@ -156,16 +179,30 @@ TILESETS_SUBDIR = graphics/tilesets/
ifeq ($(PLATFORM),gba)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/*")
else ifeq ($(PLATFORM),sdl)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*")
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*" -not -path "*/platform/psp/*" -not -path "*/platform/ps2/*")
else ifeq ($(PLATFORM),psp)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*" -not -path "*/platform/ps2/*")
else ifeq ($(PLATFORM),sdl_ps2)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*" -not -path "*/platform/psp/*" -not -path "*/platform/ps2/*")
else ifeq ($(PLATFORM),ps2)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*" -not -path "*/platform/psp/*" -not -path "*/platform/pret_sdl/*")
else ifeq ($(PLATFORM),sdl_win32)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*")
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/win32/*" -not -path "*/platform/psp/*" -not -path "*/platform/ps2/*")
else ifeq ($(PLATFORM),win32)
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/pret_sdl/*")
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c" -not -path "*/platform/pret_sdl/*" -not -path "*/platform/psp/*" -not -path "*/platform/ps2/*")
else
C_SRCS := $(shell find $(C_SUBDIR) -name "*.c")
endif
C_OBJS := $(patsubst $(C_SUBDIR)/%.c,$(C_BUILDDIR)/%.o,$(C_SRCS))

ifeq ($(PLATFORM),gba)
CXX_SRCS := $(shell find $(C_SUBDIR) -name "*.cc" -not -path "*/platform/*")
else
CXX_SRCS := $(shell find $(C_SUBDIR) -name "*.cc")
endif

CXX_OBJS := $(patsubst $(C_SUBDIR)/%.cc,$(C_BUILDDIR)/%.o,$(CXX_SRCS))

# Platform not included as we only need the headers for decomp scratches
C_HEADERS := $(shell find $(INCLUDE_DIRS) -name "*.h" -not -path "*/platform/*")

Expand All @@ -189,7 +226,7 @@ MID_OBJS := $(patsubst $(MID_SUBDIR)/%.mid,$(MID_BUILDDIR)/%.o,$(MID_SRCS))
SOUND_ASM_SRCS := $(wildcard $(SOUND_ASM_SUBDIR)/*.s)
SOUND_ASM_OBJS := $(patsubst $(SOUND_ASM_SUBDIR)/%.s,$(SOUND_ASM_BUILDDIR)/%.o,$(SOUND_ASM_SRCS))

OBJS := $(C_OBJS) $(ASM_OBJS) $(C_ASM_OBJS) $(DATA_ASM_OBJS) $(SONG_OBJS) $(MID_OBJS)
OBJS := $(C_OBJS) $(CXX_OBJS) $(ASM_OBJS) $(C_ASM_OBJS) $(DATA_ASM_OBJS) $(SONG_OBJS) $(MID_OBJS)
OBJS_REL := $(patsubst $(OBJ_DIR)/%,%,$(OBJS))

FORMAT_SRC_PATHS := $(shell find . -name "*.c" ! -path '*/src/data/*' ! -path '*/build/*' ! -path '*/ext/*')
Expand Down Expand Up @@ -225,6 +262,15 @@ else
ifeq ($(PLATFORM),sdl)
CC1FLAGS += -Wno-parentheses-equality -Wno-unused-value
CPPFLAGS += -D TITLE_BAR=$(BUILD_NAME).$(PLATFORM) -D PLATFORM_GBA=0 -D PLATFORM_SDL=1 -D PLATFORM_WIN32=0 $(shell sdl2-config --cflags)
else ifeq ($(PLATFORM),psp)
CC1FLAGS += -G0
CPPFLAGS += -D PLATFORM_GBA=0 -D PLATFORM_SDL=1 -D PLATFORM_WIN32=0 -D SDL_MAIN_HANDLED -I$(PSPDEV)/psp/include/SDL2 -I$(PSPDEV)/psp/include -I$(PSPSDK)/include -D_PSP_FW_VERSION=600
else ifeq ($(PLATFORM),sdl_ps2)
CC1FLAGS += -G0 -Wno-parentheses-equality -Wno-unused-value -ffast-math
CPPFLAGS += -D PLATFORM_GBA=0 -D PLATFORM_SDL=1 -D PLATFORM_WIN32=0 -D SDL_MAIN_HANDLED -D_EE -D__PS2__ -I$(PS2SDK)/common/include -I$(PS2SDK)/ee/include -I$(PS2SDK)/ports/include $(shell $(PS2SDK)/ports/bin/sdl2-config --cflags)
else ifeq ($(PLATFORM),ps2)
CC1FLAGS += -G0 -Wno-parentheses-equality -Wno-unused-value -ffast-math
CPPFLAGS += -D PLATFORM_GBA=0 -D PLATFORM_SDL=0 -D PLATFORM_WIN32=0 -D_EE -D__PS2__ -I$(PS2SDK)/common/include -I$(PS2SDK)/ee/include -I$(PS2DEV)/gsKit/include -I$(PS2SDK)/ports/include
else ifeq ($(PLATFORM),sdl_win32)
CPPFLAGS += -D TITLE_BAR=$(BUILD_NAME).$(PLATFORM) -D PLATFORM_GBA=0 -D PLATFORM_SDL=1 -D PLATFORM_WIN32=0 $(SDL_MINGW_FLAGS)
else ifeq ($(PLATFORM),win32)
Expand All @@ -241,24 +287,20 @@ else
endif
endif

ifeq ($(PLATFORM),gba)
ASFLAGS += -mcpu=arm7tdmi -mthumb-interwork
CC1FLAGS += -mthumb-interwork
else
ifeq ($(PLATFORM), sdl)
# for modern we are using a modern compiler
# so instead of CPP we can use gcc -E to "preprocess only"
CPP := $(CC1) -E
endif
# Allow file input through stdin on modern GCC and set it to "compile only"
CC1FLAGS += -x c -S
endif

ifeq ($(DEBUG),1)
CC1FLAGS += -g3 -O0
CPPFLAGS += -D DEBUG=1
else
CC1FLAGS += -O2
ifeq ($(PLATFORM),psp)
# -O3 for PSP (Allegrex MIPS, small D-cache)
CC1FLAGS += -O3 -funroll-loops -fomit-frame-pointer
else ifeq ($(PLATFORM),sdl_ps2)
CC1FLAGS += -O3 -funroll-loops -fomit-frame-pointer
else ifeq ($(PLATFORM),ps2)
CC1FLAGS += -O3 -fomit-frame-pointer
else
CC1FLAGS += -O2
endif
CPPFLAGS += -D DEBUG=0
endif

Expand All @@ -285,6 +327,28 @@ else
CPPFLAGS += -D ENABLE_DECOMP_CREDITS=1
endif

CXXFLAGS := $(CC1FLAGS) $(CPPFLAGS) -fno-rtti -fno-exceptions -std=c++11

ifeq ($(PLATFORM),gba)
ASFLAGS += -mcpu=arm7tdmi -mthumb-interwork
CC1FLAGS += -mthumb-interwork
else
ifeq ($(PLATFORM), sdl)
# for modern we are using a modern compiler
# so instead of CPP we can use gcc -E to "preprocess only"
CPP := $(CC1) -E
else ifeq ($(PLATFORM), psp)
CPP := $(CC1) -E
else ifeq ($(PLATFORM), sdl_ps2)
ASFLAGS += -msingle-float
else ifeq ($(PLATFORM), ps2)
ASFLAGS += -msingle-float
endif
# Allow file input through stdin on modern gcc/g++ and set it to "compile only"
CC1FLAGS += -x c -S
CXXFLAGS += -x c++ -S
endif

### LINKER FLAGS ###

# GBA
Expand All @@ -297,6 +361,13 @@ else ifeq ($(PLATFORM),sdl)
else
MAP_FLAG := -Xlinker -Map=
endif
# PSP
else ifeq ($(PLATFORM),psp)
MAP_FLAG := -Xlinker -Map=
else ifeq ($(PLATFORM),sdl_ps2)
MAP_FLAG := -Xlinker -Map=
else ifeq ($(PLATFORM),ps2)
MAP_FLAG := -Xlinker -Map=
# Win32
else
MAP_FLAG := -Xlinker -Map=
Expand All @@ -307,6 +378,12 @@ ifeq ($(PLATFORM),gba)
LIBS := $(ROOT_DIR)/tools/agbcc/lib/libgcc.a $(ROOT_DIR)/tools/agbcc/lib/libc.a $(LIBABGSYSCALL_LIBS)
else ifeq ($(PLATFORM),sdl)
LIBS := $(shell sdl2-config --cflags --libs)
else ifeq ($(PLATFORM),psp)
LIBS := -L$(PSPDEV)/psp/lib -L$(PSPSDK)/lib -lSDL2 -lm -lGL -lpspvram -lpspaudio -lpspvfpu -lpspdisplay -lpspgu -lpspge -lpsphprm -lpspctrl -lpsppower -lpspdebug -lpspnet -lpspnet_apctl -Wl,-zmax-page-size=128
else ifeq ($(PLATFORM),sdl_ps2)
LIBS := -lSDL2 $(shell $(PS2SDK)/ports/bin/sdl2-config --libs) -T$(PS2SDK)/ee/startup/linkfile -L$(PS2SDK)/common/lib -L$(PS2SDK)/ee/lib -L$(PS2DEV)/gsKit/lib -Wl,-zmax-page-size=128
else ifeq ($(PLATFORM),ps2)
LIBS := -T$(PS2SDK)/ee/startup/linkfile -L$(PS2SDK)/common/lib -L$(PS2SDK)/ee/lib -L$(PS2DEV)/gsKit/lib -L$(PS2SDK)/ports/lib -lgskit -ldmakit -lps2_drivers -lmc -lpatches -Wl,-zmax-page-size=128
else ifeq ($(PLATFORM),sdl_win32)
LIBS := -mwin32 -lkernel32 -lwinmm -lmingw32 -lxinput $(SDL_MINGW_LIBS)
else ifeq ($(PLATFORM), win32)
Expand All @@ -316,7 +393,7 @@ endif
#### MAIN TARGETS ####

# these commands will run regardless of deps being completed
.PHONY: clean tools tidy clean-tools $(TOOLDIRS) libagbsyscall
.PHONY: clean tools tidy clean-tools $(TOOLDIRS) libagbsyscall ps2

# Ensure required directories exist
$(shell mkdir -p $(C_BUILDDIR) $(ASM_BUILDDIR) $(DATA_ASM_BUILDDIR) $(SOUND_ASM_BUILDDIR) $(SONG_BUILDDIR) $(MID_BUILDDIR))
Expand Down Expand Up @@ -397,7 +474,8 @@ clean-tools:
tidy:
$(RM) -r build/*
$(RM) SDL2.dll
$(RM) $(BUILD_NAME)*.exe $(BUILD_NAME)*.elf $(BUILD_NAME)*.map $(BUILD_NAME)*.sdl $(BUILD_NAME)*.gba
$(RM) $(BUILD_NAME)*.exe $(BUILD_NAME)*.elf $(BUILD_NAME)*.map $(BUILD_NAME)*.sdl $(BUILD_NAME)*.gba $(BUILD_NAME)*.iso
$(RM) EBOOT.PBP PARAM.SFO

usa_beta: ; @$(MAKE) GAME_REGION=USA GAME_VARIANT=BETA

Expand All @@ -409,6 +487,12 @@ europe: ; @$(MAKE) GAME_REGION=EUROPE

sdl: ; @$(MAKE) PLATFORM=sdl

psp: ; @$(MAKE) PLATFORM=psp

sdl_ps2: ; @$(MAKE) PLATFORM=sdl_ps2

ps2: ; @$(MAKE) PLATFORM=ps2

tas_sdl: ; @$(MAKE) sdl TAS_TESTING=1

sdl_win32:
Expand Down Expand Up @@ -459,7 +543,7 @@ data/mb_chao_garden_japan.gba.lz: data/mb_chao_garden_japan.gba

%.bin: %.aif ; $(AIF) $< $@

$(ELF): $(OBJS) libagbsyscall
$(ELF): $(OBJS)
ifeq ($(PLATFORM),gba)
@echo "$(LD) -T $(LDSCRIPT) $(MAP_FLAG) $(MAP) <objects> <lib> -o $@"
@$(CPP) -P $(CPPFLAGS) $(LDSCRIPT) > $(OBJ_DIR)/$(LDSCRIPT)
Expand All @@ -470,14 +554,35 @@ else
@cd $(OBJ_DIR) && $(CC1) $(MAP_FLAG)$(ROOT_DIR)/$(MAP) $(OBJS_REL) $(LIBS) -o $(ROOT_DIR)/$@
endif

$(ROM): $(ELF)

ifeq ($(PLATFORM),gba)
$(ROM): $(ELF) libagbsyscall
$(OBJCOPY) -O binary --pad-to 0x8400000 $< $@
$(FIX) $@ -p -t"$(TITLE)" -c$(GAME_CODE) -m$(MAKER_CODE) -r$(GAME_REVISION) --silent
else ifeq ($(PLATFORM),sdl)
cp $< $@
else
else ifeq ($(PLATFORM),win32)
$(ROM): $(ELF) libagbsyscall
$(OBJCOPY) -O pei-x86-64 $< $@
else
$(ROM): $(ELF)
ifeq ($(PLATFORM),sdl)
cp $< $@
else ifeq ($(PLATFORM),psp)
psp-fixup-imports $<
mksfoex 'Sonic Advance 2' PARAM.SFO
psp-strip $< -o $(BUILD_NAME).psp_strip.elf
pack-pbp $@ PARAM.SFO NULL NULL NULL NULL NULL $(BUILD_NAME).psp_strip.elf NULL
-rm -f $(BUILD_NAME).psp_strip.elf
else ifeq ($(PLATFORM),sdl_ps2)
@echo Creating $(ROM) from $(ELF)
@cp -r ps2/ntsc $(OBJ_DIR)/iso
@cp $< $(OBJ_DIR)/iso/$(PS2_GAME_CODE)
@mkisofs -o $(ROM) $(OBJ_DIR)/iso/
else ifeq ($(PLATFORM),ps2)
@echo Creating $(ROM) from $(ELF)
@cp -r ps2/ntsc $(OBJ_DIR)/iso
@cp $< $(OBJ_DIR)/iso/$(PS2_GAME_CODE)
@mkisofs -o $(ROM) $(OBJ_DIR)/iso/
endif
endif

# Build c sources, and ensure alignment
Expand All @@ -491,11 +596,21 @@ ifeq ($(PLATFORM), gba)
endif
@$(AS) $(ASFLAGS) $(C_BUILDDIR)/$*.s -o $@

$(C_BUILDDIR)/%.o: $(C_SUBDIR)/%.cc
@echo "$(CXX) <flags> -o $@ $<"
@$(shell mkdir -p $(shell dirname '$(C_BUILDDIR)/$*.o'))
@$(CXX) $(CXXFLAGS) -o $(C_BUILDDIR)/$*.s $<
@$(AS) $(ASFLAGS) $(C_BUILDDIR)/$*.s -o $@

# Scan the src dependencies to determine if any dependent files have changed
$(C_BUILDDIR)/%.d: $(C_SUBDIR)/%.c
@$(shell mkdir -p $(shell dirname '$(C_BUILDDIR)/$*.d'))
$(SCANINC) -M $@ $(INCLUDE_SCANINC_ARGS) $<

$(C_BUILDDIR)/%.d: $(C_SUBDIR)/%.cc
@$(shell mkdir -p $(shell dirname '$(C_BUILDDIR)/$*.d'))
$(SCANINC) -M $@ $(INCLUDE_SCANINC_ARGS) $<

# rule for sources from the src dir (parts of libraries)
$(C_BUILDDIR)/%.o: $(C_SUBDIR)/%.s
@echo "$(AS) <flags> -o $@ $<"
Expand All @@ -515,6 +630,7 @@ $(DATA_ASM_BUILDDIR)/%.d: $(DATA_ASM_SUBDIR)/%.s

ifneq ($(NODEP),1)
-include $(addprefix $(OBJ_DIR)/,$(C_SRCS:.c=.d))
-include $(addprefix $(OBJ_DIR)/,$(CXX_SRCS:.cc=.d))
-include $(addprefix $(OBJ_DIR)/,$(DATA_ASM_SRCS:.s=.d))
endif

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ It can also build:
* **sa2.sdl** `make sdl` (Linux/MacOS SDL 64bit port)
* **sa2.sdl_win32.exe** `make sdl_win32` (Windows SDL 64bit port)
* :construction: **sa2.win32.exe** `make win32` (Win32 native port, not functional)
* **EBOOT.PBP** `make psp` (PlayStation Portable homebrew port, requires [PSPDEV](https://github.com/pspdev/pspdev))

## Current state

Expand Down
2 changes: 2 additions & 0 deletions asm/macros/portable.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
.macro mPtr value
#if defined(__aarch64__) || defined(__x86_64__)
.quad \value
#elif defined(__mips__)
.4byte \value
#else
.int \value
#endif
Expand Down
1 change: 1 addition & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ MAKER_CODE := 78
BUILD_NAME := sa2
TITLE := SONICADVANC2
GAME_CODE := A2N
PS2_GAME_CODE := SLUS_054.02


# Revision
Expand Down
16 changes: 11 additions & 5 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@

#define TAS_TESTING_WIDESCREEN_HACK 1

#define RENDERER_SOFTWARE 0
#define RENDERER_OPENGL 1
#define RENDERER_COUNT 2
#if PLATFORM_WIN32 && !PLATFORM_SDL
#define RENDERER_SOFTWARE 0
#define RENDERER_OPENGL 1
#define RENDERER_SOFTWARE_FAST 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of having 2 software renderers.
Especially considering it's to-be-removed anyway, adding another label here is extra complexity imo.

We can replace the old rendering with the faster one (leaving comments intact if possible) and if bugs occur we'll figure out, why.

My intention with these #defines was specifically to list different backends here.

#define RENDERER_COUNT 3

#ifndef RENDERER
#if defined(__PSP__) || defined(__PS2__)
#define RENDERER RENDERER_SOFTWARE_FAST
#elif PLATFORM_WIN32 && !PLATFORM_SDL
// TODO: Only win32 for now
#define RENDERER RENDERER_OPENGL
#else
#define RENDERER RENDERER_SOFTWARE
#define RENDERER RENDERER_SOFTWARE_FAST
#endif
#endif

#endif // GUARD_SA2_CONFIG_H
10 changes: 10 additions & 0 deletions include/gba/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@
#define OAM_ENTRY_COUNT 128
#if PORTABLE
// NOTE: Used in gba/types.h, so they have to be defined before the #include
#if defined(__PSP__)
// PSP: Use GBA-native resolution, SDL scales to 480x272
#define DISPLAY_WIDTH 240
#define DISPLAY_HEIGHT 160
#elif defined(__PS2__)
// Runs at 60fps with the "fast draw"
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 180
#else
#define DISPLAY_WIDTH 426
#define DISPLAY_HEIGHT 240
#endif

// NOTE: We shouldn't consider WIDESCREEN_HACK a permanent thing.
// This hack should best be removed once there's a "native" platform layer.
Expand Down
Loading