# Copyright 2022 Thales Silicon Security
#
# Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
# You may obtain a copy of the License at https://solderpad.org/licenses/
#
# Original Author: Yannick Casamatta (yannick.casamatta@thalesgroup.com)

# Guidlines:

# This pipeline can be also be triggerd underneath by CI of CVA6 repository, this requires to respected some rules:
# - In this pipeline, job artifacts must be only defined in a folder named "artifacts" at the root of the job's workdir.
# - In this pipeline, do not define before_script and after_script in the global section (avoid in job too).
# - Please prefix all jobs in this file with "pub_" which stands for "public" job.


variables:
  GIT_STRATEGY: fetch
  GIT_SUBMODULE_STRATEGY: recursive


# macro: job that does not require cloning anythings
.template_job_low_footprint:
  variables:
    GIT_STRATEGY: none
  before_script:
    - echo 'nothing'
  after_script:
    - echo 'nothing'

# macro: job launched only in case of CI triggered by core-v-verif repository (not included by cva6 CI)
.template_job_init_cva6:
  tags: [$TAGS_RUNNER]
  rules:
    - if: '$SCOPE_CVV == "true"'
      when: on_success
    - when: never

# macro: job always launched (if pipeline is created)
.template_job_full_ci:
  tags: [$TAGS_RUNNER]
  rules:
    - when: on_success

# macro: job launched only on main branches, other case set to manual (if pipeline is created)
.template_job_short_ci:
  tags: [$TAGS_RUNNER]
  rules:
    - if: '$CI_WEIGHT == "forced"'
      when: on_success
    - if: '$CI_WEIGHT == "full"'
      when: on_success
    - when: manual
      allow_failure: true

# macro: job set to manual (if pipeline is created)
.template_job_always_manual:
  tags: [$TAGS_RUNNER]
  rules:
    - if: '$CI_WEIGHT == "forced"'
      when: on_success
    - when: manual
      allow_failure: true


stages:
  - .pre
  - build_tools
  - one
  - two
  - three


# In the scope of a CI triggered by core-v-verif repository:
# This job will get the HASH of the given CVA6 branch
# This HASH will be used by the next jobs instead of the CVA6 given BRANCH name
# This prevents CI to not use the same version of CVA6 in case of new commit in CVA6 branch during the execution of the CI
initjob:
  stage: .pre
  extends:
    - .template_job_low_footprint
    - .template_job_init_cva6
  needs: []
  script:
    - '[[ -e ./cva6 ]] && rm -rf cva6'
    - git clone $CVA6_REPO -b $CVA6_BRANCH --depth=1 cva6
    - cd cva6
    - echo CVA6_HASH=$(git rev-parse origin/$CVA6_BRANCH) > ../.env
    - cd ..
  artifacts:
    reports:
      dotenv: .env

pub_check_env:
  stage: .pre
  extends:
    - .template_job_low_footprint
    - .template_job_full_ci
  script:
    - echo $CI_WEIGHT
    - echo $RISCV
    - echo $RISCV_PREFIX
    - echo $VERILATOR_ROOT
    - echo $VERILATOR_INSTALL_DIR
    - echo $SPIKE_ROOT
    - echo $SPIKE_INSTALL_DIR
    - echo $BBL_ROOT
    - echo $SYN_VCS_BASHRC
    - echo $SYN_DCSHELL_BASHRC
    - echo $QUESTA_BASHRC
    - echo $VIVADO_SETUP
    - echo $CVA6_REPO
    - echo $CVA6_BRANCH
    - echo $CVA6_HASH
    - echo $CORE_V_VERIF_REPO
    - echo $CORE_V_VERIF_BRANCH
    - echo $CORE_V_VERIF_HASH
    - echo $COMPLIANCE_REPO
    - echo $COMPLIANCE_BRANCH
    - echo $COMPLIANCE_HASH
    - echo $COMPLIANCE_PATCH
    - echo $TESTS_REPO
    - echo $TESTS_BRANCH
    - echo $TESTS_HASH
    - echo $DV_REPO
    - echo $DV_BRANCH
    - echo $DV_HASH
    - echo $DV_PATCH
    - echo $TAGS_RUNNER
    - echo $NUM_JOBS
    - echo $FOUNDRY_PATH
    - echo $NAND2_AREA
    - echo $TECH_NAME
    - echo $SYNTH_PERIOD
    - echo $LIB_VERILOG

