#!/bin/bash
tests="tests/* dg/*"
test -n "$1" && tests="$@"
export OPT816_QUIET=1
for i in $tests
do
	echo -n "$i: "
	if ! ../816-tcc -I../include -DSTACK_SIZE=0x2000 -o ./suite.asm -c $i ; then
		echo NOCOMPILE
		continue
	fi
	if test "$OPTIMIZE" = "1" ; then
	        mv suite.asm suite.asm.pre
	        if ! python ../816-opt.py suite.asm.pre >suite.asm ; then
	        	echo NOOPT
	        	continue
	        fi
	        #if ! ./optimore-816r2/optimore-816 suite.asm suite2.asm >/dev/null ; then
	        #	echo NOOPT2
	        #	continue
	        #else
	        #	mv suite2.asm suite.asm
	        #fi
	fi
        if ! ../../wla_dx/wla-65816 -io ./suite.asm ./suite.obj ; then
        	echo NOASM
        	mkdir -p failtraces
        	mv suite.asm failtraces/${i#*/}.asm
        	continue
        fi
	if ! ../../wla_dx/wlalink/wlalink -Sno ../../libs/crt0_snes.obj ../../libs/libm.obj ../../libs/libtcc.obj ../../libs/libc.obj	suite.obj ./test.smc ; then
		echo NOLINK
		mkdir -p failtraces
		mv suite.asm failtraces/${i#*/}.asm
		continue
	fi
	if ../../bsnes/bsnes ./test.smc 2>/dev/null >/dev/null ; then
		echo PASS
		rm -f failtraces/${i#*/}.{asm,trace}
	else
		echo "FAIL (exit code $?)"
		mkdir -p failtraces
		mv suite.asm failtraces/${i#*/}.asm
		mv suite.asm.pre failtraces/${i#*/}.asm.pre
	fi
	xset r on
done
test -z "$NOCLEAN" && rm -f suite.asm .sym suite.obj test.smc
