Skip to content

Commit

Permalink
Merge pull request #211 from antmicro/rrozak/60812-openocd-tests
Browse files Browse the repository at this point in the history
Add OpenOCD test
  • Loading branch information
tmichalak authored Aug 2, 2024
2 parents 54bf88d + 4ba3e1a commit 5c9580f
Show file tree
Hide file tree
Showing 35 changed files with 6,090 additions and 101 deletions.
78 changes: 78 additions & 0 deletions .github/scripts/openocd_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024 Antmicro <www.antmicro.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script runs Verilator RTL simulation in background and invokes OpenOCD
# to perform JTAG access test

SIM_LOG=`realpath sim.log`
OPENOCD_LOG=`realpath openocd.log`

set +e

if [ "$#" -lt 1 ]; then
echo "Usage: openocd_test.sh [openocd args ...]"
exit 1
fi
OPENOCD_ARGS=$@

# Utils
source `dirname ${BASH_SOURCE[0]}`/utils.sh

print_logs () {
echo -e "${COLOR_WHITE}======== Simulation log ========${COLOR_OFF}"
cat ${SIM_LOG} || true
echo -e "${COLOR_WHITE}======== OpenOCD log ========${COLOR_OFF}"
cat ${OPENOCD_LOG} || true
}

echo -e "${COLOR_WHITE}======== Launching interactive simulation ========${COLOR_OFF}"

# Start the simulation
echo -e "Starting simulation..."
obj_dir/Vtb_top >"${SIM_LOG}" 2>&1 &
SIM_PID=$!

# Wait
wait_for_phrase "${SIM_LOG}" "VerilatorTB: Start of sim"
if [ $? -ne 0 ]; then
echo -e "${COLOR_RED}Failed to start the simulation!${COLOR_OFF}"
print_logs
terminate ${SIM_PID}; exit -1
fi
echo -e "Simulation running and ready (pid=${SIM_PID})"

# Wait a bit
sleep 2s

# Run the test
echo -e "${COLOR_WHITE}======== Running OpenOCD test '$@' ========${COLOR_OFF}"
cd ${RV_ROOT}/testbench/openocd_scripts && openocd -d2 ${OPENOCD_ARGS} >"${OPENOCD_LOG}" 2>&1
EXITCODE=$?

if [ ${EXITCODE} -eq 0 ]; then
echo -e "${COLOR_GREEN}[PASSED]${COLOR_OFF}"
else
echo -e "${COLOR_RED}[FAILED]${COLOR_OFF}"
fi

# Display logs
print_logs

wait $SIM_PID

# Honor the exitcode
exit ${EXITCODE}

64 changes: 64 additions & 0 deletions .github/scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024 Antmicro <www.antmicro.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Colors
COLOR_OFF='\033[0m'
COLOR_RED='\033[31m'
COLOR_GREEN='\033[32m'
COLOR_WHITE='\033[1;37m'

# Waits until the given phrase appears in a log file (actively written to)
# Usage: wait_for_phrase <log_file> <phrase>
wait_for_phrase () {

# Check if the log exists
sleep 1s
if ! [ -f "$1" ]; then
echo -e "${COLOR_RED}Log file '$1' not found!${COLOR_OFF}"
return -1
fi

# Wait for the phrase
DEADLINE=$((${EPOCHSECONDS} + 30))
while [ ${EPOCHSECONDS} -lt ${DEADLINE} ]
do
# Check for the phrase
grep "$2" "$1" >/dev/null
if [ $? -eq 0 ]; then
return 0
fi

# Sleep and retry
sleep 1s
done

# Timeout
return -1
}

# Terminates a process. First via SIGINT and if this doesn't work after 10s
# retries with SIGKILL
# Usage: terminate <pid>
terminate () {

local PID=$1

# Gently interrupt, wait some time and then kill
/bin/kill -s SIGINT ${PID} || true
sleep 10s
/bin/kill -s SIGKILL ${PID} || true
}
54 changes: 54 additions & 0 deletions .github/workflows/build-openocd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: OpenOCD Build

