name: Regression tests DCLS

on:
  workflow_call:
    inputs:
      run_custom:
        type: boolean
        default: true

defaults:
  run:
    shell: bash

jobs:
  generate-matrix:
    name: Generate test matrix
    runs-on: ubuntu-24.04
    outputs:
      tests: ${{ steps.select.outputs.tests }}
      custom_tests: ${{ steps.select-custom.outputs.tests }}
    steps:
      - uses: actions/checkout@v4
      - name: Select tests
        id: select
        run: |
          echo "tests=$(python3 .github/scripts/select_pr_tests.py \
            '${{ github.event_name }}' '${{ github.event.pull_request.head.sha || github.sha }}' 4 \
            'hello_world,hello_world_dccm,dcls,dcls_debug,dcls_error_ctrl,dhry,ecc,csr_misa,csr_access,csr_mstatus,csr_mseccfg,perf_counters,icache,bitmanip' \
            'csr_access')" | tee -a $GITHUB_OUTPUT
      - name: Select custom tests
        id: select-custom
        run: |
          echo "tests=$(python3 .github/scripts/select_pr_tests.py \
            '${{ github.event_name }}' '${{ github.event.pull_request.head.sha || github.sha }}-custom' 4 \
            'hello_world,hello_world_dccm,dcls,dcls_debug,dcls_error_ctrl,dhry,ecc,csr_misa,csr_access,csr_mstatus,csr_mseccfg,perf_counters,icache,bitmanip' \
            'csr_access')" | tee -a $GITHUB_OUTPUT

  regression-tests:
    name: Regression tests
    needs: [generate-matrix]
    runs-on: ubuntu-latest
    container: ghcr.io/antmicro/cores-veer-el2:20260714
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        test: ${{ fromJSON(needs.generate-matrix.outputs.tests) }}
        coverage: ["all"]
        priv: ["0", "1"]
        delay: ["0", "1", "2", "3", "4"]
        tb_extra_args: [""]
        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
        include:
          # Use hello_world_iccm for testing '--test-lsu-clk-ratio'
          - test: "hello_world_iccm"
            bus: "axi"
            coverage: "branch"
            priv: "0"
            tb_extra_args: "--test-lsu-clk-ratio"
    env:
      DEBIAN_FRONTEND: "noninteractive"
      CCACHE_DIR: "/opt/regression/.cache/"
      DCLS_ENABLE: "1"
      DCLS_DELAY: ${{ matrix.delay }}

    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

      - name: Run tests
        run: |
          export RV_ROOT=`pwd`
          export TB_EXTRA_ARGS="${{ matrix.tb_extra_args }}"
          .github/scripts/run_regression_test.sh $TEST_PATH ${{ matrix.bus }} ${{ matrix.test}} ${{ matrix.coverage }} ${{ matrix.priv }} 0

  regression-tests-iccm-addr-xor:
    name: Regression tests (DCLS + ICCM address-XOR)
    runs-on: ubuntu-latest
    container: ghcr.io/antmicro/cores-veer-el2:20250411084921
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        # ICCM-exercising subset, built with DCLS + RV_ICCM_ADDR_XOR enabled. The XOR feature
        # only affects the ICCM data path and is privilege-independent, so a single priv level
        # of these ICCM/ECC tests is enough to exercise it without re-running the full matrix.
        test: ["hello_world_iccm", "hello_world", "ecc"]
        coverage: ["all"]
        priv: ["0"]
        tb_extra_args: [""]
    env:
      DEBIAN_FRONTEND: "noninteractive"
      CCACHE_DIR: "/opt/regression/.cache/"
      DCLS_ENABLE: "1"
      DCLS_DELAY: "0"
      ICCM_ADDR_XOR: "1"

    steps:
      - name: Install utils
        run: |
          echo "deb http://archive.ubuntu.com/ubuntu/ noble main universe" | sudo tee -a /etc/apt/sources.list > /dev/null
          echo "debconf debconf/frontend select Noninteractive" | sudo debconf-set-selections
          sudo apt -qqy update && sudo apt -qqy --no-install-recommends install \
            git python3 python3-pip build-essential ninja-build ccache \
            gcc-riscv64-unknown-elf
          pip3 install meson --break-system-packages

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

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

      - name: Setup environment
        run: |
          echo "/opt/verilator/bin" >> $GITHUB_PATH
          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

      - name: Run tests
        run: |
          export PATH=/opt/verilator/bin:$PATH
          export RV_ROOT=`pwd`
          export TB_EXTRA_ARGS="${{ matrix.tb_extra_args }}"
          .github/scripts/run_regression_test.sh $TEST_PATH ${{ matrix.bus }} ${{ matrix.test}} ${{ matrix.coverage }} ${{ matrix.priv }} 0

  regression-tests-dccm-wr-readback:
    name: Regression tests (DCLS + DCCM write readback)
    runs-on: ubuntu-latest
    container: ghcr.io/antmicro/cores-veer-el2:20260714
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        # DCCM-exercising subset, built with DCLS + RV_DCCM_WR_READBACK enabled. The readback
        # feature only affects the DCCM write path and is privilege-independent, so a single
        # priv level of these DCCM/ECC tests is enough to exercise it without re-running the
        # full matrix.
        test: ["dccm_wr_readback", "hello_world_dccm", "ecc"]
        coverage: ["all"]
        priv: ["0"]
        tb_extra_args: [""]
    env:
      DEBIAN_FRONTEND: "noninteractive"
      CCACHE_DIR: "/opt/regression/.cache/"
      DCLS_ENABLE: "1"
      DCLS_DELAY: "0"
      DCCM_WR_READBACK: "1"

    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

      - name: Run tests
        run: |
          export RV_ROOT=`pwd`
          export TB_EXTRA_ARGS="${{ matrix.tb_extra_args }}"
          .github/scripts/run_regression_test.sh $TEST_PATH ${{ matrix.bus }} ${{ matrix.test}} ${{ matrix.coverage }} ${{ matrix.priv }} 0

  regression-tests-icache-ecc:
    name: Regression tests (DCLS + ICache ECC/Parity)
    runs-on: ubuntu-latest
    container: ghcr.io/antmicro/cores-veer-el2:20260714
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        test: ["icache_ecc"]
        coverage: ["all"]
        priv: ["1"]
        delay: ["0", "1", "2", "3", "4"]
        ecc: ["0", "1"]
        tb_extra_args: [""]
    env:
      DEBIAN_FRONTEND: "noninteractive"
      CCACHE_DIR: "/opt/regression/.cache/"
      DCLS_ENABLE: "1"
      DCLS_DELAY: ${{ matrix.delay }}

    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

      - name: Run tests
        run: |
          export RV_ROOT=`pwd`
          export TB_EXTRA_ARGS="${{ matrix.tb_extra_args }}"
          export ECC="${{ matrix.ecc }}"
          .github/scripts/run_regression_test.sh $TEST_PATH ${{ matrix.bus }} ${{ matrix.test}} ${{ matrix.coverage }} ${{ matrix.priv }} 0

  custom-regression-tests:
    name: Custom regression tests
    if: inputs.run_custom
    needs: [generate-matrix]
    runs-on: [ self-hosted, Linux, X64, gcp-custom-runners ]
    container: antmicro/centos-dev-tools
    strategy:
      matrix:
        bus: ["axi", "ahb"]
        test: ${{ fromJSON(needs.generate-matrix.outputs.custom_tests) }}
        priv: ["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
    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_dcls_regression:10
        env:
          DCLS_ENABLE: "1"
          TEST: ${{ matrix.test }}
          BUS: ${{ matrix.bus }}
          PRIV: ${{ matrix.priv }}
