# -*- Mode: makefile -*-
#
# ARM SoftMMU tests - included from tests/tcg/Makefile
#

ARM_SRC=$(SRC_PATH)/tests/tcg/arm/system

# Set search path for all sources
VPATH 		+= $(ARM_SRC)

# Specific Test Rules

test-armv6m-undef: test-armv6m-undef.S
	$(CC) -mcpu=cortex-m0 -mfloat-abi=soft \
		-Wl,--build-id=none -x assembler-with-cpp \
		$< -o $@ -nostdlib -N -static \
		-T $(ARM_SRC)/$@.ld

run-test-armv6m-undef: QEMU_OPTS+=-semihosting -M microbit -kernel

ARM_TESTS+=test-armv6m-undef

# These objects provide the basic boot code and helper functions for all tests
CRT_OBJS=boot.o

ARM_TEST_SRCS=$(wildcard $(ARM_SRC)/*.c)
ARM_TESTS+=$(patsubst $(ARM_SRC)/%.c, %, $(ARM_TEST_SRCS))

CRT_PATH=$(ARM_SRC)
LINK_SCRIPT=$(ARM_SRC)/kernel.ld
LDFLAGS=-Wl,-T$(LINK_SCRIPT)
CFLAGS+=-nostdlib -ggdb -O0 $(MINILIB_INC)
LDFLAGS+=-static -nostdlib -N $(CRT_OBJS) $(MINILIB_OBJS) -lgcc

# building head blobs
.PRECIOUS: $(CRT_OBJS)

%.o: $(ARM_SRC)/%.S
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -x assembler-with-cpp -c $< -o $@

# Build and link the tests
%: %.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS)
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS)

memory: CFLAGS+=-DCHECK_UNALIGNED=0

# Running
QEMU_BASE_MACHINE=-M virt -cpu max -display none
QEMU_OPTS+=$(QEMU_BASE_MACHINE) -semihosting-config enable=on,target=native,chardev=output -kernel

# console test is manual only
QEMU_SEMIHOST=-serial none -chardev stdio,mux=on,id=stdio0 -semihosting-config enable=on,chardev=stdio0 -mon chardev=stdio0,mode=readline
run-semiconsole: QEMU_OPTS=$(QEMU_BASE_MACHINE) $(QEMU_SEMIHOST)  -kernel
run-semiconsole: semiconsole
	$(call skip-test, $<, "MANUAL ONLY")
	$(if $(V),@printf "  %-7s %s %s\n" "TO RUN" $(notdir $(QEMU)) "$(QEMU_OPTS) $<")
run-plugin-semiconsole-with-%: semiconsole
	$(call skip-test, $<, "MANUAL ONLY")

# Simple Record/Replay Test
.PHONY: memory-record
run-memory-record: memory-record memory
	$(call run-test, $<, \
	  $(QEMU) -monitor none -display none \
		  -chardev file$(COMMA)path=$<.out$(COMMA)id=output \
		  -icount shift=5$(COMMA)rr=record$(COMMA)rrfile=record.bin \
		  $(QEMU_OPTS) memory)

.PHONY: memory-replay
run-memory-replay: memory-replay run-memory-record
	$(call run-test, $<, \
	  $(QEMU) -monitor none -display none \
		  -chardev file$(COMMA)path=$<.out$(COMMA)id=output \
		  -icount shift=5$(COMMA)rr=replay$(COMMA)rrfile=record.bin \
		  $(QEMU_OPTS) memory)

EXTRA_RUNS+=run-memory-replay

TESTS += $(ARM_TESTS) $(MULTIARCH_TESTS)
EXTRA_RUNS+=$(MULTIARCH_RUNS)