pub_build_tools:
  stage: build_tools
  extends:
    - .template_job_full_ci
  needs: []
  script:
    # ROOT_PROJECT is used by Spike installer and designates the toplevel of core-v-verif tree.
    - 'export ROOT_PROJECT=$(pwd)'
    # If a local build of Spike is requested, clean up build and installation directories.
    - '[ -n "$SPIKE_INSTALL_DIR" -a "$SPIKE_INSTALL_DIR" = "__local__" ] && rm -rf vendor/riscv/riscv-isa-sim/build'
    - '[ -n "$SPIKE_INSTALL_DIR" -a "$SPIKE_INSTALL_DIR" = "__local__" ] && rm -rf tools/spike'
    # Create default directory corresponding to the artifact path.
    - mkdir -p tools/spike
    # Set up Spike, whether locally built or pre-installed.
    # If initially set to "__local__", SPIKE_INSTALL_DIR will be resolved
    # to an absolute path by the installation script.
    - source cva6/regress/install-spike.sh
    # Strip locally built binaries and libraries to reduce artifact size.
    - '[ -f $(pwd)/tools/spike/bin/spike ] && strip $(pwd)/tools/spike/bin/spike* $(pwd)/tools/spike/lib/lib*.*'
    - mkdir -p artifacts/tools/
    - mv tools/spike artifacts/tools/
  artifacts:
    paths:
      - artifacts/tools/spike/*

pub_smoke:
  stage: one
  extends:
    - .template_job_full_ci
  needs:
    - job: pub_build_tools
      artifacts: true
  parallel:
    matrix:
      - DV_SIMULATORS: ["veri-testharness,spike","vcs-testharness,spike","vcs-uvm,spike" ]
  variables:
    DASHBOARD_JOB_TITLE: "Smoke test $DV_SIMULATORS"
    DASHBOARD_JOB_DESCRIPTION: "Short tests to challenge most architectures with most testbenchs configurations"
    DASHBOARD_SORT_INDEX: 0
    DASHBOARD_JOB_CATEGORY: "Basic"
  script:
    - mkdir -p artifacts/reports artifacts/logs
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    # In order to capture logs in case of test failure, the test script cannot fail.
    - source cva6/regress/smoke-tests.sh || true
    # The list of files must NOT fail on various DV_SIMULATORS values, so use 'v*_sim' to match
    # 'veri-testharness_sim', 'vcs-testharness_sim' and 'vcs-uvm_sim' (one of them always applies,
    # at least until new RTL simulator configurations are added.)
    - for i in cva6/sim/*/v*_sim/*.log.iss ; do head -10000 $i > artifacts/logs/$(basename $i).head ; done
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - artifacts/reports/*.yml
      - artifacts/logs/*.log.iss.head

pub_riscv_arch_test:
  stage: two
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - DV_TARGET: [cv64a6_imafdc_sv39, cv32a60x]
  variables:
    DV_SIMULATORS: "veri-testharness,spike"
    DASHBOARD_JOB_TITLE: "arch_test $DV_TARGET"
    DASHBOARD_JOB_DESCRIPTION: "Compliance regression suite"
    DASHBOARD_SORT_INDEX: 0
    DASHBOARD_JOB_CATEGORY: "Test suites"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source cva6/regress/dv-riscv-arch-test.sh
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"

csr_test:
  stage: two
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - DV_TARGET: [cv32a60x]
  variables:
    DV_SIMULATORS: "veri-testharness,spike"
    DASHBOARD_JOB_TITLE: "csr_test $DV_TARGET"
    DASHBOARD_JOB_DESCRIPTION: "CSR regression suite"
    DASHBOARD_SORT_INDEX: 0
    DASHBOARD_JOB_CATEGORY: "Test suites"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source cva6/regress/dv-riscv-csr-access-test.sh
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"

pub_hwconfig:
  stage: two
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - DV_SIMULATORS: ["veri-testharness,spike"]
        DV_HWCONFIG_OPTS: ["--default_config=cv32a60x --isa=rv32imac --a_ext=1",
                           "--default_config=cv32a60x --isa=rv32imc --RenameEn=1"]
  variables:
    DASHBOARD_JOB_TITLE: "HW config $DV_SIMULATORS $DV_HWCONFIG_OPTS"
    DASHBOARD_JOB_DESCRIPTION: "Short tests to challenge target configurations"
    DASHBOARD_SORT_INDEX: 1
    DASHBOARD_JOB_CATEGORY: "Basic"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source ./cva6/regress/hwconfig_tests.sh
    - python3 .gitlab-ci/scripts/report_pass.py
  artifacts:
    when: always
    paths:
      - artifacts/reports/*.yml

pub_compliance:
  stage: two
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - DV_TARGET: [cv64a6_imafdc_sv39, cv32a60x]
  variables:
    DV_SIMULATORS: "veri-testharness,spike"
    DASHBOARD_JOB_TITLE: "Compliance $DV_TARGET"
    DASHBOARD_JOB_DESCRIPTION: "Compliance regression suite"
    DASHBOARD_SORT_INDEX: 2
    DASHBOARD_JOB_CATEGORY: "Test suites"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source cva6/regress/dv-riscv-compliance.sh
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"


pub_tests-v:
  stage: two
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - DV_TARGET: [cv64a6_imafdc_sv39]
  variables:
    DV_SIMULATORS: "veri-testharness,spike"
    DV_TESTLISTS: "../tests/testlist_riscv-tests-$DV_TARGET-v.yaml"
    DASHBOARD_JOB_TITLE: "Riscv-test $DV_TARGET (virtual)"
    DASHBOARD_JOB_DESCRIPTION: "Riscv-test regression suite (virtual)"
    DASHBOARD_SORT_INDEX: 3
    DASHBOARD_JOB_CATEGORY: "Test suites"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source cva6/regress/dv-riscv-tests.sh
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"


pub_tests-p:
  stage: two
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - DV_TARGET: [cv64a6_imafdc_sv39, cv32a60x]
  variables:
    DV_SIMULATORS: "veri-testharness,spike"
    DV_TESTLISTS: "../tests/testlist_riscv-tests-$DV_TARGET-p.yaml"
    DASHBOARD_JOB_TITLE: "Riscv-test $DV_TARGET (physical)"
    DASHBOARD_JOB_DESCRIPTION: "Riscv-test regression suite (physical)"
    DASHBOARD_SORT_INDEX: 4
    DASHBOARD_JOB_CATEGORY: "Test suites"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source cva6/regress/dv-riscv-tests.sh
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"


pub_synthesis:
  stage: two
  timeout: 2 hours
  extends:
    - .template_job_always_manual
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - TARGET: [cv32a6_embedded]
        PERIOD: ["0.85"]
  variables:
    INPUT_DELAY: "0.46"
    OUTPUT_DELAY: "0.11"
    DASHBOARD_JOB_TITLE: "ASIC Synthesis $TARGET"
    DASHBOARD_JOB_DESCRIPTION: "Synthesis indicator with specific Techno"
    DASHBOARD_SORT_INDEX: 5
    DASHBOARD_JOB_CATEGORY: "Synthesis"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    #ack trick to manage float gitlab-ci variables that seems to support only string or integer
    - echo $(echo $SYNTH_PERIOD)
    - echo $(echo $INPUT_DELAY)
    - echo $(echo $OUTPUT_DELAY)
    - echo $(echo $NAND2_AREA)
    - echo $FOUNDRY_PATH
    - echo $PERIOD
    - echo $TECH_NAME
    - echo $TARGET
    - source ./cva6/regress/install-cva6.sh
    - echo $SYN_DCSHELL_BASHRC; source $SYN_DCSHELL_BASHRC
    - make -C core-v-cores/cva6/pd/synth cva6_synth PERIOD=$(echo $PERIOD) NAND2_AREA=$(echo $NAND2_AREA) FOUNDRY_PATH=$FOUNDRY_PATH TECH_NAME=$TECH_NAME INPUT_DELAY=$(echo $INPUT_DELAY) OUTPUT_DELAY=$(echo $OUTPUT_DELAY) TARGET=$TARGET
    - mv core-v-cores/cva6/pd/synth/cva6_${TARGET}_synth_modified.v artifacts/cva6_${TARGET}_synth_modified.v
    - python3 .gitlab-ci/scripts/report_synth.py core-v-cores/cva6/pd/synth/cva6_${TARGET}/reports/$PERIOD/cva6_$(echo $TECH_NAME)_synth_area.rpt core-v-cores/cva6/pd/synth/synthesis_batch.log
  artifacts:
    when: always
    paths:
      - artifacts/cva6_${TARGET}_synth_modified.v
      - "artifacts/reports/*.yml"

pub_synthesis_others:
  stage: two
  timeout: 2 hours
  extends:
    - .template_job_always_manual
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  parallel:
    matrix:
      - TARGET: [cv64a6_imafdc_sv39]
        PERIOD: ["1.1"]
      - TARGET: [cv32a60x]
        PERIOD: ["0.95"]
  variables:
    INPUT_DELAY: "0.46"
    OUTPUT_DELAY: "0.11"
    DASHBOARD_JOB_TITLE: "ASIC Synthesis $TARGET"
    DASHBOARD_JOB_DESCRIPTION: "Synthesis indicator with specific Techno"
    DASHBOARD_SORT_INDEX: 5
    DASHBOARD_JOB_CATEGORY: "Synthesis"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    #ack trick to manage float gitlab-ci variables that seems to support only string or integer
    - echo $(echo $SYNTH_PERIOD)
    - echo $(echo $INPUT_DELAY)
    - echo $(echo $OUTPUT_DELAY)
    - echo $(echo $NAND2_AREA)
    - echo $FOUNDRY_PATH
    - echo $PERIOD
    - echo $TECH_NAME
    - echo $TARGET
    - source ./cva6/regress/install-cva6.sh
    - echo $SYN_DCSHELL_BASHRC; source $SYN_DCSHELL_BASHRC
    - make -C core-v-cores/cva6/pd/synth cva6_synth PERIOD=$(echo $PERIOD) NAND2_AREA=$(echo $NAND2_AREA) FOUNDRY_PATH=$FOUNDRY_PATH TECH_NAME=$TECH_NAME INPUT_DELAY=$(echo $INPUT_DELAY) OUTPUT_DELAY=$(echo $OUTPUT_DELAY) TARGET=$TARGET
    - mv core-v-cores/cva6/pd/synth/cva6_${TARGET}_synth_modified.v artifacts/cva6_${TARGET}_synth_modified.v
    - python3 .gitlab-ci/scripts/report_synth.py core-v-cores/cva6/pd/synth/cva6_${TARGET}/reports/$PERIOD/cva6_$(echo $TECH_NAME)_synth_area.rpt core-v-cores/cva6/pd/synth/synthesis_batch.log
  artifacts:
    when: always
    paths:
      - artifacts/cva6_${TARGET}_synth_modified.v
      - "artifacts/reports/*.yml"
  rules:
    - when: manual
      allow_failure: true


pub_smoke-gate:
  stage: three
  extends:
    - .template_job_always_manual
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_synthesis
      artifacts: true
  parallel:
    matrix:
      - TARGET: [cv32a6_embedded]
  variables:
    DASHBOARD_JOB_TITLE: "Smoke Gate $TARGET"
    DASHBOARD_JOB_DESCRIPTION: "Simple test to check netlist from ASIC synthesis"
    DASHBOARD_SORT_INDEX: 6
    DASHBOARD_JOB_CATEGORY: "Post Synthesis"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $LIB_VERILOG
    - echo $FOUNDRY_PATH
    - echo $PERIOD
    - echo $TECH_NAME
    - source ./cva6/regress/install-cva6.sh
    - source ./cva6/regress/install-riscv-dv.sh
    - source ./cva6/regress/install-riscv-tests.sh
    - mv artifacts/cva6_${TARGET}_synth_modified.v core-v-cores/cva6/pd/synth/cva6_${TARGET}_synth_modified.v
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - cd cva6/sim
    - make vcs_clean_all
    - python3 cva6.py --testlist=../tests/testlist_riscv-tests-cv32a60x-p.yaml --test rv32ui-p-lw --iss_yaml cva6.yaml --target $TARGET --iss=spike,vcs-gate $DV_OPTS
    - cd ../..
    - python3 .gitlab-ci/scripts/report_simu.py cva6/sim/logfile.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"

pub_coremark:
  stage: two
  extends:
    - .template_job_full_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  variables:
    DASHBOARD_JOB_TITLE: "CoreMark"
    DASHBOARD_JOB_DESCRIPTION: "Performance indicator"
    DASHBOARD_SORT_INDEX: 5
    DASHBOARD_JOB_CATEGORY: "Performance"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - bash cva6/regress/coremark.sh --no-print
    - python3 .gitlab-ci/scripts/report_benchmark.py --coremark cva6/sim/out_*/veri-testharness_sim/core_main.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"

