# adapt the following variables to your environment
SHBASE = /opt/dcgcc-4.7.3/sh-elf
KOS = /net/swamp/zeug/dc/kos-git/kos
KOS_PORTS = $(KOS)/../kos-ports

# use assembler screen update functions
ASSEMBLER = 1

# use "OCRAM" (fast cache RAM)
#OCRAM = 1

# include R: device emulation
R_DEVICE = 1

# include a romdisk in the executable
#ROMDISK = 1

# optimization settings
CFLAGS = -O2 -fomit-frame-pointer -ml -m4-single-only -ffunction-sections -fdata-sections -D_arch_dreamcast -D_arch_sub_pristine -W -Wall -g -fno-builtin -fno-strict-aliasing

LINKER_SCRIPT = $(KOS)/utils/ldscripts/shlelf.xc
DEFS = -DDREAMCAST

ifdef ASSEMBLER
DEFS += -DASSEMBLER_SCREENUPDATE
ADDOBJ += dc/displayscreen.o
endif
ifdef ROMDISK
DEFS += -DROMDISK
ADDOBJ += romdisk.o
endif
ifdef R_DEVICE
DEFS += -DR_IO_DEVICE -DR_SERIAL
ADDOBJ += rdevice.o
endif
ifdef OCRAM
DEFS += -DUSE_OCRAM
LINKER_SCRIPT = dc/shlelf-ocram.xc
endif

CC = $(SHBASE)/bin/sh-elf-gcc
SHOBJCOPY = $(SHBASE)/bin/sh-elf-objcopy
SHSTRIP   = $(SHBASE)/bin/sh-elf-strip

INCLUDE = -I$(KOS)/include -I$(KOS)/kernel/arch/dreamcast/include -I$(KOS)/addons/include -I$(KOS)/kernel/arch/dreamcast/sound -I$(KOS_PORTS)/include -I$(KOS_PORTS)/include/zlib -Idc -I.
LDFLAGS = -ml -m4-single-only -ffunction-sections -fdata-sections -g -fno-builtin -fno-strict-aliasing -Wl,-Ttext=0x8c010000 -Wl,--gc-sections -Wl,-Map,$(basename $@).map -T$(LINKER_SCRIPT) -nodefaultlibs -L$(KOS)/lib/dreamcast -L$(KOS_PORTS)/lib

LIBS = -Wl,--start-group -lz -lbz2 -lkallisti -lm -lc -lgcc -Wl,--end-group

OBJ =	artifact.o \
	atari.o \
	cpu.o \
	esc.o \
	cfg.o \
	emuos.o \
	sio.o \
	devices.o \
	antic.o \
	gtia.o \
	pokey.o \
	pia.o \
	cartridge.o \
	crc32.o \
	rtime.o \
	ui.o \
	ui_basic.o \
	afile.o \
	binload.o \
	log.o \
	compfile.o \
	memory.o \
	monitor.o \
	statesav.o \
	sysrom.o \
	colours.o \
	colours_pal.o \
	colours_ntsc.o \
	colours_external.o \
	mzpokeysnd.o \
	remez.o \
	pokeysnd.o \
	sndsave.o \
	cassette.o \
	img_tape.o \
	util.o \
	pbi.o \
	screen.o \
	dc/dc_chdir.o \
	dc/vmu.o \
	dc/atari_dc.o \
	input.o

OBJ += $(ADDOBJ)

all: atari800.elf atari800.bin

atari800.bin: atari800.elf
	$(SHOBJCOPY) -R .stack -R .comment -R .bss -R .sbss -O binary $^ $@

romdisk.o:
	$(KOS)/utils/genromfs/genromfs -f romdisk.img -d romimg -v
	KOS_ARCH=dreamcast KOS_AS=$(SHBASE)/bin/sh-elf-as KOS_OBJCOPY=$(SHBASE)/bin/sh-elf-objcopy KOS_LD=$(SHBASE)/bin/sh-elf-ld $(KOS)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o

%.o: %.S
	$(CC) -c -o $@ $(DEFS) -DASSEMBLER $(INCLUDE) $(CFLAGS) $<

%.o: %.c
	$(CC) -c -o $@ $(DEFS) $(INCLUDE) $(CFLAGS) $<

atari800.elf: $(OBJ)
	$(CC) -o $@ $(LDFLAGS) $(OBJ) $(LIBS)

dc/atari_dc.o: dc/atari_dc.c dc/config.h dc/version.h
	$(CC) -c -o $@ $(DEFS) $(INCLUDE) $(CFLAGS) $<

dep:
	@if ! makedepend -Y $(DEFS) $(INCLUDE) ${OBJ:.o=.c} 2>/dev/null; \
	then echo warning: makedepend failed; fi

clean:
	rm -f $(OBJ) atari800.map atari800.elf atari800.bin atari800.scrambled
	rm -f romdisk.img $(ADDOBJ)

# Local Variables: #
# mode: makefile #
# End: #
