# SPDX-License-Identifier: MIT
# Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
#
# Negative (WILL_FAIL) and positive tests that verify the aiebu assembler
# enforces the opcode restrictions documented in the aiebu spec for:
#   load_pdi, load_cores, load_cores_cp, preempt, start_cond_job_preempt
# and the multi-UC preemption scratchpad rules enforced on hint bitmaps.
#
# All tests run via "aiebu-asm -t aie4_config -j <config.json>" so they
# exercise the full aie4_config pipeline including aie4_preprocessor checks.

# ---------------------------------------------------------------------------
# Helper macro: register one aie4_config negative test (expected to fail).
# ---------------------------------------------------------------------------
macro(add_aie4_config_neg_test TEST_NAME CONFIG_FILE)
  add_test(NAME "${TEST_NAME}"
    COMMAND aiebu-asm -t aie4_config -j "${CMAKE_CURRENT_SOURCE_DIR}/${CONFIG_FILE}" -o "${TEST_NAME}.elf"
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  set_tests_properties("${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
endmacro()

# Helper macro: register one aie4_config positive test (expected to assemble).
macro(add_aie4_config_pos_test TEST_NAME CONFIG_FILE)
  add_test(NAME "${TEST_NAME}"
    COMMAND aiebu-asm -t aie4_config -j "${CMAKE_CURRENT_SOURCE_DIR}/${CONFIG_FILE}" -o "${TEST_NAME}.elf" -f disabledump
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endmacro()

# ---------------------------------------------------------------------------
# Negative tests
# ---------------------------------------------------------------------------

# load_pdi: two instructions in same column point to the same @label (address)
add_aie4_config_neg_test("aie4_check_dup_load_pdi_addr"
  "config_dup_load_pdi_addr.json")

# load_cores: two instructions in same column point to the same @label
add_aie4_config_neg_test("aie4_check_dup_load_cores_addr"
  "config_dup_load_cores_addr.json")

# load_pdi: count differs across columns (col0=2, col1=1)
add_aie4_config_neg_test("aie4_check_load_pdi_count_mismatch"
  "config_load_pdi_count_mismatch.json")

# load_cores: count differs across columns (col0=1, col1=0)
add_aie4_config_neg_test("aie4_check_load_cores_count_mismatch"
  "config_load_cores_count_mismatch.json")

# load_cores_cp: count differs across columns (col0=1, col1=0)
add_aie4_config_neg_test("aie4_check_load_cores_cp_count_mismatch"
  "config_load_cores_cp_count_mismatch.json")

# preempt: used without any load_pdi in the same control code elf
add_aie4_config_neg_test("aie4_check_preempt_no_load_pdi"
  "config_preempt_no_load_pdi.json")

# load_cores: used without any load_pdi in the same control code elf
add_aie4_config_neg_test("aie4_check_load_cores_no_load_pdi"
  "config_load_cores_no_load_pdi.json")

# load_cores_cp: used without any load_pdi in the same control code elf
add_aie4_config_neg_test("aie4_check_load_cores_cp_no_load_pdi"
  "config_load_cores_cp_no_load_pdi.json")

# start_cond_job_preempt: appears before any preempt in the same column
add_aie4_config_neg_test("aie4_check_start_cond_before_preempt"
  "config_start_cond_before_preempt.json")

# start_cond_job_preempt: count differs across columns (col0=1, col2=0)
add_aie4_config_neg_test("aie4_check_start_cond_count_mismatch"
  "config_start_cond_count_mismatch.json")

# preempt: hint bitmaps of two controllers overlap at the same preemption point
add_aie4_config_neg_test("aie4_check_preempt_hintmap_overlap"
  "config_preempt_hintmap_overlap.json")

# preempt: id values must be consecutive starting from 0
add_aie4_config_neg_test("aie4_check_preempt_id_non_consecutive"
  "config_preempt_id_non_consecutive.json")

# ---------------------------------------------------------------------------
# Positive tests
# ---------------------------------------------------------------------------

# preempt: non-contiguous hintmap bits (holes) are allowed; the scratchpad
# is the first-to-last set-bit span and disjoint spans must assemble
add_aie4_config_pos_test("aie4_check_preempt_hintmap_holes"
  "config_preempt_hintmap_holes.json")

# preempt: hole-absorbed spans may overlap even when set bits are disjoint;
# the assembler redistributes chunk assignments instead of rejecting the asm
add_aie4_config_pos_test("aie4_check_preempt_hintmap_hole_overlap"
  "config_preempt_hintmap_hole_overlap.json")
