ifeq ($(OS),Windows_NT)
    ARCH := Windows64
else
    ARCH := Linux64
endif

TARGET_SIM   ?= riscvOVPsim.exe
TARGET_FLAGS ?= $(RISCV_TARGET_FLAGS)
ifeq ($(shell command -v $(TARGET_SIM) 2> /dev/null),)
    $(error Target simulator executable '$(TARGET_SIM)` not found)
endif

COVERTYPE ?= basic

RUN_TARGET=\
    $(TARGET_SIM) $(TARGET_FLAGS) \
        --variant RV64I --program $(<) \
        --signaturedump --customcontrol \
        --cover ${COVERTYPE} \
        --outputfile $(*).${COVERTYPE}.coverage.yaml \
        --extensions RVZifencei \
        --override riscvOVPsim/cpu/sigdump/SignatureFile=$(*).signature.output \
				--override riscvOVPsim/cpu/sigdump/SignatureGranularity=4 \
        --override riscvOVPsim/cpu/sigdump/ResultReg=3 \
        --override riscvOVPsim/cpu/simulateexceptions=T \
        --override riscvOVPsim/cpu/defaultsemihost=F \
        --logfile $(@) \
        --override riscvOVPsim/cpu/user_version=2.3 \
        --override riscvOVPsim/cpu/priv_version=1.11 $(REDIR1); 

RISCV_PREFIX   ?= riscv32-unknown-elf-
RISCV_GCC      ?= $(RISCV_PREFIX)gcc
RISCV_OBJDUMP  ?= $(RISCV_PREFIX)objdump
RISCV_GCC_OPTS ?= -g -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles $(RVTEST_DEFINES)

COMPILE_CMD = $$(RISCV_GCC) $(1) $$(RISCV_GCC_OPTS) \
							-I$(ROOTDIR)/riscv-test-suite/env/ \
							-I$(TARGETDIR)/$(RISCV_TARGET)/ \
							-T$(TARGETDIR)/$(RISCV_TARGET)/link.ld \
							$$(<) -o $$@ 
OBJ_CMD = $$(RISCV_OBJDUMP) $$@ -D > $$@.objdump; \
					$$(RISCV_OBJDUMP) $$@ --source > $$@.debug


COMPILE_TARGET=\
				$(COMPILE_CMD); \
        if [ $$$$? -ne 0 ] ; \
                then \
                echo "\e[31m$$(RISCV_GCC) failed for target $$(@) \e[39m" ; \
                exit 1 ; \
                fi ; \
				$(OBJ_CMD); \
        if [ $$$$? -ne 0 ] ; \
                then \
                echo "\e[31m $$(RISCV_OBJDUMP) failed for target $$(@) \e[39m" ; \
                exit 1 ; \
                fi ;
