Merge "Use simple_sec for SMC tests"
diff --git a/hw/BUILD b/hw/BUILD
index ef96f16..ff9c76c 100644
--- a/hw/BUILD
+++ b/hw/BUILD
@@ -8,7 +8,6 @@
 
 package(default_visibility = ["//visibility:public"])
 
-
 # This configuration exposes fusesoc's "make_options" to enable parallel
 # compilation of the verilated model.
 string_list_flag(
@@ -41,6 +40,12 @@
     target = "sim",
 )
 
+filegroup(
+    name = "verilator_bin",
+    srcs = [":verilator_real"],
+    output_group = "binary",
+)
+
 # This is used in CI steps that do not want to run Verilator tests, and thus
 # do not want to accidentally build the Verilator model. This causes the
 # //hw:verilator target to not emit any files, which will break any tests that
@@ -67,7 +72,7 @@
     name = "verilator",
     actual = select({
         ":disable_verilator_build": ":verilator_stub",
-        "//conditions:default": ":verilator_real",
+        "//conditions:default": ":verilator_bin",
     }),
     tags = ["verilator"],
     visibility = ["//visibility:public"],
diff --git a/hw/dv/tools/dvsim/sim.mk b/hw/dv/tools/dvsim/sim.mk
index 2c809cc..cbac3af 100644
--- a/hw/dv/tools/dvsim/sim.mk
+++ b/hw/dv/tools/dvsim/sim.mk
@@ -126,32 +126,28 @@
 			fi; \
 			echo "Building with command: $${bazel_cmd} build $${bazel_opts} $${bazel_label}"; \
 			$${bazel_cmd} build $${bazel_opts} $${bazel_label}; \
