ZSNESFX=1
ASMCPU=1
#SPC700ASM=1
NETPLAY=1
UNZIP=1

ifdef ZSNESFX
FXOBJ=i386/zsnes.o
FXDEFINES=-DZSNES_FX -DEXECUTE_SUPERFX_PER_LINE
FXDEPENDS=zsnes_fx
FXNO_DEPENDS=c_fx
else
FXOBJ=fxinst.o fxemu.o fxdbg.o
FXDEFINES=-DEXECUTE_SUPERFX_PER_LINE
FXDEPENDS=c_fx
FXNO_DEPENDS=zsnes_fx
endif

LINUXDEFINES=-DUSE_DGA_EXTENSION -DUSE_VIDMODE_EXTENSION

ifdef ASMCPU
ifdef SPC700ASM
CPUOBJ=i386/cpuops.o i386/cpuexec.o i386/spc700.o
CPUDEFINES=
else
CPUOBJ=i386/cpuops.o i386/cpuexec.o spc700.o
CPUDEFINES=-DSPC700_C
endif
else
CPUOBJ=cpuops.o cpuexec.o spc700.o
CPUDEFINES=
endif

OBJECTS=$(CPUOBJ) cpu.o debug.o tile.o \
	gfx.o memmap.o snaporig.o clip.o dsp1.o \
	ppu.o dma.o snes9x.o snapshot.o apu.o soundux.o \
        cheats.o data.o globals.o unix/unix.o unix/config.o $(FXOBJ)

ifdef NETPLAY
OBJECTS += netplay.o
NETPLAYDEFINES=-DNETPLAY_SUPPORT
SERVER_OBJECTS=server.o
endif

ifdef UNZIP
OBJECTS += loadzip.o unzip/unzip.o
UNZIPDEFINES=-DUNZIP_SUPPORT
endif

CCC = gcc
CC = gcc
NASM = nasm

INCLUDES=-I/usr/X11R6/include -I/usr/local/include

OPTIMISE=-O6 -mpentium -fomit-frame-pointer -fno-exceptions -fno-rtii
#OPTIMISE=-g

CCFLAGS = $(OPTIMISE) -DMITSHM \
-fno-exceptions \
-Ii386 \
-I. \
-Iunzip \
-DJOYSTICK_SUPPORT \
-DZLIB \
-DNO_INLINE_SET_GET \
-DVAR_CYCLES \
-DDEBUGGER \
-DCPU_SHUTDOWN \
-DSPC700_SHUTDOWN \
$(LINUXDEFINES) \
$(FXDEFINES) \
$(CPUDEFINES) \
$(NETPLAYDEFINES) \
$(UNZIPDEFINES)

#-DEXECUTE_SUPERFX_PER_LINE
#-DSOUND
#-DDEBUGGER
#-DNO_INLINE_SET_GET
#-DVAR_CYCLES
#-DCPU_SHUTDOWN
#-DSPC700_SHUTDOWN
#-fomit-frame-pointer
CFLAGS=$(CCFLAGS)

.SUFFIXES: .o .cpp .c .cc .h .m .i .S .asm
LDLIBS = -L/usr/X11R6/lib

ifdef NETPLAY
all: offsets snes9x ssnes9x s9xserver
else
all: offsets snes9x ssnes9x
endif

#ggisnes9x
#xf86snes9x

$(FXDEPENDS):
	touch $(FXDEPENDS)
	$(RM) $(FXNO_DEPENDS)

offsets: offsets.o
	$(CCC) $(INCLUDES) -o $@ offsets.o
	./offsets >i386/offsets.h

snes9x: $(OBJECTS) unix/x11.o
	$(CCC) $(INCLUDES) -o $@ $(OBJECTS) unix/x11.o $(LDLIBS) -lXext -lX11 -lXxf86dga -lXxf86vm -lgz -lm

xf86snes9x: $(OBJECTS) unix/xf86.o
	$(CCC) $(INCLUDES) -o $@ $(OBJECTS) unix/xf86.o $(LDLIBS) -lXext -lX11 -lXxf86dga -lXxf86vm -lgz -lm
	/bin/chmod u+s xf86snes9x

ssnes9x: $(OBJECTS) unix/svga.o
	$(CCC) $(INCLUDES) -o $@ $(OBJECTS) unix/svga.o $(LDLIBS) -lvga -lvgagl -lgz -lm

gsnes9x: $(OBJECTS) unix/ggi.o
	$(CCC) $(INCLUDES) -o $@ $(OBJECTS) unix/ggi.o $(LDLIBS) -lggi -lgz -lm

