name: Regression tests cache waypack

on:
  workflow_call:
    inputs:
      waypack:
        required: true
        type: number
      num_ways:
        required: true
        type: number
      run_custom:
        type: boolean
        default: true

defaults:
  run:
    shell: bash

env:
  WAYPACK: ${{ inputs.waypack }}
  NUM_WAYS: ${{ inputs.num_ways }}

jobs:
  regression-tests:
    name: Regression tests
    runs-on: ubuntu-24.04
    container: ghcr.io/antmicro/cores-veer-el2:20260714
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        test: ["hello_world", "hello_world_dccm", "hello_world_iccm", "cmark", "cmark_dccm", "cmark_iccm", "dhry", "ecc",
               "csr_misa", "csr_access", "csr_mstatus", "csr_mseccfg", "modesw", "insns", "irq", "perf_counters",
               "pmp", "pmp_random", "write_unaligned", "icache", "bitmanip", "read_after_read"]
        coverage: ["all"]
        priv: ["0", "1"]
        tb_extra_args: ["--test-halt"]  # hello_world_iccm will also have --test-lsu-clk-ratio
        exclude:
          # These tests require user mode
          - priv: "0"
            test: "csr_mseccfg"
          - priv: "0"
            test: "csr_access"
          - priv: "0"
            test: "csr_mstatus"
          - priv: "0"
            test: "modesw"
          - priv: "0"
            test: "insns"
          - priv: "0"
            test: "perf_counters"
          # end tests which require user mode
    env:
      DEBIAN_FRONTEND: "noninteractive"
      CCACHE_DIR: "/opt/regression/.cache/"

    steps:
      - name: Setup repository
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install dependencies
        run: |
          python3 -m venv .venv
          source .venv/bin/activate
          pip install -r requirements.txt
          pip install -r .github/scripts/requirements-coverage.txt
          echo "PATH=$PATH" >> $GITHUB_ENV

      - name: Setup environment
        run: |
          RV_ROOT=`pwd`
          echo "RV_ROOT=$RV_ROOT" >> $GITHUB_ENV
          PYTHONUNBUFFERED=1
          echo "PYTHONUNBUFFERED=$PYTHONUNBUFFERED" >> $GITHUB_ENV
          TEST_PATH=$RV_ROOT/test_results
          echo "TEST_PATH=$TEST_PATH" >> $GITHUB_ENV
          echo "FULL_NAME=${{ matrix.bus }}-${{ matrix.test }}-${{ matrix.priv == 0 && 'm' || 'mu' }}-waypack${WAYPACK}-num-ways${NUM_WAYS}" >> $GITHUB_ENV

      - name: Run tests
        run: |
          export RV_ROOT=`pwd`
          export TB_EXTRA_ARGS="${{ matrix.tb_extra_args }}"
          # Use hello_world_iccm for testing '--test-lsu-clk-ratio'
          if [ ${{ matrix.test }} = hello_world_iccm ]; then
              export TB_EXTRA_ARGS="$TB_EXTRA_ARGS --test-lsu-clk-ratio"
          fi
          .github/scripts/run_regression_test.sh $TEST_PATH ${{ matrix.bus }} ${{ matrix.test}} ${{ matrix.coverage }} ${{ matrix.priv }} $WAYPACK

      - name: Prepare coverage data
        run: |
          source .venv/bin/activate
          mkdir -p results
          .github/scripts/convert_dat.sh ${TEST_PATH}/coverage.dat results/coverage_${FULL_NAME}

      - name: Pack artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: regression_tests_coverage_data_cache_waypack_${{ env.FULL_NAME }}_${{ matrix.coverage }}
          path: results/*.info

  custom-regression-tests:
    name: Custom regression tests
    if: inputs.run_custom
    runs-on: [ self-hosted, Linux, X64, gcp-custom-runners ]
    container: antmicro/centos-dev-tools
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        test: ["hello_world", "hello_world_dccm", "hello_world_iccm", "cmark", "cmark_dccm", "cmark_iccm", "dhry", "ecc",
               "csr_misa", "csr_access", "csr_mstatus", "csr_mseccfg", "modesw", "insns", "irq", "perf_counters", "pmp", "write_unaligned",
               "icache", "bitmanip", "read_after_read"]
        priv: ["0", "1"]
        ecc: ["0", "1"]
        exclude:
          # These tests require user mode
          - priv: "0"
            test: "csr_mseccfg"
          - priv: "0"
            test: "csr_access"
          - priv: "0"
            test: "csr_mstatus"
          - priv: "0"
            test: "modesw"
          - priv: "0"
            test: "insns"
          - priv: "0"
            test: "perf_counters"
          # end tests which require user mode
          # These tests require AHB bus
          - bus: "axi"
            test: "read_after_read"
          # end tests which require AHB bus
    env:
      GHA_EXTERNAL_DISK: additional-tools
      GHA_SA: gh-sa-veer-uploader
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Run tests
        run: _secret_custom_regression_tests_waypack:9
        env:
          TEST: ${{ matrix.test }}
          BUS: ${{ matrix.bus }}
          PRIV: ${{ matrix.priv }}
          ECC: ${{ matrix.ecc }}