-			if [[ $${build_repo} == "matcha" ]]; then \
-				find -L $$($${bazel_cmd} info output_path)/ \
-					-type f -name "$${bazel_target}*" | grep -v 'runfiles' | \
-					xargs -I % cp -f % $${run_dir}; \
-			else \
-				for dep in $$($${bazel_cmd} cquery \
-					$${bazel_cquery} \
-					--ui_event_filters=-info \
-					--noshow_progress \
-					--output=starlark); do \
-					if [[ $$dep != //hw* ]] && [[ $$dep != //util* ]] && [[ $$dep != //sw/host* ]]; then \
-						for artifact in $$($${bazel_cmd} cquery $${dep} \
-							--ui_event_filters=-info \
-							--noshow_progress \
-							--output=starlark \
-							--starlark:expr="\"\\n\".join([f.path for f in target.files.to_list()])"); do \
-							cp -f $${artifact} $${run_dir}/$$(basename $${artifact}); \
-							if [[ $$artifact == *.bin && \
+			for dep in $$($${bazel_cmd} cquery \
+				$${bazel_cquery} \
+				--ui_event_filters=-info \
+				--noshow_progress \
+				--output=starlark); do \
+				if [[ $$dep != //hw* ]] && [[ $$dep != //util* ]] && [[ $$dep != //sw/host* ]] && [[ $$dep != @lowrisc_opentitan//* ]]; then \
+					for artifact in $$($${bazel_cmd} cquery $${dep} \
+						--ui_event_filters=-info \
+						--noshow_progress \
+						--output=starlark \
+						--starlark:expr="\"\\n\".join([f.path for f in target.files.to_list()])"); do \
+						cp -f $${artifact} $${run_dir}/$$(basename $${artifact}); \
+						for ext in ".bin" ".64.scr.vmem" ".64.vmem" ".32.vmem"; do \
+							if [[ $${artifact} == *$${ext} && \
 								-f "$$(echo $${artifact} | cut -d. -f 1).elf" ]]; then \
 								cp -f "$$(echo $${artifact} | cut -d. -f 1).elf" \
-									$${run_dir}/$$(basename -s .bin $${artifact}).elf; \
+									$${run_dir}/$$(basename -s $${ext} $${artifact}).elf; \
 							fi; \
 						done; \
-					fi; \
-				done; \
-			fi; \
+					done; \
+				fi; \
+			done; \
 		fi; \
 	done;
 endif
diff --git a/hw/top_matcha/dv/chip_sim_cfg.hjson b/hw/top_matcha/dv/chip_sim_cfg.hjson
index b33513d..f32f306 100644
--- a/hw/top_matcha/dv/chip_sim_cfg.hjson
+++ b/hw/top_matcha/dv/chip_sim_cfg.hjson
@@ -1148,7 +1148,7 @@
     {
       name: chip_sw_alert_handler_escalation
       uvm_test_seq: chip_sw_alert_handler_escalation_vseq
-      sw_images: ["//sw/device/tests/sim_dv:alert_handler_escalation_test:1"]
+      sw_images: ["//sw/device/tests/sim_dv:alert_handler_escalation_test:1:matcha"]
       en_run_modes: ["sw_test_mode_test_rom"]
       // Disable scoreboard to avoid incorrect alert prediction from the alert_monitor. Due to the
       // cross-domain alert senders and receivers, the monitor from the chip level did not support
@@ -1158,7 +1158,7 @@
     {
       name: chip_sw_alert_handler_ping_timeout
       uvm_test_seq: chip_sw_base_vseq
-      sw_images: ["//sw/device/tests:alert_handler_ping_timeout_test:1"]
+      sw_images: ["//sw/device/tests:alert_handler_ping_timeout_test:1:matcha"]
       en_run_modes: ["sw_test_mode_test_rom"]
       // Disable scoreboard to avoid incorrect alert prediction from the alert_monitor. Due to the
       // cross-domain alert senders and receivers, the monitor from the chip level did not support
@@ -1874,6 +1874,8 @@
       tests: [
         "chip_rv_dm_ndm_reset_req",
         "chip_sw_aes_smoketest",
+        "chip_sw_alert_handler_escalation",
+        "chip_sw_alert_handler_ping_timeout",
         "chip_sw_alert_test",
         "chip_sw_aon_timer_smoketest",
         "chip_sw_clkmgr_smoketest",
@@ -1898,6 +1900,8 @@
         "chip_sw_rstmgr_sw_rst",
         "chip_sw_rv_plic_smoketest",
         "chip_sw_rv_timer_irq",
+        "chip_sw_smc_env_test",
+        "chip_sw_smc_isp_wrapper_tpg_64_64_test",
         "chip_sw_spi_device_tx_rx",
         "chip_sw_sram_ctrl_smoketest",
         "chip_sw_uart_smoketest",
@@ -1909,6 +1913,8 @@
       tests: [
         "chip_rv_dm_ndm_reset_req",
         "chip_sw_aes_smoketest",
+        "chip_sw_alert_handler_escalation",
+        "chip_sw_alert_handler_ping_timeout",
         "chip_sw_alert_test",
         "chip_sw_aon_timer_smoketest",
         "chip_sw_clkmgr_smoketest",
@@ -1933,6 +1939,8 @@
         "chip_sw_rstmgr_sw_rst",
         "chip_sw_rv_plic_smoketest",
         "chip_sw_rv_timer_irq",
+        "chip_sw_smc_env_test",
+        "chip_sw_smc_isp_wrapper_tpg_64_64_test",
         "chip_sw_spi_device_tx_rx",
         "chip_sw_sram_ctrl_smoketest",
         "chip_sw_uart_smoketest",
diff --git a/rules/matcha_test.bzl b/rules/matcha_test.bzl
index 5d791bf..f24991f 100644
--- a/rules/matcha_test.bzl
+++ b/rules/matcha_test.bzl
@@ -14,7 +14,12 @@
 
 """Rules to build matcha test targets"""
 
-load("//rules:matcha.bzl", "flash_binary", "opentitan_rom_binary")
+load(
+    "//rules:matcha.bzl",
+    "DV_CORE_TARGETS",
+    "flash_binary",
+    "opentitan_rom_binary",
+)
 
 def matcha_verilator_test(
         name,
@@ -56,7 +61,7 @@
     sh_test_tags = ["verilator"] + tags
     sh_test_runner = "@//util:run_chip_verilator_sim.sh"
     sh_test_args = [
-        "$(locations %s)" % (verilator_testbench),
+        "$(location %s)" % (verilator_testbench),
         "$(location %s)" % (rom_img),
         "$(location %s)" % (sec_verilator_binary),
         "$(location %s)" % (otp_img),
@@ -168,6 +173,8 @@
         signed = False,
         dv = None,
         test_binary = None,
+        per_device_deps = {"sim_dv": [DV_CORE_TARGETS.get("secure_core")]},
+        word_size = 64,
         **kwargs):
     """A helper macro for generating Matcha functional tests.
 
@@ -188,6 +195,9 @@
       signed: Whether to sign the test image. Unsigned by default.
       dv: DV test parameters.
       test_binary: Use the named binary as the test program rather than building one from srcs/deps.
+      per_device_deps: Target deps that are forwared to flash_binary. Default to build for secure
+                       core DV only.
+      word_size: The word size that is forwared to flash_binary. Default to 64 for secure core.
       **kwargs: Arguments to forward to `flash_binary`.
 
     This macro emits the following rules:
@@ -208,7 +218,8 @@
         flash_binary(
             name = test_binary,
             signed = signed,
-            word_size = 64,
+            per_device_deps = per_device_deps,
+            word_size = word_size,
             deps = deps,
             **kwargs
         )
diff --git a/sw/device/examples/hello_world/BUILD b/sw/device/examples/hello_world/BUILD
index 00ddb67..fa9fce8 100644
--- a/sw/device/examples/hello_world/BUILD
+++ b/sw/device/examples/hello_world/BUILD
@@ -42,3 +42,26 @@
         "@lowrisc_opentitan//sw/device/silicon_creator/lib:manifest_def",
     ],
 )
+
+# Use shell test to run the interactive verilator test on UART and GPIO
+sh_test(
+    name = "verilator_hello_world_test",
+    timeout = "long",
+    srcs = [":run_verilator_hello_world.sh"],
+    args = [
+        "$(location //hw:verilator)",
+        "$(location //sw/device/lib/testing/test_rom:test_rom_sim_verilator_scr_vmem)",
+        "$(location :hello_world_sim_verilator_vmem)",
+        "$(location //hw/top_matcha/data:otp_img_rma)",
+    ],
+    data = [
+        ":hello_world_sim_verilator_vmem",
+        ":hello_world_test_expected_gpio.txt",
+        "//hw:verilator",
+        "//hw/top_matcha/data:otp_img_rma",
+        "//sw/device/lib/testing/test_rom:test_rom_sim_verilator_scr_vmem",
+    ],
+    tags = [
+        "verilator",
+    ],
+)
diff --git a/sw/device/examples/hello_world/hello_world_test_expected_gpio.txt b/sw/device/examples/hello_world/hello_world_test_expected_gpio.txt
new file mode 100644
index 0000000..4617c93
--- /dev/null
+++ b/sw/device/examples/hello_world/hello_world_test_expected_gpio.txt
@@ -0,0 +1,16 @@
+XXXXXXXXXXXXXXXX1111111111111111
+XXXXXXXXXXXXXXXX0000000000000000
+XXXXXXXXXXXXXXXX0110110001101100
+XXXXXXXXXXXXXXXX0110010101100101
+XXXXXXXXXXXXXXXX0110010001100100
+XXXXXXXXXXXXXXXX0010000000100000
+XXXXXXXXXXXXXXXX0110011101100111
+XXXXXXXXXXXXXXXX0111000001110000
+XXXXXXXXXXXXXXXX0110100101101001
+XXXXXXXXXXXXXXXX0110111101101111
+XXXXXXXXXXXXXXXX0010000000100000
+XXXXXXXXXXXXXXXX0111010001110100
+XXXXXXXXXXXXXXXX0110010101100101
+XXXXXXXXXXXXXXXX0111001101110011
+XXXXXXXXXXXXXXXX0111010001110100
+XXXXXXXXXXXXXXXX0000101000001010
diff --git a/sw/device/examples/hello_world/run_verilator_hello_world.sh b/sw/device/examples/hello_world/run_verilator_hello_world.sh
new file mode 100755
index 0000000..932dbdd
--- /dev/null
+++ b/sw/device/examples/hello_world/run_verilator_hello_world.sh
@@ -0,0 +1,80 @@
+#!/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>"
+)
+
+if [[ $1 == "--help" ]]; then
+  print_usage
+  exit 0
+fi
+
+if [[ $# -ne 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
+
+
+TEST_CYCLES=650000
+
+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=$!
+
+# 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.
+sleep 300
+echo 'led gpio test' > "${TEST_UNDECLARED_OUTPUTS_DIR}/uart0_device"
+
+# Wait up to another 350s to complete the verilator simulation.
+timeout 350 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
diff --git a/sw/device/tests/BUILD b/sw/device/tests/BUILD
index 9343b18..e8e813d 100644
--- a/sw/device/tests/BUILD
+++ b/sw/device/tests/BUILD
@@ -607,6 +607,7 @@
         "verilator_supervisor_mode_test",
         "verilator_tlul_mailbox_test",
         "verilator_virtual_memory_test",
+        "//sw/device/examples/hello_world:verilator_hello_world_test",
         "//sw/device/tests/smc:verilator_kelvin_checksum_test",
         "//sw/device/tests/smc:verilator_kelvin_hello_test",
         "//sw/device/tests/smc:verilator_ml_top_irq_test",
@@ -739,6 +740,22 @@
     ],
 )
 
+matcha_dv_test(
+    name = "alert_handler_ping_timeout_test",
+    srcs = ["@lowrisc_opentitan//sw/device/tests:alert_handler_ping_timeout_test.c"],
+    deps = [
+        ":test_dv_lib_opentitan",
+        "//hw/top_matcha:alert_handler_regs",
+        "//sw/device/lib/dif:alert_handler",
+        "@lowrisc_opentitan//sw/device/lib/base:math",
+        "@lowrisc_opentitan//sw/device/lib/dif:rv_core_ibex",
+        "@lowrisc_opentitan//sw/device/lib/dif:rv_plic",
+        "@lowrisc_opentitan//sw/device/lib/testing:alert_handler_testutils",
+        "@lowrisc_opentitan//sw/device/lib/testing:isr_testutils",
+        "@lowrisc_opentitan//sw/device/lib/testing:rv_plic_testutils",
+    ],
+)
+
 # Smoke tests
 matcha_dv_test(
     name = "aes_smoketest",
diff --git a/sw/device/tests/sim_dv/BUILD b/sw/device/tests/sim_dv/BUILD
index 17d264a..fe999fc 100644
--- a/sw/device/tests/sim_dv/BUILD
+++ b/sw/device/tests/sim_dv/BUILD
@@ -92,3 +92,21 @@
         "@lowrisc_opentitan//sw/device/lib/testing/test_framework:status",
     ],
 )
+
+matcha_dv_test(
+    name = "alert_handler_escalation_test",
+    srcs = ["@lowrisc_opentitan//sw/device/tests/sim_dv:alert_handler_escalation.c"],
+    deps = [
+        "//hw/top_matcha:alert_handler_regs",
+        "//sw/device/lib/arch:device",
+        "//sw/device/lib/dif:alert_handler",
+        "//sw/device/lib/dif:clkmgr",
+        "//sw/device/lib/dif:rstmgr",
+        "//sw/device/tests:test_dv_lib_opentitan",
+        "@lowrisc_opentitan//sw/device/lib/dif:keymgr",
+        "@lowrisc_opentitan//sw/device/lib/dif:rv_core_ibex",
+        "@lowrisc_opentitan//sw/device/lib/testing:alert_handler_testutils",
+        "@lowrisc_opentitan//sw/device/lib/testing:keymgr_testutils",
+        "@lowrisc_opentitan//sw/device/lib/testing:rstmgr_testutils",
+    ],
+)
diff --git a/util/run_chip_verilator_sim.sh b/util/run_chip_verilator_sim.sh
index 6358763..7819142 100755
--- a/util/run_chip_verilator_sim.sh
+++ b/util/run_chip_verilator_sim.sh
@@ -21,12 +21,6 @@
   exit 0
 fi
 
-# In bazel build the verilator target has multiple outputs, so we need to trim
-# the non-executable one.
-while [[ $1 != *"Vchip_sim_tb" ]]; do
-  shift 1
-done
-
 VCHIP_TB=$1
 ROM_BIN=$2
 FLASH_BIN=$3