name: XiangShan Nightly Regression

on:
  workflow_dispatch:
  schedule:
    # 14:00 UTC == 22:00 UTC+8
    - cron: '00 14 * * *'

jobs:
  xiangshan-pldm-sim:
    strategy:
      fail-fast: false
      matrix:
        branch: [ kunminghu-v2, kunminghu-v3 ]
        topo:
          - name: single
            num_cores: 1
            workload: "$NOOP_HOME/ready-to-run/linux.bin"
            ref: "$NOOP_HOME/ready-to-run/riscv64-nemu-interpreter-so"
          - name: dual
            num_cores: 2
            workload: "/nfs/home/share/ci-workloads/linux-hello-smp-new/bbl.bin"
            ref: "$NOOP_HOME/ready-to-run/riscv64-nemu-interpreter-dual-so"
        debug_args: [ "--difftest-config ZESNHP" ]
        config: [ DefaultConfig ]

    runs-on: [self-hosted, open]
    timeout-minutes: 720
    name: XS-PLDM(${{ matrix.branch }}-${{ matrix.topo.name }})
    steps:
      - uses: actions/checkout@v6

      - name: Prepare XiangShan
        run: |
          cd $GITHUB_WORKSPACE/..
          NOOP_HOME="$PWD/XiangShan-${{ matrix.branch }}"
          echo "NOOP_HOME=$NOOP_HOME" >> "$GITHUB_ENV"
          if [ ! -d "$NOOP_HOME" ]; then
            git clone -b ${{ matrix.branch }} --single-branch https://github.com/OpenXiangShan/XiangShan.git $NOOP_HOME
          fi
          cd $NOOP_HOME
          git fetch --depth=1 origin ${{ matrix.branch }}
          git checkout ${{ matrix.branch }}
          git reset --hard origin/${{ matrix.branch }}
          rm -rf difftest && make init
          rm -rf difftest && cp -r $GITHUB_WORKSPACE .
          make clean

      - name: Generate Verilog
        run: |
          cd $NOOP_HOME
          make sim-verilog PLDM=1 NUM_CORES=${{ matrix.topo.num_cores }} DEBUG_ARGS="${{ matrix.debug_args }}" CONFIG=${{ matrix.config }} WITH_CHISELDB=0 WITH_CONSTANTIN=0 -j200

      - name: Build SIMV
        run: |
          cd $NOOP_HOME
          make simv VCS=verilator NUM_CORES=${{ matrix.topo.num_cores }} WITH_CHISELDB=0 WITH_CONSTANTIN=0 DIFFTEST_PERFCNT=1 EMU_THREADS=16 -j200

      - name: Test with Linux
        run: |
          cd $NOOP_HOME
          ulimit -s 32768
          ./build/simv +workload=${{ matrix.topo.workload }} +diff=${{ matrix.topo.ref }} 2>&1 | tee pldm.log

  xiangshan-fpga-sim:
    strategy:
      fail-fast: false
      matrix:
        branch: [ kunminghu-v2, kunminghu-v3 ]
    runs-on: [self-hosted, open]
    timeout-minutes: 720
    name: XS-FPGA(${{ matrix.branch }})
    steps:
      - uses: actions/checkout@v6

      - name: Set Env
        run: |
          source /nfs/home/ci-runner/ci-runner-difftest/tools/env.sh

      - name: Prepare XiangShan
        run: |
          cd $GITHUB_WORKSPACE/..
          NOOP_HOME="$PWD/XiangShan-${{ matrix.branch }}"
          echo "NOOP_HOME=$NOOP_HOME" >> "$GITHUB_ENV"
          if [ ! -d "$NOOP_HOME" ]; then
            git clone -b ${{ matrix.branch }} --single-branch https://github.com/OpenXiangShan/XiangShan.git $NOOP_HOME
          fi
          cd $NOOP_HOME
          git fetch --depth=1 origin ${{ matrix.branch }}
          git checkout ${{ matrix.branch }}
          git reset --hard origin/${{ matrix.branch }}
          rm -rf difftest && make init
          rm -rf difftest && cp -r $GITHUB_WORKSPACE .
          make clean

      - name: Generate Verilog
        run: |
          cd $NOOP_HOME
          make sim-verilog FPGA=1 DEBUG_ARGS="--difftest-config ESBIFDU" CONFIG=FpgaDiffDefaultConfig WITH_CHISELDB=0 WITH_CONSTANTIN=0 -j200

      - name: Build Fpga Host
        run: |
          cd $NOOP_HOME
          make -C difftest fpga-build FPGA=1 FPGA_SIM=1 DIFFTEST_PERFCNT=1 -j200

      - name: Build SIMV
        run: |
          cd $NOOP_HOME
          make simv VCS=verilator FPGA_SIM=1 WITH_CHISELDB=0 WITH_CONSTANTIN=0 DIFFTEST_PERFCNT=1 EMU_THREADS=16 -j200

      - name: Test with Microbench
        run: |
          cd $NOOP_HOME
          ulimit -s 32768
          bash difftest/scripts/fpga_sim/cosim.sh WORKLOAD=./ready-to-run/microbench.bin DIFF=./ready-to-run/riscv64-nemu-interpreter-so | tee fpga-microbench.log

      - name: Test with Linux
        run: |
          cd $NOOP_HOME
          ulimit -s 32768
          bash difftest/scripts/fpga_sim/cosim.sh WORKLOAD=./ready-to-run/linux.bin DIFF=./ready-to-run/riscv64-nemu-interpreter-so | tee fpga-linux.log
