# Makefile for Raz Tamagotchi
# Requires: arm-none-eabi-gcc (ARM embedded toolchain)
#
# Install on Linux:   sudo apt install gcc-arm-none-eabi
# Install on macOS:   brew install arm-none-eabi-gcc
# Install on Windows: download from developer.arm.com
#
# Usage:
#   make          - build tamagotchi.bin
#   make clean    - remove build artifacts
#   make size     - show memory usage

PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
OBJCOPY = $(PREFIX)objcopy
SIZE = $(PREFIX)size

# Target MCU flags
CFLAGS  = -mcpu=cortex-m0 -mthumb
CFLAGS += -Os -g
CFLAGS += -Wall -Wextra -Wno-unused-parameter
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -nostdlib -nostartfiles

LDFLAGS = -T linker.ld
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -Wl,-Map=tamagotchi.map

SRCS = main.c
TARGET = tamagotchi

all: $(TARGET).bin

$(TARGET).elf: $(SRCS) n32g031_regs.h sprites.h linker.ld
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRCS)

$(TARGET).bin: $(TARGET).elf
	$(OBJCOPY) -O binary $< $@
	@echo ""
	@echo "=== Build complete ==="
	@echo "Binary: $(TARGET).bin"
	@$(SIZE) $<

size: $(TARGET).elf
	$(SIZE) $<

clean:
	rm -f $(TARGET).elf $(TARGET).bin $(TARGET).map

.PHONY: all clean size
