Merge "Use simple_sec for SMC tests"
diff --git a/sw/device/tests/BUILD b/sw/device/tests/BUILD
index 1f7375c..e8e813d 100644
--- a/sw/device/tests/BUILD
+++ b/sw/device/tests/BUILD
@@ -8,6 +8,10 @@
 
 package(default_visibility = ["//visibility:public"])
 
+sec_fpga_device = {
+    "fpga_nexus": [NEXUS_CORE_TARGETS.get("secure_core")],
+}
+
 # TODO: We could put all matcha related tests into a separate directory.
 sec_flash_binary(
     name = "smc_ctrl_test",
@@ -41,13 +45,18 @@
 sec_flash_binary(
     name = "i2s_rx_test",
     srcs = [
-        "i2s_rx_test.c",
+        "simple_sec.c",
         "//sw/device/tests/smc:smc_i2s_rx_test_fpga_nexus_bin_c.h",
     ],
     copts = [
         "-nostdlib",
         "-ffreestanding",
     ],
+    defines = [
+        "I2S_RX_TEST",
+        "HAS_SMC_BIN",
+    ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:i2s",
@@ -60,13 +69,18 @@
 sec_flash_binary(
     name = "i2s_tx_test",
     srcs = [
-        "i2s_tx_test.c",
+        "simple_sec.c",
         "//sw/device/tests/smc:smc_i2s_tx_test_fpga_nexus_bin_c.h",
     ],
     copts = [
         "-nostdlib",
         "-ffreestanding",
     ],
+    defines = [
+        "I2S_TX_TEST",
+        "HAS_SMC_BIN",
+    ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:i2s",
@@ -169,13 +183,18 @@
 sec_flash_binary(
     name = "supervisor_mode_test",
     srcs = [
-        "supervisor_mode_test.c",
+        "simple_sec.c",
         "//sw/device/tests/smc:smc_supervisor_mode_test_fpga_nexus_bin_c.h",
     ],
     copts = [
         "-nostdlib",
         "-ffreestanding",
     ],
+    defines = [
+        "SUPERVISOR_MODE_TEST",
+        "HAS_SMC_BIN",
+    ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -185,13 +204,18 @@
 sec_flash_binary(
     name = "virtual_memory_test",
     srcs = [
-        "virtual_memory_test.c",
+        "simple_sec.c",
         "//sw/device/tests/smc:smc_virtual_memory_test_fpga_nexus_bin_c.h",
     ],
     copts = [
         "-nostdlib",
         "-ffreestanding",
     ],
+    defines = [
+        "VIRTUAL_MEMORY_TEST",
+        "HAS_SMC_BIN",
+    ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -212,6 +236,7 @@
         "HAS_SMC_BIN",
         "PAGE_BOUNDARY_TEST",
     ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -232,6 +257,7 @@
         "HAS_SMC_BIN",
         "LSU_PAGE_BOUNDARY_TEST",
     ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -252,6 +278,7 @@
         "HAS_SMC_BIN",
         "INTERRUPT_BOUNDARY_TEST",
     ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -272,6 +299,7 @@
         "HAS_SMC_BIN",
         "LSU_INTERRUPT_BOUNDARY_TEST",
     ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -298,16 +326,18 @@
 sec_flash_binary(
     name = "cam_i2c_test",
     srcs = [
-        "cam_i2c_test.c",
+        "simple_sec.c",
         "//sw/device/tests/smc:smc_cam_i2c_test_fpga_nexus_bin_c.h",
     ],
     copts = [
         "-nostdlib",
         "-ffreestanding",
     ],
-    per_device_deps = {
-        "fpga_nexus": [NEXUS_CORE_TARGETS.get("secure_core")],
-    },
+    defines = [
+        "CAM_I2C_TEST",
+        "HAS_SMC_BIN",
+    ],
+    per_device_deps = sec_fpga_device,
     deps = [
         ":test_lib",
         "//sw/device/lib/dif:smc_ctrl",
@@ -539,14 +569,14 @@
 matcha_verilator_test(
     name = "verilator_i2s_rx_test",
     timeout = "eternal",
-    sec_flash_binary = ":i2s_rx_test",
+    sec_flash_binary = ":simple_sec",
     smc_flash_binary = "//sw/device/tests/smc:smc_i2s_rx_test",
 )
 
 matcha_verilator_test(
     name = "verilator_i2s_tx_test",
     timeout = "eternal",
-    sec_flash_binary = ":i2s_tx_test",
+    sec_flash_binary = ":simple_sec",
     smc_flash_binary = "//sw/device/tests/smc:smc_i2s_tx_test",
 )
 
diff --git a/sw/device/tests/cam_i2c_test.c b/sw/device/tests/cam_i2c_test.c
deleted file mode 100644
index 2628e94..0000000
--- a/sw/device/tests/cam_i2c_test.c
+++ /dev/null
@@ -1,36 +0,0 @@
-// Copyright 2023 Google LLC.
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "hw/top_matcha/sw/autogen/top_matcha.h"
-#include "sw/device/lib/arch/device.h"
-#include "sw/device/lib/dif/dif_smc_ctrl.h"
-#include "sw/device/lib/dif/dif_uart.h"
-#include "sw/device/lib/runtime/print.h"
-#include "sw/device/lib/testing/test_framework/check.h"
-#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
-#include "sw/device/lib/testing/test_framework/test_util.h"
-#include "sw/device/tests/smc/smc_cam_i2c_test_fpga_nexus_bin_c.h"
-
-OTTF_DEFINE_TEST_CONFIG();
-
-static dif_smc_ctrl_t smc_ctrl;
-static dif_uart_t uart;
-
-void _ottf_main(void) {
-  // Initialize the UART to enable logging for non-DV simulation platforms.
-  if (kDeviceType != kDeviceSimDV) {
-    init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart);
-  }
-  LOG_INFO("Start cam_i2c_test");
-  test_status_set(kTestStatusInTest);
-  // Copy embedded binary to SMC RAM.
-  memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
-  CHECK_DIF_OK(dif_smc_ctrl_init(
-      mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl));
-  CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl));
-  while (true) {
-    asm volatile("wfi");
-  }
-}
diff --git a/sw/device/tests/i2s_rx_test.c b/sw/device/tests/i2s_rx_test.c
deleted file mode 100644
index 26b6eb2..0000000
--- a/sw/device/tests/i2s_rx_test.c
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "hw/top_matcha/sw/autogen/top_matcha.h"
-#include "sw/device/lib/arch/device.h"
-#include "sw/device/lib/dif/dif_smc_ctrl.h"
-#include "sw/device/lib/dif/dif_uart.h"
-#include "sw/device/lib/runtime/hart.h"
-#include "sw/device/lib/runtime/log.h"
-#include "sw/device/lib/runtime/print.h"
-#include "sw/device/lib/testing/test_framework/check.h"
-#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
-#include "sw/device/lib/testing/test_framework/status.h"
-#include "sw/device/lib/testing/test_framework/test_util.h"
-#include "sw/device/tests/smc/smc_i2s_rx_test_fpga_nexus_bin_c.h"
-
-OTTF_DEFINE_TEST_CONFIG();
-
-static dif_uart_t uart;
-static dif_smc_ctrl_t smc_ctrl;
-
-void _ottf_main(void) {
-  // Initialize the UART to enable logging for non-DV simulation platforms.
-  if (kDeviceType != kDeviceSimDV) {
-    init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart);
-  }
-
-  // Start testing
-  test_status_set(kTestStatusInTest);
-
-  // Copy embedded binary to SMC RAM.
-  if (kDeviceType == kDeviceFpgaNexus) {
-    memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
-  }
-
-  CHECK_DIF_OK(dif_smc_ctrl_init(
-      mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl));
-  CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl));
-
-  while (true) {
-    asm volatile("wfi");
-  }
-}
diff --git a/sw/device/tests/i2s_tx_test.c b/sw/device/tests/i2s_tx_test.c
deleted file mode 100644
index b60a4eb..0000000
--- a/sw/device/tests/i2s_tx_test.c
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "hw/top_matcha/sw/autogen/top_matcha.h"
-#include "sw/device/lib/arch/device.h"
-#include "sw/device/lib/dif/dif_smc_ctrl.h"
-#include "sw/device/lib/dif/dif_uart.h"
-#include "sw/device/lib/runtime/hart.h"
-#include "sw/device/lib/runtime/log.h"
-#include "sw/device/lib/runtime/print.h"
-#include "sw/device/lib/testing/test_framework/check.h"
-#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
-#include "sw/device/lib/testing/test_framework/status.h"
-#include "sw/device/lib/testing/test_framework/test_util.h"
-#include "sw/device/tests/smc/smc_i2s_tx_test_fpga_nexus_bin_c.h"
-
-OTTF_DEFINE_TEST_CONFIG();
-
-static dif_uart_t uart;
-static dif_smc_ctrl_t smc_ctrl;
-
-void _ottf_main(void) {
-  // Initialize the UART to enable logging for non-DV simulation platforms.
-  if (kDeviceType != kDeviceSimDV) {
-    init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart);
-  }
-
-  // Start testing
-  test_status_set(kTestStatusInTest);
-
-  // Copy embedded binary to SMC RAM.
-  if (kDeviceType == kDeviceFpgaNexus) {
-    memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
-  }
-
-  CHECK_DIF_OK(dif_smc_ctrl_init(
-      mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl));
-  CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl));
-
-  while (true) {
-    asm volatile("wfi");
-  }
-}
diff --git a/sw/device/tests/simple_sec.c b/sw/device/tests/simple_sec.c
index 41d7dd1..c39cab4 100644
--- a/sw/device/tests/simple_sec.c
+++ b/sw/device/tests/simple_sec.c
@@ -8,9 +8,11 @@
 #include "hw/top_matcha/sw/autogen/top_matcha.h"
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/dif/dif_smc_ctrl.h"
+#include "sw/device/lib/dif/dif_uart.h"
 #include "sw/device/lib/runtime/hart.h"
 #include "sw/device/lib/testing/test_framework/check.h"
 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
+#include "sw/device/lib/testing/test_framework/test_util.h"
 
 #if defined(PAGE_BOUNDARY_TEST)
 #include "sw/device/tests/smc/smc_page_boundary_test_fpga_nexus_bin_c.h"
@@ -20,13 +22,28 @@
 #include "sw/device/tests/smc/smc_lsu_interrupt_boundary_test_fpga_nexus_bin_c.h"
 #elif defined(LSU_PAGE_BOUNDARY_TEST)
 #include "sw/device/tests/smc/smc_lsu_page_boundary_test_fpga_nexus_bin_c.h"
+#elif defined(CAM_I2C_TEST)
+#include "sw/device/tests/smc/smc_cam_i2c_test_fpga_nexus_bin_c.h"
+#elif defined(I2S_RX_TEST)
+#include "sw/device/tests/smc/smc_i2s_rx_test_fpga_nexus_bin_c.h"
+#elif defined(I2S_TX_TEST)
+#include "sw/device/tests/smc/smc_i2s_tx_test_fpga_nexus_bin_c.h"
+#elif defined(SUPERVISOR_MODE_TEST)
+#include "sw/device/tests/smc/smc_supervisor_mode_test_fpga_nexus_bin_c.h"
+#elif defined(VIRTUAL_MEMORY_TEST)
+#include "sw/device/tests/smc/smc_virtual_memory_test_fpga_nexus_bin_c.h"
 #endif
 
 OTTF_DEFINE_TEST_CONFIG();
 
 static dif_smc_ctrl_t smc_ctrl;
+static dif_uart_t uart;
 
 void _ottf_main(void) {
+  // Initialize the UART to enable logging for non-DV simulation platforms.
+  if (kDeviceType != kDeviceSimDV) {
+    init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart);
+  }
 #if defined(HAS_SMC_BIN)
   if (kDeviceType == kDeviceFpgaNexus) {
     memcpy((void*)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
@@ -39,5 +56,7 @@
 
   // Note: the SMC must end the simulation through testing or otherwise, as the
   // secure core indefinitely sleeps.
-  asm volatile("wfi");
+  while (true) {
+    asm volatile("wfi");
+  }
 }
diff --git a/sw/device/tests/supervisor_mode_test.c b/sw/device/tests/supervisor_mode_test.c
deleted file mode 100644
index 0f6632d..0000000
--- a/sw/device/tests/supervisor_mode_test.c
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright 2023 Google LLC.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "hw/top_matcha/sw/autogen/top_matcha.h"
-#include "sw/device/lib/arch/device.h"
-#include "sw/device/lib/dif/dif_smc_ctrl.h"
-#include "sw/device/lib/dif/dif_uart.h"
-#include "sw/device/lib/runtime/print.h"
-#include "sw/device/lib/testing/test_framework/check.h"
-#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
-#include "sw/device/lib/testing/test_framework/test_util.h"
-#include "sw/device/tests/smc/smc_supervisor_mode_test_fpga_nexus_bin_c.h"
-
-OTTF_DEFINE_TEST_CONFIG();
-
-static dif_smc_ctrl_t smc_ctrl;
-static dif_uart_t uart;
-
-void _ottf_main(void) {
-  // Initialize the UART to enable logging for non-DV simulation platforms.
-  if (kDeviceType != kDeviceSimDV) {
-    init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart);
-  }
-  LOG_INFO("Start supervisor_mode_test");
-  test_status_set(kTestStatusInTest);
-  // Copy embedded binary to SMC RAM.
-  if (kDeviceType == kDeviceFpgaNexus) {
-    memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
-  }
-  CHECK_DIF_OK(dif_smc_ctrl_init(
-      mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl));
-  CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl));
-  while (true) {
-    asm volatile("wfi");
-  }
-}
diff --git a/sw/device/tests/virtual_memory_test.c b/sw/device/tests/virtual_memory_test.c
deleted file mode 100644
index da99288..0000000
--- a/sw/device/tests/virtual_memory_test.c
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright 2023 Google LLC.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "hw/top_matcha/sw/autogen/top_matcha.h"
-#include "sw/device/lib/arch/device.h"
-#include "sw/device/lib/dif/dif_smc_ctrl.h"
-#include "sw/device/lib/dif/dif_uart.h"
-#include "sw/device/lib/runtime/print.h"
-#include "sw/device/lib/testing/test_framework/check.h"
-#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
-#include "sw/device/lib/testing/test_framework/test_util.h"
-#include "sw/device/tests/smc/smc_virtual_memory_test_fpga_nexus_bin_c.h"
-
-OTTF_DEFINE_TEST_CONFIG();
-
-static dif_smc_ctrl_t smc_ctrl;
-static dif_uart_t uart;
-
-void _ottf_main(void) {
-  // Initialize the UART to enable logging for non-DV simulation platforms.
-  if (kDeviceType != kDeviceSimDV) {
-    init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart);
-  }
-  LOG_INFO("Start virtual_memory_test");
-  test_status_set(kTestStatusInTest);
-  // Copy embedded binary to SMC RAM.
-  if (kDeviceType == kDeviceFpgaNexus) {
-    memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
-  }
-  CHECK_DIF_OK(dif_smc_ctrl_init(
-      mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl));
-  CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl));
-  while (true) {
-    asm volatile("wfi");
-  }
-}