SoC编译HEX脚本(基于RISC-V的SoC)
脚本使用
./compile hello
脚本:设置RISC-V工具链riscv_set_env
############## RISC-V ##############
setenv RISCV_PATH /mnt/Software/FreedomStudio/SiFive/riscv64-unknown-elf-gcc-8.1.0-2018.12.0-x86_64-linux-ubuntu14/
set path = ($RISCV_PATH/bin $path)
set RISCV_ARCH = rv32imac
set RISCV_ABI = ilp32
set RISCV_GCC = ${RISCV_PATH}/bin/riscv64-unknown-elf-gcc
set RISCV_GXX = ${RISCV_PATH}/bin/riscv64-unknown-elf-g++
set RISCV_OBJDUMP = ${RISCV_PATH}/bin/riscv64-unknown-elf-objdump
set RISCV_GDB = ${RISCV_PATH}/bin/riscv64-unknown-elf-gdb
set RISCV_AR = ${RISCV_PATH}/bin/riscv64-unknown-elf-ar
set RISCV_ELF2HEX = /usr/local/bin/riscv64-unknown-elf-elf2hex
脚本:编译过程compile
#!/bin/tcsh -f
# usage : ./compile hello
# please set toolchain dir
# Directory structure
# .
# ├── compile
# ├── riscv_set_env
# ├── driver
# │ ├── bits.h
# │ ├── const.h
# │ ├── coreplexip-arty.h
# │ ├── encoding.h
# │ ├── env
# │ │ └── start.S
# │ ├── init.c
# │ ├── one_tim.lds
# │ ├── platform.h
# │ ├── sim_show.c
# │ ├── sim_show.h
# │ └── tim_split.lds
# └── testcase
# └── hello
# ├── obj
# └── src
# └── demo_print.c
source ./riscv_set_env
set INFO = "###(info) ${0}"
set ERROR = "###(error) ${0}"
######################
# Configuration
######################
# Default target
if ($#argv == 0) then
set PROGRAM = hello
else
set PROGRAM = $argv[1]
endif
set BOOT_MODE = "SYSTEM_PORT_BOOT"
set DRV_BASE = "driver"
set PROGRAM_DIR = testcase/${PROGRAM}
set ENV_DIR = ${DRV_BASE}/env
######################
# file
######################
set SRC_DIR = ${PROGRAM_DIR}/src
set OBJ_DIR = ${PROGRAM_DIR}/obj
# Exit if no .S, .s. or .c files found
if (! -d $SRC_DIR) then
echo "${ERROR}: $SRC_DIR: No such file or directory"
exit 0
endif
set ASM_FILES = `find ${SRC_DIR} -name "*.S" -print`
set C_FILES = `find ${SRC_DIR} -name "*.c" -print`
set HEX_FILES = `find ${SRC_DIR} -name "*.hex" -print`
set ASM_FILES = "${ASM_FILES} ${ENV_DIR}/start.S"
set C_FILES = "${C_FILES} ${DRV_BASE}/init.c ${DRV_BASE}/sim_show.c"
if(("${ASM_FILES}" == "") && ("${C_FILES}" == "" && ("${HEX_FILES}" == ""))) then
echo "ERROR: No test files or dirctory found"
exit 0
else
echo "${INFO}: Test files found are"
echo ${ASM_FILES}
echo ${C_FILES}
echo ${HEX_FILES}
endif
# Create the work dir if it doesn't already exist
if (! -d $OBJ_DIR) then
echo "${INFO}: Creating $OBJ_DIR"
mkdir -p $OBJ_DIR
else
echo "${INFO}: Delecting files in $OBJ_DIR"
rm -rf ${OBJ_DIR}/*
endif
if ($BOOT_MODE == "SYSTEM_PORT_BOOT") then
echo ""
echo "*************************"
echo "****SYSTEM PORT BOOT*****"
set LINK_TARGET = "one_tim"
echo ""
else
echo ""
echo "*************************"
set LINK_TARGET = "flash"
echo ""
endif
# comile parameter
set LINK_SCRPT = ${DRV_BASE}/${LINK_TARGET}.lds
set INCLUDES = "-I${DRV_BASE} -I${ENV_DIR}"
echo ${INCLUDES}
set LINK_FILES = "${ASM_FILES} ${C_FILES}"
set CFLASS = "-o0 -g -march=${RISCV_ARCH} -mabi=${RISCV_ABI} -mcmodel=medany ${INCLUDES}"
set LDFLASS = "-march=${RISCV_ARCH} -mabi=${RISCV_ABI} -T ${LINK_SCRPT} -nostartfiles -L{ENV_DIR} --specs=nano.specs ${INCLUDES}"
######################
# Compilation
######################
# if .hex found, only copy he first .hex to simulate
if ("${HEX_FILES}" != "") then
echo "${INFO}: already exist hex file of ${HEX_FILES}"
foreach i ($HEX_FILES)
echo "${INFO}: run simulation with $i"
cp $i ./riscv_rom.hex
exit 0
end
endif
# compile C code
if ("${C_FILES}" != "") then
foreach i (${C_FILES})
set base_name = "$i:t"
set base_name = "$base_name:r"
set obj_name = ${OBJ_DIR}/"${base_name}".o
echo "${INFO} Compile $i"
${RISCV_GCC} ${CFLASS} -c -Wa,-adlhn $i -o $obj_name > ${OBJ_DIR}/"${base_name}.s"
if ( $status ) exit 1
end
endif
# compile asm code
if ("${ASM_FILES}" != "") then
foreach i (${ASM_FILES})
set base_name = "$i:t"
set base_name = "$base_name:r"
set obj_name = ${OBJ_DIR}/"${base_name}".o
echo "${INFO} Compile $i"
${RISCV_GCC} ${CFLASS} -c -o $obj_name $i
if ( $status ) exit 1
end
endif
# link library and test object file
set OBJ_FILES = `find ${OBJ_DIR} -name "*.o" -print`
set PROGRAM_ELF = ${OBJ_DIR}/${PROGRAM}.elf
set PROGRAM_HEX = ${OBJ_DIR}/${PROGRAM}.hex
echo "${INFO}: Link library and object file, generate file of ${PROGRAM_ELF}"
${RISCV_GCC} ${LDFLASS} ${OBJ_FILES} --output ${PROGRAM_ELF}
echo "${INFO}: Convert elf file to hex of ${PROGRAM_HEX}"
${RISCV_ELF2HEX} --bit-width 32 --input ${PROGRAM_ELF} --output ${PROGRAM_HEX}
cp ${PROGRAM_HEX} riscv_rom.hex
${RISCV_OBJDUMP} -D ${PROGRAM_ELF} > riscv_rom.asm
## 附:RISC-V 工具链
[1].Prebuilt RISC‑V GCC Toolchain
[2].elf2hex