pub_dhrystone:
  stage: two
  extends:
    - .template_job_full_ci
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  variables:
    DASHBOARD_JOB_TITLE: "Dhrystone"
    DASHBOARD_JOB_DESCRIPTION: "Performance indicator"
    DASHBOARD_SORT_INDEX: 5
    DASHBOARD_JOB_CATEGORY: "Performance"
  script:
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - bash cva6/regress/dhrystone.sh
    - python3 .gitlab-ci/scripts/report_benchmark.py --dhrystone cva6/sim/out_*/veri-testharness_sim/dhrystone_main.log
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"

pub_fpga-build:
  stage: two
  timeout: 90 minutes
  extends:
    - .template_job_short_ci
  needs:
    - job: pub_smoke
      artifacts: false
  variables:
    DASHBOARD_JOB_TITLE: "FPGA Build $TARGET"
    DASHBOARD_JOB_DESCRIPTION: "Test of FPGA build flow"
    DASHBOARD_SORT_INDEX: 9
    DASHBOARD_JOB_CATEGORY: "Synthesis"
  parallel:
    matrix:
      - TARGET: [cv64a6_imafdc_sv39, cv32a60x]
  script:
    - mkdir -p artifacts/reports
    - python3 .gitlab-ci/scripts/report_fail.py
    - source $VIVADO_SETUP
    - source cva6/regress/install-cva6.sh
    - make -C core-v-cores/cva6 fpga target=$TARGET
    - mkdir -p artifacts/reports
    - mv core-v-cores/cva6/corev_apu/fpga/work-fpga/ariane_xilinx.bit artifacts/ariane_xilinx_$TARGET.bit
    - python3 .gitlab-ci/scripts/report_fpga.py core-v-cores/cva6/corev_apu/fpga/reports/ariane.utilization.rpt
  artifacts:
    when: always
    paths:
      - "artifacts/ariane_xilinx_$TARGET.bit"
      - "artifacts/reports/*.yml"

