#! /bin/bash

CUR_DIR=$(pwd)

usage() {
    echo "usage: ./build.sh <cpu_id 0~3 or all>"
}

CPU1_SRAM_BASE=0xfff88000
CPU1_SRAM_SIZE=0x00008000

CPU2_SRAM_BASE=0xfff90000
CPU2_SRAM_SIZE=0x00010000

CPU3_SRAM_BASE=0xfffa0000
CPU3_SRAM_SIZE=0x00010000

CPU0_SRAM_BASE=0xfffb0000
CPU0_SRAM_SIZE=0x00010000

CPU1_MEM_BASE=0x00800000
CPU1_MEM_SIZE=0x00a00000

CPU2_MEM_BASE=0x01200000
CPU2_MEM_SIZE=0x00a00000

CPU3_MEM_BASE=0x01c00000
CPU3_MEM_SIZE=0x00a00000

# Example of memory resource partitioning for Linux(CPU0/1/2) + HAL(CPU3)
# CPU3_MEM_BASE=0x02e00000
# CPU3_MEM_SIZE=0x00100000

CPU0_MEM_BASE=0x02600000
CPU0_MEM_SIZE=0x00900000

SHMEM_BASE=0x02f00000
SHMEM_SIZE=0x00100000

SHRPMSG_SIZE=0x00080000
SHRAMFS_SIZE=0x00020000
SHLOG0_SIZE=0x00001000
SHLOG1_SIZE=0x00001000
SHLOG2_SIZE=0x00001000
SHLOG3_SIZE=0x00001000

make_hal() {
    export FIRMWARE_CPU_BASE=$(eval echo \$CPU$1_MEM_BASE)
    export DRAM_SIZE=$(eval echo \$CPU$1_MEM_SIZE)
    export SRAM_BASE=$(eval echo \$CPU$1_SRAM_BASE)
    export SRAM_SIZE=$(eval echo \$CPU$1_SRAM_SIZE)
    export SHMEM_BASE=$SHMEM_BASE
    export SHMEM_SIZE=$SHMEM_SIZE
    export SHRPMSG_SIZE=$SHRPMSG_SIZE
    export SHRAMFS_SIZE=$SHRAMFS_SIZE
    export SHLOG0_SIZE=$SHLOG0_SIZE
    export SHLOG1_SIZE=$SHLOG1_SIZE
    export SHLOG2_SIZE=$SHLOG2_SIZE
    export SHLOG3_SIZE=$SHLOG3_SIZE
    export CUR_CPU=$1
    make clean
    rm $CUR_DIR/hal$1.elf $CUR_DIR/hal$1.bin
    make -j8
    cp $CUR_DIR/TestDemo.elf $CUR_DIR/hal$1.elf
    mv $CUR_DIR/TestDemo.bin $CUR_DIR/hal$1.bin
}

case $1 in
    0|1|2|3) make_hal $1 ;;
    all) make_hal 0; make_hal 1; make_hal 2; make_hal 3 ;;
    *) usage; exit ;;
esac
