Skip to content

Commit

Permalink
Enable using OpenOCD for interactive debugging of simulated Caliptra …
Browse files Browse the repository at this point in the history
…core (#72)

* Added third-party code for JTAG DPI and TCP server necessary for communication with OpenOCD

Signed-off-by: Maciej Kurc <[email protected]>

* Integrated the JTAG DPI interface with the testbench

Signed-off-by: Maciej Kurc <[email protected]>

* Added a dummy infinite_loop test to exercise interactive JTAG connections

Signed-off-by: Maciej Kurc <[email protected]>

* Added OpenOCD interface and target scripts

Signed-off-by: Maciej Kurc <[email protected]>

* Increased TCP buffer size

Signed-off-by: Maciej Kurc <[email protected]>

* Added debugging capability to JTAG DPI via optional ASCII art waveform display

Signed-off-by: Maciej Kurc <[email protected]>

* Enable debugging in Caliptra testbench

Signed-off-by: Maciej Kurc <[email protected]>

* Updated the infinite loop program, tweaked OpenOCD config.

Signed-off-by: Maciej Kurc <[email protected]>

* Interactive debugging test in CI

Signed-off-by: Maciej Kurc <[email protected]>

* Updated OpenOCD config and test script

Signed-off-by: Maciej Kurc <[email protected]>

* Updated Interactive test CI flow

Signed-off-by: Maciej Kurc <[email protected]>

* Added a test that dumps core registers and compares them with a golden reference

Signed-off-by: Maciej Kurc <[email protected]>

* Set OpenOCD memory access mode to abstract

Signed-off-by: Tomasz Michalak <[email protected]>

* jtagdpi: Fix typo in README

Signed-off-by: Tomasz Michalak <[email protected]>

* Added a delay to the GDB script to give the simulation time to react

Signed-off-by: Maciej Kurc <[email protected]>

* Added FST waveform dump capability to Verilated simulation

Signed-off-by: Maciej Kurc <[email protected]>

* Set RISC-V abstract access in OpenOCD target config as default, limited number of parallel jobs when building verilated simulation

Signed-off-by: Maciej Kurc <[email protected]>

* Added tests for memory and peripheral access via JTAG (to the CI as well)

Signed-off-by: Maciej Kurc <[email protected]>

* Tweaked JTAG simulation CI script

Signed-off-by: Maciej Kurc <[email protected]>

* Add possibility to control Caliptra debug lock at the makefile invocation level

Signed-off-by: Maciej Kurc <[email protected]>

* Unlocked debugging in interactive simulation CI

Signed-off-by: Maciej Kurc <[email protected]>

* Switched to OpenOCD fork in CI

Signed-off-by: Maciej Kurc <[email protected]>

* Adjusted the infinite_loop test to fit in the build system for the tests.

Signed-off-by: Maciej Kurc <[email protected]>

* Added a makefile option to enforce reset on CPU

Signed-off-by: Maciej Kurc <[email protected]>

* Added OpenOCD config files for the case when the CPU is held in reset

Signed-off-by: Maciej Kurc <[email protected]>

* Separated bash utility functions for interactive JTAG debugging tests

Signed-off-by: Maciej Kurc <[email protected]>

* Added OpenOCD without GDB test to the CI

Signed-off-by: Maciej Kurc <[email protected]>

* Removed leftover comments.

Signed-off-by: Maciej Kurc <[email protected]>

---------

Signed-off-by: Maciej Kurc <[email protected]>
Signed-off-by: Tomasz Michalak <[email protected]>
Co-authored-by: Tomasz Michalak <[email protected]>
  • Loading branch information
mkurc-ant and tmichalak committed Jun 26, 2023
1 parent 965a7b4 commit 90d0882
Show file tree
Hide file tree
Showing 33 changed files with 1,896 additions and 20 deletions.
87 changes: 87 additions & 0 deletions .github/scripts/gdb_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash
#
# This script runs Verilator RTL simulation and OpenOCD in background, invokes
# the supplied test command and shuts everything down.

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

set +e

if [ "$#" -lt 1 ]; then
echo "Usage: gdb_test.sh <command> [args ...]"
exit 1
fi

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

terminate_all () {
terminate ${OPENOCD_PID}
terminate ${SIM_PID}
}

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/Vcaliptra_top_tb >"${SIM_LOG}" 2>&1 &
SIM_PID=$!

# Wait
wait_for_phrase "${SIM_LOG}" "CLP: ROM Flow in progress..."
if [ $? -ne 0 ]; then
echo -e "${COLOR_RED}Failed to start the simulation!${COLOR_OFF}"
print_logs
terminate_all; exit -1
fi
echo -e "Simulation running and ready (pid=${SIM_PID})"

# Launch OpenOCD
echo -e "Launching OpenOCD..."
cd ${CALIPTRA_ROOT}/tools/scripts/openocd && openocd -d2 -f board/caliptra-verilator.cfg >"${OPENOCD_LOG}" 2>&1 &
OPENOCD_PID=$!

# Wait
wait_for_phrase "${OPENOCD_LOG}" "Listening on port 3333 for gdb connections"
if [ $? -ne 0 ]; then
echo -e "${COLOR_RED}Failed to start OpenOCD!${COLOR_OFF}"
print_logs
terminate_all; exit -1
fi
echo -e "OpenOCD running and ready (pid=${OPENOCD_PID})"

# Wait a bit
sleep 1s

# Run the test
echo -e "${COLOR_WHITE}======== Running test '$@' ========${COLOR_OFF}"

bash -c "$(printf ' %q' "$@")"
EXITCODE=$?

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

sleep 1s

# Terminate
echo -e "${COLOR_WHITE}Terminating...${COLOR_OFF}"
terminate_all

# Display logs
print_logs

# Honor the exitcode
exit ${EXITCODE}

68 changes: 68 additions & 0 deletions .github/scripts/openocd_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
#
# 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/Vcaliptra_top_tb >"${SIM_LOG}" 2>&1 &
SIM_PID=$!

# Wait
wait_for_phrase "${SIM_LOG}" "CLP: ROM Flow in progress..."
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 5s

# Run the test
echo -e "${COLOR_WHITE}======== Running OpenOCD test '$@' ========${COLOR_OFF}"
cd ${CALIPTRA_ROOT}/tools/scripts/openocd && 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

sleep 1s

# Terminate
echo -e "${COLOR_WHITE}Terminating...${COLOR_OFF}"
terminate ${SIM_PID}

# Display logs
print_logs

# Honor the exitcode
exit ${EXITCODE}

49 changes: 49 additions & 0 deletions .github/scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# 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
}
Loading

0 comments on commit 90d0882

Please sign in to comment.