set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(V850_GCC_DIR "D:/gcc-v850-elf-windows/bin")

set(CMAKE_C_COMPILER ${V850_GCC_DIR}/v850-elf-gcc.exe)
set(CMAKE_OBJCOPY ${V850_GCC_DIR}/v850-elf-objcopy.exe)
set(CMAKE_SIZE ${V850_GCC_DIR}/v850-elf-size.exe)

# -mlong-calls:
# Treat all calls as being far away (near). If calls are assumed to be far away, the
# compiler always loads the function’s address into a register, and calls indirect
# through the pointer.
#
# see `gcc.pdf` `V850 Options` for details.
#
# JARL
# [Instruction format]
# (1) JARL disp22, reg2
# (2) JARL disp32, reg1
# (3) JARL [reg1], reg3
#
# c code:
# dummy();
#
# asm code generated by gcc without `-mlong-calls` is 22-bit addressing:
# jarl _dummy, lp
#
# asm code generated by gcc with `-mlong-calls` is 32-bit addressing:
# mov hilo(_dummy), r10
# jarl [r10], lp
#
# Therefore, if the address of the function dummy is larger than 0x3fffff (e.g. placed in ram),
# the `-mlong-calls` option must be added.
set(CMAKE_C_FLAGS "-mv850e3v5 -msoft-float -fdata-sections -ffunction-sections -gdwarf-2 -mlong-calls")
set(CMAKE_ASM_FLAGS "-mv850e3v5 -msoft-float -fdata-sections -ffunction-sections -gdwarf-2 -mlong-calls")

set(LD_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/sample/GCC/r7f701013.ld)

set(CMAKE_EXE_LINKER_FLAGS "-nostartfiles -Wl,-T${LD_SCRIPT},-Map,${PROJECT_NAME}.map,--gc-sections")

set(FREERTOS_TOOLCHAIN "GCC")
