| #!/bin/bash |
| # |
| # Copyright 2023 Google LLC |
| # |
| # 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 |
| # |
| # https://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. |
| |
| # Run verilator interactive simulation on sw/device/examples/hello_world. |
| |
| function print_usage { |
| echo "Usage: run_verilator_hello_world.sh <verilator testbench> <rom binary> <flash binary> <otp binary> [gpio_test]" |
| } |
| |
| if [[ $1 == "--help" ]]; then |
| print_usage |
| exit 0 |
| fi |
| |
| if [[ $# -lt 4 ]]; then |
| print_usage |
| exit 1 |
| fi |
| |
| if [[ -z ${TEST_UNDECLARED_OUTPUTS_DIR} ]]; then |
| echo "The script only works under bazel test" |
| exit 1 |
| fi |
| |
| # This cycle count is tuned so we don't need to wait too long for the test to |
| # finish. |
| TEST_CYCLES=730000 |
| |
| GPIO_TEST=0 |
| |
| if [[ $# -eq 5 ]] && [[ $5 == "gpio_test" ]]; then |
| GPIO_TEST=1 |
| TEST_CYCLES=900000 |
| fi |
| |
| VCHIP_TB=$1 |
| ROM_BIN=$2 |
| FLASH_BIN=$3 |
| OTP_BIN=$4 |
| |
| if [[ ! -f $(realpath ${VCHIP_TB}) ]]; then |
| echo "Verilator testbench not found. Please make sure //hw:verilator is in data." |
| exit 1 |
| fi |
| |
| if [[ ! -f $(realpath ${ROM_BIN}) ]] || [[ ! -f $(realpath ${FLASH_BIN}) ]] || |
| [[ ! -f $(realpath ${OTP_BIN}) ]]; then |
| echo "Software binaries not found. Please make sure the targets are in data and args." |
| exit 1 |
| fi |
| |
| # Run verilator testbench in the background |
| ${VCHIP_TB} \ |
| "--meminit=rom,${ROM_BIN}" \ |
| "--meminit=flash,${FLASH_BIN}" \ |
| "--meminit=otp,${OTP_BIN}" -c "${TEST_CYCLES}" & |
| |
| VERILATOR_PID=$! |
| |
| if (( ${GPIO_TEST} == 0 )); then |
| # Pipe GPIO output to a log file |
| sleep 5 |
| cat gpio0-read | tee "${TEST_UNDECLARED_OUTPUTS_DIR}/gpio0_read.log" & |
| |
| # Wait for 300s to reach the end of the hello_world initialization. |
| # Note: The value is tuned for the CICD to run. For local failing tests, |
| # consider to reduce the wait or increase the TEST_CYCLES. |
| sleep 300 |
| echo 'led gpio test' > "${TEST_UNDECLARED_OUTPUTS_DIR}/uart0_device" |
| |
| tail --pid=${VERILATOR_PID} -f /dev/null |
| |
| # Check results in GPIO and UART logs. |
| cat "${TEST_UNDECLARED_OUTPUTS_DIR}/uart0.log" | grep -q "led gpio test" || exit 1 |
| |
| diff -q "${TEST_UNDECLARED_OUTPUTS_DIR}/gpio0_read.log" \ |
| "sw/device/examples/hello_world/hello_world_test_expected_gpio.txt" || exit 1 |
| else |
| # Wait for 260s to reach the end of the hello_world initialization. |
| # Note: This value may need to tuned by the machine performance. Need to |
| # reduce the wait if the verilator simulation is fast (or increase the |
| # TEST_CYCLES variable). |
| sleep 260 |
| echo 'h28 h29 h31' > gpio0-write |
| |
| tail --pid=${VERILATOR_PID} -f /dev/null |
| |
| # Check GPIO toggle result in the UART log |
| for switch in "9" "10" "11"; do |
| cat "${TEST_UNDECLARED_OUTPUTS_DIR}/uart0.log" | \ |
| grep -q "GPIO switch #${switch} changed to 1" || exit 1 |
| done |
| fi |