# This now assumes GNU make or equivalent

MAKEFLAGS += -R -r

Z80ASM      = z80asm		# Get rid of these...
PERL        = perl

Z80_CPP     = gcc -E -x assembler-with-cpp
CROSS       = ../../../tools/gnu/bin/z80-none-elf-
Z80_AS      = $(CROSS)as
Z80_LD      = $(CROSS)ld
Z80_OBJCOPY = $(CROSS)objcopy
Z80_OBJDUMP = $(CROSS)objdump
Z80_NM      = $(CROSS)nm

INCLUDE = -I$(O) -I./abc80 -I.
SFLAGS  = $(INCLUDE) -undef -nostdinc -D__ASSEMBLER__ -D__Z80__
ASFLAGS = $(INCLUDE) -ahlsgm=$*.lst --no-pad-sections -g
LDFLAGS =

# Used for make clean
AUXRAM   = abc80/nvram_20k abc80/nvram_22k abc80/no_nvram
UFDDOS   = ../$(O)/ufddos80.rom
PRINT    = ../$(O)/print80_29.rom ../$(O)/print80_30.rom ../$(O)/print80_28.rom
ROMS     = $(UFDDOS) $(PRINT)
HDRS80   = $(O)/nvram.inc abc80/abc80.inc abc80/auxram.inc abc80/print80.inc
HDRS     = z80.inc $(HDRS80)

.SUFFIXES:

.SECONDARY:

.DELETE_ON_ERROR:

all:
	set -e; for r in $(AUXRAM); do $(MAKE) O=$$r all_O; done

all_O: $(ROMS)

# .asm rather than .S to avoid problems on monocase filesystems
$(O)/%.s: $(O)/../%.asm $(HDRS)
	$(Z80_CPP) $(SFLAGS) -o $@ $<

%.o: %.s
	$(Z80_AS) $(ASFLAGS) -o $@ $<

%.elf: %.o z80.ld z80ld.conf
	$(Z80_LD) @z80ld.conf -T z80.ld $(LDFLAGS) \
		-Map=$*.map --cref -o $@ $<

%.bin: %.elf
	$(Z80_OBJCOPY) -O binary $< $@

# Default rule
../$(O)/%.rom: $(O)/%.bin
	mkdir -p $(@D) && cp -f $< $@

clean:
	find . -type f \
		-name '*.o' -o -name '*.lst' -o -name '*.map' -o \
		-name '*.s' -o -name '*.elf' -o -name '*.bin' \
		| xargs rm -f

spotless: clean
	$(MAKE) O=*/* spotless_O

spotless_O:
	rm -f $(ROMS)
