name: Ibex RTL CI Steps
description: Ibex RTL CI Steps

inputs:
  ibex_config:
    required: true
    description: Ibex configuration to run CI for

runs:
  using: "composite"
  steps:
    # ibex_config.py will exit with error code 1 on any error which will cause
    # the CI to fail if there's an issue with the configuration file or an
    # incorrect configuration name being used
    - name: Test and display fusesoc config for ${{ inputs.ibex_config }}
      shell: bash
      run: |
        IBEX_CONFIG_OPTS=`./util/ibex_config.py ${{ inputs.ibex_config }} fusesoc_opts`
        echo $IBEX_CONFIG_OPTS
        echo "IBEX_CONFIG_OPTS=$IBEX_CONFIG_OPTS" >> $GITHUB_ENV

    - name: Lint Verilog source files with Verilator for ${{ inputs.ibex_config }}
      shell: bash
      run: |
        set +e
        fusesoc --cores-root . run --target=lint --tool=verilator lowrisc:ibex:ibex_top_tracing $IBEX_CONFIG_OPTS
        if [ $? != 0 ]; then
          echo -n "::error::"
          echo "Verilog lint failed. Run 'fusesoc --cores-root . run --target=lint --tool=verilator lowrisc:ibex:ibex_top_tracing $IBEX_CONFIG_OPTS' to check and fix all errors."
          exit 1
        fi

    - name: Lint Verilog source files with Verible Verilog Lint for ${{ inputs.ibex_config }}
      shell: bash
      run: |
        set +e
        fusesoc --cores-root . run --target=lint --tool=veriblelint lowrisc:ibex:ibex_top_tracing $IBEX_CONFIG_OPTS
        if [ $? != 0 ]; then
          echo -n "::error::"
          echo "Verilog lint failed. Run 'fusesoc --cores-root . run --target=lint --tool=veriblelint lowrisc:ibex:ibex_top_tracing $IBEX_CONFIG_OPTS' to check and fix all errors."
          exit 1
        fi

    - name: Run RISC-V Compliance test for Ibex RV32IMC for ${{ inputs.ibex_config }}
      shell: bash
      run: |
        set +e
        # Build simulation model of Ibex
        fusesoc --cores-root=. run --target=sim --setup --build lowrisc:ibex:ibex_riscv_compliance $IBEX_CONFIG_OPTS
        if [ $? != 0 ]; then
          echo -n "::error::"
          echo "Unable to build Verilator model of Ibex for compliance testing."
          exit 1
        fi

        # Run compliance test suite
        export TARGET_SIM=$PWD/build/lowrisc_ibex_ibex_riscv_compliance_0.1/sim-verilator/Vibex_riscv_compliance
        export RISCV_PREFIX=riscv32-unknown-elf-
        export RISCV_TARGET=ibex
        export RISCV_DEVICE=rv32imc
        fail=0
        for isa in rv32i rv32im rv32imc rv32Zicsr rv32Zifencei; do
          make -C build/riscv-compliance RISCV_ISA=$isa 2>&1 | tee run.log
          if [ ${PIPESTATUS[0]} != 0 ]; then
            echo -n "::error::"
            echo "The RISC-V compliance test suite failed for $isa"

            # There's no easy way to get the test results in machine-readable
            # form to properly exclude known-failing tests. Going with an
            # approximate solution for now.
            if [ $isa == rv32i ] && grep -q 'FAIL: 4/48' run.log; then
              echo -n "::error::"
              echo "Expected failure for rv32i, see lowrisc/ibex#100 more more information."
            else
              fail=1
            fi
          fi
        done
        exit $fail

    - name: Run Verilator co-sim tests for for ${{ inputs.ibex_config }}
      shell: bash
      run: |
        source ci/setup-cosim.sh
        # Build simple system with co-simulation
        fusesoc --cores-root=. run --target=sim --setup --build lowrisc:ibex:ibex_simple_system_cosim $IBEX_CONFIG_OPTS

        # Run directed tests against simple system co-simulation
        ./ci/run-cosim-test.sh --skip-pass-check CoreMark examples/sw/benchmarks/coremark/coremark.elf

        if ./util/ibex_config.py ${{ inputs.ibex_config }} query_fields PMPEnable | grep -q 'PMPEnable=1'; then
          ./ci/run-cosim-test.sh --skip-pass-check pmp_smoke examples/sw/simple_system/pmp_smoke_test/pmp_smoke_test.elf
        else
          echo "PMP not supported on ${{ inputs.ibex_config }}, skipping pmp_smoke_test"
        fi

        if ./util/ibex_config.py ${{ inputs.ibex_config }} query_fields SecureIbex | grep -q 'SecureIbex=1'; then
          ./ci/run-cosim-test.sh dit_test examples/sw/simple_system/dit_test/dit_test.elf
          ./ci/run-cosim-test.sh dummy_instr_test examples/sw/simple_system/dummy_instr_test/dummy_instr_test.elf
        else
          echo "Security features not supported on ${{ inputs.ibex_config }}, skipping security feature tests"
        fi
