#
# SPIM S20 MIPS Simulator.
# Makefile for SPIM.
# Copyright (C) 1990 by James Larus (larus@cs.wisc.edu).
#
# SPIM is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 1, or (at your option) any
# later version.
#
# SPIM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU CC; see the file COPYING.  If not, write to James R.
# Larus, Computer Sciences Department, University of Wisconsin--Madison,
# 1210 West Dayton Street, Madison, WI 53706, USA or to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

#
# The following parameters may need to be set for the target machine:
#    TRAP_PATH - the path to the trap.handler file
#    TOP_DIR - where to find X stuff
#    MY_LEX - needed only if you want to change the scanner (scanner.l)
#

#
# To make spim, type:
#
#   make spim
#
# To make xpsim, type:
#
#   make xspim
#
# To verify spim works, type:
#
#   make test
#

RCSDIR=RCS

#
# Note: SPIM does not work when compiled by a compiler that does
# not distinguish floats from doubles (e.g., pcc and pcc-compatible compilers
# on the VAX).
#

# Full path for the trap handler file:
# You will want to change this.
# 
#TRAP_PATH = \"`pwd`/trap.handler\"
#TRAP_PATH = \"/u/scottk/cs354/spim/trap.handler\"
TRAP_PATH = \"./trap.handler\"


# You don't need lex or flex if you have not wish to modify scanner.l.
# If you have flex, use it instead of lex.  If you use flex, define this
# variable and set LEXFLAGS.
MYLEX = flex

# SPIM needs the -I flag to flex since the scanner is used interactively.
# You can set the -8 flag so that funny characters do not hang the scanner.
LEXFLAGS = -I -8 $(SKEL)


# If you use lex, set the variables this way:
#MYLEX = lex
#LEXFLAGS =


# Location of X11 root directory.
TOPDIR = /usr/X11R6


# Size of the segments when spim starts up (data segment must be >= 64K).
MEM_SIZES = -DTEXT_SIZE=65536 -DDATA_SIZE=131072 -DK_TEXT_SIZE=65536

# System dependant flags.  Anything else you may need.
#
# -DNOMEMIO disables memory mapped I/O feature.
# On some systems (mostly non-BSD) CBREAK, select,
# and usec time are not found, so -memio can't be used, yet.
#
# If you don't have CBREAK, you may want to use
# -DTERMIO, this could work on system V, but it may not.
#
#		scottk@cs.wisc.edu
#
SYS_FLAGS = -DNOMEMIO


#
# End of parameters
#


IFLAGS = -I/usr/X11R6/include/X11 -I/usr/X11R6/include
CFLAGS = -g $(IFLAGS) $(ENDIAN) $(MEM_SIZES) $(SYS_FLAGS)
YFLAGS = -d
YCFLAGS =
LDFLAGS =
XLDFLAGS = -L$(TOPDIR)/lib -lXaw -lXmu -lXt -lXext -lX11


# lex.yy.c is usually compiled with -O to speed it up.

LEXCFLAGS = -O



OBJS = spim-utils.o run.o mem.o inst.o data.o sym_tbl.o y.tab.o lex.yy.o memio.o


XOBJS = windows.o buttons.o


spim:	$(OBJS) spim.o
	$(CC) -g $(OBJS) spim.o $(LDFLAGS) -o spim -lm


xspim:	$(OBJS) $(XOBJS) xspim.o
	$(CC) -g $(OBJS) $(XOBJS) xspim.o $(LDFLAGS) $(XLDFLAGS) -o xspim -lm


spim.o:	spim.h inst.h sym_tbl.h reg.h mem.h y.tab.h memio.h

xspim.o: xspim.h spim.c spim.h reg.h mem.h y.tab.h windows.h buttons.h memio.h


spim-utils.o: spim.h inst.h mem.h reg.h y.tab.h data.h version.h \
		spim-utils.c memio.h
	$(CC) $(IFLAGS) $(CFLAGS) -DDEFAULT_TRAP_HANDLER=$(TRAP_PATH) -c spim-utils.c

run.o:	spim.h reg.h y.tab.h inst.h mem.h sym_tbl.h spim-syscall.h \
	mips-syscall.h memio.h endian.h

mem.o:	spim.h reg.h inst.h mem.h memio.h

inst.o: spim.h reg.h inst.h mem.h sym_tbl.h y.tab.h op.h data.h y.tab.h memio.h

data.o: spim.h mem.h reg.h sym_tbl.h data.h endian.h memio.h

sym_tbl.o: spim.h inst.h mem.h sym_tbl.h memio.h

memio.o: spim.h inst.h mem.h reg.h memio.h

y.tab.h: y.tab.c

y.tab.c: parser.y
	yacc $(YFLAGS) parser.y

y.tab.o: y.tab.c spim.h inst.h mem.h reg.h sym_tbl.h data.h endian.h memio.h
	$(CC) $(IFLAGS) $(CFLAGS) $(YCFLAGS) -c y.tab.c

lex.yy.c: scanner.l
	$(MYLEX) $(LEXFLAGS) scanner.l

lex.yy.o: lex.yy.c y.tab.h spim.h inst.h mem.h op.h memio.h
	$(CC) $(IFLAGS) $(LEXCFLAGS) -c lex.yy.c

windows.o:  xspim.h windows.h xspim.h

buttons.o:  spim.h buttons.h xspim.h inst.h mem.h reg.h memio.h


#
# Test spim with the torture test.
#

test:	spim
	/bin/rm -f Tests/tt.out

	@echo
	csh -c "./spim -quiet -notrap -file Tests/tt.MAL.s | grep -v Version >& Tests/tt.out"
	@echo The next command should not produce any output:
	diff Tests/tt.out Tests/tt.MAL.OK
	/bin/rm -f Tests/tt.out

	@echo
	csh -c "./spim -file Tests/tt.SAL.s | grep -v Version >& Tests/tt.out"
	@echo The next command should not produce any output:
	diff Tests/tt.out Tests/tt.SAL.OK
	/bin/rm -f Tests/tt.out

test-memio: spim
	@echo
	csh -c "./spim -notrap -quiet -memio -file Tests/tt.memio.s < /dev/null | grep -v Version >& Tests/tt.out"
	@echo The next command should not produce any output:
	diff Tests/tt.out Tests/tt.memio.OK
	/bin/rm -f Tests/tt.out

TAGS:	*.c *.h *.l *.y
	etags *.l *.y *.c *.h


clean:
	rm -f spim xspim *.o y.output core a.out TAGS spim.tar.* spim.aux spim.log spim.dvi spim.shar*


very-clean: clean
	rm -f y.tab.h y.tab.c lex.yy.c spim.tar* Documents/spim.ps


FILES = BLURB.4.4 README COPYING INSTALL RLOG Makefile *.c *.h *.y *.l \
	trap.handler trap.handler.reent Tests/tt.* Documents/*

RLOG: RCS/*
	rlog RCS/* > RLOG

spim.ps: Documents/spim.ps

Documents/spim.ps: Documents/spim.tex
	(cd Documents; latex spim.tex; latex spim.tex; dvips spim.dvi; \
	rm -f spim.aux spim.log spim.dvi)


spim.tar: spim.ps $(FILES)
	tar cvf spim.tar $(FILES) Documents/spim.ps Documents/spim.tex


spim.tar.Z: spim.tar
	rm -f spim.tar.Z
	compress -v spim.tar


shar:
	mv -f y.tab.c y.tab.c.xx
	mv -f lex.yy.c lex.yy.c.xx
	rm -f spim.shar spim.shar0?
	makekit -n spim.shar $(FILES)
	mv -f y.tab.c.xx y.tab.c