on:
workflow_call:

jobs:
openOCD:
name: Build OpenOCD
runs-on: ubuntu-latest
env:
# A custom fork is needed to allow bypassing core examination and accessing
# peripherals regardless of core state.
OPENOCD_REPO: https://github.com/antmicro/openocd
OPENOCD_VERSION: riscv-nohalt

steps:
- name: Setup Cache Metadata
id: cache_metadata
run: |
cache_date=$(date +"%Y_%m_%d")
cache_name=cache_openocd
echo "Cache date: "$cache_date
echo "Cache name: "$cache_name
echo "cache_date=$cache_date" >> "$GITHUB_ENV"
echo "cache_name=$cache_name" >> "$GITHUB_ENV"
- name: Setup cache
uses: actions/cache@v3
id: cache
timeout-minutes: 60
with:
path: |
/opt/openocd
/opt/openocd/.cache
key: ${{ env.cache_name }}_${{ env.cache_date }}
restore-keys: ${{ env.cache_name }}_

- name: Install prerequisities
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
run: |
sudo apt -qqy update && sudo apt -qqy --no-install-recommends install \
make libtool pkg-config autoconf automake texinfo
- name: Build and install OpenOCD
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
run: |
git clone -b "${OPENOCD_VERSION}" "${OPENOCD_REPO}"
pushd openocd
./bootstrap
./configure --prefix=/opt/openocd --enable-remote-bitbang \
CFLAGS="-Wno-error=misleading-indentation -Wno-error=stringop-overflow"
make -j`nproc`
sudo make install
popd
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
name: Build-Spike
uses: ./.github/workflows/build-spike.yml

Build-OpenOCD:
name: Build-OpenOCD
uses: ./.github/workflows/build-openocd.yml

Test-Regression:
name: Test-Regression
needs: [Build-Verilator]
Expand Down Expand Up @@ -50,9 +54,14 @@ jobs:
name: Test-Renode
uses: ./.github/workflows/test-renode.yml

Test-OpenOCD:
name: Test-OpenOCD
needs: [Build-Verilator, Build-OpenOCD]
uses: ./.github/workflows/test-openocd.yml

Report-Coverage:
name: Report-Coverage
needs: [Test-Regression, Test-Verification, Test-Microarchitectural, Test-RISCV-DV, Test-RISCOF]
needs: [Test-Regression, Test-Verification, Test-Microarchitectural, Test-RISCV-DV, Test-RISCOF, Test-OpenOCD]
uses: ./.github/workflows/report-coverage.yml

Build-Docs:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/report-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ jobs:
name: riscof_coverage_data
path: ./

- name: Download coverage reports
uses: actions/download-artifact@v3
with:
name: openocd_coverage_data
path: ./

- name: Generate reports
run: |
export PATH=${{ env.LCOV_PATH }}:${PATH}
Expand Down
101 changes: 101 additions & 0 deletions .github/workflows/test-openocd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Test-OpenOCD

on:
workflow_call:

jobs:

tests:
name: Run OpenOCD tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
coverage: ["all", "branch", "toggle"] #TODO: add functional coverage
bus: ["axi4", "ahb_lite"]
env:
DEBIAN_FRONTEND: "noninteractive"
CCACHE_DIR: "/opt/openocd-tests/.cache/"
VERILATOR_VERSION: v5.010