pub_generated_tests:
  stage: two
  tags: [$TAGS_RUNNER]
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  variables:
    DASHBOARD_SORT_INDEX: 11
    DASHBOARD_JOB_CATEGORY: "Code Coverage"
  parallel:
    matrix:
      - list_num: 1
        DASHBOARD_JOB_TITLE: "Generated Random Arithmetic tests"
        DASHBOARD_JOB_DESCRIPTION: "Generate Random Arithmetic tests using the RISCV-DV"
      - list_num: 2
        DASHBOARD_JOB_TITLE: "Generated CSR tests"
        DASHBOARD_JOB_DESCRIPTION: "Generate Random CSR tests using the RISCV-DV"
      - list_num: 3
        DASHBOARD_JOB_TITLE: "Generated MMU tests"
        DASHBOARD_JOB_DESCRIPTION: "Generate Random MMU tests using the RISCV-DV"
      - list_num: 4
        DASHBOARD_JOB_TITLE: "Generated Random Load_store tests"
        DASHBOARD_JOB_DESCRIPTION: "Generate Random Load_store tests using the RISCV-DV"
      - list_num: 5
        DASHBOARD_JOB_TITLE: "Generated Jump tests"
        DASHBOARD_JOB_DESCRIPTION: "Generate Random Arithmetic Jump tests using the RISCV-DV"
  script:
    - mkdir -p artifacts/coverage
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source ./cva6/regress/dv-generated-tests.sh
    - mv cva6/sim/vcs_results/default/vcs.d/simv.vdb artifacts/coverage
    - mv cva6/sim/seedlist.yaml artifacts/coverage
    - python3 .gitlab-ci/scripts/report_pass.py
  rules:
    - when: manual
      allow_failure: true
  timeout: 4h
  artifacts:
    when: always
    paths:
      - artifacts/coverage/simv.vdb
      - artifacts/coverage/seedlist.yaml
      - "artifacts/reports/*.yml"
    expire_in: 3 week

