FROM wallysoc/ubuntu_wally

# SET ENVIRONMENT VARIABLES
# assume 4 threads are available to speed up
ARG NUM_THREADS=4
ENV RISCV=/opt/riscv
ENV PATH="$PATH:${RISCV}/bin"
ENV MANPATH="$MANPATH:${RISCV}/share/man"

# this is required by podman
USER root
WORKDIR /opt/riscv

# TOOLCHAIN
RUN git clone https://github.com/riscv/riscv-gnu-toolchain && \
    cd riscv-gnu-toolchain && \
    sed -i 's/https/git/' .gitmodules && git submodule sync && \
    ./configure --prefix=${RISCV} --enable-multilib \
        --with-multilib-generator="rv32e-ilp32e--;rv32i-ilp32--;rv32im-ilp32--;rv32iac-ilp32--;rv32imac-ilp32--;rv32imafc-ilp32f--;rv32imafdc-ilp32d--;rv64i-lp64--;rv64ic-lp64--;rv64iac-lp64--;rv64imac-lp64--;rv64imafdc-lp64d--;rv64im-lp64--;" && \
    make --jobs ${NUM_THREADS} && \
    make install && cd ${RISCV} && \
    rm -rf ${RISCV}/riscv-gnu-toolchain

# elf2hex
RUN git clone https://github.com/sifive/elf2hex.git && \
    cd elf2hex && \
    autoreconf -i && \
    ./configure --target=riscv64-unknown-elf --prefix=${RISCV} && \
    make && \
    make install && cd ${RISCV} && \
    rm -rf ${RISCV}/elf2hex

# QEMU
RUN git clone --recursive https://github.com/qemu/qemu && \
    cd qemu && \
    ./configure --target-list=riscv64-softmmu --prefix=${RISCV}  && \
    make --jobs ${NUM_THREADS} && \
    make install && cd ${RISCV} && \
    rm -rf ${RISCV}/qemu

# Spike
RUN git clone https://github.com/riscv-software-src/riscv-isa-sim && \
    mkdir riscv-isa-sim/build && \
    cd riscv-isa-sim/build && \
    ../configure --prefix=$RISCV --enable-commitlog && \
    make --jobs ${NUM_THREADS} && \
    make install && cd ${RISCV} && \
    rm -rf ${RISCV}/riscv-isa-sim

# SAIL
RUN opam init -y --disable-sandboxing && \
    opam switch create 5.1.0 && \
    opam install sail -y && \
    eval $(opam config env) && \
    git clone https://github.com/riscv/sail-riscv.git && \
    cd sail-riscv && \
    ARCH=RV32 make c_emulator/riscv_sim_RV32 && \
    ARCH=RV64 make c_emulator/riscv_sim_RV64 && \
    cp ${RISCV}/sail-riscv/c_emulator/riscv_sim_RV64 ${RISCV}/bin/riscv_sim_RV64 && \
    cp ${RISCV}/sail-riscv/c_emulator/riscv_sim_RV32 ${RISCV}/bin/riscv_sim_RV32 && \
    rm -rf ${RISCV}/sail-riscv

COPY ./buildroot-config-src /opt/riscv/buildroot-config-src
COPY ./testvector-generation /opt/riscv/testvector-generation

# # Buildroot
RUN git clone https://github.com/buildroot/buildroot.git && \
    cd buildroot && \
    git checkout 2021.05 && \
    cp -r /opt/riscv/buildroot-config-src ./board/wally && \
    cp ./board/wally/main.config .config && \
    make --jobs ${NUM_THREADS} && \
    # generate files for buildroot regression
    mkdir -p ${RISCV}/linux-testvectors && \
    cd /opt/riscv/testvector-generation && \
    curl https://raw.githubusercontent.com/openhwgroup/cvw/main/linux/devicetree/wally-virt.dts --output ${RISCV}/buildroot/output/images/wally-virt.dts && \
    dtc -I dts -O dtb ${RISCV}/buildroot/output/images/wally-virt.dts > ${RISCV}/buildroot/output/images/wally-virt.dtb && \
    make && ./genInitMem.sh && \
    chmod -R a+rw ${RISCV}/linux-testvectors && \
    rm -rf ${RISCV}/buildroot

RUN pip3 install --no-cache-dir \
    testresources riscv_config \
    git+https://github.com/riscv/riscof.git

# Wally needs Verilator 5.021 or later.
# Verilator needs to be built from scratch to get the latest version
# apt-get install verilator installs version 4.028 as of 6/8/23
RUN apt-get install -y \
    perl g++ ccache help2man libgoogle-perftools-dev numactl perl-doc zlib1g \
    libfl2  libfl-dev && \
    apt-get clean
RUN git clone https://github.com/verilator/verilator && \
    cd verilator && \
    git pull && \
    git checkout v5.022 && \
    autoconf && \
    ./configure --prefix=${RISCV} && \
    make -j ${NUM_THREADS} && \
    make install && \
    cd ${RISCV} && rm -rf ${RISCV}/verilator

USER ${USERNAME}
WORKDIR /home/${USERNAME}/cvw