#!/bin/bash
# Copyright (c) 2018 Princeton University
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Princeton University nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY PRINCETON UNIVERSITY "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL PRINCETON UNIVERSITY BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
echo $ASM_DIAG_NAME_NO_EXT
riscv32-unknown-elf-gcc -march=rv32ima -mabi=ilp32 -nostdlib -nostartfiles   \
                        -DTEST_FUNC_NAME=${ASM_DIAG_NAME_NO_EXT}             \
                        -DTEST_FUNC_TXT='"${ASM_DIAG_NAME_NO_EXT}"'          \
                        -DTEST_FUNC_RET=${ASM_DIAG_NAME_NO_EXT}_ret          \
                        -I${DV_ROOT}/verif/diag/assembly/include/riscv/pico  \
                        -T${DV_ROOT}/verif/diag/assembly/include/riscv/pico/link.ld diag.S -o diag.exe

# create mem.image
#riscv32-unknown-elf-objcopy --reverse-bytes 4 -I elf32-littleriscv -O binary diag.exe diag.o
riscv32-unknown-elf-objcopy -I elf32-littleriscv -O binary diag.exe diag.o
du diag.o -b | awk '{print(128 - ($1 % 128));}' | xargs -t -ISIZE truncate diag.o -s +SIZE
printf "\n@0000000040000000\t// Section '.RED_SEC', segment 'text'\n" >mem.image
#od -tx -v -An -w32 diag.o >>mem.image
xxd -g 16 -c 32 diag.o | awk '{printf("%s %s\n", $2, $3);}' >>mem.image

#create symbol.tbl
riscv32-unknown-elf-objdump -d diag.exe | grep "<pass>:" | awk '{printf("good_trap %s X %s\n", $1, $1);}' >symbol.tbl
riscv32-unknown-elf-objdump -d diag.exe | grep "<fail>:" | awk '{printf("bad_trap %s X %s\n", $1, $1);}' >>symbol.tbl

#create empty diag.ev
touch diag.ev