steps:
- name: Install utils
run: |
sudo apt -qqy update && sudo apt -qqy --no-install-recommends install \
cpanminus ccache ninja-build gcc-riscv64-unknown-elf
pip3 install meson
sudo cpanm Bit::Vector
- name: Setup Cache Metadata
id: cache_metadata
run: |
date=$(date +"%Y_%m_%d")
time=$(date +"%Y%m%d_%H%M%S_%N")
cache_verilator_restore_key=cache_verilator_
cache_verilator_key=${cache_verilator_restore_key}${{ env.VERILATOR_VERSION }}
cache_openocd_restore_key=cache_openocd_
cache_openocd_key=${cache_openocd_restore_key}
cache_test_restore_key=${{ matrix.coverage }}_
cache_test_key=${cache_test_restore_key}${time}
echo "date=$date" | tee -a "$GITHUB_ENV"
echo "time=$time" | tee -a "$GITHUB_ENV"
echo "cache_verilator_restore_key=$cache_verilator_restore_key" | tee -a "$GITHUB_ENV"
echo "cache_verilator_key=$cache_verilator_key" | tee -a "$GITHUB_ENV"
echo "cache_openocd_restore_key=$cache_openocd_restore_key" | tee -a "$GITHUB_ENV"
echo "cache_openocd_key=$cache_openocd_key" | tee -a "$GITHUB_ENV"
echo "cache_test_restore_key=$cache_test_restore_key" | tee -a "$GITHUB_ENV"
echo "cache_test_key=$cache_test_key" | tee -a "$GITHUB_ENV"
- name: Restore Verilator cache
id: cache-verilator-restore
uses: actions/cache/restore@v3
with:
path: |
/opt/verilator
/opt/verilator/.cache
key: ${{ env.cache_verilator_key }}
restore-keys: ${{ env.cache_verilator_restore_key }}

- name: Restore OpenOCD cache
id: cache-openocd-restore
uses: actions/cache/restore@v3
with:
path: |
/opt/openocd
/opt/openocd/.cache
key: ${{ env.cache_openocd_key }}
restore-keys: ${{ env.cache_openocd_restore_key }}

- name: Setup repository
uses: actions/checkout@v3
with:
submodules: recursive

- name: Build verilated simulation
run: |
export PATH=/opt/verilator/bin:/opt/openocd/bin:$PATH
export RV_ROOT=$(pwd)
mkdir run
make -C run -f ${RV_ROOT}/tools/Makefile verilator-build program.hex TEST=infinite_loop \
CONF_PARAMS="-set build_${{ matrix.bus }} -set openocd_test" COVERAGE=${{ matrix.coverage }}
cd run
${RV_ROOT}/.github/scripts/openocd_test.sh \
-f ${RV_ROOT}/testbench/openocd_scripts/verilator-rst.cfg \
-f ${RV_ROOT}/testbench/openocd_scripts/jtag_cg.tcl
- name: Prepare coverage data
run: |
export PATH=/opt/verilator/bin:$PATH
export RV_ROOT=$(pwd)
.github/scripts/convert_coverage_data.sh ${RV_ROOT}/run
echo "convert_coverage_data.sh exited with RET_CODE = "$?
mkdir -p results
mv ${RV_ROOT}/run/coverage.info \
results/coverage_openocd_${{ matrix.bus }}_${{ matrix.coverage }}.info
- name: Pack artifacts
if: always()
uses: actions/upload-artifact@v3
with:
name: openocd_coverage_data
path: results/*.info
4 changes: 4 additions & 0 deletions configs/veer.config
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ my $fast_interrupt_redirect = 1; # ON by default
my $lsu_num_nbload=4;
my $ahb = 0;
my $axi = 1;
my $openocd_test = 0;
my $text_in_iccm = 0;

my $lsu2dma = 0;
Expand Down Expand Up @@ -1045,6 +1046,7 @@ our %config = (#{{{
"build_ahb_lite" => "$ahb",
"build_axi4" => "$axi",
"build_axi_native" => "1",
"openocd_test" => "$openocd_test",
"ext_datawidth" => "64",
"ext_addrwidth" => "32",
"sterr_rollback" => "0",
Expand Down Expand Up @@ -1929,6 +1931,8 @@ if (defined($config{"testbench"}{"build_axi_native"}) && ($config{"testbench"}{"

delete $config{core}{fpga_optimize} if ($config{core}{fpga_optimize} == 0);

delete $config{testbench}{openocd_test} if ($config{testbench}{openocd_test} == 0);

# Remove TECH_SPECIFIC_* defines if they are set to 0
foreach my $key (sort keys(%config)) {
if (grep(/tech_specific_/, $key)) {
Expand Down
Loading

0 comments on commit 5c9580f

Please sign in to comment.