s9xserver: $(SERVER_OBJECTS)
	$(CCC) $(INCLUDES) -o $@ $(SERVER_OBJECTS) 

.cpp.o:
	$(CCC) $(INCLUDES) -c $(CCFLAGS) $*.cpp -o $@

.c.o:
	$(CC) $(INCLUDES) -c $(CCFLAGS) $*.c -o $@

.cpp.S:
	$(CCC) $(INCLUDES) -S $(CCFLAGS) $*.cpp -o $@

.cpp.i:
	$(CCC) $(INCLUDES) -E $(CCFLAGS) $*.cpp -o $@

.S.o:
	$(CCC) $(INCLUDES) -c $(CCFLAGS) $*.S -o $@

.S.i:
	$(CCC) $(INCLUDES) -c -E $(CCFLAGS) $*.S -o $@

.asm.o:
	$(NASM) -f elf -i . -i i386 -o $@ $*.asm

clean:
	rm -f $(OBJECTS) offsets.o unix/svga.o unix/x11.o unix/ggi.o unix/xf86.o

#release: CCFLAGS += -DNO_DEBUGGER

release: clean all

ppu.o: $(FXDEPENDS)
cpu.o: $(FXDEPENDS)
memmap.o: $(FXDEPENDS)
globals.o: $(FXDEPENDS)
i386/cpuexec.o: $(FXDEPENDS)
snapshot.o: $(FXDEPENDS)
snaporig.o: $(FXDEPENDS)

i386/cpuexec.o: i386/asmstruc.h i386/offsets.h
i386/cpuops.o: i386/asmstruc.h i386/asmops.h i386/getset.S i386/asmaddr.h i386/offsets.h
i386/spc700.o: i386/asmstruc.h i386/spcops.h i386/offsets.h
cpuexec.o: cpuexec.h cpuops.h snes9x.h snapshot.h gfx.h \
	memmap.h ppu.h debug.h port.h display.h apu.h spc700.h apu.h
cpuops.o: cpuops.h memmap.h snes9x.h spc700.h apu.h \
	cpuaddr.h cpumacro.h debug.h port.h cpuexec.h
debug.o: cpuops.h cpuexec.h snes9x.h \
	memmap.h ppu.h debug.h missing.h port.h display.h apu.h
ppu.o: snes9x.h memmap.h ppu.h missing.h port.h cpuexec.h \
	apu.h spc700.h fxemu.h fxinst.h
dsp1.o: snes9x.h port.h dsp1.h
snapshot.o: snapshot.h memmap.h snes9x.h 65c816.h ppu.h \
	cpuexec.h display.h apu.h spc700.h
snes96.o: port.h snes9x.h memmap.h
memmap.o: cpuexec.h snes9x.h memmap.h ppu.h port.h cheats.h getset.h
unix.o: cpuexec.h snes9x.h port.h snapshot.h display.h apu.h gfx.h cheats.h
x11.o: display.h snes9x.h memmap.h debug.h ppu.h snapshot.h gfx.h
ggi.o: display.h snes9x.h memmap.h debug.h ppu.h snapshot.h gfx.h 
svga.o: display.h snes9x.h memmap.h debug.h ppu.h snapshot.h gfx.h soundux.h
gfx.o: memmap.h snes9x.h ppu.h gfx.h display.h port.h
tile.o: memmap.h snes9x.h ppu.h display.h gfx.h tile.h
spc700.o: spc700.h apu.h apumem.h snes9x.h memmap.h
apu.o: spc700.h apu.h apumem.h snes9x.h soundux.h
soundux.o: snes9x.h soundux.h apu.h
dma.o: ppu.h dma.h memmap.h getset.h snes9x.h port.h apu.h spc700.h
cheats.o: cheats.h snes9x.h port.h memmap.h
fxemu.o: fxemu.h fxinst.h
fxinst.o: fxemu.h fxinst.h
fxdbg.o: fxemu.h fxinst.h
offsets.o: port.h snes9x.h memmap.h ppu.h apu.h cpuexec.h 65c816.h
globals.o: memmap.h spc700.h apu.h cpuexec.h ppu.h cheats.h snes9x.h gfx.h \
	   missing.h dma.h dsp1.h
xf86.o: display.h snes9x.h memmap.h debug.h ppu.h snapshot.h gfx.h
server.o: snes9x.h port.h memmap.h netplay.h
netplay.o: snes9x.h port.h memmap.h netplay.h
