RI5CY       = $(ROOTDIR)/riscv-target/ri5cy/device/rv32imc
LDSCRIPT    = $(RI5CY)/link.ld
TRAPHANDLER = $(RI5CY)/handler.S
DEFINES     = -DPRIV_MISA_S=0 -DPRIV_MISA_U=0 -DTRAPHANDLER="\"$(TRAPHANDLER)\""

TARGET_SIM   ?= vsim -work $(VSIM_WORK)
TARGET_FLAGS ?= $(RISCV_TARGET_FLAGS)

RUN_TARGET=\
    $(TARGET_SIM) $(TARGET_FLAGS) -c -quiet tb_top_vopt -do 'run -all; exit -f' \
        +firmware=$<.bin \
	+maxcycles=10000 \
	+signature=$(*).signature.output

RISCV_PREFIX   ?= riscv32-unknown-elf-
RISCV_GCC      ?= $(RISCV_PREFIX)gcc
RISCV_OBJDUMP  ?= $(RISCV_PREFIX)objdump
RISCV_OBJCOPY  ?= $(RISCV_PREFIX)objcopy
RISCV_NM       ?= $(RISCV_PREFIX)nm
RISCV_READELF  ?= $(RISCV_PREFIX)readelf
RISCV_GCC_OPTS ?= -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles $(RVTEST_DEFINES)

COMPILE_TARGET=\
	$$(RISCV_GCC) $(1) $$(RISCV_GCC_OPTS) \
		-I$(ROOTDIR)/riscv-test-env/ \
		-I$(ROOTDIR)/riscv-test-env/p/ \
		-I$(TARGETDIR)/$(RISCV_TARGET)/ \
		$(DEFINES) -T$(LDSCRIPT) $$(<) \
		-o $$@; \
    $$(RISCV_OBJDUMP) -D $$@ > $$@.objdump; \
    $$(RISCV_READELF) -a $$@ > $$@.readelf; \
    $$(RISCV_NM) $$@ > $$@.nm; \
    $$(RISCV_OBJCOPY) -O verilog $$@ $$@.bin;
