#!/bin/bash

# arg handling
for arg in "$@"; do
	# check args
	case ${arg} in
		-n|--no-gui)
			headless=true
			continue
			;;
		-a|--asm)
			asm=true
			continue
			;;
		-h|--help)
			help=true
			continue
			;;
		-c|--compile)
			recompile=true
			continue
	esac
done

[ ! -z $help ] && echo "usage: execute_test.sh [-h|--help] [-n|--no-gui] [-a|--asm]
  -h|--help:   show this message
  -n|--no-gui: don't open modelsim gui
  -a|--asm:    use asm (.S) files instead of c (.c)" && exit 0

target=sim_gui
language=c
language_file_ending=c
[ ! -z $headless ] && target=sim
[ ! -z $asm ] && language=asm && language_file_ending=S
[ ! -z $recompile ] && (pwd=$(pwd); cd ../software/$language/; make clean; make; cd $pwd)

# make
make compile

# search and select
select program in $(ls ../software/$language/ | grep -e "\.$language_file_ending" | sed -e "s/\.$language_file_ending$//g") exit; do 
   case $program in
      exit) echo "exiting"
            break ;;
         *) echo "running $program.$language_file_ending"; make $target IMEM=../software/$language/$program.imem.mif DMEM=../software/$language/$program.dmem.mif TIME=1ms;
   esac
done
