#########################################################################
# Apple II emulator by Alexander Jean-Claude Bottema (C) 1994		#
#									#
# Makefile for Linux apple2 emulator					#
#									#
# $Id: Makefile,v 1.18 1998/08/23 17:12:49 chernabog Exp $		#
#									#
# MODIFICATION HISTORY							#
#   v0.3 by Aaron Culliney <chernabog@baldmountain.bbn.com>, Jan 1997.	#
#   v0.4 by Aaron Culliney <chernabog@baldmountain.bbn.com>, Jun 1997.  #
#   v0.5 by Aaron Culliney <chernabog@baldmountain.bbn.com>, Feb 1998.	#
#   v0.6 by Aaron Culliney <chernabog@baldmountain.bbn.com>, Aug 1998.	#
#	This code has nothing to do with my employer, GTE		#
#	Internetworking, BBN Technologies.  It was written completely	#
#	on my own time and on my own machine.				#
#									#
# ASSUMPTIONS:								#
#	GNU gcc, GNU make, GNU install					#
#									#
# Edit the variables at the beginning of this file to			#
# customize the emulator.						#
#########################################################################

# EMULATION DELAY RANGE - bump this up if you've got a fast CPU.  100 is a
# good setting for 386/486/Pentium, 1000 is good for a really fast PII.
DELAY=100

# Apple2 debugger support.  You only need this if you want to inspect/step the
# virtual machine, hack/krack old Apple ][ programs, etc.
#DO_DEBUGGER=y

# Support 128k //e mode.  //e mode lets you run programs that need more RAM, a
# different video mode, and an augmented instruction set,  (MarbleMadness,
# Airheart, Prodos 2.0.3 come to mind).
DO_IIE=y

# PC joystick support (you must have the joystick 0.8.0 kernel module).  If
# you have a joystick and have installed and tested the joystick module, go
# ahead and compile this into the emulator.  It's a lot easier to play some
# old arcade games with a real joystick.
#DO_PCJOYSTICK=y

# Directories to install the executable and manpage.
BINDIR=/usr/local/bin
MANDIR=/usr/local/man/man6


default:	echoclean sapple2_320x200 xapple2_320x200 clean xapple2_640x400

echoclean:
		@echo ----------------
		@echo Making realclean
		@echo ----------------
		$(MAKE) realclean

sapple2_320x200:
		@echo ----------------------------------------------------------
		@echo Building apple2 emulator for svgalib at 320x200 resolution
		@echo ----------------------------------------------------------
		$(MAKE) sapple2
		$(MV) sapple2 sapple2_320x200

xapple2_320x200:
		@echo ----------------------------------------------------
		@echo Building apple2 emulator for X at 320x200 resolution
		@echo ----------------------------------------------------
		$(MAKE) xapple2
		$(MV) xapple2 xapple2_320x200

xapple2_640x400:
		@echo ----------------------------------------------------
		@echo Building apple2 emulator for X at 640x400 resolution
		@echo ----------------------------------------------------
		$(MAKE) CFLAGS="$(CFLAGS) -D_640x400" xapple2
		$(MV) xapple2 xapple2_640x400

all:		default
install:	_install
uninstall:	_uninstall


CC =		gcc
RM =		/bin/rm -f
MV =		/bin/mv
LN =		/bin/ln
LEX =		flex

CFLAGS =	-I/usr/include -I/usr/local/include -Wall -DMAX_APPLE_DELAY=$(DELAY)
LDFLAGS =
PROGS =		sapple2_320x200 xapple2_320x200 xapple2_640x400
INSTALL =	install
VGALIB =	-lvga
XLIBS =		-L/usr/X11R6/lib -lX11 -lXext

###############
# //e support #
###############
ifeq ($(DO_IIE),y)
	CFLAGS += -DAPPLE_IIE
endif

###################
# apple2 debugger #
###################
ifeq ($(DO_DEBUGGER),y)
	CFLAGS += -DDEBUGGER
	apple2.c += debugger.c debug.c
endif

###############
# pc joystick #
###############
ifeq ($(DO_PCJOYSTICK),y)
	CFLAGS += -DPC_JOYSTICK
	apple2.c += joystick.c
endif

####################################
# program debugging / optimization #
####################################
# optimization may cause problems with emulator...
CFLAGS += -O2
#CFLAGS += -g
#CFLAGS += -pg


#####################
# compilation rules #
#####################

LEX_SUFFIXES = .l .L .c
.SUFFIXES:	.c .S .o $(LEX_SUFFIXES)

%.o : %.c
		$(CC) $(CFLAGS) -c $*.c
%.o : %.S
		$(CC) $(CFLAGS) -c $*.S
%.c : %.l
		$(CC) $(CFLAGS) -x c -E -P $*.l > $*.L
		$(LEX) -i -P$* -o$*.c $*.L
%.c : %.L
		$(LEX) -i -P$* -o$*.c $*.L
%.L : %.l
		$(CC) $(CFLAGS) -x c -E -P $*.l > $*.L

##################
# apple2 program #
##################

# X graphics
xvideo.c =	xvideo.c
xvideo.o =	$(xvideo.c:.c=.o)

# svga graphics
svideo.c =	svideo.c
svideo.o =	$(svideo.c:.c=.o)

# core apple2 engine
apple2.man =	apple2.6
apple2.c +=	keys.c prefs.c disk.c interface.c misc.c
apple2.S =	apple2.S diskio.S
apple2.o =	$(apple2.c:.c=.o) $(apple2.S:.S=.o)

# vga apple2
sapple2:	$(apple2.o) $(svideo.o)
		$(CC) $(LDFLAGS) -o $@ $(apple2.o) $(svideo.o) $(VGALIB)

# X apple2
xapple2:	$(apple2.o) $(xvideo.o)
		$(CC) $(LDFLAGS) -o $@ $(apple2.o) $(xvideo.o) $(XLIBS)

#################################
# installation (need suid root) #
#################################
_install:
		$(INSTALL) --mode=4755 $(PROGS) $(BINDIR)
		$(INSTALL) --mode=0644 $(apple2.man) $(MANDIR)
		$(RM) $(BINDIR)/xapple2
		$(LN) -s $(BINDIR)/xapple2_640x400 $(BINDIR)/xapple2
		$(RM) $(BINDIR)/sapple2
		$(LN) -s $(BINDIR)/sapple2_320x200 $(BINDIR)/sapple2

_uninstall:
		$(RM) $(BINDIR)/xapple2
		$(RM) $(BINDIR)/xapple2_320x200
		$(RM) $(BINDIR)/xapple2_640x400
		$(RM) $(BINDIR)/sapple2
		$(RM) $(BINDIR)/sapple2_320x200
		$(RM) $(MANDIR)/$(apple2.man)


#################
# other targets #
#################
.PHONY:		all clean realclean

clean:
		$(RM) *.o *.L core sapple2 xapple2

realclean:	clean
		$(RM) $(PROGS) prefs.c debug.c *~ *.s *.E
