SOURCEDIR := $(abspath $(patsubst %/,%,$(dir $(abspath $(lastword \
	$(MAKEFILE_LIST))))))

NAME := sameboy
JGNAME := $(NAME)-jg

SRCDIR := $(SOURCEDIR)/..

include $(SRCDIR)/version.mk

RGBASM ?= rgbasm
RGBGFX ?= rgbgfx
RGBLINK ?= rgblink

FLAGS := -std=gnu11
FLAGS_PB12 := -Os -std=c99
DEFS := -D_GNU_SOURCE -DGB_INTERNAL -DGB_DISABLE_REWIND -DGB_DISABLE_DEBUGGER \
	-DGB_VERSION=\"$(VERSION)\"

WARNINGS := -Wall -Wno-missing-braces -Wno-multichar -Wno-unused-result
WARNINGS_PB12 := -Wall -Wextra -Wshadow

# Only relative include paths are used in sameboy
INCLUDES_JG = -I$(SRCDIR)

LIBS = -lm

DOCS := ../LICENSE README

# Object dirs
MKDIRS := Core

override INSTALL_DATA := 1
override INSTALL_EXAMPLE := 0
override INSTALL_SHARED := 0

include $(SOURCEDIR)/mk/jg.mk

EXT := c
LINKER := $(CC)

CSRCS := Core/apu.c \
	Core/camera.c \
	Core/cheats.c \
	Core/display.c \
	Core/gb.c \
	Core/joypad.c \
	Core/mbc.c \
	Core/memory.c \
	Core/random.c \
	Core/rumble.c \
	Core/save_state.c \
	Core/sgb.c \
	Core/sm83_cpu.c \
	Core/timing.c

JGSRCS := jg.c

# BootROMs
BOOTROMS := $(NAME)/agb_boot.bin \
	$(NAME)/cgb_boot.bin \
	$(NAME)/cgb_boot_fast.bin \
	$(NAME)/dmg_boot.bin \
	$(NAME)/mgb_boot.bin \
	$(NAME)/sgb_boot.bin \
	$(NAME)/sgb2_boot.bin

DATA := $(notdir $(BOOTROMS))
DATA_TARGET := bootroms

PB12_COMPRESS := $(OBJDIR)/pb12
PB12_BOOTLOGO := $(OBJDIR)/SameBoyLogo.pb12
2BPP_BOOTLOGO := $(OBJDIR)/SameBoyLogo.2bpp
BOOTROMS_PATH := $(SRCDIR)/BootROMs

# List of object files
OBJS := $(patsubst %,$(OBJDIR)/%,$(CSRCS:.c=.o))
OBJS_JG := $(patsubst %,$(OBJDIR)/%,$(JGSRCS:.c=.o))

# BootROMs commands
BUILD_PB12 = $(call COMPILE_C_BUILD, $(FLAGS_PB12) $(WARNINGS_PB12))
BUILD_RGBASM = $(RGBASM) -I $(BOOTROMS_PATH) -I $(OBJDIR) -o $@ $<
BUILD_RGBGFX = $(RGBGFX) -Z -u -c embedded -o $@ $<

# Core commands
BUILD_JG = $(call COMPILE_C, $(FLAGS) $(WARNINGS) $(DEFS) $(INCLUDES_JG) \
	$(CFLAGS_JG))
BUILD_MAIN = $(call COMPILE_C, $(FLAGS) $(WARNINGS) $(DEFS))

.PHONY: $(PHONY) bootroms

all: $(TARGET)

# BootROMs rules
$(PB12_COMPRESS): $(BOOTROMS_PATH)/pb12.c $(OBJDIR)/.tag
	$(call COMPILE_INFO,$(BUILD_PB12))
	@$(BUILD_PB12)

$(2BPP_BOOTLOGO): $(BOOTROMS_PATH)/SameBoyLogo.png $(OBJDIR)/.tag
	$(call COMPILE_INFO,$(BUILD_RGBGFX))
	@$(BUILD_RGBGFX)

$(PB12_BOOTLOGO): $(2BPP_BOOTLOGO) $(PB12_COMPRESS)
	./$(PB12_COMPRESS) < $< > $@

$(OBJDIR)/%.bin.tmp: $(BOOTROMS_PATH)/%.asm $(PB12_BOOTLOGO)
	$(call COMPILE_INFO,$(BUILD_RGBASM))
	@$(BUILD_RGBASM)

$(OBJDIR)/%.bin: $(OBJDIR)/%.bin.tmp
	$(RGBLINK) -o $@ $<

$(NAME)/%.bin: $(OBJDIR)/%.bin
	@mkdir -p $(NAME)
	dd if=$< of=$@ count=1 bs=$(if $(findstring dmg,$@)$(findstring sgb,$@),256,2304)

bootroms: $(BOOTROMS)

# Core rules
$(OBJDIR)/%.o: $(SRCDIR)/%.$(EXT) $(PREREQ)
	$(call COMPILE_INFO,$(BUILD_MAIN))
	@$(BUILD_MAIN)

# Data rules
install-data: all
	@mkdir -p $(DESTDIR)$(DATADIR)/jollygood/$(NAME)
	cp $(NAME)/agb_boot.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/
	cp $(NAME)/cgb_boot.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/
	cp $(NAME)/cgb_boot_fast.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/
	cp $(NAME)/dmg_boot.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/
	cp $(NAME)/mgb_boot.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/
	cp $(NAME)/sgb_boot.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/
	cp $(NAME)/sgb2_boot.bin $(DESTDIR)$(DATADIR)/jollygood/$(NAME)/

include $(SOURCEDIR)/mk/rules.mk