pub_directed_isacov-tests:
  stage: two
  tags: [$TAGS_RUNNER]
  needs:
    - job: pub_build_tools
      artifacts: true
    - job: pub_smoke
      artifacts: false
  variables:
    DASHBOARD_SORT_INDEX: 13
    DASHBOARD_JOB_CATEGORY: "Functional Coverage"
  parallel:
    matrix:
      - list_num: 0
        DASHBOARD_JOB_TITLE: "Directed tests"
        DASHBOARD_JOB_DESCRIPTION: "Execute directed tests to improve functional coverage of ISA"
  script:
    - mkdir -p artifacts/coverage
    - mkdir -p artifacts/reports
    - mv artifacts/tools/spike tools
    - python3 .gitlab-ci/scripts/report_fail.py
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - source ./cva6/regress/dv-generated-tests.sh
    - mv cva6/sim/vcs_results/default/vcs.d/simv.vdb artifacts/coverage
    - python3 .gitlab-ci/scripts/report_pass.py
  rules:
    - when: manual
      allow_failure: true
  timeout: 4h
  artifacts:
    when: always
    paths:
      - artifacts/coverage/simv.vdb
      - "artifacts/reports/*.yml"
    expire_in: 3 week

pub_fpga-boot:
  stage: three
  tags: [fpga,shell]
  needs:
    - job: pub_fpga-build
      artifacts: true
  variables:
    VERILATOR_INSTALL_DIR: "NO" # Skip install and checks of verilator
    SPIKE_ROOT: "NO"  # Skip install and checks of spike
    DASHBOARD_JOB_TITLE: "FPGA Linux64 Boot "
    DASHBOARD_JOB_DESCRIPTION: "Test of Linux 64 bits boot on FPGA Genesys2"
    DASHBOARD_SORT_INDEX: 10
    DASHBOARD_JOB_CATEGORY: "Synthesis"
  script:
    - mkdir -p artifacts/reports
    - python3 .gitlab-ci/scripts/report_fail.py
    - source cva6/regress/install-cva6.sh
    - source $VIVADO2022_SETUP
    - mkdir -p core-v-cores/cva6/corev_apu/fpga/work-fpga
    - mv artifacts/ariane_xilinx_cv64a6_imafdc_sv39.bit core-v-cores/cva6/corev_apu/fpga/work-fpga/ariane_xilinx.bit
    - cd core-v-cores/cva6/corev_apu/fpga/scripts
    - source check_fpga_boot.sh
    - cd -
    - python3 .gitlab-ci/scripts/report_fpga_boot.py core-v-cores/cva6/corev_apu/fpga/scripts/fpga_boot.rpt
  artifacts:
    when: always
    paths:
      - "artifacts/reports/*.yml"

