# Name of the ROM.
NAME             := JOY BOY

# Version number of the ROM. Only one byte allowed so no SemVer. :(
VERSION          := 0x00

# Path to SD Card, used by the `sd` target.
SD_CARD_PATH     ?= /Volumes/DMG/

# Current operating system.
OPERATING_SYSTEM := $(shell uname -s)

# Phony targets is "recipes" and not the name of a file.
.PHONY: all clean bgb sd sloc check

# Build Game Boy ROM.
all: joy-boy.gb

# Build Game Boy ROM.
joy-boy.gb: joy-boy.o
	rgblink -d -t -n "$(@:.gb=.sym)" -m "$(@:.gb=.map)" -o "$@" "$<"
	rgbfix -j -p 0x0 -t "$(NAME)" -n "$(VERSION)" -v "$@"

# Assamble object file from source.
%.o: %.asm
	rgbasm -E -v -o "$@" "$<"

# Remove (almost) all generated files.
clean:
	rm -f *.gb *.map *.o *.sym *.js

# Start ROM in the BGB Game Boy emulator.
bgb: joy-boy.gb
	bgb "$<"

# Copy ROM to SD Card.
sd: joy-boy.gb
	until cp "$<" "$(SD_CARD_PATH)" && diskutil unmount "$(SD_CARD_PATH)"; do sleep 3; done

# Count number of source code lines, excluding comments.
sloc: joy-boy.asm
	grep -cvE "^;|^$$" "$<"

# Verify that it's a Game Boy ROM of correct size.
check: joy-boy.gb
	file "$<" | grep -iE "(Game Boy|GameBoy).*$(NAME).*256Kbit"