code_coverage-report:
  stage: three
  tags: [$TAGS_RUNNER]
  needs:
    - job: pub_generated_tests
      artifacts: true
    - job: pub_directed_isacov-tests
      artifacts: true
  variables:
    DASHBOARD_JOB_TITLE: "Report merge coverage"
    DASHBOARD_JOB_DESCRIPTION: "Report merge coverage of generated tests"
    DASHBOARD_SORT_INDEX: 12
    DASHBOARD_JOB_CATEGORY: "Code Coverage"
  script:
    - mkdir -p artifacts/reports
    - mkdir -p artifacts/cov_reports/
    - python3 .gitlab-ci/scripts/report_fail.py
    - mkdir -p cva6/sim/vcs_results/default/vcs.d
    - mv artifacts/coverage/simv.vdb cva6/sim/vcs_results/default/vcs.d/
    - mv artifacts/coverage/seedlist.yaml cva6/sim/seedlist.yaml
    - echo $SYN_VCS_BASHRC; source $SYN_VCS_BASHRC
    - make -C cva6/sim generate_cov_dash
    - mv cva6/sim/urgReport artifacts/cov_reports/
    - python3 .gitlab-ci/scripts/report_pass.py
  rules:
    - when: on_success
  artifacts:
    when: always
    paths:
      - "artifacts/cov_reports/urgReport"
      - "artifacts/reports/*.yml"
    expire_in: 3 week

merge_report:
  stage: .post
  tags: [$TAGS_RUNNER]
  rules:
    - if: '$DASHBOARD_URL'
      when: always
    - when: never
  script:
    - mkdir -p artifacts/reports
    - ls -al artifacts/reports
    - python3 .gitlab-ci/scripts/merge_job_reports.py artifacts/reports pipeline_report_$CI_PIPELINE_ID.yml
  artifacts:
    when: always
    paths:
      - "artifacts/reports/pipeline_report_$CI_PIPELINE_ID.yml"

